-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello.py
104 lines (79 loc) · 2.9 KB
/
hello.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from flask import Flask, render_template, flash
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from flask_login import UserMixin
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = "spectres key"
db = SQLAlchemy(app)
app.app_context().push()
class Users(db.Model, UserMixin):
Register_id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), nullable=False, unique=True)
email = db.Column(db.String(120), nullable=False, unique=True)
data = db.Column(db.String(120))
# password_hash = db.Column(db.String(128))
date_added = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Name %r>' % self.name
if __name__ == "__main__":
app.run()
# form class , call this class
class NamerForm(FlaskForm):
name = StringField("whats your name?", validators=[DataRequired()])
submit = SubmitField("submit")
class UserForm(FlaskForm):
name = StringField("Name", validators=[DataRequired()])
Register_id = StringField("Register_Id", validators=[DataRequired()])
email = StringField("Email", validators=[DataRequired()])
submit = SubmitField("submit")
@app.route('/user/add', methods=['GET', 'POST'])
def add_user():
name = None
form = UserForm()
if form.validate_on_submit():
user = Users.query.filter_by(email=form.email.data).first()
if user is None:
user = Users(username=form.username.data, name=form.name.data, email=form.email.data,)
db.session.add(user)
db.session.commit()
name = form.name.data
form.name.data = ''
form.username.data = ''
form.email.data = ''
form.favorite_color.data = ''
form.password_hash.data = ''
flash("User Added Successfully!")
our_users = Users.query.order_by(Users.date_added)
return render_template("add_user.html",
form=form,
name=name,
our_users=our_users)
@app.route('/')
def index():
return render_template("index.html")
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html"), 404
@app.errorhandler(500)
def page_not_found(e):
return render_template("500.html"), 500
@app.route('/name', methods=['GET', 'POST'])
def name():
name = None
form = NamerForm()
# Validate Form
if form.validate_on_submit():
name = form.name.data
form.name.data = ''
flash("Form Submitted Successfully!")
return render_template("name.html",
name=name,
form=form)
# def test_connection(self):
# with app.app_context():
# #test code