Esempio n. 1
0
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
Esempio n. 2
0
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))
Esempio n. 3
0
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
Esempio n. 4
0
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
Esempio n. 5
0
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")
Esempio n. 6
0
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()
Esempio n. 7
0
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()