def patch(self, id): json_data = request.get_json(force=True) if not json_data: return error(message='Invalid data.') data, errors = user_schema.load(json_data) if errors: return unprocessable_entity(data=errors) user = User.query.get(id) if not user: return not_found('User does not exists.') if data.get('password'): user.password = data.get('password') user.role_id = data.get('role_id') user.status = data.get('status') user.updated_at = datetime.utcnow() try: user.save() result = user_schema.dump(user).data return success(result, 'Successfully edit a user.') except Exception as e: return internal_error()
def post(self): identity = get_jwt_identity() access_token = create_access_token( identity=identity, expires_delta=timedelta(minutes=int(current_app.config['JWT_TTL'])) ) return success({'access_token': access_token})
def get(self, page=1): data = request.get_json() if data and data.get('page'): page = data.get('page') per_page = int(current_app.config.get('USER_PER_PAGE', 20)) pagination = User.query.order_by(User.created_at.desc()).paginate( page=page, per_page=per_page, error_out=False) users = users_schema.dump(pagination.items).data response_data = { 'users': users, 'page': pagination.page, 'prev': url_for('api_users', page=page - 1) if pagination.has_prev else None, 'next': url_for('api_users', page=page + 1) if pagination.has_next else None, 'total': pagination.total } return success(response_data)
def blog_filters() -> Dict: """ API to fetch all filter's related to blog :return: """ result = get_filters() return success(data=result)
def post(self): jti = get_raw_jwt()['jti'] try: revoked_token = RevokedToken(jti=jti) revoked_token.add() return success(message='Access token has been revoked.') except Exception as e: return internal_error()
def get(self, id): user = User.query.get(id) if not user: return not_found('User does not exists.') result = user_schema.dump(user).data return success(result)
def blog_create_view() -> Dict: """ API to create a particular blog. :return: id of the created blog or any error message. """ payload = request.get_json() obj, error = create_blog(payload) return success(data=[{"id": str(obj.id)}], message=BlogMessage.CREATE_SUCCESS)\ if not error else failure(message=error)
def blogs_view() -> Dict: """ API to fetch all the blogs. :return: List of JSON response. """ params = request.args start = int(params.get('start', 0)) limit = int(params.get('limit', 20)) search_by = params.get('search') sort_by = params.get('sort') tags = params.get('tags') result = get_blogs(search_by, sort_by, tags, start=start, limit=limit) return success(data=result)
def delete(self, id): user = User.query.get(id) if not user: return not_found('User does not exists.') user.deleted_at = datetime.utcnow() try: user.save() result = user_schema.dump(user).data return success(result, 'Successfully delete a user.') except Exception as e: return internal_error()
def post(self): json_data = request.get_json(force=True) if not json_data: return error(message='Invalid data.') data, errors = user_schema.load(json_data) if errors: return unprocessable_entity(data=errors) user = User.query.filter_by(name=data.get('name')).first() if user: return error( message='User `{}` already exists.'.format(data.get('name'))) user = User(**data) try: user.save() result = user_schema.dump(user).data return success(result, 'Successfully add new user.') except Exception as e: return internal_error()
def blog_view(id: str) -> Dict: """ API to get, update and delete a particular blog. :return: success JSON response or failure message. """ result = None error = None message = None if request.method == 'GET': result, error = get_blog(id) elif request.method == 'PUT': payload = request.get_json() result, error = update_blog(id, payload) message = BlogMessage.UPDATE_SUCCESS elif request.method == "DELETE": obj, error = delete_blog(id) message = BlogMessage.DELETE_SUCCESS return success(data=result, message=message) if not error else failure( message=error)
def get(self): roles = Role.query.all() result = roles_schema.dump(roles).data return success(result)