def add_walk(): """ User form to add data for a new walk to the Mongo Database. If method is POST, selects all named inputs on form and retrieves info, checkboxes are assigned as booleans rather than "On"/"Off" as unchecked returns null and is less useful for other logic. """ page_title = "Add A Walk" # If session "user" is not there, redirect to register if session.get("user") is None: return redirect(url_for("login")) addForm = WalkForm() # distinct used to select only the fields wanted from collection. # https://docs.mongodb.com/manual/reference/method/db.collection.distinct/ categories = mongo.db.categories.distinct("category_name") category_choices = [(category, category) for category in categories] addForm.category_name.choices.extend(category_choices) difficulties = mongo.db.difficulty.find() difficulty_choices = [] # # cycles through each entry for challenge field to maintain ordering. for d in difficulties: difficulty_choices.append((d["challenge"], d["challenge"])) addForm.difficulty.choices.extend(difficulty_choices) if addForm.validate_on_submit(): dogs_allowed = True if request.form.get("dogs_allowed") else False free_parking = True if request.form.get("free_parking") else False paid_parking = True if request.form.get("paid_parking") else False walk = { "category_name": request.form.get("category_name"), "title": request.form.get("title"), "description": request.form.get("description"), # Split function references from w3 schools at # https://www.w3schools.com/python/ref_string_split.asp # Split used instead of splitline to maintain carriage return "directions": request.form.get("directions").split("\n"), "imageUrl": request.form.get("imageUrl"), "difficulty": request.form.get("difficulty"), "time": request.form.get("time"), "distance": request.form.get("distance"), "startpoint": request.form.get("startpoint"), "dogs_allowed": dogs_allowed, "free_parking": free_parking, "paid_parking": paid_parking, "user": mongo.db.users.find_one({"username": session["user"]})["username"] } mongo.db.routes.insert_one(walk) flash("Walk successfully added!") return redirect(url_for("user_profile", username=session["user"])) return render_template("addwalk.html", addForm=addForm, page_title=page_title)
def edit_walk(route_id): """ Reloads the add_walk def and pre-fills information to update database. Same logic as add_walk but loads walk data using the Object Id. """ page_title = "Change A Walk" walk = mongo.db.routes.find_one_or_404({"_id": ObjectId(route_id)}) editForm = WalkForm(data=walk) # If session "user" is not there, redirect to register. if session.get("user") is None: return redirect(url_for("login")) # If user tries to edit a walk which doesn"t belong to them, redirected. elif session.get("user") != walk["user"] and session.get( "user") != "admin": return redirect(url_for("home")) categories = mongo.db.categories.distinct("category_name") difficulties = mongo.db.difficulty.find() # Cycles through each entry for challenge field to maintain ordering. # .data as means to update form code/join found at respectively: # https://stackoverflow.com/questions/42984453/wtforms-populate-form-with- # data-if-data-exists?noredirect=1&lq=1 # https://www.w3schools.com/python/ref_string_join.asp editForm.directions.data = "".join(walk["directions"]) # distinct used to select only the fields wanted from collection. # https://docs.mongodb.com/manual/reference/method/db.collection.distinct/ categories = mongo.db.categories.distinct("category_name") category_choices = [(category, category) for category in categories] editForm.category_name.choices.extend(category_choices) difficulties = mongo.db.difficulty.find() difficulty_choices = [] # cycles through each entry for challenge field to maintain ordering. for d in difficulties: difficulty_choices.append((d["challenge"], d["challenge"])) editForm.difficulty.choices.extend(difficulty_choices) # Only call this if form is submitted and flaskforms validates. if editForm.validate_on_submit(): dogs_allowed = True if request.form.get("dogs_allowed") else False free_parking = True if request.form.get("free_parking") else False paid_parking = True if request.form.get("paid_parking") else False updated = { "category_name": request.form.get("category_name"), "title": request.form.get("title"), "description": request.form.get("description"), # Splitlines function references from w3 schools at # https://www.w3schools.com/python/ref_string_split.asp "directions": request.form.get("directions").split("\n"), "imageUrl": request.form.get("imageUrl"), "difficulty": request.form.get("difficulty"), "time": request.form.get("time"), "distance": request.form.get("distance"), "startpoint": request.form.get("startpoint"), "dogs_allowed": dogs_allowed, "free_parking": free_parking, "paid_parking": paid_parking, "user": mongo.db.users.find_one({"username": walk["user"]})["username"] } mongo.db.routes.update({"_id": ObjectId(route_id)}, updated) flash("Walk edited successfully!") return redirect(url_for("user_profile", username=walk["user"])) return render_template("editwalk.html", walk=walk, editForm=editForm, page_title=page_title)