def upload_profile(id): user = Restaurant.objects(id=id).first() if not len(user): return jsonify({'error': 'Food Truck could not be found!'}), 400 file = request.files['file'] link = s3_upload(file) user = Restaurant.objects(id=id).first() user.modify(**dict(profile_picture=link)) user['pwd'] = None return jsonify(results=user)
def register(): content = request.get_json() register = Register(**content) #1. check email if not register.check_email(): return (jsonify({'error': 'Not valid email address.'}), 400) if len(Restaurant.objects(email=content['email'])): return (jsonify({'error': 'Email is alredy in used.'}), 400) #2. check password check_password = register.check_password() if check_password: return (jsonify({'error': check_password}), 400) #3. hash password res = Restaurant(**content) res.set_password(content['pwd']) register.send_email() #4. save try: res.save() except Exception as e: return (jsonify({ 'error': "There is an error at the database. Please try later..." }), 500) content.pop('pwd', None) return (jsonify(content), 200)
def get_by_location(): content = request.get_json() res = Restaurant.objects(lat__lte=content['maxLatitude'], lat__gte=content['minLatitude'], lng__lte=content['maxLongitude'], lng__gte=content['minLongitude'], isOpen=True) return jsonify(results=res)
def update(id): user = Restaurant.objects(id=id).first() if not len(user): return jsonify({'error': 'Food Truck could not be found!'}), 400 #get by id if request.method == 'GET': user['pwd'] = None return jsonify(results=user) #delete by id if request.method == 'DELETE': user.delete() return jsonify({'message': 'User has been deleted'}), 400 #update by id if request.method == 'PUT': content = request.get_json() user.modify(**content) user = Restaurant.objects(id=id).first() user['pwd'] = None return jsonify(results=user)
def get_token(): content = request.get_json() user = Restaurant.objects(email=content['email']).first() if user: token = user.get_token() token = token.split('.', 2)[2] content['token'] = token content['name'] = user['name'] reset = Reset(**content) reset.send_email() return jsonify({"message": "Token has been sent."}) return jsonify({'error': 'Email not found.'}), 400
def login(): content = request.get_json() email = content['email'] if 'email' in session: return jsonify({'error': 'User alredy login.'}) user = Restaurant.objects(email=email).first() if not user: return jsonify({"error": "Error logging in. Please try again."}), 401 print user.check_password(content['pwd']) if not user.check_password(content['pwd']): return jsonify({"error": "Error logging in. Please try again."}), 401 session['email'] = content['email'] return jsonify({'message': 'You are login!'})
def change_password(): content = request.get_json() user = Restaurant.objects(email=content['email']).first() if not user: return jsonify({"error": "Invalid email address."}), 404 if not user.check_password(content['old_pwd']): return jsonify({"error": "Old password is incorrect."}), 404 register = Register(pwd=content['new_pwd']) check_password = register.check_password() if check_password: return (jsonify({'error': check_password}), 404) user.set_password(content['new_pwd']) user.save() return jsonify({'message': 'Password updated.'})
def reset_password(): content = request.get_json() user = Restaurant.objects(email=content['email']).first() if not user: return jsonify({'error': 'Email not found.'}), 400 try: user.check_token_password(content['token']) except SignatureExpired: return jsonify({"error": "Your token has expired."}), 400 except BadData: return jsonify({'error': 'Wrong token.'}), 400 rg = Register(**dict(pwd=content['new_pwd'])) check_password = rg.check_password() if check_password: return (jsonify({'error': check_password}), 400) user.change_password(content['new_pwd']) user.save() return jsonify({"message": "Password changed."})
def profile(): user = Restaurant.objects(email=session['email']).first() id = str(user['id']) return jsonify(results=user)