def post(self): args = parser.parse_args() user_id = get_jwt_identity()['id'] # Check if passwords are the same if args['password'] is not None and args['passwordConfirm'] != args[ 'password']: return response( {'errors': ['Password and Confirm Password must be same']}, 400) # Check if the email is already taken or not email = args['email'] user = User().where('email', email).first() if user.exists() and user.ATTRIBUTES['id'] != user_id: return response({'errors': ['This email is already taken']}, 400) # Update user user = User().where('id', '=', user_id).first() if user.exists() is True: user.update({ 'name': args['name'], 'email': args['email'], 'slug': user.generateSlug(name=args['name']), 'password': bcrypt.generate_password_hash(args['password']).decode('utf-8') }) return response({'user': user.data()}) return response({'errors': ['User could not found']}, 404)
def post(self, user_id): args = parser.parse_args() # Check if the email is already taken or not email = args['email'] user = User().where('email', email).first() if user.exists() and user.ATTRIBUTES['id'] != user_id: return response({ 'errors': 'This email is already taken' }, 400) # Update user user = User().where('id', '=', user_id).first() if user.exists() is True: user.update({ 'name': args['name'], 'email': args['email'], 'slug': user.generateSlug(name=args['name']), }) return response({ 'user': user.data() }) return response({ 'errors': [ 'User 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 get(self, slug): user = User().where('slug', slug).first() if user.exists() is True: return response({ 'user': user.data() }) return response({ 'errors': ['User could not found'] })
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 validate(self): user = User().where('id', self.ATTRIBUTES['user_id']).first() if user.exists() is False: self.setError("User not found in the database!") else: self.plus('user', user) lecturer = Lecturer().where('email', self.ATTRIBUTES['email']).first() if lecturer.exists() is True: self.setError("There is already a lecturer added with this email.") else: self.plus('lecturer', lecturer) if self.getErrors(): return False return True
def validate(self): # term id exist term = Term().where('id', self.ATTRIBUTES['term_id']).first() if term.exists() is False: self.setError("Term not found") course = Course().where('id', self.ATTRIBUTES['course_id']).first() if course.exists() is False: self.setError("Course not found") user = User().where('id', self.ATTRIBUTES['user_id']).first() if user.exists() is False: self.setError("You are not the owner of this note") if self.getErrors(): return False return True
def post(self): args = parser.parse_args() email = args['email'] password = args['password'] user = User().where([['email', '=', email]]).first() if user.exists() and bcrypt.check_password_hash( user.HIDDEN['password'], password): return response({ 'user': user.plus('token', user.generateToken()['jwt']).plus( 'admin', user.hasRole('admin')).data() }) return response( {'errors': ['Credentials do not match with our records.']}, 401)
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 = 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)