示例#1
0
def class_result_update(request, slug):
    subject = Subject.objects.get(slug=slug)
    if request.user != subject.teacher.user:
        return HttpResponse(
            "<h1>You are not allowed to access the given details<h1>")
    students = Student.objects.filter(
        current_standard=subject.standard).order_by('roll_number')
    examinations = Examination.objects.filter(standard=subject.standard)

    if request.method == 'POST':
        for student in students:
            for examination in examinations:
                exam = examination.name
                result = Result.objects.get(student=student,
                                            subject=subject,
                                            examination=examination)
                x = str(student.roll_number) + " " + exam.name
                marks = request.POST.get(x)
                try:
                    result.marks_secured = int(marks)
                except:
                    result.marks_secured = None
                result.save()
        return redirect('teachers:home')

    results = {}
    for student in students:
        results[student] = {}
        for examination in examinations:
            try:
                result = Result.objects.get(student=student,
                                            subject=subject,
                                            examination=examination)
            except Result.DoesNotExist:
                result = Result(student=student,
                                subject=subject,
                                examination=examination)
                result.save()
            results[student][examination] = result

    my_dict = {
        'results': results,
        'subject': subject,
        'students': students,
        'examinations': examinations,
        'size': str(len(examinations))
    }

    return render(request, 'teachers/update_result.html', context=my_dict)
def load_data_results(electiondate_arg, live_arg, test_arg, all_arg):

    ## load from ResultStage...
    results = ResultStage.objects.filter(electiondate=electiondate_arg)

    ## ...unless for some reason there are none there, then load via Elex
    if all_arg and results.count() == 0:
        results = election_connection(electiondate_arg, live_arg,
                                      test_arg).results
    # elif not all_arg and results.count() == 0:
    ## load from json
    # from subprocess import call
    # import json
    # import os

    # file_path = os.environ["SAVER_PATH"]
    # json_file = file_path + "/tmp/results.json"
    # results = json.loads(json_file)

    # mcc_id = unicode(result_item.candidateid) + '-' + unicode(result_item.reportingunitid) + '-' + unicode(result_item.level)

    result_list = [
        ## this one uses datetime_parser
        Result(
            dataentry="automated",
            datasource="Associated Press",
            ballotorder=result_item.ballotorder,
            candidateid=result_item.candidateid,
            # delegatecount=result_item.delegatecount,
            description=result_item.description,
            electiondate=result_item.electiondate,
            fipscode=result_item.fipscode,
            first=result_item.first,
            # id=mcc_id,
            # unique_id=result_item.unique_id,
            incumbent=result_item.incumbent,
            initialization_data=result_item.initialization_data,
            is_ballot_measure=result_item.is_ballot_measure,
            last=result_item.last,
            level=result_item.level,
            # mccelectionid=mccelectionid_constructor,
            national=result_item.national,
            officeid=result_item.officeid,
            officename=result_item.officename,
            party=result_item.party,
            polid=result_item.polid,
            polnum=result_item.polnum,
            precinctsreporting=result_item.precinctsreporting,
            precinctsreportingpct=result_item.precinctsreportingpct,
            precinctstotal=result_item.precinctstotal,
            raceid=result_item.raceid,
            racetype=result_item.racetype,
            racetypeid=result_item.racetypeid,
            reportingunitid=result_item.reportingunitid,
            reportingunitname=result_item.reportingunitname,
            runoff=result_item.runoff,
            seatname=result_item.seatname,
            seatnum=result_item.seatnum,
            statename=result_item.statename,
            statepostal=result_item.statepostal,
            test=result_item.test,
            uncontested=result_item.uncontested,
            lastupdated=result_item.lastupdated,
            votecount=result_item.votecount,
            votepct=result_item.votepct,
            winner=result_item.winner) for result_item in results
    ]

    results_length = len(result_list)

    ## bulk create method
    Result.objects.bulk_create(result_list)
    # import pdb; pdb.set_trace()

    message = "Loaded:\t\t\t" + str(results_length) + " results"
    slackbot(message)