コード例 #1
0
ファイル: views.py プロジェクト: monstertek/nomosdb
def export_feedback(
        request, code, year, assessment_slug, student_id, attempt='first'):
    """Will export either one or multiple feedback sheets.

    This needs to be given the student id or the string 'all' if
    you want all marksheets for the assessment. It will only work if
    the person requesting is a teacher, an admin or the student the
    marksheet is about.
    """
    module = Module.objects.get(code=code, year=year)
    assessment = Assessment.objects.get(module=module, slug=assessment_slug)
    if attempt == 'first':
        assessment_type = assessment.marksheet_type
    else:
        assessment_type = assessment.resit_marksheet_type
    if student_id == 'all':
        if is_staff(request.user):
            response = HttpResponse(content_type='application/pdf')
            filename_string = (
                'attachment; filename=' +
                assessment.filename() +
                '_-_all_marksheets.pdf'
            )
            all_students = module.students.all()
            documentlist = []
            students = []  # Only the students where feedback has been entered
            for student in all_students:
                performance = Performance.objects.get(
                    student=student, module=module)
                try:
                    result = AssessmentResult.objects.get(
                        part_of=performance, assessment=assessment)
                    try:
                        feedback = IndividualFeedback.objects.get(
                            assessment_result=result, attempt=attempt)
                        if feedback.completed:
                            students.append(student)
                    except IndividualFeedback.DoesNotExist:
                        pass
                except AssessmentResult.DoesNotExist:
                    pass
            for student in students:
                if assessment.group_assessment:
                    elements = group_presentation_marksheet(
                        assessment, student, attempt)
                    pass
                else:
                    elements = individual_marksheet(
                        assessment, student, attempt)
                for element in elements:
                    documentlist.append(element)
                documentlist.append(PageBreak())
            response['Content-Disposition'] = filename_string
            document = SimpleDocTemplate(response)
            uni_name = Setting.objects.get(name="uni_name").value
            document.setAuthor = uni_name
            document.setTitle = 'Marksheet'
            document.build(documentlist)
            return response
        else:
            return HttpResponseForbidden()
    else:
        student = Student.objects.get(student_id=student_id)
        own_marksheet = False  # Just for the filename
        allowed = False
        if is_staff(request.user):
            allowed = True
        elif is_student(request.user):
            if student.user == request.user:
                own_marksheet = True
                allowed = True
        if allowed:
            response = HttpResponse(content_type='application/pdf')
            filename_string = 'attachment; filename=' + assessment.filename()
            if not own_marksheet:
                ln = student.last_name.replace(' ', '_')
                fn = student.first_name.replace(' ', '_')
                filename_string += ln + '_' + fn
            filename_string += '.pdf'
            response['Content-Disposition'] = filename_string
            document = SimpleDocTemplate(response)
            uni_name = Setting.objects.get(name="uni_name").value
            document.setAuthor = uni_name
            if assessment.group_assessment:
                elements = group_presentation_marksheet(
                    assessment, student, attempt)
                pass
            else:
                elements = individual_marksheet(
                    assessment, student, attempt)
            document.build(elements)
            return response
        else:
            return HttpResponseForbidden()