Пример #1
0
def birds_for_breeding_pdf(request, exact_method=False):
    try:
        styles = getSampleStyleSheet()
        styleN = styles['Normal']
        styleH = styles['Heading3']
        styleH2 = styles['Heading4']

        ts = TableStyle([
            ('FONT', (0, 0), (-1, -1), 'Helvetica', 9),
            ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold', 10),
            ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
            ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
            ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
        ])

        ts2 = TableStyle([
            ('FONT', (0, 0), (-1, -1), 'Helvetica', 7),
            ('FONT', (0, 0), (-1, 0), 'Helvetica-Bold', 9),
            ('FONT', (6, 0), (6, 0), 'Helvetica-Bold', 6),
            ('FONT', (7, 0), (7, 0), 'Helvetica-Bold', 8),
            ('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
            ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
            ('ALIGN', (0, 0), (-1, -1), 'LEFT'),
        ])

        style = ParagraphStyle(
            name='Normal',
            fontName='Helvetica',
            fontSize=7,
        )
        ''' get list of animals '''
        from birdlist.utils.breeding import sort_birds_for_breeding, get_birds_for_breeding
        couples, males, females, males_id, females_id = get_birds_for_breeding(
            request.session, exact_method)
        couples, males, females, sort_by = sort_birds_for_breeding(
            couples, males, females, request.GET)
        ''' start document '''
        title = "Birds for breeding"
        author = 'Andreas Kotowicz'

        Story, buffer, doc, response = pdf_header(
            page_size=portrait(A4),
            filename="birds_for_breeding.pdf",
            title=title,
            author=author)
        Story = add_title(title, Story, request, title_size=14)

        Story.append(Spacer(1, 15))
        paragraph = Paragraph(
            'Please do not use birds that have been separated (previous breeding couple) less than 60 days ago!',
            styleH2)
        Story.append(paragraph)
        Story.append(Spacer(1, 15))

        colwidth0 = 40
        colwidth = 65
        colwidth2 = 80
        colwidth3 = 130
        ''' previous couples '''
        paragraph = Paragraph(
            "Previously successful couples - available today (%s)" %
            couples.__len__(), styleH)
        paragraph.keepWithNext = True
        Story.append(paragraph)
        data = [[
            'male', 'cage', 'age', 'female', 'cage', 'age', 'avg # broods',
            'avg # juveniles', '# days separated (m/f)'
        ]]
        for c in couples:
            data.append([c['male'], c['male_cage'], c['male_age'], c['female'], \
            c['female_cage'], c['female_age'], str(round(c['AvgNoBroods'], 1)), str(round(c['AvgNoJuvs'], 1)), \
            str(c['male_last_separation']) + ' / ' + str(c['female_last_separation'])])

        colwidths = (colwidth0, colwidth0, colwidth0, colwidth0, colwidth0,
                     colwidth0, colwidth2, colwidth2, colwidth3)
        table = Table(data, colwidths, style=ts, repeatRows=1)
        # table.keepWithNext = True
        Story.append(table)
        Story.append(Spacer(1, 15))
        ''' males '''
        paragraph = Paragraph(
            "Males for breeding - %s found" % males.__len__(), styleH)
        paragraph.keepWithNext = True
        Story.append(paragraph)
        data = [[
            'name', 'cage', 'age (+-)', 'father', 'mother', 'prev. mates',
            '# days\n separated', 'reserv.', 'comment'
        ]]
        for b in males:
            bird = b['bird']
            bird_name = bird.name
            if bird_name:
                bird_name = Paragraph(bird_name * 1, style)

            reserved = bird.reserved_until
            if reserved:
                reserved = 'Yes'

            mates = b['mates']
            if mates:
                mates = Paragraph(mates * 1, style)

            comment = bird.comment
            if comment:
                comment = Paragraph(comment * 1, style)

            father = b['father']
            if father:
                father = Paragraph(father.name * 1, style)

            mother = b['mother']
            if mother:
                mother = Paragraph(mother.name * 1, style)


            data.append([bird_name, bird.cage.name, \
            str(b['age']) + ' (' + str(bird.age_uncertainty) +' )', \
            father, mother, mates, b['last_separation'], \
            reserved, comment  ])

        colwidths = (47, 35, 45, 47, 47, \
                     colwidth3, colwidth0, colwidth0, colwidth3)
        table = Table(data, colwidths, style=ts2, repeatRows=1)
        table.keepWithNext = True
        Story.append(table)
        ''' space between '''

        spacer = Spacer(1, 15)
        spacer.keepWithNext = True
        Story.append(spacer)
        ''' females '''
        paragraph = Paragraph(
            "females for breeding - %s found" % females.__len__(), styleH)
        paragraph.keepWithNext = True
        Story.append(paragraph)
        data = [[
            'name', 'cage', 'age (+-)', 'father', 'mother', 'prev. mates',
            '# days\n separated', 'reserv.', 'comment'
        ]]
        for b in females:
            bird = b['bird']
            bird_name = bird.name
            if bird_name:
                bird_name = Paragraph(bird_name * 1, style)

            reserved = bird.reserved_until
            if reserved:
                reserved = 'Yes'

            mates = b['mates']
            if mates:
                mates = Paragraph(mates * 1, style)

            comment = bird.comment
            if comment:
                comment = Paragraph(comment * 1, style)

            father = b['father']
            if father:
                father = Paragraph(father.name * 1, style)

            mother = b['mother']
            if mother:
                mother = Paragraph(mother.name * 1, style)


            data.append([bird_name, bird.cage.name, \
            bird.get_phd() + ' (' + str(bird.age_uncertainty) +' )', \
            father, mother, mates, b['last_separation'], \
            reserved, comment  ])

        colwidths = (47, 35, 45, 47, 47, \
                     colwidth3, colwidth0, colwidth0, colwidth3)
        table = Table(data, colwidths, style=ts2, repeatRows=1)
        Story.append(table)

        return pdf_close_and_return(doc, Story, buffer, response)

    except:
        return server_error(request)