Пример #1
0
 def test_get_bookings_for_person(self):
     with self.app.app_context():
         with self.app.test_client() as app:
             person1 = Person.query.get(self.person1)
             booking1 = Booking.query.get(self.booking1)
             car1 = Car.query.get(self.car1)
             response = app.get(f'api/person/{person1.username}/booking')
             self.assertEqual(200, response.status_code)
             data = response.get_json()
             self.assertIsNotNone(data)
             for booking_info in data:
                 self.assertEqual(
                     type(booking1),
                     type(BookingSchema().loads(booking_info['booking'])))
                 self.assertEqual(
                     type(person1),
                     type(PersonSchema().loads(booking_info['person'])))
                 self.assertEqual(
                     type(car1),
                     type(CarSchema().loads(booking_info['car'])))
Пример #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)
Пример #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)