def getStudentForms():
    student_id = ObjectId(request.json['student_id'])
    curr_link = request.json['parent_key']
    try:
        parentId = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    if studentsDOM.isArchived(student_id):
        raise AuthError({'archived': True}, 401)

    if parentsDOM.isExpired(parent_id):
        emailParent(parent_id, '', 'Your updated link is below:')
        raise AuthError({'expired': True}, 426)

    form_ids = studentsDOM.getAllFormIds(student_id)
    form_data = []
    for id in form_ids:
        if parent_id == FormsDOM.getParentId(id):
            blank_form_data = FormsDOM.getBlankFormId(
                id)  # will assert if formid does not exist
            form_data.append({
                'form_id': str(id),
                'form_name': FormsDOM.getFormName(id),
                'last_updated': FormsDOM.getLastUpdated(id),
                'last_viewed': FormsDOM.getLastViewed(id),
                'completed': FormsDOM.isComplete(id)
            })
    return {
        'form_data': form_data,
        'student_info': studentsDOM.getBasicInfo(student_id),
    }
def submitForm():
    curr_link = request.json['curr_link']
    try:
        parentId = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    form_id = request.json['form_id']
    answer_data = request.json['answer_data']
    FormsDOM.updateFormData(form_id, answer_data)
    return '0'
def submitForm():
    curr_link = request.json['curr_link']
    try:
        parent_id = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    if parentsDOM.isExpired(parent_id):
        emailParent(parent_id, '', 'Your updated link is below:')
        raise AuthError({'expired': True}, 426)

    form_id = request.json['form_id']
    answer_data = request.json['answer_data']
    FormsDOM.updateFormData(form_id, answer_data)
    return '0'
def getParentInfo():
    curr_link = request.json['curr_link']
    try:
        parent_id = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    if parentsDOM.isExpired(parent_id):
        emailParent(parent_id, '', 'Your updated link is below:')
        raise AuthError({'expired': True}, 426)

    parentInfo = parentsDOM.getParentProfile(parent_id)

    return {
        'first_name': parentInfo['first_name'],
        'last_name': parentInfo['last_name'],
        'email': parentInfo['email'],
    }
def addStudent():
    student = request.json['studentData']

    parentIds = []
    parents = request.json['parentData']
    for parent in parents:
        currID = None
        if parentsDOM.exists(parent['email']):
            currID = parentsDOM.get(email=parent['email'])
        else:
            currID = parentsDOM.createParent(parent['firstName'],
                                             parent['lastName'],
                                             parent['email'])
        parentIds.append(currID)

    formIds = []
    for form in request.json['forms']:
        for parentId in parentIds:
            id = form['id']
            # createForm(id, date, required, comp, data, parentID):
            currID = FormsDOM.createForm(ObjectId(id), None, None, True, False,
                                         [], parentId)
            formIds.append(currID)

    dateOfBirth = datetime.strptime(student['dob'], '%m/%d/%Y')
    studentId = studentsDOM.createStudent(student['firstName'],
                                          student['middleName'],
                                          student['lastName'], dateOfBirth,
                                          int(student['grade']), formIds,
                                          parentIds, student['class'])

    for parentId in parentIds:
        parentsDOM.addStudentId(parentId, studentId)

    # send emails
    failed = []

    for parentId in parentIds:
        failed = failed + emailParent(parentId)

    return {
        'failed': failed,
    }
def getForm():
    curr_link = request.json['curr_link']
    try:
        parentId = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    if parentsDOM.isExpired(parent_id):
        emailParent(parent_id, '', 'Your updated link is below:')
        raise AuthError({'expired': True}, 426)

    form_id = ObjectId(request.json['form_id'])
    blank_form_id = FormsDOM.getBlankFormId(form_id)
    blank_form_data = blankFormsDOM.getFormData(blank_form_id)
    form_data = FormsDOM.getFormData(form_id)

    return {
        'blank_form_data': blank_form_data,
        'form_data': form_data,
        'submitted': form_data != [],
    }
def getStudentsOfParent():
    curr_link = request.json['curr_link']
    try:
        parentId = parentsDOM.get(currLink=curr_link)
    except AssertionError as e:
        raise AuthError({'wrong link': True}, 401)

    if parentsDOM.isExpired(parent_id):
        emailParent(parent_id, '', 'Your updated link is below:')
        raise AuthError({'expired': True}, 426)

    all_student_ids = parentsDOM.getStudentIds(parentId)
    unarchived_student_ids = []
    for id in all_student_ids:
        if not studentsDOM.isArchived(id):
            unarchived_student_ids.append(id)

    students = []
    for id in unarchived_student_ids:
        students.append({
            'id': str(id),
            'name': studentsDOM.getFirstName(id),
        })
    return {'students': students}