Ejemplo n.º 1
0
    def post():
        data = request.get_json()
        schema = {'type': 'object',
                  'properties': {'education_id': {'type': 'integer'},
                                 'name': {'type': 'string'},
                                 'description': {'type': 'string'}}}

        try:
            validictory.validate(data, schema)
        except Exception:
            return make_api_response(400,
                                     'Data does not correspond to scheme.')

        if Education.query\
                .filter(Education.id == data['education_id']).count() == 0:
            return make_api_response(400, 'No object has been associated with '
                                     'the education ID that has been '
                                     'specified.')

        if Course.query.filter(Course.name == data['name']).count() > 0:
            return make_api_response(400, 'There is already an object with '
                                     'the name that has been specified.')

        course = Course(data['name'], data['description'])
        db.session.add(course)
        db.session.commit()

        return course.to_dict(), '201 The object has been created.'
Ejemplo n.º 2
0
    def post():
        data = request.get_json()
        schema = {'type': 'object',
                  'properties': {'name': {'type': 'string'}}}

        try:
            validictory.validate(data, schema)
        except Exception:
            return make_api_response(400,
                                     'Data does not correspond to scheme.')

        if Education.query.filter(Education.name == data['name']).count() > 0:
            return make_api_response(400, 'There is already an object with '
                                     'the name that has been specified.')

        education = Education(data['name'])
        db.session.add(education)
        db.session.commit()

        return make_api_response(201, 'The object has been created.')
Ejemplo n.º 3
0
    def delete():
        data = request.get_json()
        schema = {'type': [{'type': 'integer'},
                           {'type': 'array', 'items': {'type': 'integer'}}]}

        try:
            validictory.validate(data, schema)
        except Exception:
            return make_api_response(400,
                                     'Data does not correspond to scheme.')

        return data
Ejemplo n.º 4
0
    def get(education_id=None):
        if education_id:
            education = Education.query.get(education_id)

            if not education:
                return make_api_response(400, 'No object has been associated '
                                         'with the education ID that has been '
                                         'specified.')

            return education.to_dict()
        else:
            results = []

            for education in Education.query.all():
                results.append(education.to_dict())

            return results
Ejemplo n.º 5
0
    def get():
        data = request.get_json()
        schema = {
            'type': 'object',
            'properties': {
                'id': {'type': [
                    {'type': 'null'},
                    {'type': 'integer'},
                    {'type': 'array', 'items': {'type': 'integer'}}
                ]},
            }
        }
        course_ids = []
        result = {}

        try:
            validictory.validate(data, schema)
        except Exception:
            return make_api_response(400,
                                     'Data does not correspond to scheme.')

        if isinstance(data['id'], list):
            course_ids = data['id']
        elif isinstance(data['id'], int):
            course_ids = [data['id']]

        query = Course.query

        if len(course_ids) > 0:
            query = query.filter(Course.id.in_(course_ids))

        result['courses'] = []

        for course in query.all():
            result['courses'].append(course.to_dict())

        return result