def post(self, lecturer_id): args = parser.parse_args() # Check if the email is already taken or not email = args['email'] lecturer = Lecturer().where('email', email).first() if lecturer.exists() and lecturer.ATTRIBUTES['id'] != lecturer_id: return response({ 'errors': 'This email is already taken' }, 400) lecturer = Lecturer().where('id', '=', lecturer_id).first() if lecturer.exists() is True: lecturer.update({ 'name': args['name'], 'email': args['email'], 'slug': lecturer.generateSlug(name=args['name']) }) return response({ 'lecturer': lecturer.data() }) return response({ 'errors': [ 'Lecturer could not found' ] }, 404)
def validate(self): term = Term().where('id', self.ATTRIBUTES['term_id']).first() if term.exists() is False: self.setError('Term could not found') else: self.plus('term', term.data()) lecturer = Lecturer().where('id', self.ATTRIBUTES['lecturer_id']).first() if lecturer.exists() is False: self.setError('Lecturer could not found') else: self.plus('lecturer', lecturer.data()) course = Course().where('id', self.ATTRIBUTES['course_id']).first() if course.exists() is False: self.setError('Course could not found') else: self.plus('course', course) user = User().where('id', self.ATTRIBUTES['user_id']).first() if user.exists() is False: self.setError('User could not found') else: self.plus('user', user) if not self.getErrors(): return True return False
def post(self, lecturer_id): lecturer = Lecturer().where('id', lecturer_id).first() if lecturer.exists() is True: Comment().where([['type', '=', 'lecturers'], ['type_id', '=', lecturer_id]]).get().delete() GradeDistribution().where('lecturer_id', '=', lecturer_id).get().delete() lecturer.delete() return response({ 'message': 'Lecturer deleted' }) return response({ 'errors': [ 'Lecturer could not found' ] }, 401)
def get(self, slug): lecturer = Lecturer().where('slug', slug).first() if lecturer.exists() is True: comments = Comment().where([['type_id', '=', lecturer.ATTRIBUTES['id']], ['type', '=', 'lecturers']])\ .get().data() grade_distributions = GradeDistribution().where( 'lecturer_id', lecturer.ATTRIBUTES['id']).get().data() return response({ 'lecturer': lecturer.plus('comments', comments).plus('grade_distributions', grade_distributions).data() }) return response({'errors': ['Lecturer could not found!']})
def post(self, lecturer_id): user_id = get_jwt_identity()['id'] lecturer = Lecturer().where([['id', '=', lecturer_id], ['user_id', '=', user_id]]).first() comments = Comment().where([['type_id', '=', lecturer_id], ['type', '=', 'lecturers']]).get() for comment in comments: comment.delete() dists = GradeDistribution().where('lecturer_id', lecturer_id).get().delete() if lecturer.exists() is True: lecturer.delete() return response({'message': 'Lecturer deleted'}) return response({'errors': ['Lecturer could not found']}, 401)
def post(self): args = parser.parse_args() name = args['name'] email = args['email'] user_id = get_jwt_identity()['id'] lecturer = Lecturer() lecturer.create({ 'name': name, 'email': email, 'slug': lecturer.generateSlug(name=name), 'user_id': user_id }) if lecturer.validate() is True: lecturer.save() return response({'lecturer': lecturer.data()}, 200) return response({'errors': lecturer.getErrors()}, 400)
def validate(self): user = User().where('id', self.ATTRIBUTES['user_id']).first() if user.exists() is False: self.setError("User not found in the database!") if self.ATTRIBUTES['type'] == 'lecturers': lecturer = Lecturer().where('id', self.ATTRIBUTES['type_id']).first() if lecturer.exists() is False: self.setError("Lecturer not found in the database!") elif self.ATTRIBUTES['type'] == 'notes': note = Note().where('id', self.ATTRIBUTES['type_id']).first() if note.exists() is False: self.setError("Note not found in the database!") else: self.setError("Comment type is not valid") if self.getErrors(): return False return True
def post(self, user_id): user_id = get_jwt_identity()['id'] user = User().where('id', user_id).first() if user.exists() is True: Comment().where('user_id', user_id).get().delete() GradeDistribution().where('user_id', user_id).get().delete() Lecturer().where('user_id', user_id).get().delete() Note().where('user_id', user_id).get().delete() user.delete() return response({'message': 'User deleted with success'}, 200) return response({'errors': ['User could not found!']}, 401)
def post(self, user_id): user = User().where('id', '=', user_id).first() print(user_id, file=sys.stderr) if user.exists(): Comment().where('user_id', user_id).get().delete() Event().where('user_id', user_id).get().delete() GradeDistribution().where('user_id', user_id).get().delete() lecturers = Lecturer().where('user_id', user_id).get() for lecturer in lecturers.data(): Comment().where([['type', '=', 'lecturers'], ['type_id', '=', lecturer['id']]]).get().delete() GradeDistribution().where('lecturer_id', '=', lecturer['id']).get().delete() lecturers.delete() notes = Note().where('user_id', user_id).get() for note in notes.data(): Comment().where([['type', '=', 'notes'], ['type_id', '=', note['id']]]).get().delete() notes.delete() user.delete() return response({ 'message': 'User deleted successfully' }, 202) return response({ 'message': 'User does not exist' }, 404)
def post(self, lecturer_id): args = parser.parse_args() user_id = get_jwt_identity()['id'] lecturer = Lecturer().where([['id', '=', lecturer_id], ['user_id', '=', user_id]]).first() if lecturer.exists() is True: lecturer.update({ 'name': args['name'], 'email': args['email'], 'slug': lecturer.generateSlug(name=args['name']) }) return response({'lecturer': lecturer.data()}) return response({'errors': ['Lecturer could not found']}, 404)
def get(self): lecturers = Lecturer().orderBy().get() return response({'lecturers': lecturers.data()})