Beispiel #1
0
class LectureSchema(Schema):
    id = fields.Integer(dump_only=True)
    assistants = fields.Nested(AssistantSchema,
                               required=True,
                               many=True,
                               only=allowed_attributes.user())
    name = fields.String(required=True)
    type = fields.String()
    term = fields.Method("get_term", deserialize="load_term")
    lsf_id = fields.String()
    lecturer = fields.String()
    url = fields.Url()
    password = fields.String()
    is_visible = fields.Boolean()
    tutorials = fields.Nested(TutorialSchema,
                              many=True,
                              only=allowed_attributes.tutorial())
    tutors = fields.Nested(UserSchema, many=True, dump_only=True)

    # Converts the Muesli defined type Term to it's string representation
    def get_term(self, obj):
        return obj.term.__html__()

    # Constructs a Term from input like 20181
    def load_term(self, value):
        term = [Term(str(value)), Term(str(value))]
        if term in getTerms():
            return term[0]
Beispiel #2
0
    def get(self):
        """
        ---
        get:
          security:
            - Bearer: [read]
            - Basic: [read]
          tags:
            - "v1"
          summary: "return a specific tutorial"
          description: ""
          operationId: "tutorial_get"
          produces:
            - "application/json"
          responses:
            200:
              description: "response for 200 code"
              schema:
                $ref: "#/definitions/Tutorial"
        """
        try:
            tutorial = self.request.db.query(models.Tutorial).options(
                joinedload(models.Tutorial.tutor),
                joinedload(models.Tutorial.lecture)).filter(
                    models.Tutorial.id == self.request.matchdict['tutorial_id'] # pylint: disable=C0121
                ).one()
        except NoResultFound:
            raise HTTPBadRequest("Ungueltige Tutorial ID!") # pylint: disable=W0707
        exa = tutorial.lecture.exams.filter((models.Exam.results_hidden == False)|(models.Exam.results_hidden == None)) # pylint: disable=C0121
        if self.request.has_permission('viewAll'):
            tut_schema = models.TutorialSchema()
        else:
            tut_schema = models.TutorialSchema(only=allowed_attributes.tutorial())
        exam_schema = models.ExamSchema(many=True, only=["id", "name"])

        result = tut_schema.dump(tutorial)
        try:
            lecture_student = tutorial.lecture.lecture_students.filter(models.LectureStudent.student_id == self.request.user.id).one()
        except NoResultFound:
            lecture_student = None
        # If the user is part of the tutorial he is allowed to view the exams
        if self.request.has_permission('viewAll') or lecture_student:
            result.update({"exams": exam_schema.dump(exa)})
        return result
Beispiel #3
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)