-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-with-sqlite.py
87 lines (77 loc) · 2.92 KB
/
app-with-sqlite.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
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import text
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./email.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
with app.app_context():
drop_table = text('DROP TABLE IF EXISTS users;')
users_table = text("""
CREATE TABLE users(
username VARCHAR NOT NULL PRIMARY KEY,
email VARCHAR);
""")
data = text("""
INSERT INTO users
VALUES
("John", "[email protected]");
""")
db.session.execute(drop_table)
db.session.execute(users_table)
db.session.execute(data)
db.session.commit()
def find_emails(keyword):
with app.app_context():
query = text(f"""
SELECT * FROM users WHERE username like '%{keyword}%';
""")
result = db.session.execute(query)
user_emails = [(row[0], row[1]) for row in result]
if not any(user_emails):
user_emails = [("Not Found", "Not Found")]
return user_emails
def insert_email(name,email):
with app.app_context():
query = text(f"""
SELECT * FROM users WHERE username like '{name}'
""")
result = db.session.execute(query)
response = ''
if len(name) == 0 or len(email) == 0:
response = 'Username or email can not be empty!!'
elif not any(result):
insert = text(f"""
INSERT INTO users
VALUES ('{name}', '{email}');
""")
result = db.session.execute(insert)
db.session.commit()
response = text(f"User {name} and {email} have been added successfully")
else:
response = text(f"User {name} already exist")
return response
@app.route('/', methods=['GET', 'POST'])
def emails():
with app.app_context():
if request.method == 'POST':
user_app_name = request.form['user_keyword']
user_emails = find_emails(user_app_name)
return render_template('emails.html', name_emails=user_emails, keyword=user_app_name, show_result=True)
else:
return render_template('emails.html', show_result=False)
@app.route('/add', methods=['GET', 'POST'])
def add_email():
with app.app_context():
if request.method == 'POST':
user_app_name = request.form['username']
user_app_email = request.form['useremail']
result_app = insert_email(user_app_name, user_app_email)
return render_template('add-email.html', result_html=result_app, show_result=True)
else:
return render_template('add-email.html', show_result=False)
# - Add a statement to run the Flask application which can be reached from any host on port 80.
if __name__=='__main__':
app.run(debug=True)
# app.run(host='0.0.0.0', port=80)
# https://flask-sqlalchemy.palletsprojects.com/en/2.x/config/