def upload(): utils.record_requests(app.config['INSTANCE_ID']) user = session['user'] if 'user' in session else None if not user: return redirect(url_for('login')) else: return render_template('upload.html')
def upload_image(): utils.record_requests(app.config['INSTANCE_ID']) try: valid, msg, file = check_image_request(request) print(msg) output_img = '' if valid: output_img = save_file(file) return output_img except Exception as e: # print(e) traceback.print_tb(e.__traceback__) return ''
def upload_api(): utils.record_requests(app.config['INSTANCE_ID']) try: if request.method != 'POST': return 'invalid request method' # check user valid = True username = request.values['username'] password = request.values['password'] user = User.query.filter_by(username=username).first() if user: password_hash = hash_password(user.salt, password) user = User.query.filter_by(username=username, password_hash=password_hash).first() if not user: valid = False message = "Username or Password does not exist" else: valid = False message = "Username or Password does not exist" if valid: if 'file' in request.files: file = request.files['file'] filename = secure_filename(file.filename) if filename != '': if not file: message = 'file is empty' elif not allowed_file(filename): print(filename) message = 'invalid file type' else: # updload file save_file(file, user.userid) message = file.filename + ' upload success' else: message = 'no selected file' else: message = 'no name [file] in form data' return message except Exception as e: print(e) traceback.print_tb(e.__traceback__) return 'Upload Fail'
def home(): utils.record_requests(app.config['INSTANCE_ID']) user = session['user'] if 'user' in session else None if not user: return redirect(url_for('login')) else: try: images = Image.query.filter_by(userid=user['userid']) images = [[image.imageid, get_thumbs(image.path), image.path, get_thumbs(get_faces(image.path)), get_faces(image.path)] for image in images] return render_template('home.html', images=images) except Exception as e: # print(e) traceback.print_tb(e.__traceback__) return render_template('error.html', msg='something goes wrong~')
def register(): utils.record_requests(app.config['INSTANCE_ID']) try: if 'user' in session: logout() return redirect(url_for('home')) form = forms.RegisterForm() if form.validate_on_submit(): valid = True message = '' username = form.username.data password1 = form.password1.data password2 = form.password2.data user = User.query.filter_by(username=username).first() if user: valid = False message = 'Username has already existed' else: salt = base64.urlsafe_b64encode(uuid.uuid4().bytes) salt = salt.decode('utf-8') password_hash = hash_password(salt, password1) user = User(username=username, password_hash=password_hash, salt=salt) db.session.add(user) db.session.commit() message="Sign up successfully!" if valid: flash(message, "success") else: flash(message, "danger") return render_template('register.html', form=form) except Exception as e: # print(e) traceback.print_tb(e.__traceback__) return render_template('error.html', msg='something goes wrong~')
def login(): utils.record_requests(app.config['INSTANCE_ID']) try: if 'user' in session: logout() form = forms.LoginForm() if form.validate_on_submit(): valid = True message = '' username = form.username.data password = form.password.data user = User.query.filter_by(username=username).first() if user: password_hash = hash_password(user.salt, password) user = User.query.filter_by(username=username, password_hash=password_hash).first() if not user: valid = False message = "Username or Password does not exist" else: valid = False message = "Username or Password does not exist" if valid: session['user']=user.serialize() return redirect(url_for('home')) else: flash(message) return render_template('login.html', form=form) except Exception as e: # print(e) traceback.print_tb(e.__traceback__) return render_template('error.html', msg='something goes wrong~')
def register_api(): utils.record_requests(app.config['INSTANCE_ID']) try: username = str(request.args.get('username')) password = str(request.args.get('password')) # if user is exist user = User.query.filter_by(username=username).first() if user: return 'Username has already existed' else: salt = base64.urlsafe_b64encode(uuid.uuid4().bytes) salt = salt.decode('utf-8') password_hash = hash_password(salt, password) user = User(username=username, password_hash=password_hash, salt=salt) db.session.add(user) db.session.commit() return 'Sign up successfully!' except Exception as e: # print(e) traceback.print_tb(e.__traceback__) return 'Fail to sign up!'
def logout(): utils.record_requests(app.config['INSTANCE_ID']) session.pop('user', None) return redirect(url_for('login'))