-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
72 lines (62 loc) · 2.04 KB
/
Copy pathupload.py
File metadata and controls
72 lines (62 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import os
from flask import Blueprint
from flask import flash
from flask import g
from flask import redirect
from flask import render_template
from flask import request
from flask import url_for
from werkzeug.utils import secure_filename
from db import get_db
from login import login_required
from dog_breed_identification import breed_identification
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
bp = Blueprint("upload",__name__)
@bp.route('/upload', methods=('GET', 'POST'))
@login_required
def upload():
error = None
if request.method == 'POST':
print(0)
if 'file' not in request.files:
return redirect(request.url)
print(1)
file = request.files['file']
print(2)
# dog_breed = request.form['dog_breed']
print(3)
if not file or not allowed_file(file.filename):
error = 'Image is required.\n' + 'Allowed extensions are:' + str(ALLOWED_EXTENSIONS)
else:
filename = secure_filename(file.filename)
file.save(os.path.join("./static/images/", filename))
photo_path = filename
if error is not None:
flash(error)
else:
dog_breed = breed_identification(os.path.join("./static/images/", photo_path))
db = get_db()
db.execute(
'INSERT INTO dog (breed, photo_path, user_id)'
' VALUES (?, ?, ?)',
(dog_breed, photo_path, g.user['id'])
)
db.commit()
return redirect(url_for('mydogs.mydogs'))
return render_template('upload.html', error = error)
@bp.route('/<id>/<file>/delete_pic', methods=('GET', 'POST'))
@login_required
def delete_pic(id, file):
db = get_db()
filename='static/images/' + file
os.remove(filename)
db.execute(
'DELETE FROM dog'
' WHERE id=?',
(id, )
)
db.commit()
return redirect(url_for('mydogs.mydogs'))