def update_picture(user_id): if request.method == "POST": user = User.get_one_user_coll(session["user"]) # Maybe not needed !! password = request.form.get("password") # Check if the password is correct if check_password_hash(user["password"], password): # Get the file and check if the path is not empty, # save it to current directory using FileStorage from werkzeug profile_image_filename = request.form.get("my_picture") profile_img = request.files["profile_picture"] if profile_img.filename != "": profile_img.save(profile_img.filename) # Covert image url to base64 img_url_encoded = User.convert_img_to_base64( profile_image_filename) # Create a dic with new value and Add new_info to db new_info = {"profile_picture": img_url_encoded} User.update_user(new_info, user_id) flash(ProfileMsg.picture_updated) return redirect( url_for('users.profile', username=session['user'])) # Check if user is logged in if session["user"]: user = User.get_one_user_coll(session["user"]) return render_template("update_picture.html", user=user) return redirect(url_for("users.login"))
def leave_event(username): if session["user"] and session["user"] == username: # Get the event id from the form event_id = request.form.get("leave_event") # Get the user id from the db user = User.get_one_user_coll(username) user_id = user["_id"] # Set the attribute to update in the user doc get_user_attr = "events_joined" # Delete the event id from the corresponding user field User.remove_info_from_user_list((get_user_attr, event_id), user_id) # Set the attribute to update in the event doc get_event_attr = "event_joined_by" # Delete the user id from the corresponding event field Event.remove_info_from_event_list((get_event_attr, user_id), event_id) flash(EventsMsg.event_left) events_list = Event.get_all_events() return render_template("events.html", events_list=events_list, user=user) else: flash(EventsMsg.didnt_work) return redirect(url_for('index.home'))
def delete_profile(): if request.method == "POST": user = User.get_one_user_coll(session["user"]) email = request.form.get("email") password = request.form.get("password") # Check credentials if user["email"] == email: # Check if hashed password matches input password if check_password_hash(user["password"], password): User.delete_user(user["_id"]) # Remove user from session cookie session.pop("user") flash(ProfileMsg.profile_deleted) return redirect(url_for('users.signup')) else: flash(ProfileMsg.incorrect_details) return redirect(url_for('users.delete_profile')) else: flash(ProfileMsg.incorrect_details) return redirect(url_for('users.delete_profile')) # Check if user is logged in if session["user"]: user = User.get_one_user_coll(session["user"]) return render_template("delete_profile.html", user=user) return redirect(url_for("main.home"))
def create_event(username): if request.method == "POST": # Add event to db new_event = Event(**request.form) Event.insert_event_to_db(new_event) # Update user info with event_created user = User.get_one_user_coll(username) user_id = user["_id"] get_attr = "events_created" # Create an Istance of the new event to get its id event_created = Event.get_last_event_crated_by_user(user_id) if event_created: event_id = event_created._id # Update user info User.append_info_to_user((get_attr, event_id), user_id) flash(EventsMsg.event_created) return redirect(url_for('users.profile', username=session["user"])) # Check if user is logged in and if session's user correspond to username if session["user"] and session["user"] == username: # Get user from the db and return a user collection user = User.get_one_user_coll(username) return render_template('create_event.html', user=user) else: return redirect(url_for('index.home'))
def profile(username): # Check if user is logged in and if session's user correspond to username if session["user"] and session["user"] == username: # Get user from the db and return a user collection user = User.get_one_user_coll(username) user_id = user["_id"] # Get the events created by the user field_for_creator = "event_created_by" events_list_created = Event.get_some_events(field_for_creator, user_id) # Get events joined by the user events_list_joined = Event.get_events_joined(user_id) # Get all events events_list = Event.get_all_events() return render_template("profile.html", user=user, events_list_created=events_list_created, events_list_joined=events_list_joined, events_list=events_list) return redirect(url_for("users.login"))
def login(): if request.method == "POST": email = request.form.get("email") password = request.form.get("password") # Check if user exists existing_user = User.check_if_email_exists(email) if existing_user: # Check if hashed password matches input password if check_password_hash(existing_user["password"], password): # Put user into session cookie session["user"] = existing_user["username"] flash(ProfileMsg.logged_in) return redirect( url_for('users.profile', username=session["user"])) else: flash(ProfileMsg.incorrect_details) return redirect(url_for('users.login')) else: flash(ProfileMsg.incorrect_details) return redirect(url_for('users.login')) return render_template("login.html")
def see_event(): # Check if the user is logged in. if session: user = User.get_one_user_coll(session["user"]) # Get the event_id passed by the form from browse_events # to display the relevant event event_id = request.form.get("event_id") event = Event.get_one_event(event_id) return render_template("see_event.html", event=event, user=user)
def like_event(username): if request.method == "POST": user = User.get_one_user_coll(username) user_id = user["_id"] event_id = request.form.get("like_event") if str(event_id) not in user["events_liked"]: # Add the like to event_likes field in db Event.append_info_to_event(("event_likes", user_id), event_id) # Add the event to events_liked field in db User.append_info_to_user(("events_liked", event_id), user_id) # Refresh see_event.html event = Event.get_one_event(event_id) user = User.get_one_user_coll(username) return render_template('see_event.html', event=event, user=user) return redirect(url_for("events.browse_events"))
def change_password(user_id): if request.method == "POST": user = User.get_one_user_coll(session["user"]) password = request.form.get("password") # Check if current password match input if check_password_hash(user["password"], password): # Check if the new passwords are valid and match new_pass1 = request.form.get("new_password") new_pass2 = request.form.get("new_conf_password") if validate_passwords(new_pass1, new_pass2): # Create a dic with new value and Add new_info to db new_pass = generate_password_hash(new_pass2) new_info = {"password": new_pass} User.update_user(new_info, user_id) flash(ProfileMsg.password_changed) return redirect( url_for('users.profile', username=session["user"])) else: flash(ProfileMsg.incorrect_details) return redirect( url_for("users.change_password", user_id=user["_id"])) else: flash(ProfileMsg.invalid_passwords) return redirect( url_for("users.change_password", user_id=user["_id"])) # Check if user is logged in if session["user"]: user = User.get_one_user_coll(session["user"]) return render_template("change_password.html", user=user) return redirect(url_for("users.login"))
def browse_events(): # Check if the user is logged in. if session: user = User.get_one_user_coll(session['user']) # Get all the events to display events_list = Event.get_all_events() return render_template("events.html", events_list=events_list, user=user) # Get all the events to display events_list = Event.get_all_events() return render_template("events.html", events_list=events_list)
def update_profile(user_id): if request.method == "POST": # Check if new info are valid email = request.form.get("email") username = request.form.get("username") password = request.form.get("password") existing_email = User.check_if_email_exists(email) existing_username = User.check_if_username_exists(username) user = User.get_one_user_coll(session["user"]) if existing_email and email != user["email"]: flash(ProfileMsg.email_exists) return redirect( url_for('users.update_profile', user_id=user["_id"])) if existing_username and username != user["username"]: flash(ProfileMsg.username_exists) return redirect( url_for('users.update_profile', user_id=user["_id"])) # Check if the password is correct if check_password_hash(user["password"], password): # Create a dic with new values from the form new_info = { "first_name": request.form.get("first_name"), "last_name": request.form.get("last_name"), "email": request.form.get("email"), "username": request.form.get("username") } # Add new_info to db User.update_user(new_info, user_id) # Update the session['user] session["user"] = new_info["username"] flash(ProfileMsg.info_updated) user = User.get_one_user_coll(session["user"]) return redirect( url_for('users.profile', user=user, username=session['user'])) flash(ProfileMsg.incorrect_password) return redirect(url_for('users.update_profile', user_id=user["_id"])) # Check if user is logged in if session["user"]: user = User.get_one_user_coll(session["user"]) return render_template("update_profile.html", user=user) return redirect(url_for("users.login"))
def search_events(): # Check if the user is logged in. if session: user = User.get_one_user_coll(session['user']) # Get the search from the form query = request.form.get("query_search_events") # Get some events to display events_list = list(events_coll.find({"$text": {"$search": query}})) return render_template("events.html", events_list=events_list, user=user) # Get the search from the form query = request.form.get("query_search_events") # Get some events to display events_list = list(events_coll.find({"$text": {"$search": query}})) return render_template("events.html", events_list=events_list)
def select_events(): # Check if the user is logged in. if session: user = User.get_one_user_coll(session['user']) # Get the category selected category = request.form.get("event_category") # Set the field to search on field = "event_category" # Get some events to display events_list = Event.get_some_events(field, category) return render_template("events.html", events_list=events_list, user=user) # Get the category selected category = request.form.get("event_category") # Set the field to search on field = "event_category" # Get some events to display events_list = Event.get_some_events(field, category) return render_template("events.html", events_list=events_list)
def cancel_event(username): if session["user"] and session["user"] == username: # Get the event id from the form event_id = request.form.get("cancel_event") # Delete event from the event collection in db Event.delete_event(event_id) # Get the user id from the db user_id = User.get_one_user_coll(username)["_id"] # Set the attribute to update in the user doc get_user_attr = "events_created" # Delete event_id from corresponding field of the user User.remove_info_from_user_list((get_user_attr, event_id), user_id) # get all user from db all_users = users_coll.find() # Check if user has joined the event and if so remove the event # from his document for user in all_users: # Set the attribute to update in the user doc get_user_attr = "events_joined" if event_id in user[get_user_attr]: # Get the corresponding user id from the db user_id = user["_id"] # Delete event_id from corresponding field of the user User.remove_info_from_user_list((get_user_attr, event_id), user_id) flash(EventsMsg.event_deleted) user = User.get_one_user_coll(session['user']) events_list = Event.get_all_events() return render_template("events.html", events_list=events_list, user=user) else: flash(EventsMsg.didnt_work) return redirect(url_for('index.home'))
def signup(): # POST method if request.method == "POST": # Check if username already exists username = request.form.get("username") if User.check_if_username_exists(username): flash(ProfileMsg.username_exists) return redirect(url_for('users.signup')) # Check if email already exists email = request.form.get("email") if User.check_if_email_exists(email): flash(ProfileMsg.email_exists) return redirect(url_for('users.signup')) # Check if the passwords are valid and match pass1 = request.form.get("password") pass2 = request.form.get("conf_password") if validate_passwords(pass1, pass2): # Create an instance of User with the form input fields new_user = User(**request.form) # Insert to the database new_user.insert_user_to_db() # Put the new user into session cookie session["user"] = new_user.username flash(ProfileMsg.signed_in) return redirect(url_for("users.profile", username=session["user"])) else: flash(ProfileMsg.invalid_passwords) return redirect(url_for("users.signup")) # Default GET method return render_template("signup.html")
def contact(username): user = User.get_one_user_coll(username) return render_template("contact.html", user=user)
def update_event(): user = User.get_one_user_coll(session['user']) event_id = request.form.get("update_event") event = Event.get_one_event(event_id) return render_template("update_event.html", event=event, user=user)
def not_found(e): # Check if the user is logged in. if session: user = User.get_one_user_coll(session['user']) return render_template("404.html", user=user) return render_template("404.html")
def home(): # Check if the user is logged in. if session: user = User.get_one_user_coll(session['user']) return render_template("home.html", user=user) return render_template("home.html")
def privacy_policy(): if session: user = User.get_one_user_coll(session['user']) return render_template("privacy_policy.html", user=user)