Skip to content

Commit 98aecdc

Browse files
author
Théo Vidal
authored
Merge pull request #1 from BecauseOfProg/develop
Release V2.2.0
2 parents e9b605a + 48f9163 commit 98aecdc

23 files changed

+453
-476
lines changed

Pipfile

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ verify_ssl = true
44
name = "pypi"
55

66
[packages]
7-
Flask = "==1.0.2"
8-
"argon2-cffi" = "==18.3.0"
9-
bcrypt = "==3.1.4"
7+
"argon2-cffi" = "==20.1.0"
8+
bcrypt = "==3.1.7"
109
pony = "==0.7.6"
1110
flup = "==1.0.3"
11+
flask = "==1.1.2"
12+
flask-cors = "*"
13+
python-urlify = {editable = true,git = "git://github.com/dreikanter/python-urlify"}
1214

1315
[dev-packages]
1416

Pipfile.lock

Lines changed: 126 additions & 138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controllers/auth.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
from pony.orm import db_session
22
from app.models.users import User
33
from app.controllers.users import UsersController
4-
from core.exceptions import InvalidCreds
4+
from core.exceptions import InvalidCredentials
55
from core.utils.passwords import ArgonHasher, BcryptHasher
66

77

88
class AuthController:
99
@staticmethod
1010
@db_session
11-
def create_session(email, password):
12-
for user in User.select(lambda u: u.email == email):
13-
if user.password_type == 'bcrypt':
14-
if BcryptHasher.are_password_same(user.password, password):
15-
return {
16-
'user': UsersController.get_one_by_token(user.token),
17-
'token': user.token
18-
}
19-
else:
20-
raise InvalidCreds
11+
def create_session(email: str, password: str):
12+
user = User.get(email=email)
13+
if user is None:
14+
raise InvalidCredentials
15+
16+
if user.password_type == 'bcrypt':
17+
if BcryptHasher.are_password_same(user.password, password):
18+
return UsersController.get_one_by_token(user.token), user.token
2119
else:
22-
if ArgonHasher.are_password_same(user.password, password):
23-
return {
24-
'user': UsersController.get_one_by_token(user.token),
25-
'token': user.token
26-
}
27-
else:
28-
raise InvalidCreds
29-
30-
raise InvalidCreds
20+
raise InvalidCredentials
21+
else:
22+
if ArgonHasher.are_password_same(user.password, password):
23+
return UsersController.get_one_by_token(user.token), user.token
24+
else:
25+
raise InvalidCredentials

app/controllers/blog_posts.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,59 @@
11
import time
2-
32
from pony.orm import *
4-
from werkzeug.exceptions import NotFound
3+
from core.utils.ids import generate_url
54
from app.controllers.users import UsersController
65
from app.models.blog_posts import BlogPost
76

87

98
class BlogPostsController:
109
@staticmethod
11-
def fill_informations(post: BlogPost, without_content: bool = False):
12-
if without_content:
13-
to_exclude = 'content'
14-
else:
15-
to_exclude = None
16-
p = post.to_dict(exclude=to_exclude)
17-
p['author'] = UsersController.get_one(p['author'])
18-
return p
10+
def fill_information(post: BlogPost, include_content: bool = False):
11+
to_exclude = None if include_content else 'content'
12+
post = post.to_dict(exclude=to_exclude)
13+
post['author'] = UsersController.get_one(post['author'])
14+
return post
1915

2016
@staticmethod
2117
@db_session
22-
def get_all():
23-
posts = list(BlogPost.select().order_by(desc(BlogPost.timestamp)))
18+
def multi_fill_information(posts: [BlogPost], include_content: bool = False):
19+
posts = list(posts)
2420
for post in posts:
25-
posts[posts.index(post)] = BlogPostsController.fill_informations(post, True)
21+
posts[posts.index(post)] = BlogPostsController.fill_information(post, include_content)
2622
return posts
2723

24+
@staticmethod
25+
@db_session
26+
def fetch_all():
27+
return BlogPost.select().sort_by(desc(BlogPost.timestamp))
28+
29+
@staticmethod
30+
@db_session
31+
def filter_by_category(posts, category):
32+
return posts.where(category=category)
33+
34+
@staticmethod
35+
@db_session
36+
def filter_by_type(posts, type):
37+
return posts.where(type=type)
38+
2839
@staticmethod
2940
@db_session
3041
def get_last():
31-
posts = list(BlogPost.select().order_by(desc(BlogPost.timestamp)))
32-
return BlogPostsController.fill_informations(posts[0])
42+
posts = BlogPostsController.fetch_all()
43+
return BlogPostsController.fill_information(posts.first(), include_content=True)
3344

