beetroit

Untitled

Mar 12th, 2023 (edited)
1,484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | Source Code | 0 0
  1. import os
  2. from flask import Flask, render_template, request
  3. from flask_sqlalchemy import SQLAlchemy
  4. from sqlalchemy.sql import func  # REMOVE &
  5. # To keep track of when what was added was added.
  6. from datetime import datetime
  7.  
  8. # A little trick that says turn this file into a flask application.
  9. app = Flask('My Flask App') # you can also specify the name of the app
  10. # Adding data base
  11. # Using use the os.path.abspath() function to get the absolute path of the current file’s directory. ##UNNECESSARY
  12. # DB IS AUTO CREATED IN SAME DIR AS FILE
  13. app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
  14. # + os.path.join("Users/Ude-Ohanta/Desktop/flask project", "froshims.db) ##REMOVE
  15.  
  16. # Creating an SQLAlchemy instance
  17. db = SQLAlchemy(app)
  18.  
  19. # Models
  20.  
  21.  
  22. class Users(db.Model):
  23.     # id : Field which stores unique id for every row in
  24.     # database table.
  25.     # first_name: Used to store the first name if the user
  26.     # last_name: Used to store last name of the user
  27.     # Age: Used to store the age of the user
  28.     id = db.Column(db.Integer, primary_key=True)
  29.     name = db.Column(db.String(10), nullable=False)
  30.     sports = db.Column(db.String(10), nullable=False)
  31.     created_at = db.Column(db.DateTime, default=datetime.utcnow)
  32.  
  33.    # repr method represents how one object of this datatable
  34.     # will look like
  35.     def __repr__(self):
  36.         return f"Name : {self.name}, Sports: {self.sports}"
  37.  
  38.  
  39. SPORTS = [
  40.     "Basketball",
  41.     "Soccer",
  42.     "Ulitimate Frisbee"
  43. ]
  44.  
  45.  
  46. @app.route("/")
  47. def index():
  48.     return render_template("index.html", sports=SPORTS)
  49.  
  50.  
  51. @app.route("/register", methods=['POST'])
  52. def register():
  53.     if not db.engine.has_table('user'): #CHECKS IF THE TABLE EXISTS IN DB
  54.         db.create_all() #CREATES IT IF IT DOESN'T
  55.  
  56.     # validates submission
  57.     name = request.form.get("name")
  58.     sports = request.form.get("sports")
  59.  
  60.     if not name and sports not in SPORTS:
  61.         return render_template("failure.html")
  62.    
  63.     user = Users(
  64.         name = name,
  65.         sports = sports,
  66.         created_at =datetime.now()
  67.     )
  68.    
  69.     db.session.add(user)
  70.     db.session.commit()
  71.     # remeber registrant
  72.     # ("INSERT INTO registrants (name, sport) VALUES(?, ?)", name, sport #REMOVE
  73.     # index into the dict the students name and set it equal to sport.
  74.     RESGISTRANTS[name] = sport
  75.     return render_template("success.html")
  76.  
  77.  
  78. @app.route("/registrants")
  79. def registrants():
  80.     users = Users.query.all()
  81.     for i,user in enumerate(users):
  82.         print(f"index: {i} User: {user}")
  83.     return render_template("registrants.html", registrants=RESGISTRANTS)
  84.  
  85. app.run(debug=True) #TO RUN THE APP BY SIMPLY EXECUTING THE FILE OR WITH python filename.py, where filename is the name of this file
  86.  
Advertisement
Add Comment
Please, Sign In to add comment