Skip to content

Commit

Permalink
Merge pull request #1 from BecauseOfProg/develop
Browse files Browse the repository at this point in the history
Release V2.2.0
  • Loading branch information
Théo Vidal authored Jun 29, 2020
2 parents e9b605a + 48f9163 commit 98aecdc
Show file tree
Hide file tree
Showing 23 changed files with 453 additions and 476 deletions.
8 changes: 5 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ verify_ssl = true
name = "pypi"

[packages]
Flask = "==1.0.2"
"argon2-cffi" = "==18.3.0"
bcrypt = "==3.1.4"
"argon2-cffi" = "==20.1.0"
bcrypt = "==3.1.7"
pony = "==0.7.6"
flup = "==1.0.3"
flask = "==1.1.2"
flask-cors = "*"
python-urlify = {editable = true,git = "git://github.com/dreikanter/python-urlify"}

[dev-packages]

Expand Down
264 changes: 126 additions & 138 deletions Pipfile.lock

Large diffs are not rendered by default.

35 changes: 15 additions & 20 deletions app/controllers/auth.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
from pony.orm import db_session
from app.models.users import User
from app.controllers.users import UsersController
from core.exceptions import InvalidCreds
from core.exceptions import InvalidCredentials
from core.utils.passwords import ArgonHasher, BcryptHasher


class AuthController:
@staticmethod
@db_session
def create_session(email, password):
for user in User.select(lambda u: u.email == email):
if user.password_type == 'bcrypt':
if BcryptHasher.are_password_same(user.password, password):
return {
'user': UsersController.get_one_by_token(user.token),
'token': user.token
}
else:
raise InvalidCreds
def create_session(email: str, password: str):
user = User.get(email=email)
if user is None:
raise InvalidCredentials

if user.password_type == 'bcrypt':
if BcryptHasher.are_password_same(user.password, password):
return UsersController.get_one_by_token(user.token), user.token
else:
if ArgonHasher.are_password_same(user.password, password):
return {
'user': UsersController.get_one_by_token(user.token),
'token': user.token
}
else:
raise InvalidCreds

raise InvalidCreds
raise InvalidCredentials
else:
if ArgonHasher.are_password_same(user.password, password):
return UsersController.get_one_by_token(user.token), user.token
else:
raise InvalidCredentials
68 changes: 35 additions & 33 deletions app/controllers/blog_posts.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,59 @@
import time

from pony.orm import *
from werkzeug.exceptions import NotFound
from core.utils.ids import generate_url
from app.controllers.users import UsersController
from app.models.blog_posts import BlogPost


class BlogPostsController:
@staticmethod
def fill_informations(post: BlogPost, without_content: bool = False):
if without_content:
to_exclude = 'content'
else:
to_exclude = None
p = post.to_dict(exclude=to_exclude)
p['author'] = UsersController.get_one(p['author'])
return p
def fill_information(post: BlogPost, include_content: bool = False):
to_exclude = None if include_content else 'content'
post = post.to_dict(exclude=to_exclude)
post['author'] = UsersController.get_one(post['author'])
return post

@staticmethod
@db_session
def get_all():
posts = list(BlogPost.select().order_by(desc(BlogPost.timestamp)))
def multi_fill_information(posts: [BlogPost], include_content: bool = False):
posts = list(posts)
for post in posts:
posts[posts.index(post)] = BlogPostsController.fill_informations(post, True)
posts[posts.index(post)] = BlogPostsController.fill_information(post, include_content)
return posts

@staticmethod
@db_session
def fetch_all():
return BlogPost.select().sort_by(desc(BlogPost.timestamp))

@staticmethod
@db_session
def filter_by_category(posts, category):
return posts.where(category=category)

@staticmethod
@db_session
def filter_by_type(posts, type):
return posts.where(type=type)

@staticmethod
@db_session
def get_last():
posts = list(BlogPost.select().order_by(desc(BlogPost.timestamp)))
return BlogPostsController.fill_informations(posts[0])
posts = BlogPostsController.fetch_all()
return BlogPostsController.fill_information(posts.first(), include_content=True)