3445
@staticmethod
3546
@db_session
3647
def get_one(url):
37-
try:
38-
return BlogPostsController.fill_informations(BlogPost[url])
39-
except core.ObjectNotFound:
40-
raise NotFound
48+
return BlogPostsController.fill_information(BlogPost[url], include_content=True)
4149

4250
@staticmethod
4351
@db_session
4452
def create_one(params, optional_data):
4553
timestamp = int(time.time())
4654

4755
# Required fields
48-
post = BlogPost(url=params['url'],
56+
post = BlogPost(url=generate_url(params['title']),
4957
title=params['title'],
5058
timestamp=timestamp,
5159
author=params['author_username'],
@@ -65,20 +73,14 @@ def create_one(params, optional_data):
6573
@staticmethod
6674
@db_session
6775
def update_one(url, params, optional_data):
68-
try:
69-
post = BlogPost[url]
70-
for field in optional_data:
71-
if field in params:
72-
setattr(post, field, params[field])
73-
commit()
74-
except core.ObjectNotFound:
75-
raise NotFound
76+
post = BlogPost[url]
77+
for field in optional_data:
78+
if field in params:
79+
setattr(post, field, params[field])
80+
commit()
7681

7782
@staticmethod
7883
@db_session
7984
def delete_one(url):
80-
try:
81-
BlogPost[url].delete()
82-
return True
83-
except core.ObjectNotFound:
84-
raise NotFound
85+
BlogPost[url].delete()
86+
return True

app/controllers/posts.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,68 @@
11
import time
2-
32
from pony.orm import *
4-
from werkzeug.exceptions import NotFound
5-
from core.exceptions import DataError
6-
3+
from core.utils.ids import generate_url
74
from app.controllers.users import UsersController
85
from app.models.posts import Post
9-
from core.utils import ids
106

117

128
class PostsController:
139
@staticmethod
14-
def fill_informations(post: Post, additional_fields: list = []):
15-
fields = ['title', 'url', 'category', 'author', 'timestamp', 'banner'] + additional_fields
16-
post = post.to_dict(only=fields)
10+
@db_session
11+
def fill_information(post: Post, include_content: bool = False):
12+
to_exclude = None if include_content else 'content'
13+
post = post.to_dict(exclude=to_exclude)
1714
post['author'] = UsersController.get_one(post['author'])
1815
return post
1916

2017
@staticmethod
2118
@db_session
22-
def get_all():
23-
posts = list(Post.select().order_by(desc(Post.timestamp)))
19+
def multi_fill_information(posts: [Post], include_content: bool = False):
20+
posts = list(posts)
2421
for post in posts:
25-
posts[posts.index(post)] = PostsController.fill_informations(post)
22+
posts[posts.index(post)] = PostsController.fill_information(post, include_content)
2623
return posts
2724

25+
@staticmethod
26+
@db_session
27+
def fetch_all():
28+
return Post.select().sort_by(desc(Post.timestamp))
29+
2830
@staticmethod
2931
@db_session
3032
def get_last():
31-
posts = list(Post.select().order_by(desc(Post.timestamp)))
32-
return PostsController.fill_informations(posts[0])
33+
posts = PostsController.fetch_all()
34+
return PostsController.fill_information(posts.first(), True)
3335

3436
@staticmethod
3537
@db_session
3638
def get_one(url):
37-
try:
38-
return PostsController.fill_informations(Post[url], ['content'])
39-
except core.ObjectNotFound:
40-
raise NotFound
39+
return PostsController.fill_information(Post[url], True)
4140

4241
@staticmethod
4342
@db_session
44-
def create_one(title, url, category, content, banner, author_username):
43+
def create_one(params):
4544
timestamp = int(time.time())
46-
post = Post(title=title,
47-
url=url,
48-
content=content,
49-
category=category,
50-
banner=banner,
45+
post = Post(title=params['title'],
46+
url=generate_url(params['title']),
47+
content=params['content'],
48+
category=params['category'],
49+
banner=params['banner'],
5150
timestamp=timestamp,
52-
author=author_username)
51+
author=params['author_username'])
5352
commit()
5453
return True
5554

5655
@staticmethod
5756
@db_session
5857
def update_one(url, params, optional_data):
59-
try:
60-
post = Post[url]
61-
for field in optional_data:
62-
if field in params:
63-
setattr(post, field, params[field])
64-
commit()
65-
except core.ObjectNotFound:
66-
raise NotFound
58+
post = Post[url]
59+
for field in optional_data:
60+
if field in params:
61+
setattr(post, field, params[field])
62+
commit()
6763

6864
@staticmethod
6965
@db_session
7066
def delete_one(url):
71-
try:
72-
Post[url].delete()
73-
return True
74-
except core.ObjectNotFound:
75-
raise NotFound
67+
Post[url].delete()
68+
return True

0 commit comments

Comments
 (0)