def userform(action): form = UserForm() # conn = datastorage.init() # Cancel if form.cancel.data == True: return redirect('/index') if form.validate_on_submit(): if form.user_id.data: flash('Add task for id: {}, action: {}'.format( form.user_id.data, action)) # datastorage.add_task(conn, form.description_task.data, form.duedate_for_task.data) return redirect('/index') elif form.screen_name.data: flash('Add task for screen name: {}, action: {}'.format( form.screen_name.data, action)) # datastorage.add_task(conn, form.description_task.data, form.duedate_for_task.data) return redirect('/index') return render_template('UserForm.html', title='User ID or Screen name', form=form)
def new_user(): """New user route endpoint. Render form for custom record. Check uniqueness by email field. :return render user.html template if success """ form = UserForm() if form.validate_on_submit(): data = { "name": { "first": form.first_name.data, "last": form.last_name.data }, "gender": form.gender.data, "cell": form.phone.data, "email": form.email.data, "location": { "city": form.city.data, "country": form.country.data, "street": { "name": form.street.data }, }, "picture": { "large": form.picture.data }, } user = db.users.find_one({"email": form.email.data}) if not user: db.users.insert_one(data) flash("Successfully added") user = db.users.find_one_or_404({"email": form.email.data}) return render_template("user.html", user=user) flash("User with that email already exist") else: for field, errors in form.errors.items(): for error in errors: flash("Mistake in field '{}': {}".format( getattr(form, field).label.text, error)) return render_template("new_user.html", form=form)
def update_user(user_id: str): """Update user route endpoint. Updated user info and render user.html template in case of success. :param user_id: unique record id :return render user.html template """ form = UserForm() if form.validate_on_submit(): data = { "name": { "first": form.first_name.data, "last": form.last_name.data }, "gender": form.gender.data, "cell": form.phone.data, "email": form.email.data, "location": { "city": form.city.data, "country": form.country.data, "street": { "name": form.street.data }, }, "picture": { "large": form.picture.data }, } db.users.find_one_and_replace({"_id": ObjectId(user_id)}, data) flash("Successfully added") user = db.users.find_one_or_404({"_id": ObjectId(user_id)}) return render_template("user.html", user=user) else: for field, errors in form.errors.items(): for error in errors: flash("Mistake in field '{}': {}".format( getattr(form, field).label.text, error)) user = db.users.find_one_or_404({"_id": ObjectId(user_id)}) return render_template("update_user.html", user=user, form=form)