Пример #1
0
def create_single_class_pdf(
    teacher_name,
    class_id,
    class_name,
    class_average,
    total_students,
    total_grades,
    standards_list,
    grade_standard_dict,
    grade_student_dict,
    standard_total_dict,
):
    pdfName = class_name + "_CLR" + ".pdf"
    response.headers["Content-Type"] = "application/pdf"
    response.headers["Content-Disposition"] = "attachment;filename=%s;" % pdfName
    styles = getSampleStyleSheet()
    HeaderStyle = styles["Heading1"]
    buff = StringIO()
    doc = SimpleDocTemplate(buff, pagesize=letter, rightMargin=72, leftMargin=72, topMargin=72, bottomMargin=18)
    doc.title = pdfName
    Story = []
    Elements = []
    formatted_time = time.ctime()
    styles = getSampleStyleSheet()
    styles.add(ParagraphStyle(name="Justify", alignment=TA_JUSTIFY))
    styles.add(ParagraphStyle(name="Indent", rightIndent=3))
    styles.add(
        ParagraphStyle(
            name="Title2",
            parent=styles["Normal"],
            fontName="DejaVuSansCondensed",
            fontSize=18,
            leading=22,
            # alignment = TA_LEFT,
            spaceAfter=6,
        ),
        alias="title2",
    )

    ptext = "<font size=12>%s</font>" % formatted_time

    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    # Teacher Name
    ptext = "<font size=12><b>%s %s</b></font>" % (teacher_name.first_name, teacher_name.last_name)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    # Class Name
    ptext = "<font size=12>%s: </font>" % (class_name)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 7))
    Elements.extend(ptext)

    # Class Average
    ptext = "<font size=12>Class Average:%s%%</font>" % (class_average)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    # Total Students
    ptext = "<font size=12>Total Students:%s</font>" % (total_students)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    # Total Assignments
    ptext = "<font size=12>Total Assignments:%s</font>" % (total_grades)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Story.append(Spacer(1, 40))

    ptext = "<font size=15><b>Standards Progress</b></font>"
    Story.append(Paragraph(ptext, styles["title"]))

    test_values = [[10, 20, 50, 90, 80]]
    standard_table = []
    i = 0
    minimum = 100
    standard_averages = [[]]
    # Go through the standard_total_dict keys and add all the necessary values to the standard_averages 2d list.
    for standard in sorted(standard_total_dict.keys()):
        standard_table.append([])
        current_avg = (standard_total_dict[standard][1] / standard_total_dict[standard][0]) * 100
        if minimum > current_avg:
            minimum = current_avg
        standard_table[i].append(
            standard_total_dict[standard][3]
            + ": "
            + format((standard_total_dict[standard][1] / standard_total_dict[standard][0]) * 100, ".2f")
            + "%"
        )
        standard_averages[0].append(
            int(round((standard_total_dict[standard][1] / standard_total_dict[standard][0]) * 100))
        )

        # add assignments to correct buckets
        for grade in grade_standard_dict.keys():
            for standardId in grade_standard_dict[grade][1]:
                if standardId == standard:
                    standard_table[i].append(
                        grade_standard_dict[grade][0]
                        + ":"
                        + format((grade_student_dict[grade][1] / grade_student_dict[grade][0]) * 100, ".2f")
                        + "%"
                    )
        i += 1
    sorted(standard_table, key=lambda l: l[0])

    # graph
    drawing = Drawing(600, 200)
    data = standard_averages
    bc = VerticalBarChart()

    # location in the document (x,y)
    bc.x = 10
    bc.y = 30

    # width and height of the graph
    bc.height = 225
    bc.width = 400
    bc.data = data
    bc.categoryAxis.drawGridLast = True
    bc.categoryAxis.gridStart = 0
    bc.categoryAxis.gridStrokeLineCap = 2
    bc.categoryAxis.gridEnd = 3
    bc.barLabels = [10, 20, 30, 40, 50]

    # Update colors of the bars in the graph
    bc.bars.symbol = ShadedRect()
    bc.bars.symbol.fillColorStart = colors.lightblue
    bc.bars.symbol.fillColorEnd = colors.lightblue
    bc.bars.symbol.strokeWidth = 0

    # this draws a line at the top of the graph to close it.
    bc.strokeColor = colors.black

    # Y-axis min, max, and steps.
    if minimum != 100:
        bc.valueAxis.valueMin = minimum - 10
    else:
        bc.valueAxis.valueMin = 50
    bc.valueAxis.valueMax = 100
    bc.valueAxis.valueStep = 5

    # where to anchor the origin of the graph
    bc.categoryAxis.labels.boxAnchor = "ne"

    # Locations of labels for the X-axis
    bc.categoryAxis.labels.dx = 2
    bc.categoryAxis.labels.dy = -2

    # The angle of the lables for the X-axis
    bc.categoryAxis.labels.angle = 30
    # List of the categories to place on the X-axis
    bc.categoryAxis.categoryNames = standards_list
    drawing.add(bc)

    # Graph Legend
    legend = Legend()
    legend.alignment = "right"
    legend.x = 420
    legend.y = 150
    legend.deltax = 60
    legend.dxTextSpace = 10
    legend.columnMaximum = 4

    legend.colorNamePairs = [(colors.lightblue, "grade average")]
    drawing.add(legend, "legend")
    drawing_title = "Bar Graph"
    Story.append(drawing)

    t = Table(standard_table)
    t.setStyle(
        t.setStyle(
            TableStyle(
                [
                    ("BOX", (0, 0), (-1, -1), 0.25, colors.black),
                    ("FONTSIZE", (0, 0), (-1, -1), 7),
                    ("BACKGROUND", (0, 0), (0, -1), colors.lightgrey),
                    ("INNERGRID", (0, 0), (-1, -1), 0.25, colors.black),
                ]
            )
        )
    )
    Story.append(t)
    doc.build(Story)
    pdf = buff.getvalue()
    buff.close()
    return pdf
