def signin(role, email, pwd_submitted): # verify `email` against `password`; return the user on success accounts = role.objects(email=email) if accounts.count() == 0: raise ActionError(f"Incorrect username or password") user = accounts.first() if not pbkdf2_sha256.verify(pwd_submitted, user.password): raise ActionError(f"Incorrect username or password") return user
def change_password(role, user_email, old_password, password): # verify old password hashed_password = role.objects(email=user_email).get().password if not pbkdf2_sha256.verify(old_password, hashed_password): raise ActionError(f"Incorrect password") # update password role.objects(email=user_email).update(set__password=pbkdf2_sha256.hash(password))
def unfulfill_request(instructor, request): if request not in instructor.requests_received: raise DoesNotExist(f'{request} has not been received by {instructor} or has been revoked') if request.status != STATUS_FULFILLED: raise ActionError(f'{request} not yet fulfilled') request.update(set__status=STATUS_UNFULFILLED, unset__date_fulfilled=True) return request
def fulfill_request(instructor, request, when=None): if request not in instructor.requests_received: raise DoesNotExist(f'{request} has not been received by {instructor} or has been revoked') if request.status == STATUS_FULFILLED: raise ActionError(f'{request} already fulfilled') # mark `request.status` as `STATUS_FULFILLED` request.update(set__status=STATUS_FULFILLED, set__date_fulfilled=when or date.today()) return request
def withdraw_request(student, request): r4c = student.req_for_courses.filter(course=request.course, recommender=request.instructor).get() if request in r4c.requests_sent: if request.status == STATUS_FULFILLED: raise ActionError("This request has been fulfilled") r4c.requests_sent.remove(request) r4c.requests_quota += 1 student.save() request.delete() else: raise DoesNotExist(f"Request {request} doesn't exist")
def signup(role, email, password, first_name, last_name, gender=None): if role not in USER_ROLLS: raise RuntimeError(f"Unknown roll: {role}") # check for existing `email` in database if role.objects(email=email).count() > 0: raise ActionError(f"User {email} already exists") # hash `password` pwd_hash = pbkdf2_sha256.hash(password) # save to database user = role(email=email, password=pwd_hash, first_name=first_name, last_name=last_name) if gender: user.gender = gender return user.save()
def set_letter_quota(student, recommender, course, quota, reset=False): if quota < 0: raise ValidationError(f"quota={quota} is too small.") # register `student` to `course` if necessary course.update(add_to_set__students=student) # register `course` to `student` if necessary. Check out the following documentation # 1) https://stackoverflow.com/a/50658375 # 2) https://docs.mongoengine.org/apireference.html#mongoengine.base.datastructures.EmbeddedDocumentList req_for_course = student.req_for_courses.filter(course=course, recommender=recommender) if req_for_course.count() == 0: student.req_for_courses.create(course=course, recommender=recommender, requests_quota=quota) elif reset: req_for_course.update(requests_quota=quota) else: raise ActionError(f"Letter quota already assigned to {recommender} for {course} exists") return student.save()