예제 #1
0
 def get(self):
     """
     ---
     get:
       security:
         - Bearer: [read]
         - Basic: [read]
       tags:
         - "v1"
       summary: "return a specific lecture"
       description: ""
       operationId: "lecture_get"
       produces:
         - "application/json"
       responses:
         200:
           description: successfull return of the lecture
           schema:
             $ref: "#/definitions/Lecture"
     """
     lecture_id = self.request.matchdict['lecture_id']
     lecture = self.db.query(models.Lecture).options(
         undefer('tutorials.student_count'),
         joinedload(models.Lecture.assistants),
         joinedload(models.Lecture.tutorials)).get(lecture_id)
     subscribed = self.request.user.id in [s.id for s in lecture.students]
     allowed_attr = allowed_attributes.lecture()
     if lecture.mode == 'off':
         allowed_attr = allowed_attributes.lecture()
         allowed_attr.remove('tutorials')
     schema = models.LectureSchema(only=allowed_attr)
     response = schema.dump(lecture)
     if lecture.mode == 'off':
         response.update({'tutorials': []})
     return {'lecture': response, 'subscribed': subscribed}
예제 #2
0
 def collection_get(self):
     """
     ---
     get:
       security:
         - Bearer: [read]
         - Basic: [read]
       tags:
         - "v1"
       summary: "return all lectures"
       description: ""
       operationId: "lecture_collection_get"
       consumes:
         - "application/json"
       produces:
         - "application/json"
       responses:
         200:
           description: "response for 200 code"
           schema:
             $ref: "#/definitions/CollectionLecture"
     """
     lectures = (
         self.db.query(models.Lecture).order_by(desc(
             models.Lecture.term), models.Lecture.name).options(
                 joinedload(models.Lecture.assistants)).filter(
                     models.Lecture.is_visible == True)  # pylint: disable=C0121
         .all())
     schema = models.LectureSchema(
         many=True, only=allowed_attributes.collection_lecture())
     return schema.dump(lectures)
예제 #3
0
 def collection_post(self):
     """
     ---
     post:
       security:
         - Bearer: [write]
         - Basic: [write]
       tags:
         - "v1"
       summary: "create a lecture"
       operationId: "lecture_collection_post"
       produces:
         - "application/json"
       consumes:
         - "application/json"
       parameters:
       - in: "body"
         name: "body"
         description: ""
         required: true
         schema:
           $ref: "#/definitions/Lecture"
       responses:
         200:
           description: successfull creation of a lecture
           schema:
             type: object
             properties:
               result:
                 type: string
                 example: ok
               created:
                 $ref: "#/definitions/CollectionLecture"
         400:
           description: HTTPBadRequest (Example uses A bad attribute)
           schema:
             type: object
             properties:
               result:
                 type: string
                 example: error
               error:
                 type: array
                 example: [{'description': {'name': ['Missing data for required field.'], 'test123': ['Unknown field.']}, 'name': 'fail', 'location': 'body'}]
     """
     schema = models.LectureSchema()
     schema.context['session'] = self.request.db
     try:
         result = schema.load(self.request.json_body)
     except ValidationError as e:
         self.request.errors.add('body', 'fail', e.messages)
         return {}
     except JSONDecodeError:
         self.request.errors.add('body', 'fail', 'Invalid JSON')
         return {}
     else:
         lecture = models.Lecture(**result)
         self.db.add(lecture)
         self.db.commit()
         return {'result': 'ok', 'created': schema.dump(lecture)}
예제 #4
0
 def put(self):
     """
     ---
     put:
       security:
         - Bearer: [write]
         - Basic: [write]
       tags:
         - "v1"
       summary: "update a lecture object"
       operationId: "lecture_put"
       produces:
         - "application/json"
       consumes:
         - "application/json"
       parameters:
       - in: "body"
         name: "body"
         description: ""
         required: true
         schema:
           $ref: "#/definitions/Lecture"
       responses:
         200:
           description: successfull update of a lecture
           schema:
             type: object
             properties:
               result:
                 type: string
                 example: ok
               updated:
                 $ref: "#/definitions/CollectionLecture"
         400:
           description: HTTPBadRequest (Example uses A bad attribute)
           schema:
             type: object
             properties:
               result:
                 type: string
                 example: error
               error:
                 type: array
                 example: [{'description': {'name': ['Missing data for required field.'], 'test123': ['Unknown field.']}, 'name': 'fail', 'location': 'body'}]
     """
     lecture_id = self.request.matchdict['lecture_id']
     lecture = self.db.query(models.Lecture).options(
         undefer('tutorials.student_count'),
         joinedload(models.Lecture.assistants)).get(lecture_id)
     schema = models.LectureSchema()
     # attatch db session to schema so it can be used during validation
     schema.context['session'] = self.request.db
     try:
         result = schema.load(self.request.json_body, partial=True)
     except ValidationError as e:
         self.request.errors.add('body', 'fail', e.messages)
     else:
         for k, v in result.items():
             setattr(lecture, k, v)
         try:
             self.db.commit()
         except SQLAlchemyError:
             self.db.rollback()
         else:
             return {'result': 'ok', 'update': self.get()}
예제 #5
0
def api_spec(request):
    """ Return something.
    ---
    get:
      description: "Outputs the Open-API specification with version 2.0.0. More information can be found on https://swagger.io/docs/specification/2-0/basic-structure/"
      operationId: "openapi_spec_get"
      tags:
      - "Open API"
      produces:
        - "application/json"
    """
    spec = APISpec(title='MÜSLI-API',
                   version='0.0.1',
                   openapi_version='2.0.0',
                   securityDefinitions={
                       "Bearer": {
                           "type": "apiKey",
                           "in": "header",
                           "name": "Authorization",
                           "description": "JWT Tokens"
                       },
                       "Basic": {
                           "type": "basic",
                           "description":
                           "Die regulären Zugangsdaten zum Müsli :)"
                       }
                   },
                   plugins=[MarshmallowPlugin()])
    # Paths for API v1
    add_pyramid_paths(spec, 'collection_lecture', request=request)
    add_pyramid_paths(spec, 'lecture', request=request)

    add_pyramid_paths(spec, 'collection_tutorial', request=request)
    add_pyramid_paths(spec, 'tutorial', request=request)

    add_pyramid_paths(spec, 'collection_exercise', request=request)
    add_pyramid_paths(spec, 'exercise', request=request)

    add_pyramid_paths(spec, 'exam', request=request)

    add_pyramid_paths(spec, 'openapi_spec', request=request)

    add_pyramid_paths(spec, 'whoami', request=request)

    # Be careful how the schemes are defined:
    #
    # If one (or its member) are instantiated as object and some are as class
    # they can cause weird behavior with double definitions of schemes in the
    # spec.

    spec.components.schema(
        'User', schema=models.UserSchema(only=allowed_attributes.user()))

    spec.components.schema(
        'Tutorial',
        schema=models.TutorialSchema(only=allowed_attributes.tutorial()))
    spec.components.schema('CollectionTutorial',
                           schema=models.TutorialSchema(
                               only=allowed_attributes.collection_tutorial()))

    spec.components.schema(
        'Lecture',
        schema=models.LectureSchema(only=allowed_attributes.lecture()))
    spec.components.schema('CollectionLecture',
                           schema=models.LectureSchema(
                               only=allowed_attributes.collection_lecture()))

    spec.components.schema('ExerciseStudent',
                           schema=models.ExerciseStudentSchema)
    spec.components.schema('Exercise', schema=models.ExerciseSchema)

    openapi_json = spec.to_dict()

    return remove_regex(openapi_json)