-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from BecauseOfProg/develop
Release V2.2.0
- Loading branch information
Showing
23 changed files
with
453 additions
and
476 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.