Ejemplo n.º 1
0
def person(id: int):
    """
    Endpoint to get/update/delete person
    Args:
        id (int): person id

    Returns: Error or 200 if task successful

    """
    person = Person.query.get(id)
    if person is None:
        return abort(404, description='Person not found')
    schema = PersonSchema()
    if request.method == 'GET':
        person_data = schema.dump(person)
        return jsonify(json.dumps(person_data)), 200

    elif request.method == 'PUT':
        try:
            new_person = schema.loads(request.get_json())
        except ValidationError as ve:
            return abort(400, description=ve.messages)
        person.username = new_person.username
        person.first_name = new_person.first_name
        person.last_name = new_person.last_name
        person.email = new_person.email
        person.type = new_person.type
        person.type = new_person.type

        # check if password is plaintext or hashed
        if not new_person.password.startswith('pbkdf2'):
            person.password = generate_password_hash(new_person.password)
        else:
            person.password = new_person.password

        handle_db_operation(db.session.commit)
        return jsonify('Person updated'), 200

    elif request.method == 'DELETE':
        Person.query.filter_by(id=id).delete()
        handle_db_operation(db.session.commit)
        return jsonify('Person deleted'), 200
Ejemplo n.º 2
0
def person(person_id):
    _person = cache.get(f'person_{person_id}')

    if _person is None:
        _person = Person.query.get(person_id)

    if request.method == 'GET':
        schema = PersonSchema()
        result = schema.dump(_person)
        cache.set(f'person_{person_id}', result, helper.get_hours_in_seconds(1))
        return make_response(jsonify(
            {
                'message': 'Success',
                'data': {'person': result}
            }), 200)

    if helper.is_empty_content_length(request):
        app.logger.error(f'Exception: {request.content_type}')
        return make_response(jsonify(
            {
                'message': 'Payload can not be empty'
            }), 411)

    if not helper.is_json_content(request):
        app.logger.error(f'Exception: {request.content_type}')
        return make_response(jsonify(
            {
                'message': 'Unsupported Media Type'
            }), 415)

    if request.method == 'PUT':
        try:
            _person.name = request.json['name']
            _person.email = request.json['email']
        except KeyError as err:
            app.logger.error(f'Exception: {err}')
            return make_response(jsonify(
                {
                    'message': 'Some parameter is missing on request'
                }), 400)

        try:
            db.session.commit()
            cache.delete(f'person_{person_id}')
            cache.delete('person_list')
            return make_response(jsonify(
                {
                    'message': 'Success',
                    'data': {'person_id': person_id}
                }), 200)
        except Exception as err:
            app.logger.error(f'Exception: {err}')
            return make_response(jsonify(
                {
                    'message': 'Error while updating person information',
                    'data': {'person_id': person_id}
                }), 500),

    if request.method == 'PATCH':
        _person.name = request.json.get('name') or _person.name
        _person.email = request.json.get('email') or _person.email

        try:
            db.session.commit()
            cache.delete(f'person_{person_id}')
            cache.delete('person_list')
            return make_response(jsonify(
                {
                    'message': 'Success',
                    'data': {'person_id', person_id}
                }), 200)
        except Exception as err:
            app.logger.error(f'Exception: {err}')
            return make_response(jsonify(
                {
                    'message': 'Error while updating person information',
                    'data': {'person_id': person_id}
                }), 500)

    if request.method == 'DELETE':
        if _person is None:
            return make_response(jsonify(
                {
                    'message': 'Person not found',
                    'data': {'person_id': person_id}
                }), 200)

        try:
            db.session.delete(_person)
            db.session.commit()
            cache.delete(f'person_{person_id}')
            cache.delete('person_list')
            return make_response(jsonify(
                {
                    'message': 'Success',
                    'data': {'person_id': person_id}
                }), 200)
        except Exception as err:
            app.logger.error(f'Exception: {err}')
            return make_response(jsonify(
                {
                    'message': 'Error while deleting person',
                    'data': {'person_id': person_id}
                }), 500)
Ejemplo n.º 3
0
def person():
    if request.method == 'GET':
        person_list = cache.get('person_list')

        if person_list is not None:
            return make_response(jsonify(
                {
                    'message': 'Success',
                    'data': {'person_list': person_list}
                }), 200)

        person_list = Person.query.order_by(Person.create_date).all()
        schema = PersonSchema(many=True)
        result = schema.dump(person_list)
        cache.set('person_list', result, helper.get_hours_in_seconds(1))
        return make_response(jsonify(
            {
                'message': 'Success',
                'data': {'person_list': result}
            }), 200)

    if request.method == 'POST':
        if helper.is_empty_content_length(request):
            app.logger.error(f'Exception: {request.content_type}')
            return make_response(jsonify(
                {
                    'message': 'Payload can not be empty'
                }), 411)

        if not helper.is_json_content(request):
            app.logger.error(f'Exception: {request.content_type}')
            return make_response(jsonify(
                {
                    'message': 'Unsupported Media Type'
                }), 415)

        name = request.json['name']
        email = request.json['email']

        if name or email:
            _person = Person(name=name, email=email)

            try:
                db.session.add(_person)
                db.session.commit()
            except Exception as err:
                app.logger.error(f'Exception: {err}')
                return make_response(jsonify(
                    {
                        'message': 'Error while creating person! That email address is already in use.'
                    }), 500)
        else:
            return make_response(jsonify(
                {
                    'message': 'Name or email is missing.'
                }), 200)

        cache.delete('person_list')

        return make_response(jsonify(
            {
                'message': 'Success!',
                'data': {'person_id': _person.id}
            }), 200)