Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- forms.py
- from flask_wtf import FlaskForm
- from wtforms.fields import EmailField
- from wtforms.validators import DataRequired, Length
- class EmptyForm(FlaskForm):
- pass
- class EmailForm(FlaskForm):
- email = EmailField('Email', validators=
- [
- DataRequired('Email is required'),
- # Is the line below useful
- Length(min=4, max=25, message='Must be between 4 and 25 characters'),
- ])
- functions.py
- from flask import flash
- from app import db
- def add_foreign_key(email_form):
- '''
- This runs in the /donation route.
- if the email exists in the User table and the email exists in the Payment table exists add the FK.
- The function only works if you are making a donation with an User who has an account.
- You will always have a registered account when adding Foreign key.
- '''
- # makes sure the tables are not (None / empty) list + contain same emails in the db
- if db.session.execute(db.select(User).filter_by(email=email_form)).scalar_one_or_none() and \
- db.session.execute(db.select(Payments).filter_by(email=email_form)).scalar_one_or_none():
- user_db = db.session.execute(db.select(User).filter_by(email=email_form)).scalar_one_or_none()
- payment_db = db.session.execute(db.select(Payments).filter_by(email=email_form)).scalar_one_or_none()
- if user_db.email == payment_db.email:
- user_id_db = user_db.id
- payment_db.fk_user_id = user_id_db
- db.session.commit()
- flash('Success the Foreign key is added')
- return 'success'
- # executes if the "if" statements doesn't activate?
- print('The Foreign key is not added')
Advertisement
Add Comment
Please, Sign In to add comment