Ejemplo n.º 1
0
    def patch(self, politician_id):

        json_data = request.get_json()

        data, errors = politician_schema.load(data=json_data, partial=True)

        if errors:
            return {'message': 'Validation errors', 'errors': errors}, HTTPStatus.BAD_REQUEST

        politician = Politician.get_by_id(politician_id=politician_id)

        if politician is None:
            return {'message': 'politician not found'}, HTTPStatus.NOT_FOUND

        current_user = get_jwt_identity()

        if current_user != politician.user_id:
            return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN

        politician.name = data.get('name') or politician.name
        politician.position = data.get('position') or politician.position
        politician.description = data.get('description') or politician.description
        politician.age = data.get('age') or politician.age
        politician.gender = data.get('gender') or politician.gender
        politician.bio_data = data.get('bio_data') or politician.bio_data
        politician.c_vitae = data.get('c_vitae') or politician.c_vitae
        politician.county = data.get('county') or politician.county
        politician.constituency = data.get('constituency') or politician.constituency
        politician.ward = data.get('ward') or politician.ward

        politician.save()

        clear_cache('/politicians')

        return politician_schema.dump(politician).data, HTTPStatus.OK
Ejemplo n.º 2
0
    def put(self, politician_id):

        file = request.files.get('cover')

        if not file:
            return {'message': 'Not a valid image'}, HTTPStatus.BAD_REQUEST

        if not image_set.file_allowed(file, file.filename):
            return {'message': 'File type not allowed'}, HTTPStatus.BAD_REQUEST

        politician = Politician.get_by_id(politician_id=politician_id)

        if politician is None:
            return {'message': 'Politician not found'}, HTTPStatus.NOT_FOUND

        current_user = get_jwt_identity()

        if current_user != politician.user_id:
            return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN

        if politician.cover_image:
            cover_path = image_set.path(folder='politicians', filename=politician.cover_image)
            if os.path.exists(cover_path):
                os.remove(cover_path)

        filename = save_image(image=file, folder='politicians')

        politician.cover_image = filename
        politician.save()

        clear_cache('/politicians')

        return politician_cover_schema.dump(politician).data, HTTPStatus.OK
Ejemplo n.º 3
0
    def get(self, politician_id):

        politician = Politician.get_by_id(politician_id=politician_id)

        if politician is None:
            return {'message': 'Politician not found'}, HTTPStatus.NOT_FOUND

        current_user = get_jwt_identity()

        if politician.is_publish == False and politician.user_id != current_user:
            return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN

        return politician_schema.dump(politician).data, HTTPStatus.OK
Ejemplo n.º 4
0
    def delete(self, politician_id):

        politician = Politician.get_by_id(politician_id=politician_id)

        if politician is None:
            return {'message': 'politician not found'}, HTTPStatus.NOT_FOUND

        current_user = get_jwt_identity()

        if current_user != politician.user_id:
            return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN

        politician.delete()

        clear_cache('/politicians')

        return {}, HTTPStatus.NO_CONTENT