Exemplo n.º 1
0
def create_grads():
    """
    Put the grad students created before into GradStudent records.
    """
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='MSc Project', description='MSc Project option')
    gp.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='MSc Thesis', description='MSc Thesis option')
    gp.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='PhD', description='PhD')
    gp.save()
    req = GradRequirement(program=gp, description='Defended Thesis')
    req.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='ensc'), label='MEng', description='Masters in Engineering')
    gp.save()
    gp = GradProgram(unit=Unit.objects.get(slug='ensc'), label='PhD', description='PhD')
    gp.save()
    st = ScholarshipType(unit=Unit.objects.get(slug='cmpt'), name="Some Scholarship")
    st.save()
    gf = GradFlag(unit=Unit.objects.get(slug='cmpt'), label='Special Specialist Program')
    gf.save()

    programs = list(GradProgram.objects.all())
    supervisors = list(set([m.person for m in Member.objects.filter(offering__owner__slug='cmpt', role='INST')]))
    for p in Person.objects.filter(userid__endswith='grad'):
        gp = random.choice(programs)
        campus = random.choice(list(CAMPUSES))
        gs = GradStudent(person=p, program=gp, campus=campus)
        gs.save()

        startsem = random.choice(list(Semester.objects.filter(name__lt=Semester.current().name)))
        st = GradStatus(student=gs, status='COMP', start=startsem)
        st.save()
        st = GradStatus(student=gs, status=random.choice(['ACTI', 'ACTI', 'LEAV']), start=startsem.next_semester())
        st.save()
        if random.random() > 0.5:
            st = GradStatus(student=gs, status=random.choice(['GRAD', 'GRAD', 'WIDR']), start=startsem.next_semester().next_semester().next_semester())
            st.save()

        if random.random() > 0.25:
            sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='SEN')
            sup.save()
            sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='COM')
            sup.save()
            if random.random() > 0.5:
                sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='COM')
                sup.save()
            else:
                sup = Supervisor(student=gs, external="Some External Supervisor", supervisor_type='COM', config={'email': '*****@*****.**'})
                sup.save()
Exemplo n.º 2
0
def create_grads():
    """
    Put the grad students created before into GradStudent records.
    """
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='MSc Project', description='MSc Project option')
    gp.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='MSc Thesis', description='MSc Thesis option')
    gp.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='cmpt'), label='PhD', description='PhD')
    gp.save()
    req = GradRequirement(program=gp, description='Defended Thesis')
    req.save()
    req = GradRequirement(program=gp, description='Formed Committee')
    req.save()
    gp = GradProgram(unit=Unit.objects.get(slug='ensc'), label='MEng', description='Masters in Engineering')
    gp.save()
    gp = GradProgram(unit=Unit.objects.get(slug='ensc'), label='PhD', description='PhD')
    gp.save()
    st = ScholarshipType(unit=Unit.objects.get(slug='cmpt'), name="Some Scholarship")
    st.save()
    gf = GradFlag(unit=Unit.objects.get(slug='cmpt'), label='Special Specialist Program')
    gf.save()

    programs = list(GradProgram.objects.all())
    supervisors = list(set([m.person for m in Member.objects.filter(offering__owner__slug='cmpt', role='INST')]))
    for p in Person.objects.filter(userid__endswith='grad'):
        gp = random.choice(programs)
        campus = random.choice(list(CAMPUSES))
        gs = GradStudent(person=p, program=gp, campus=campus)
        gs.save()

        startsem = random.choice(list(Semester.objects.filter(name__lt=Semester.current().name)))
        st = GradStatus(student=gs, status='COMP', start=startsem)
        st.save()
        st = GradStatus(student=gs, status=random.choice(['ACTI', 'ACTI', 'LEAV']), start=startsem.next_semester())
        st.save()
        if random.random() > 0.5:
            st = GradStatus(student=gs, status=random.choice(['GRAD', 'GRAD', 'WIDR']), start=startsem.next_semester().next_semester().next_semester())
            st.save()

        if random.random() > 0.25:
            sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='SEN')
            sup.save()
            sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='COM')
            sup.save()
            if random.random() > 0.5:
                sup = Supervisor(student=gs, supervisor=random.choice(supervisors), supervisor_type='COM')
                sup.save()
            else:
                sup = Supervisor(student=gs, external="Some External Supervisor", supervisor_type='COM', config={'email': '*****@*****.**'})
                sup.save()