@staticmethod
@db_session
def get_one(url):
try:
return BlogPostsController.fill_informations(BlogPost[url])
except core.ObjectNotFound:
raise NotFound
return BlogPostsController.fill_information(BlogPost[url], include_content=True)

@staticmethod
@db_session
def create_one(params, optional_data):
timestamp = int(time.time())

# Required fields
post = BlogPost(url=params['url'],
post = BlogPost(url=generate_url(params['title']),
title=params['title'],
timestamp=timestamp,
author=params['author_username'],
Expand All @@ -65,20 +73,14 @@ def create_one(params, optional_data):
@staticmethod
@db_session
def update_one(url, params, optional_data):
try:
post = BlogPost[url]
for field in optional_data:
if field in params:
setattr(post, field, params[field])
commit()
except core.ObjectNotFound:
raise NotFound
post = BlogPost[url]
for field in optional_data:
if field in params:
setattr(post, field, params[field])
commit()

@staticmethod
@db_session
def delete_one(url):
try:
BlogPost[url].delete()
return True
except core.ObjectNotFound:
raise NotFound
BlogPost[url].delete()
return True
67 changes: 30 additions & 37 deletions app/controllers/posts.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,68 @@
import time

from pony.orm import *
from werkzeug.exceptions import NotFound
from core.exceptions import DataError

from core.utils.ids import generate_url
from app.controllers.users import UsersController
from app.models.posts import Post
from core.utils import ids


class PostsController:
@staticmethod
def fill_informations(post: Post, additional_fields: list = []):
fields = ['title', 'url', 'category', 'author', 'timestamp', 'banner'] + additional_fields
post = post.to_dict(only=fields)
@db_session
def fill_information(post: Post, include_content: bool = False):
to_exclude = None if include_content else 'content'
post = post.to_dict(exclude=to_exclude)
post['author'] = UsersController.get_one(post['author'])
return post

@staticmethod
@db_session
def get_all():
posts = list(Post.select().order_by(desc(Post.timestamp)))
def multi_fill_information(posts: [Post], include_content: bool = False):
posts = list(posts)
for post in posts:
posts[posts.index(post)] = PostsController.fill_informations(post)
posts[posts.index(post)] = PostsController.fill_information(post, include_content)
return posts

@staticmethod
@db_session
def fetch_all():
return Post.select().sort_by(desc(Post.timestamp))

@staticmethod
@db_session
def get_last():
posts = list(Post.select().order_by(desc(Post.timestamp)))
return PostsController.fill_informations(posts[0])
posts = PostsController.fetch_all()
return PostsController.fill_information(posts.first(), True)

@staticmethod
@db_session
def get_one(url):
try:
return PostsController.fill_informations(Post[url], ['content'])
except core.ObjectNotFound:
raise NotFound
return PostsController.fill_information(Post[url], True)

@staticmethod
@db_session
def create_one(title, url, category, content, banner, author_username):
def create_one(params):
timestamp = int(time.time())
post = Post(title=title,
url=url,
content=content,
category=category,
banner=banner,
post = Post(title=params['title'],
url=generate_url(params['title']),
content=params['content'],
category=params['category'],
banner=params['banner'],
timestamp=timestamp,
author=author_username)
author=params['author_username'])
commit()
return True

@staticmethod
@db_session
def update_one(url, params, optional_data):
try:
post = Post[url]
for field in optional_data:
if field in params:
setattr(post, field, params[field])
commit()
except core.ObjectNotFound:
raise NotFound
post = Post[url]
for field in optional_data:
if field in params:
setattr(post, field, params[field])
commit()

@staticmethod
@db_session
def delete_one(url):
try:
Post[url].delete()
return True
except core.ObjectNotFound:
raise NotFound
Post[url].delete()
return True
Loading

0 comments on commit 98aecdc

Please sign in to comment.