Guest User

Untitled

a guest
Nov 6th, 2024
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. forms.py
  2.  
  3.  
  4. from flask_wtf import FlaskForm
  5. from wtforms.fields import EmailField
  6. from wtforms.validators import DataRequired, Length
  7.  
  8.  
  9. class EmptyForm(FlaskForm):
  10.     pass
  11.  
  12. class EmailForm(FlaskForm):
  13.    
  14.     email = EmailField('Email', validators=
  15.     [
  16.     DataRequired('Email is required'),
  17.     # Is the line below useful
  18.     Length(min=4, max=25, message='Must be between 4 and 25 characters'),
  19.     ])
  20.  
  21.  
  22.  
  23. functions.py
  24.  
  25.  
  26.  
  27.  
  28. from flask import flash
  29. from app import db
  30.  
  31.  
  32. def add_foreign_key(email_form):
  33.     '''
  34.    This runs in the /donation route.
  35.    if the email exists in the User table and the email exists in the Payment table exists add the FK.
  36.    The function only works if you are making a donation with an User who has an account.
  37.    You will always have a registered account when adding Foreign key.
  38.    '''
  39.  
  40.     # makes sure the tables are not (None / empty) list + contain same emails in the db
  41.     if db.session.execute(db.select(User).filter_by(email=email_form)).scalar_one_or_none() and \
  42.        db.session.execute(db.select(Payments).filter_by(email=email_form)).scalar_one_or_none():
  43.        
  44.         user_db = db.session.execute(db.select(User).filter_by(email=email_form)).scalar_one_or_none()
  45.         payment_db = db.session.execute(db.select(Payments).filter_by(email=email_form)).scalar_one_or_none()
  46.        
  47.         if user_db.email == payment_db.email:
  48.             user_id_db = user_db.id
  49.             payment_db.fk_user_id = user_id_db
  50.             db.session.commit()
  51.             flash('Success the Foreign key is added')
  52.             return 'success'
  53.     # executes if the "if" statements doesn't activate?    
  54.     print('The Foreign key is not added')    
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
Advertisement
Add Comment
Please, Sign In to add comment