Exemplo n.º 3
0
def process_pcs_row(row, column, rownum, unit, semester, user):
    """
    Process a single row from the PCS import
    """
    appsemester = semester.previous_semester()
    warnings = []
    ident = "in row %i" % (rownum)
    appid = row[column['appid']]
    emplid = row[column['emplid']]
    program = row[column['program']]

    # get Person, from SIMS if necessary
    try:
        p = Person.objects.get(emplid=int(emplid))
    except ValueError:
        warnings.append("Bad emplid %s: not processing that row." % (ident))
        return warnings
    except Person.DoesNotExist:
        try:
            p = add_person(emplid)
        except SIMSProblem as e:
            return str(e)

    ident = 'for "%s"' % (p.name())

    # update information on the Person
    email = row[column['email']]
    if email: p.config['applic_email'] = email

    dob = row[column['dob']]
    if dob:
        try:
            dt = datetime.datetime.strptime(dob, "%Y-%m-%d")
            p.config['birthdate'] = dt.date().isoformat()
        except ValueError:
            warnings.append("Bad birthdate %s." % (ident))
    
    # get extended SIMS data
    data = grad_student_info(emplid)
    p.config.update(data)
    
    p.save()
    
    #print "Importing %s" % (p)
    
    # get GradStudent, creating if necessary
    
    # a unique identifier for this application, so we can detect repeated imports (and handle gracefully)
    uid = "%s-%s-%s-%s" % (unit.slug, semester.name, appid, emplid)
    # TODO: wrong, wrong, wrong. Figure out how to select program from import data
    program = GradProgram.objects.filter(unit=unit)[0]

    # find the old GradStudent if possible
    gss = GradStudent.objects.filter(program__unit=unit, person=p)
    gs = None
    for g in gss:
        if 'app_id' in g.config and g.config['app_id'] == uid:
            gs = g
            break
    if not gs:
        gs = GradStudent(program=program, person=p)
        gs.config['app_id'] = uid
    
    resarea = row[column['resarea']]
    firstlang = row[column['firstlang']]
    
    gs.research_area = resarea
    gs.mother_tongue = firstlang
    gs.created_by = user.userid
    gs.updated_by = user.userid
    gs.config['start_semester'] = semester.name
    gs.save()
    
    complete = row[column['complete']].strip()
    decision = row[column['decision']].strip()
    notes = row[column['notes']].strip()
    gs.config['decisionnotes'] = notes
    
    old_st = GradStatus.objects.filter(student=gs, start__name__gte=semester.name)
    if not old_st:
        # if no old status for current semester, create one
        
        # application completion status
        if complete == 'AppConfirm':
            st = GradStatus(student=gs, status="COMP", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif complete == '':
            st = GradStatus(student=gs, status="INCO", start=appsemester, end=None, notes="PCS import")
            st.save()
        else:
            warnings.append('Unknown "Confirmation of Completion of Application" value %s.' % (ident))
        
        # decision status
        if decision == 'DECL':
            st = GradStatus(student=gs, status="DECL", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif decision == '':
            st = GradStatus(student=gs, status="OFFO", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif decision == 'R':
            st = GradStatus(student=gs, status="REJE", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif decision == 'HOLD':
            st = GradStatus(student=gs, status="HOLD", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif decision == 'AMScT':
            # TODO: bump program to MSc thesis
            st = GradStatus(student=gs, status="CONF", start=appsemester, end=None, notes="PCS import")
            st.save()
        elif decision == 'AMScC':
            # TODO: bump program to MSc course-based
            st = GradStatus(student=gs, status="CONF", start=appsemester, end=None, notes="PCS import")
            st.save()


    # potential supervisor
    potsuper = row[column['potsuper']]
    if potsuper:
        superv = None
        external = None
        try:
            ps_last, ps_first = potsuper.split(', ')
        except ValueError:
            warnings.append('Bad potential supervisor name %s: will store them as an "external" supervisor.' % (ident))
            external = potsuper
        else:
            potentials = possible_supervisor_people([unit])
            potential_ids = [p.id for p in potentials]
            query = Q(last_name=ps_last, first_name=ps_first) | Q(last_name=ps_last, pref_first_name=ps_first)
            people = Person.objects.filter(query, id__in=potential_ids)
            if people.count() == 1:
                superv = people[0]
            else:
                warnings.append('Coundn\'t find potential supervisor %s: will store them as an "external" supervisor.' % (ident))
                external = potsuper

        old_s = Supervisor.objects.filter(student=gs, supervisor_type='POT')
        if old_s:
            s = old_s[0]
        else:
            s = Supervisor(student=gs, supervisor_type='POT')
        s.superv = superv
        s.external = external
        s.position = 0
        s.created_by = user.userid
        s.modified_by = user.userid
        s.save()
                
        
    l = LogEntry(userid=user.userid, description="Imported grad record for %s (%s) from PCS" % (p.name(), p.emplid), related_object=gs)
    l.save()
    
    return warnings
Exemplo n.º 4
0
def manage_defence(request, grad_slug):
    """
    Page for managing all defence-related stuff.
    
    Slightly complicated since this info cuts across several models.
    """
    grad = get_object_or_404(GradStudent,
                             slug=grad_slug,
                             program__unit__in=request.units)
    supervisors = Supervisor.objects.filter(
        student=grad).select_related('supervisor')
    supervisor_people = [s.supervisor for s in supervisors if s.supervisor]
    supervisor_choices = possible_supervisors(units=request.units,
                                              extras=supervisor_people,
                                              null=True)

    if request.method == 'POST':
        form = GradDefenceForm(request.POST)
        form.set_supervisor_choices(supervisor_choices)
        if form.is_valid():
            with transaction.atomic():
                grad.config['thesis_type'] = form.cleaned_data['thesis_type']
                grad.config['work_title'] = form.cleaned_data['work_title']
                grad.config['exam_date'] = form.cleaned_data['exam_date']
                grad.config['thesis_outcome'] = form.cleaned_data[
                    'thesis_outcome']
                grad.config['thesis_location'] = form.cleaned_data[
                    'thesis_location']
                grad.save()

                if form.cleaned_data['internal']:
                    p = form.cleaned_data['internal']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(
                        student=grad, removed=False,
                        supervisor_type='SFU').exclude(supervisor=p)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()

                    existing_sup = Supervisor.objects.filter(
                        student=grad,
                        removed=False,
                        supervisor_type='SFU',
                        supervisor=p)
                    if not existing_sup:
                        # doesn't exist: create
                        sup = Supervisor(student=grad,
                                         supervisor_type='SFU',
                                         supervisor=p)
                        sup.save()
                    # else: already there, so nothing to change

                if form.cleaned_data['chair']:
                    p = form.cleaned_data['chair']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(
                        student=grad, removed=False,
                        supervisor_type='CHA').exclude(supervisor=p)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()

                    existing_sup = Supervisor.objects.filter(
                        student=grad,
                        removed=False,
                        supervisor_type='CHA',
                        supervisor=p)
                    if not existing_sup:
                        # doesn't exist: create
                        sup = Supervisor(student=grad,
                                         supervisor_type='CHA',
                                         supervisor=p)
                        sup.save()
                    # else: already there, so nothing to change

                if form.cleaned_data['external']:
                    name = form.cleaned_data['external']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(
                        student=grad, removed=False,
                        supervisor_type='EXT').exclude(external=name)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()

                    # creqate/update
                    existing_sup = Supervisor.objects.filter(
                        student=grad,
                        removed=False,
                        supervisor_type='EXT',
                        external=name)
                    if existing_sup:
                        sup = existing_sup[0]
                    else:
                        sup = Supervisor(student=grad,
                                         supervisor_type='EXT',
                                         external=name)

                    sup.config['email'] = form.cleaned_data['external_email']
                    sup.config['contact'] = form.cleaned_data[
                        'external_contact']
                    sup.config['attend'] = form.cleaned_data['external_attend']
                    sup.save()

            messages.success(request,
                             "Updated defence info for %s." % (grad.person))
            l = LogEntry(userid=request.user.username,
                         description="Updated grad defence info for %s." %
                         (grad),
                         related_object=grad)
            l.save()
            return HttpResponseRedirect(
                reverse('grad.views.view', kwargs={'grad_slug': grad.slug}))
    else:
        initial = {}
        if 'thesis_type' in grad.config:
            initial['thesis_type'] = grad.config['thesis_type']
        if 'work_title' in grad.config:
            initial['work_title'] = grad.config['work_title']
        if 'exam_date' in grad.config:
            initial['exam_date'] = grad.config['exam_date']
        if 'thesis_outcome' in grad.config:
            initial['thesis_outcome'] = grad.config['thesis_outcome']
        if 'thesis_location' in grad.config:
            initial['thesis_location'] = grad.config['thesis_location']
        internals = Supervisor.objects.filter(student=grad,
                                              removed=False,
                                              supervisor_type='SFU')
        if internals:
            initial['internal'] = internals[0].supervisor_id
        chairs = Supervisor.objects.filter(student=grad,
                                           removed=False,
                                           supervisor_type='CHA')
        if chairs:
            initial['chair'] = chairs[0].supervisor_id
        externals = Supervisor.objects.filter(student=grad,
                                              removed=False,
                                              supervisor_type='EXT')
        if externals:
            ext = externals[0]
            initial['external'] = ext.external
            initial['external_email'] = ext.config.get('email', '')
            initial['external_contact'] = ext.config.get('contact', '')
            initial['external_attend'] = ext.config.get('attend', '')

        form = GradDefenceForm(initial=initial)
        form.set_supervisor_choices(supervisor_choices)

    context = {
        'form': form,
        'grad': grad,
    }
    return render(request, 'grad/manage_defence.html', context)
Exemplo n.º 5
0
def process_pcs_row(row, column, rownum, unit, semester, user):
    """
    Process a single row from the PCS import
    """
    appsemester = semester.previous_semester()
    warnings = []
    ident = "in row %i" % (rownum)
    appid = row[column['appid']]
    emplid = row[column['emplid']]
    program = row[column['program']]

    # get Person, from SIMS if necessary
    try:
        p = Person.objects.get(emplid=int(emplid))
    except ValueError:
        warnings.append("Bad emplid %s: not processing that row." % (ident))
        return warnings
    except Person.DoesNotExist:
        try:
            p = add_person(emplid)
        except SIMSProblem as e:
            return e.message

    ident = 'for "%s"' % (p.name())

    # update information on the Person
    email = row[column['email']]
    if email: p.config['applic_email'] = email

    dob = row[column['dob']]
    if dob:
        try:
            dt = datetime.datetime.strptime(dob, "%Y-%m-%d")
            p.config['birthdate'] = dt.date().isoformat()
        except ValueError:
            warnings.append("Bad birthdate %s." % (ident))

    # get extended SIMS data
    data = grad_student_info(emplid)
    p.config.update(data)

    p.save()

    #print "Importing %s" % (p)

    # get GradStudent, creating if necessary

    # a unique identifier for this application, so we can detect repeated imports (and handle gracefully)
    uid = "%s-%s-%s-%s" % (unit.slug, semester.name, appid, emplid)
    # TODO: wrong, wrong, wrong. Figure out how to select program from import data
    program = GradProgram.objects.filter(unit=unit)[0]

    # find the old GradStudent if possible
    gss = GradStudent.objects.filter(program__unit=unit, person=p)
    gs = None
    for g in gss:
        if 'app_id' in g.config and g.config['app_id'] == uid:
            gs = g
            break
    if not gs:
        gs = GradStudent(program=program, person=p)
        gs.config['app_id'] = uid

    resarea = row[column['resarea']]
    firstlang = row[column['firstlang']]

    gs.research_area = resarea
    gs.mother_tongue = firstlang
    gs.created_by = user.userid
    gs.updated_by = user.userid
    gs.config['start_semester'] = semester.name
    gs.save()

    complete = row[column['complete']].strip()
    decision = row[column['decision']].strip()
    notes = row[column['notes']].strip()
    gs.config['decisionnotes'] = notes

    old_st = GradStatus.objects.filter(student=gs,
                                       start__name__gte=semester.name)
    if not old_st:
        # if no old status for current semester, create one

        # application completion status
        if complete == 'AppConfirm':
            st = GradStatus(student=gs,
                            status="COMP",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif complete == '':
            st = GradStatus(student=gs,
                            status="INCO",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        else:
            warnings.append(
                'Unknown "Confirmation of Completion of Application" value %s.'
                % (ident))

        # decision status
        if decision == 'DECL':
            st = GradStatus(student=gs,
                            status="DECL",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif decision == '':
            st = GradStatus(student=gs,
                            status="OFFO",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif decision == 'R':
            st = GradStatus(student=gs,
                            status="REJE",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif decision == 'HOLD':
            st = GradStatus(student=gs,
                            status="HOLD",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif decision == 'AMScT':
            # TODO: bump program to MSc thesis
            st = GradStatus(student=gs,
                            status="CONF",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()
        elif decision == 'AMScC':
            # TODO: bump program to MSc course-based
            st = GradStatus(student=gs,
                            status="CONF",
                            start=appsemester,
                            end=None,
                            notes="PCS import")
            st.save()

    # potential supervisor
    potsuper = row[column['potsuper']]
    if potsuper:
        superv = None
        external = None
        try:
            ps_last, ps_first = potsuper.split(', ')
        except ValueError:
            warnings.append(
                'Bad potential supervisor name %s: will store them as an "external" supervisor.'
                % (ident))
            external = potsuper
        else:
            potentials = possible_supervisor_people([unit])
            potential_ids = [p.id for p in potentials]
            query = Q(last_name=ps_last, first_name=ps_first) | Q(
                last_name=ps_last, pref_first_name=ps_first)
            people = Person.objects.filter(query, id__in=potential_ids)
            if people.count() == 1:
                superv = people[0]
            else:
                warnings.append(
                    'Coundn\'t find potential supervisor %s: will store them as an "external" supervisor.'
                    % (ident))
                external = potsuper

        old_s = Supervisor.objects.filter(student=gs, supervisor_type='POT')
        if old_s:
            s = old_s[0]
        else:
            s = Supervisor(student=gs, supervisor_type='POT')
        s.superv = superv
        s.external = external
        s.position = 0
        s.created_by = user.userid
        s.modified_by = user.userid
        s.save()

    l = LogEntry(userid=user.userid,
                 description="Imported grad record for %s (%s) from PCS" %
                 (p.name(), p.emplid),
                 related_object=gs)
    l.save()

    return warnings
Exemplo n.º 6
0
def manage_defence(request, grad_slug):
    """
    Page for managing all defence-related stuff.
    
    Slightly complicated since this info cuts across several models.
    """
    grad = get_object_or_404(GradStudent, slug=grad_slug, program__unit__in=request.units)
    supervisors = Supervisor.objects.filter(student=grad).select_related('supervisor')
    supervisor_people = [s.supervisor for s in supervisors if s.supervisor]
    supervisor_choices = possible_supervisors(units=request.units, extras=supervisor_people, null=True)
        
    if request.method == 'POST':
        form = GradDefenceForm(request.POST)
        form.set_supervisor_choices(supervisor_choices)
        if form.is_valid():
            with transaction.atomic():
                grad.config['thesis_type'] = form.cleaned_data['thesis_type']
                grad.config['work_title'] = form.cleaned_data['work_title']
                grad.config['exam_date'] = form.cleaned_data['exam_date']
                grad.config['thesis_outcome'] = form.cleaned_data['thesis_outcome']
                grad.config['thesis_location'] = form.cleaned_data['thesis_location']
                grad.save()
                
                if form.cleaned_data['internal']:
                    p = form.cleaned_data['internal']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='SFU').exclude(supervisor=p)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()
                    
                    existing_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='SFU', supervisor=p)
                    if not existing_sup:
                        # doesn't exist: create
                        sup = Supervisor(student=grad, supervisor_type='SFU', supervisor=p)
                        sup.save()
                    # else: already there, so nothing to change
                
                if form.cleaned_data['chair']:
                    p = form.cleaned_data['chair']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='CHA').exclude(supervisor=p)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()
                    
                    existing_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='CHA', supervisor=p)
                    if not existing_sup:
                        # doesn't exist: create
                        sup = Supervisor(student=grad, supervisor_type='CHA', supervisor=p)
                        sup.save()
                    # else: already there, so nothing to change

                if form.cleaned_data['external']:
                    name = form.cleaned_data['external']
                    # remove any old ones
                    remove_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='EXT').exclude(external=name)
                    for sup in remove_sup:
                        sup.removed = True
                        sup.save()
                    
                    # creqate/update
                    existing_sup = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='EXT', external=name)
                    if existing_sup:
                        sup = existing_sup[0]
                    else:
                        sup = Supervisor(student=grad, supervisor_type='EXT', external=name)
                    
                    sup.config['email'] = form.cleaned_data['external_email']
                    sup.config['contact'] = form.cleaned_data['external_contact']
                    sup.config['attend'] = form.cleaned_data['external_attend']
                    sup.save()

            messages.success(request, "Updated defence info for %s." % (grad.person))
            l = LogEntry(userid=request.user.username,
                  description="Updated grad defence info for %s." % (grad),
                  related_object=grad)
            l.save()    
            return HttpResponseRedirect(reverse('grad.views.view', kwargs={'grad_slug':grad.slug}))
    else:
        initial = {}
        if 'thesis_type' in grad.config:
            initial['thesis_type'] = grad.config['thesis_type']
        if 'work_title' in grad.config:
            initial['work_title'] = grad.config['work_title']
        if 'exam_date' in grad.config:
            initial['exam_date'] = grad.config['exam_date']
        if 'thesis_outcome' in grad.config:
            initial['thesis_outcome'] = grad.config['thesis_outcome']
        if 'thesis_location' in grad.config:
            initial['thesis_location'] = grad.config['thesis_location']
        internals = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='SFU')
        if internals:
            initial['internal'] = internals[0].supervisor_id
        chairs = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='CHA')
        if chairs:
            initial['chair'] = chairs[0].supervisor_id
        externals = Supervisor.objects.filter(student=grad, removed=False, supervisor_type='EXT')
        if externals:
            ext = externals[0]
            initial['external'] = ext.external
            initial['external_email'] = ext.config.get('email', '')
            initial['external_contact'] = ext.config.get('contact', '')
            initial['external_attend'] = ext.config.get('attend', '')

        form = GradDefenceForm(initial=initial)
        form.set_supervisor_choices(supervisor_choices)        

    context = {
               'form': form,
               'grad': grad,
               }
    return render(request, 'grad/manage_defence.html', context)