コード例 #1
0
ファイル: views.py プロジェクト: rectory-school/rectory-apps
def full_zip(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/zip")

    with ZipFile(response, mode="w", compression=ZIP_STORED) as zip_file:
        # Generate ALL the possibilities
        for formatter in (("Color", (color_formatter, color_accent)), ("Black", (black_formatter, colors.black))):
            for embed in ("Print", "Embed", "Embed without titles"):
                for year, month in sorted(structured_data.keys()):
                    file_name = "{embed:}/{formatter:}/{year:}-{month:0>2}.pdf".format(
                        embed=embed, formatter=formatter[0], year=year, month=month
                    )

                    out = BytesIO()
                    pdf = canvas.Canvas(out, pagesize=(11 * inch, 8.5 * inch))

                    grid_formatter = formatter[1][0]
                    title_color = formatter[1][1]

                    grid = structured_data[(year, month)]

                    first_of_month = date(year, month, 1)
                    month_title = first_of_month.strftime("%B %Y")

                    grid_drawer = GridDrawer(grid, header_days, grid_formatter)

                    if embed == "Print":
                        pdf.setFillColor(title_color)
                        pdf.setFont("HelveticaNeue-Bold", 72)
                        pdf.drawString(0.5 * inch, 7.25 * inch, month_title)

                        grid_drawer.draw_on(pdf, 0.5 * inch, 7 * inch, 10 * inch, 6.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    elif embed == "Embed":
                        pdf.setFillColor(title_color)
                        pdf.setFont("HelveticaNeue-Bold", 72)
                        pdf.drawString(1, 7.75 * inch, month_title)

                        grid_drawer.draw_on(pdf, 0, 7.5 * inch, 11 * inch, 7.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    elif embed == "Embed without titles":
                        grid_drawer.draw_on(pdf, 0, 8.5 * inch, 11 * inch, 8.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    zip_file.writestr(file_name, out.getvalue())

    response["Content-Disposition"] = 'filename="{title:}.zip"'.format(title=calendar.title)
    return response
コード例 #2
0
ファイル: views.py プロジェクト: rectory-school/rectory-apps
def one_page_calendar(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/pdf")

    pdf = canvas.Canvas(response, pagesize=(8.5 * inch, 11 * inch))
    pdf.setTitle("{title:}".format(title=calendar.title))

    cols = 2
    col_width = 7.5 * inch / cols

    rows = math.ceil(len(structured_data) / cols)
    row_height = 10 * inch / rows

    inner_cell_height = (11 - 1) / rows * inch

    for i, (year, month) in enumerate(sorted(structured_data.keys())):
        col = i % cols
        row = math.floor(i / cols)

        x = 0.5 * inch + col * col_width
        y = 11 * inch - (0.5 * inch + row * row_height)

        grid = structured_data[(year, month)]

        first_of_month = date(year, month, 1)
        month_title = first_of_month.strftime("%B %Y")

        grid_drawer = GridDrawer(grid, header_days, black_formatter)

        pdf.setFont("HelveticaNeue-Bold", 12)
        pdf.setFillColor(colors.black)
        pdf.drawString(x + 0.25 * inch, y - 6, month_title)

        grid_drawer.draw_on(
            pdf, x + 0.25 * inch, y - 0.035 * inch - 6, col_width - 0.5 * inch, row_height - 0.25 * inch, 0.5
        )

    pdf.showPage()
    pdf.save()

    return response
コード例 #3
0
def full_calendar_pdf(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    if "color" in request.GET:
        formatter = color_formatter
        color = True
    else:
        formatter = black_formatter
        color = False

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/pdf")

    pdf = canvas.Canvas(response, pagesize=(11 * inch, 8.5 * inch))

    pdf.setTitle("{title:}".format(title=calendar.title))

    for year, month in sorted(structured_data.keys()):
        grid = structured_data[(year, month)]

        first_of_month = date(year, month, 1)
        month_title = first_of_month.strftime("%B %Y")

        grid_drawer = GridDrawer(grid, header_days, formatter)

        if color:
            pdf.setFillColor(color_accent)
        else:
            pdf.setFillColor(colors.black)

        pdf.setFont("HelveticaNeue-Bold", 72)
        pdf.drawString(.5 * inch, 7.25 * inch, month_title)

        grid_drawer.draw_on(pdf, .5 * inch, 7 * inch, 10 * inch, 6.5 * inch,
                            line_width)
        pdf.showPage()

    pdf.save()

    response['Content-Disposition'] = 'filename="{title:}.pdf"'.format(
        title=calendar.title)
    return response
コード例 #4
0
def one_page_calendar(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/pdf")

    pdf = canvas.Canvas(response, pagesize=(8.5 * inch, 11 * inch))
    pdf.setTitle("{title:}".format(title=calendar.title))

    cols = 2
    col_width = 7.5 * inch / cols

    rows = math.ceil(len(structured_data) / cols)
    row_height = 10 * inch / rows

    inner_cell_height = (11 - 1) / rows * inch

    for i, (year, month) in enumerate(sorted(structured_data.keys())):
        col = i % cols
        row = math.floor(i / cols)

        x = .5 * inch + col * col_width
        y = 11 * inch - (.5 * inch + row * row_height)

        grid = structured_data[(year, month)]

        first_of_month = date(year, month, 1)
        month_title = first_of_month.strftime("%B %Y")

        grid_drawer = GridDrawer(grid, header_days, black_formatter)

        pdf.setFont("HelveticaNeue-Bold", 12)
        pdf.setFillColor(colors.black)
        pdf.drawString(x + .25 * inch, y - 6, month_title)

        grid_drawer.draw_on(pdf, x + .25 * inch, y - .035 * inch - 6,
                            col_width - .5 * inch, row_height - .25 * inch, .5)

    pdf.showPage()
    pdf.save()

    return response
コード例 #5
0
ファイル: views.py プロジェクト: rectory-school/rectory-apps
def full_calendar_pdf(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    if "color" in request.GET:
        formatter = color_formatter
        color = True
    else:
        formatter = black_formatter
        color = False

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/pdf")

    pdf = canvas.Canvas(response, pagesize=(11 * inch, 8.5 * inch))

    pdf.setTitle("{title:}".format(title=calendar.title))

    for year, month in sorted(structured_data.keys()):
        grid = structured_data[(year, month)]

        first_of_month = date(year, month, 1)
        month_title = first_of_month.strftime("%B %Y")

        grid_drawer = GridDrawer(grid, header_days, formatter)

        if color:
            pdf.setFillColor(color_accent)
        else:
            pdf.setFillColor(colors.black)

        pdf.setFont("HelveticaNeue-Bold", 72)
        pdf.drawString(0.5 * inch, 7.25 * inch, month_title)

        grid_drawer.draw_on(pdf, 0.5 * inch, 7 * inch, 10 * inch, 6.5 * inch, line_width)
        pdf.showPage()

    pdf.save()

    response["Content-Disposition"] = 'filename="{title:}.pdf"'.format(title=calendar.title)
    return response
コード例 #6
0
def full_zip(request, id):
    calendar = get_object_or_404(Calendar, pk=id)
    days = get_days(calendar)

    header_days = calendar.numeric_days

    structured_data = structured_calendar_layout(days)

    response = HttpResponse(content_type="application/zip")

    with ZipFile(response, mode='w', compression=ZIP_STORED) as zip_file:
        #Generate ALL the possibilities
        for formatter in (("Color", (color_formatter, color_accent)),
                          ("Black", (black_formatter, colors.black))):
            for embed in ("Print", "Embed", "Embed without titles"):
                for year, month in sorted(structured_data.keys()):
                    file_name = "{embed:}/{formatter:}/{year:}-{month:0>2}.pdf".format(
                        embed=embed,
                        formatter=formatter[0],
                        year=year,
                        month=month)

                    out = BytesIO()
                    pdf = canvas.Canvas(out, pagesize=(11 * inch, 8.5 * inch))

                    grid_formatter = formatter[1][0]
                    title_color = formatter[1][1]

                    grid = structured_data[(year, month)]

                    first_of_month = date(year, month, 1)
                    month_title = first_of_month.strftime("%B %Y")

                    grid_drawer = GridDrawer(grid, header_days, grid_formatter)

                    if embed == "Print":
                        pdf.setFillColor(title_color)
                        pdf.setFont("HelveticaNeue-Bold", 72)
                        pdf.drawString(.5 * inch, 7.25 * inch, month_title)

                        grid_drawer.draw_on(pdf, .5 * inch, 7 * inch,
                                            10 * inch, 6.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    elif embed == "Embed":
                        pdf.setFillColor(title_color)
                        pdf.setFont("HelveticaNeue-Bold", 72)
                        pdf.drawString(1, 7.75 * inch, month_title)

                        grid_drawer.draw_on(pdf, 0, 7.5 * inch, 11 * inch,
                                            7.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    elif embed == "Embed without titles":
                        grid_drawer.draw_on(pdf, 0, 8.5 * inch, 11 * inch,
                                            8.5 * inch, line_width)
                        pdf.showPage()
                        pdf.save()

                    zip_file.writestr(file_name, out.getvalue())

    response['Content-Disposition'] = 'filename="{title:}.zip"'.format(
        title=calendar.title)
    return response