def login(): # For GET requests, display the login form; for POST, log in the current user by processing the form. print(f"LOGGING IN\n", file=sys.stdout) if current_user.is_authenticated: return redirect(url_for("index")) form = LoginForm(request.form) if request.method == 'POST': user = User.objects(email=request.form['email']).first() print(f"user is {user}\n", file=sys.stdout) if user is None or not user.check_password(bcrypt, request.form['password']): flash("Invalid username or password") return redirect(url_for('login')) login_user(user, remember=True) user = { 'email': current_user.email, 'first_name': current_user.first_name, 'last_name': current_user.last_name, } if isinstance(current_user, CompanyUser): user['company_name'] = current_user.company_name else: user['company_name'] = None response = make_response(json.dumps(user)) response.status_code = 200 print(f"login {response}\n") return response context_dict = {"title": "Sign In", "form": form, "user": current_user} return render_template('login.html', **context_dict)
f"The model file does not exists ({self.model_filename}). Please refer to the README.md for instructions about how to download the trained ML model!" ) # Update current model attributes from the file with open(self.model_filename, 'rb') as f: self.model = pickle.load(f) self.recc = self.model["model"] self.snackID_to_index = self.model["snackID_to_index"] self.index_to_snackID = self.model["index_to_snackID"] self.index_snack = self.model["index_snack"] self.userID_to_index = self.model["userID_to_index"] self.index_to_userID = self.model["index_to_userID"] self.index_user = self.model["index_user"] if __name__ == "__main__": # TESTING with: # Salty user Katrina beck, ID: 5bfcc6e767afee10a880f8f5 mongo = prepare_mongodb( mongo_uri= "mongodb+srv://Jayde:[email protected]/test?retryWrites=true" ) salty_user_id = "5bfcc6e767afee10a880f8f5" katrina = User.objects(id=salty_user_id)[0] country_katrina = "Vietnam" review_from_katrina = Review.objects(user_id=salty_user_id) print(katrina) print(f"Katrina has done {len(review_from_katrina)} reviews!!") # Create a new recommender rec = Recommender() print(rec.recommend_snacks(katrina, review_from_katrina, country_katrina))
def load_user(user_id): return User.objects(pk=user_id).first()
def validate_email(self, field): # Prevent multiple users from having the same email address. user = User.objects(email=field.data).first() if user is not None: raise ValidationError("This email is already registered, please use another email address")