forked from serhii-korobchenko/Python_Web_Team2_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_flask.py
More file actions
291 lines (217 loc) · 10.2 KB
/
app_flask.py
File metadata and controls
291 lines (217 loc) · 10.2 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from flask import Flask, render_template, request, redirect, url_for, session, flash, make_response
from models import Email, Record, Adress, Phone, Birthday, Note, Tag, User, News
from db import db_session
from Pekemons_IT_CLI_bot_with_SQLite import main, help_information
from datetime import datetime
from logging import DEBUG
import users
from libs.validation_schemas import LoginSchema, RegistrationSchema
from marshmallow import ValidationError
from sqlalchemy.exc import IntegrityError
import uuid
from datetime import datetime, timedelta
from scraping_bbc_news import scriping_bbc
from pandas import Timestamp
# from flask_sqlalchemy import SQLAlchemy
# from flask_migrate import Migrate
app = Flask(__name__)
app.secret_key = b'pythonwebteam4'
app.debug = True
app.env = "development"
app.logger.setLevel(DEBUG)
# db = SQLAlchemy(app)
# migrate = Migrate(app, db)
class Record_Object:
def __init__(self, name, phone, adress=None, email=None, birthday=None, user_id =None):
self.name = name
self.phone = phone
self.adress = adress
self.email = email
self.birthday = birthday
self.user_id = user_id
class Note_Object:
def __init__(self, note_title, note_text, created, tags=None, user_id =None):
self.note_title = note_title
self.note_text = note_text
self.created = created
self.tags = tags
self.user_id = user_id
def myFunc(item):
res = datetime.timestamp(item.created)
print(res)
return res
@app.before_request
def before_func():
auth = True if 'username' in session else False
if not auth:
token_user = request.cookies.get('username')
if token_user:
user = users.get_user_by_token(token_user)
if user:
session['username'] = {"username": user.username, "id": user.id}
@app.route('/healthcheck/', strict_slashes=False)
def healthcheck():
return 'I am working'
#
# @app.route('/', strict_slashes=False)
# def index():
# auth = True if 'username' in session else False
# return render_template('index.html', title='Future is near!', auth=auth)
@app.route("/", methods=['GET', 'POST'], strict_slashes=False)
def bot():
auth = True if 'username' in session else False
command = None
if request.method == "POST":
command = request.form.get("command")
if 'addbirthday' in command:
command_args = command.split(" ")
messages = command_args[1]
return redirect(url_for('add_birthday', messages=messages))
else:
main(command)
return redirect(url_for('bot'))
if auth:
values = list(session.values())
username_session = values[-1]["username"]
return render_template("bot1.html", help_information=help_information, auth=auth, title='Please sign in!', username_session = username_session)
else:
return render_template("bot1.html", help_information=help_information, auth=auth, title='Please sign in!')
@app.route('/registration/', methods=['GET', 'POST'], strict_slashes=False)
def registration():
auth = True if 'username' in session else False
if auth:
return redirect(url_for('bot'))
if request.method == 'POST':
try:
RegistrationSchema().load(request.form)
except ValidationError as err:
return render_template('registration.html', messages=err.messages)
email = request.form.get('email')
password = request.form.get('password')
nick = request.form.get('nick')
try:
user = users.create_user(email, password, nick)
print(user)
return redirect(url_for('login'))
except IntegrityError as err:
print(err)
return render_template('registration.html', messages={'error': f'User with email {email} exist!'})
return render_template('registration.html')
@app.route('/login/', methods=['GET', 'POST'], strict_slashes=False)
def login():
auth = True if 'username' in session else False
if request.method == 'POST':
try:
LoginSchema().load(request.form)
except ValidationError as err:
return render_template('login.html', messages=err.messages)
email = request.form.get('email')
password = request.form.get('password')
remember = True if request.form.get('remember') == 'on' else False
user = users.login(email, password)
if user is None:
return render_template('login.html', messages={'err': 'Invalid credentials! Goto admin bro :)'})
session['username'] = {"username": user.username, "id": user.id}
response = make_response(redirect(url_for('bot')))
if remember:
# Треба створить token, та покласти його в cookie та БД
token = str(uuid.uuid4())
expire_data = datetime.now() + timedelta(days=60)
response.set_cookie('username', token, expires=expire_data)
users.set_token(user, token)
return response
if auth:
return redirect(url_for('bot'))
else:
return render_template('login.html')
@app.route('/logout/', strict_slashes=False)
def logout():
auth = True if 'username' in session else False
if not auth:
return redirect(request.url) # Відправляє туди звідки він прийшов
session.pop('username')
response = make_response(redirect(url_for('bot')))
response.set_cookie('username', '', expires=-1)
return response
@app.route("/birthday/", methods=["GET", "POST"], strict_slashes=False)
def add_birthday():
user_id = db_session.query(User.id).filter(User.username == list(session.values())[-1]["username"]).first()[0]
if request.method == "GET":
messages = request.args['messages']
global name_up
name_up = messages
else:
name = name_up
birthday_date_str = request.form.get("birthday_date")
birthday_date = datetime.strptime(birthday_date_str, '%Y-%m-%d')
print(f'TYPE OF birthday_date {type(birthday_date)}, RESULT = {birthday_date}')
try:
birthday_for_id = db_session.query(Birthday.birthday_date).filter(
Birthday.rec_id == str(db_session.query(Record.id).filter(Record.name == name).first()[0])).all()
print(birthday_for_id)
if not birthday_for_id:
birthday1 = Birthday(birthday_date=birthday_date,
rec_id=str(db_session.query(Record.id).filter(Record.name == name).first()[0]), user_id=user_id)
db_session.add(birthday1)
db_session.commit()
print('Birthday has been added successfully!')
flash('Birthday has been added successfully!')
except Exception as e:
# flash (e.args)
flash('Record with mentioned name does not exist. Try again!')
return redirect("/")
db_session.close()
return render_template("add_birthday.html", messages=messages)
@app.route("/records_DB/", methods=["GET"], strict_slashes=False)
def data_base_rec():
command = None
user_id = db_session.query(User.id).filter(User.username == list(session.values())[-1]["username"]).first()[0]
user_name = list(session.values())[-1]["username"]
records = db_session.query(Record).filter(Record.user_id == user_id).all()
# phones = db_session.query(Phone).all()
record_list = []
for record in records:
record_id = record.id
phone = db_session.query(Phone.phone_name).filter(Phone.rec_id == record.id).all() if db_session.query(Phone.phone_name).filter(Phone.rec_id == record.id).all() else '*'
adress = db_session.query(Adress.adress_name).filter(Adress.rec_id == record.id).all() if db_session.query(Adress.adress_name).filter(Adress.rec_id == record.id).all() else '*'
email = db_session.query(Email.email_name).filter(Email.rec_id == record.id).all() if db_session.query(Email.email_name).filter(Email.rec_id == record.id).all() else '*'
birthday = db_session.query(Birthday.birthday_date).filter(Birthday.rec_id == record.id).all() if db_session.query(Birthday.birthday_date).filter(Birthday.rec_id == record.id).all() else '*'
item_rec = Record_Object(record.name, phone, adress, email, birthday)
item_rec.record_id = record_id
record_list.append(item_rec)
db_session.close()
return render_template("data_base_rd.html", record_list=record_list, user_name = user_name)
@app.route("/notes_DB/", methods=["GET"], strict_slashes=False)
def data_base_notes():
user_id = db_session.query(User.id).filter(User.username == list(session.values())[-1]["username"]).first()[0]
user_name = list(session.values())[-1]["username"]
notes = db_session.query(Note).filter(Note.user_id == user_id).all()
notes_list = []
for note in notes:
note_id = note.id
tags = db_session.query(Tag.tag_text).filter(Tag.note_id == note.id).all() if db_session.query(Tag.tag_text).filter(Tag.note_id == note.id).all() else '*'
item_note = Note_Object(note.note_title, note.note_text, note.created, tags)
item_note.note_id = note_id
notes_list.append(item_note)
db_session.close()
return render_template("data_base_nt.html", notes_list=notes_list, user_name = user_name)
@app.route("/news/", methods=['GET'], strict_slashes=False)
def news():
scriping_bbc()
user_id = db_session.query(User.id).filter(User.username == list(session.values())[-1]["username"]).first()[0]
user_name = list(session.values())[-1]["username"]
auth = True if 'username' in session else False
command = None
if auth:
values = list(session.values())
username_session = values[-1]["username"]
all_news_list = []
all_news = db_session.query(News).all()
for news in all_news:
all_news_list.append(news)
all_news_list.sort(reverse=True, key=myFunc)
return render_template("news.html", all_news_list=all_news_list, auth=auth, title='News', username_session = username_session)
else:
return render_template("bot1.html", help_information=help_information, auth=auth, title='Please sign in!')
if __name__ == "__main__":
app.run()