-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathroutes.py
31 lines (26 loc) · 1.15 KB
/
routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""Application routes."""
from datetime import datetime
from flask import current_app as app
from flask import make_response, redirect, render_template, request, url_for
from .models import User, db
@app.route("/", methods=["GET"])
def user_records():
"""Create a user via query string parameters."""
username = request.args.get("user")
email = request.args.get("email")
if username and email:
existing_user = User.query.filter(User.username == username or User.email == email).first()
if existing_user:
return make_response(f"{username} ({email}) already created!")
new_user = User(
username=username,
email=email,
created=datetime.now(),
bio="In West Philadelphia born and raised, \
on the playground is where I spent most of my days",
admin=False,
) # Create an instance of the User class
db.session.add(new_user) # Adds new User record to database
db.session.commit() # Commits all changes
redirect(url_for("user_records"))
return render_template("users.jinja2", users=User.query.all(), title="All Users")