Пример #2
0
def create_single_class_pdf(teacher_name, class_id, class_name, class_average,
                            total_students, total_grades, standards_list,
                            grade_standard_dict, grade_student_dict,
                            standard_total_dict):
    pdfName = class_name + "_CLR" + ".pdf"
    response.headers['Content-Type'] = 'application/pdf'
    response.headers[
        'Content-Disposition'] = 'attachment;filename=%s;' % pdfName
    styles = getSampleStyleSheet()
    HeaderStyle = styles["Heading1"]
    buff = StringIO()
    doc = SimpleDocTemplate(buff,
                            pagesize=letter,
                            rightMargin=72,
                            leftMargin=72,
                            topMargin=72,
                            bottomMargin=18)
    doc.title = pdfName
    Story = []
    Elements = []
    formatted_time = time.ctime()
    styles = getSampleStyleSheet()
    styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
    styles.add(ParagraphStyle(name='Indent', rightIndent=3))
    styles.add(
        ParagraphStyle(
            name='Title2',
            parent=styles['Normal'],
            fontName='DejaVuSansCondensed',
            fontSize=18,
            leading=22,
            #alignment = TA_LEFT,
            spaceAfter=6),
        alias='title2')

    ptext = '<font size=12>%s</font>' % formatted_time

    Story.append(Paragraph(ptext, styles["Normal"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    #Teacher Name
    ptext = '<font size=12><b>%s %s</b></font>' % (teacher_name.first_name,
                                                   teacher_name.last_name)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    #Class Name
    ptext = '<font size=12>%s: </font>' % (class_name)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 7))
    Elements.extend(ptext)

    #Class Average
    ptext = '<font size=12>Class Average:%s%%</font>' % (class_average)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    #Total Students
    ptext = '<font size=12>Total Students:%s</font>' % (total_students)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Elements.extend(ptext)

    #Total Assignments
    ptext = '<font size=12>Total Assignments:%s</font>' % (total_grades)
    Story.append(Paragraph(ptext, styles["Justify"]))
    Story.append(Spacer(1, 12))
    Story.append(Spacer(1, 40))

    ptext = '<font size=15><b>Standards Progress</b></font>'
    Story.append(Paragraph(ptext, styles["title"]))

    test_values = [[10, 20, 50, 90, 80]]
    standard_table = []
    i = 0
    minimum = 100
    standard_averages = [[]]
    #Go through the standard_total_dict keys and add all the necessary values to the standard_averages 2d list.
    for standard in sorted(standard_total_dict.keys()):
        standard_table.append([])
        current_avg = (standard_total_dict[standard][1] /
                       standard_total_dict[standard][0]) * 100
        if minimum > current_avg:
            minimum = current_avg
        standard_table[i].append(standard_total_dict[standard][3] + ": " +
                                 format((standard_total_dict[standard][1] /
                                         standard_total_dict[standard][0]) *
                                        100, '.2f') + "%")
        standard_averages[0].append(
            int(
                round((standard_total_dict[standard][1] /
                       standard_total_dict[standard][0]) * 100)))

        #add assignments to correct buckets
        for grade in grade_standard_dict.keys():
            for standardId in grade_standard_dict[grade][1]:
                if (standardId == standard):
                    standard_table[i].append(
                        grade_standard_dict[grade][0] + ":" +
                        format((grade_student_dict[grade][1] /
                                grade_student_dict[grade][0]) * 100, '.2f') +
                        "%")
        i += 1
    sorted(standard_table, key=lambda l: l[0])

    #graph
    drawing = Drawing(600, 200)
    data = standard_averages
    bc = VerticalBarChart()

    #location in the document (x,y)
    bc.x = 10
    bc.y = 30

    #width and height of the graph
    bc.height = 225
    bc.width = 400
    bc.data = data
    bc.categoryAxis.drawGridLast = True
    bc.categoryAxis.gridStart = 0
    bc.categoryAxis.gridStrokeLineCap = 2
    bc.categoryAxis.gridEnd = 3
    bc.barLabels = [10, 20, 30, 40, 50]

    #Update colors of the bars in the graph
    bc.bars.symbol = ShadedRect()
    bc.bars.symbol.fillColorStart = colors.lightblue
    bc.bars.symbol.fillColorEnd = colors.lightblue
    bc.bars.symbol.strokeWidth = 0

    #this draws a line at the top of the graph to close it.
    bc.strokeColor = colors.black

    #Y-axis min, max, and steps.
    if minimum != 100:
        bc.valueAxis.valueMin = minimum - 10
    else:
        bc.valueAxis.valueMin = 50
    bc.valueAxis.valueMax = 100
    bc.valueAxis.valueStep = 5

    #where to anchor the origin of the graph
    bc.categoryAxis.labels.boxAnchor = 'ne'

    #Locations of labels for the X-axis
    bc.categoryAxis.labels.dx = 2
    bc.categoryAxis.labels.dy = -2

    #The angle of the lables for the X-axis
    bc.categoryAxis.labels.angle = 30
    #List of the categories to place on the X-axis
    bc.categoryAxis.categoryNames = standards_list
    drawing.add(bc)

    #Graph Legend
    legend = Legend()
    legend.alignment = 'right'
    legend.x = 420
    legend.y = 150
    legend.deltax = 60
    legend.dxTextSpace = 10
    legend.columnMaximum = 4

    legend.colorNamePairs = [(colors.lightblue, 'grade average')]
    drawing.add(legend, 'legend')
    drawing_title = "Bar Graph"
    Story.append(drawing)

    t = Table(standard_table)
    t.setStyle(
        t.setStyle(
            TableStyle([
                ('BOX', (0, 0), (-1, -1), 0.25, colors.black),
                ('FONTSIZE', (0, 0), (-1, -1), 7),
                ('BACKGROUND', (0, 0), (0, -1), colors.lightgrey),
                ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
            ])))
    Story.append(t)
    doc.build(Story)
    pdf = buff.getvalue()
    buff.close()
    return pdf