Beispiel #1
0
def delete_post(post_id):
    post = post_repo.find(post_id)
    if post_repo.post_exists(post) is False:
        abort(404, description='Post does not exist anymore')
    if post.author.id == current_identity.id:
        post_repo.delete_post(post_id)
        return resp_comm.response(message='Post deleted'), 200
    return resp_comm.response(success=False, message='Other user post'), 200
Beispiel #2
0
def update_post(req):
    post_id = req.json['id']
    new_title = req.json['title']
    new_body = req.json['body']
    updated_post = post_repo.modify_post(post_id, new_title, new_body)
    if updated_post:
        return resp_comm.response(
            data={'post': post_repo.to_dto(updated_post)}), 200
    return resp_comm.response(success=False,
                              message='Error while updating post'), 200
Beispiel #3
0
def create_post(req):
    title = req.json['title']
    body = req.json['body']
    author = current_identity.id
    post = post_repo.create(title, body, author)
    if post:
        post_dto = post_repo.to_dto(post)
        return resp_comm.response(message='Post created',
                                  data={'post': post_dto}), 201
    abort(500, description='Error while creating post')
Beispiel #4
0
def change_password(req):
    current_password = req.json['current_password']
    new_password = req.json['new_password']
    confirm_new_password = req.json['confirm_new_password']
    current_hashed_password = current_identity.password
    if bcrypt.check_password_hash(current_hashed_password,
                                  current_password) is False:
        return resp_comm.response(
            message='Incorrect password',
            data={'user': user_repo.to_dto(current_identity)},
            success=False), 400
    if new_password != confirm_new_password:
        return resp_comm.response(message='Password confirmation not matched',
                                  data={},
                                  success=False), 400
    changed_password = user_repo.change_password(
        current_identity.id,
        bcrypt.generate_password_hash(new_password).decode('utf-8'))
    return resp_comm.response(message='Password updated',
                              data=user_repo.to_dto(changed_password)), 200
Beispiel #5
0
def create_user(req):
    email = req.json['email']
    username = req.json['username']
    uexist = user_repo.find_by_email(email)
    upseudo = user_repo.find_by_username(username)
    if uexist:
        abort(400, description='Email already in use')
    if upseudo:
        abort(400, description='Username already in use')
    plain_password = req.json['password']
    password = bcrypt.generate_password_hash(plain_password)
    user = user_repo.create(email, username, password)
    userdata = user_repo.to_dto(user)
    return resp_comm.response(message='New user subscribed',
                              data={
                                  'user': userdata,
                              }), 201
Beispiel #6
0
def my_profile():
    me = user_repo.to_dto(current_identity)
    return resp_comm.response(message='Profile retrieved', data={
        'user': me,
    }), 200
Beispiel #7
0
def read_post(post_id):
    post = post_repo.find(post_id)
    if post:
        return resp_comm.response(message='Post found',
                                  data={'post': post_repo.to_dto(post)}), 200
    return resp_comm.response(success=False, message='Post not found'), 404
Beispiel #8
0
def my_posts():
    posts = post_repo.find_by_author(current_identity.id)
    post_dtos = post_repo.list_to_dto(posts)
    return resp_comm.response(message='Posts retrieved',
                              data={'posts': post_dtos}), 200
Beispiel #9
0
def handle_error(e):
    return resp_serv.response(success=False,
                              message=e.name,
                              data={'description': e.description}), e.code
Beispiel #10
0
from app.common import response as resp_common

from . import app

configs = app.config
name = app
default_return = resp_common.response(success=True,
                                      message='Awaiting API calls',
                                      data={
                                          'version': '1.1.0',
                                      })