def collection_post(self): """ --- post: security: - Bearer: [write] - Basic: [write] tags: - "v1" summary: "create a tutorial" operationId: "tutorial_collection_post" produces: - "application/json" consumes: - "application/json" parameters: - in: "body" name: "body" description: "" required: true schema: $ref: "#/definitions/Tutorial" responses: 200: description: successfull creation of a tutorial schema: type: object properties: result: type: string example: ok created: $ref: "#/definitions/CollectionTutorial" 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.TutorialSchema() 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: tutorial = models.Tutorial(**result) self.db.add(tutorial) self.db.commit() return {'result': 'ok', 'created': schema.dump(tutorial)}
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
def collection_get(self): """ --- get: security: - Bearer: [read] - Basic: [read] tags: - "v1" summary: "return all tutorials" description: "" operationId: "tutorial_collection_get" produces: - "application/json" responses: 200: description: "response for 200 code" schema: $ref: "#/definitions/CollectionTutorial" """ tutorials = self.request.user.tutorials.options(joinedload(models.Tutorial.tutor), joinedload(models.Tutorial.lecture)).all() tutorials_as_tutor = self.request.user.tutorials_as_tutor.options(joinedload(models.Tutorial.tutor), joinedload(models.Tutorial.lecture)).all() schema = models.TutorialSchema(many=True, only=allowed_attributes.collection_tutorial()) return schema.dump(tutorials+tutorials_as_tutor)
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)