-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
343 lines (271 loc) · 9 KB
/
main.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
'''
Module DocString-To be added
'''
# SQLAlchemy's database model creates it's atributes at runtime so pylint throws errors
# pylint:disable=E1101
from math import ceil
import os
import time
from random import randint
from flask import Flask, render_template, redirect, url_for, flash, request
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
from models import DB, User, Post, Tag, Image, LoginForm, PostForm, \
RegisterForm, ProfileForm, CommentForm, Comment
APP = Flask(__name__)
APP.config['SECRET_KEY'] = "testkey"
APP.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
APP.config['UPLOAD_FOLDER'] = 'static///uploads///images'
DB.init_app(APP)
TAGS = ['golden', 'husky', 'crossbreed',
'labrador', 'adoption', 'foster',
'cute', 'big', 'medium', 'small',
'young','old','hurt']
# with APP.app_context():
# DB.create_all()
# for tag in TAGS:
# n=Tag(tag_name=tag)
# DB.session.add(n)
# DB.session.commit()
_ADMIN_RANK_ = 1
LOGIN_MANAGER = LoginManager()
LOGIN_MANAGER.init_app(APP)
LOGIN_MANAGER.login_view = 'login'
# Function to get the user object from the database so the login manager knows which user to login
@LOGIN_MANAGER.user_loader
def load_user(user_id):
'''
User loading function
'''
return User.query.filter_by(id=user_id).first()
class Paginator:
'''
Class to create pagination on the adoptions page
'''
def __init__(self, page, per_page, total):
self.page = page
self.per_page = per_page
self.total = total
@property
def pages(self):
'''
Number of pages
'''
return int(ceil(float(self.total)/self.per_page))
@property
def has_next(self):
'''
If there is a next page
'''
return self.page < self.pages
@property
def has_prev(self):
'''
If there is a previous page
'''
return self.page > 1
@APP.route('/')
@APP.route('/index')
def index():
'''
Index
'''
return render_template('index.html')
@APP.route('/login', methods=['GET', 'POST'])
def login():
'''
Login view
Workaround to have 2 forms on the same page
'''
if current_user.is_authenticated:
return redirect(url_for('index'))
form_register = RegisterForm()
form_login = LoginForm()
return render_template('login.html', form_login=form_login, form_register=form_register)
@APP.route('/loginn', methods=['POST'])
def loginn():
'''
Login endpoint
'''
form_login = LoginForm()
form_register = RegisterForm()
if request.method == 'POST':
if form_login.validate_on_submit():
new = User.query.filter_by(username=form_login.username.data).first()
if new is not None:
if check_password_hash(new.password, form_login.password.data):
login_user(new, remember=form_login.remember.data)
return redirect(url_for('index'))
else:
flash('Username or password is incorect')
else:
flash('Username or password is incorect')
else:
if current_user.is_authenticated:
return redirect(url_for('index'))
return render_template('login.html', form_login=form_login, form_register=form_register)
@APP.route('/register', methods=['POST'])
def register():
'''
Register endpoint
'''
if current_user.is_authenticated:
return redirect(url_for('index'))
form_login = LoginForm()
form_register = RegisterForm()
if form_register.validate_on_submit():
query_email = DB.session.query(User.id).filter_by(
email=form_register.email.data).scalar() is not None
query_user = DB.session.query(User.id).filter_by(
username=form_register.username.data).scalar() is not None
if not query_email and not query_user:
hashed_password = generate_password_hash(
form_register.password.data, method='sha256')
new = User(username=form_register.username.data,
password=hashed_password, email=form_register.email.data)
DB.session.add(new)
DB.session.commit()
return redirect(url_for('login'))
else:
flash('Username or Email already exist')
return render_template('login.html', form_login=form_login, form_register=form_register)
@APP.route('/adddog', methods=['GET', 'POST'])
@login_required
def adddog():
'''
Post page
'''
form = PostForm()
if request.method == 'POST' and form.validate_on_submit():
new = Post(title=form.title.data, text=form.post.data, user=current_user,
gender=form.gender.data, age=form.age.data)
for split in form.tags.data.split(' '):
tag = Tag.query.filter_by(tag_name=split).first()
if tag is not None:
new.tags.append(tag)
images = request.files.getlist("upload")
for img in images:
ext = img.filename.split(".")[1]
filename = str(int(time.time() * 1e6)+randint(300, 43242))+'.'+ext
image_file = os.path.join(APP.config['UPLOAD_FOLDER'], filename)
img.save(image_file)
new.images.append(Image(name=filename))
DB.session.add(new)
DB.session.commit()
return redirect(url_for('index'))
return render_template('post.html', form=form, tags=TAGS)
@APP.route('/dog', methods=['GET', 'POST'])
def dog():
'''
View dog
'''
form = CommentForm()
arguments = request.args
if 'id' in arguments:
post_id = int(arguments['id'])
else:
return redirect(url_for('index'))
query = Post.query.filter_by(id=post_id).first()
if query is None:
return redirect(url_for('index'))
if form.validate_on_submit():
gigi = Comment(text=form.text.data, user_id=current_user.id, post_id=post_id)
query.comments.append(gigi)
DB.session.commit()
comments = Comment.query.filter_by(post_id=post_id)
print(comments)
return render_template('dog.html', dog=query, comments=comments, form=form)
def is_admin(user):
'''
Check if user is admin
'''
if hasattr(user, 'rank') and user.rank == _ADMIN_RANK_:
return True
else:
return False
APP.jinja_env.globals['is_admin'] = is_admin
@APP.route('/adoptions', methods=['GET', 'POST'])
def adoptions():
'''
Adoptions page
'''
arguments = request.args
if 'page' in arguments:
page_number = int(arguments['page'])
else:
page_number = 1
count = 9
total = Post.query.count()
pag = Paginator(page_number, count, total)
posts = Post.query.offset((page_number-1)*count).limit(count).all()
return render_template('adoptions.html', posts=posts, paginator=pag)
@APP.route('/delete_post', methods=['POST'])
@login_required
def delete_post():
'''
Delete a post
'''
arguments = request.args
if 'id' in arguments:
post_id = int(arguments['id'])
query_post = Post.query.filter_by(id=post_id).first()
if query_post.user_id == current_user.id or is_admin(current_user):
DB.session.delete(query_post)
DB.session.commit()
return redirect(url_for('index'))
@APP.route('/bone', methods=['POST'])
@login_required
def bone():
'''
Like a dog
'''
data = request.get_json()
print(data)
pid = data['id']
action = data['action']
query_post = Post.query.filter_by(id=pid).first_or_404()
if action == 'like':
query_post.bones.append(current_user)
DB.session.commit()
if action == 'unlike' and current_user in query_post.bones:
query_post.bones.remove(current_user)
DB.session.commit()
print(query_post.bones)
return 'OK'
@APP.route('/logout')
@login_required
def logout():
'''
Logout endpoint
'''
logout_user()
return redirect(url_for('index'))
@APP.route('/profile', methods=['POST', 'GET'])
@login_required
def profile():
'''
Profile
'''
form = ProfileForm()
if request.method == 'POST' and form.validate_on_submit():
if form.last_name.data:
current_user.last_name = form.last_name.data
if form.first_name.data:
current_user.first_name = form.first_name.data
if form.phone:
current_user.phone = form.phone.data
if form.email:
current_user.email = form.email.data
DB.session.commit()
return redirect(url_for('index'))
else:
form.email.data = current_user.email
form.first_name.data = current_user.first_name
form.last_name.data = current_user.last_name
form.phone.data = current_user.phone
return render_template('profile.html', form=form)
@APP.route('/about', methods=['GET'])
def about():
return render_template('about.html')
if __name__ == '__main__':
APP.run(host='0.0.0.0', debug=True)