class TutorialSchema(Schema): id = fields.Integer(dump_only=True) lecture_id = fields.Integer(required=True) place = fields.String(required=True) time = fields.Method("get_time", deserialize="load_time") max_students = fields.Integer(required=True) tutor = fields.Nested(UserSchema, only=allowed_attributes.user(), dump_only=True) comment = fields.String() students = fields.Nested(UserSchema, many=True, only=allowed_attributes.user(), dump_only=True) exams = fields.Nested(ExamSchema, many=True, only=["id", "name"], dump_only=True) student_count = fields.Method("get_student_num") def get_time(self, obj): return obj.time.__html__() def load_time(self, value): return TutorialTime(str(value)) def get_student_num(self, obj): return obj.students.count()
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]
def get(self): """ --- get: security: - Bearer: [read] - Basic: [read] tags: - "v1" summary: "return my user" description: "" operationId: "whoami_get" produces: - "application/json" responses: 200: description: "response for 200 code" schema: $ref: "#/definitions/User" """ schema = UserSchema(only=allowed_attributes.user()) if self.request.user: user = self.request.user else: user = None return schema.dump(user)
class ExerciseSchema(Schema): id = fields.Integer(dump_only=True) exam_id = fields.Integer() nr = fields.Integer() maxpoints = fields.Float() students = fields.Nested(UserSchema, only=allowed_attributes.user(), many=True)
class ExerciseStudentSchema(Schema): student = fields.Nested(UserSchema, only=allowed_attributes.user()) points = fields.Float()
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)