Ignore:
Timestamp:
9 Dec 2011, 12:34:30 (13 years ago)
Author:
Henrik Bettermann
Message:

It seems that font attributes can't be defined in Tables if Paragraphs are being used. The latter have to be used to render the widgets properly.

Reorganise slip pages in students section.

Sort course lists by semester and course code.

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/browser.py

    r7276 r7318  
    623623    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
    624624    prefix = 'form'
     625    title = 'Payment Data'
    625626
    626627    @property
     
    636637            self.request)
    637638        students_utils = getUtility(IStudentsUtils)
    638         return students_utils.renderPDF(self,'Payment', 'payment_receipt.pdf',
     639        return students_utils.renderPDF(self,'payment_receipt.pdf',
    639640            self.context.__parent__, applicantview)
    640641
  • main/waeup.sirp/trunk/src/waeup/sirp/students/browser.py

    r7310 r7318  
    9090    """
    9191    grok.context(IStudentsContainer)
    92     title = u'Students'
     92    title = 'Students'
    9393
    9494class StudentBreadcrumb(Breadcrumb):
     
    104104    """
    105105    grok.context(IStudentStudyCourse)
    106     title = u'Study Course'
     106    title = 'Study Course'
    107107
    108108class PaymentsBreadcrumb(Breadcrumb):
     
    110110    """
    111111    grok.context(IStudentPaymentsContainer)
    112     title = u'Payments'
     112    title = 'Payments'
    113113
    114114class OnlinePaymentBreadcrumb(Breadcrumb):
     
    125125    """
    126126    grok.context(IStudentAccommodation)
    127     title = u'Accommodation'
     127    title = 'Accommodation'
    128128
    129129    #@property
     
    536536    grok.require('waeup.viewStudent')
    537537    form_fields = grok.AutoFields(IStudentClearance).omit('clearance_locked')
     538    form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le')
    538539    prefix = 'form'
     540    title = 'Clearance Data'
    539541
    540542    @property
     
    547549        students_utils = getUtility(IStudentsUtils)
    548550        return students_utils.renderPDF(
    549             self,'Clearance', 'clearance.pdf',
     551            self, 'clearance.pdf',
    550552            self.context.getStudent(), studentview)
    551553
     
    819821    form_fields = grok.AutoFields(IStudentStudyLevel)
    820822    prefix = 'form'
     823    title = 'Level Data'
     824    content_title = 'Course List'
    821825
    822826    @property
     
    828832            self.request)
    829833        students_utils = getUtility(IStudentsUtils)
     834        tabledata = sorted(self.context.values(),
     835            key=lambda value: str(value.semester) + value.code)
    830836        return students_utils.renderPDF(
    831             self,'Course Registration', 'course_registration.pdf',
     837            self, 'course_registration.pdf',
    832838            self.context.getStudent(), studentview,
    833839            tableheader=[('Sem.','semester', 1.5),('Code','code', 2.5),
     
    838844                         ('Score', 'score', 1.5),('Auto', 'automatic', 1.5)
    839845                         ],
    840             tabledata=self.context.values())
     846            tabledata=tabledata)
    841847
    842848class StudyLevelManageActionButton(ManageActionButton):
     
    12551261    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
    12561262    prefix = 'form'
     1263    title = 'Payment Data'
    12571264
    12581265    @property
     
    12681275            self.request)
    12691276        students_utils = getUtility(IStudentsUtils)
    1270         return students_utils.renderPDF(self,'Payment', 'payment_receipt.pdf',
     1277        return students_utils.renderPDF(self, 'payment_receipt.pdf',
    12711278            self.context.getStudent(), studentview)
    12721279
     
    14791486    form_fields['booking_date'].custom_widget = FriendlyDateDisplayWidget('le')
    14801487    prefix = 'form'
     1488    title = 'Bed Allocation Data'
    14811489
    14821490    @property
    14831491    def label(self):
    1484         return 'Bed Allocation %s' % self.context.bed_coordinates
     1492        return 'Bed Allocation: %s' % self.context.bed_coordinates
    14851493
    14861494    def render(self):
     
    14891497        students_utils = getUtility(IStudentsUtils)
    14901498        return students_utils.renderPDF(
    1491             self,'Bed Allocation', 'bed_allocation.pdf',
     1499            self, 'bed_allocation.pdf',
    14921500            self.context.getStudent(), studentview)
    14931501
  • main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py

    r7310 r7318  
    2222from datetime import datetime
    2323from reportlab.pdfgen import canvas
     24from reportlab.lib import colors
    2425from reportlab.lib.units import cm
    2526from reportlab.lib.enums import TA_RIGHT
     
    3536from waeup.sirp.students.interfaces import IStudentsUtils
    3637
    37 SLIP_STYLE = TableStyle(
    38     [('VALIGN',(0,0),(-1,-1),'TOP')]
    39     )
    40 
    41 CONTENT_STYLE = TableStyle(
    42     [('VALIGN',(0,0),(-1,-1),'TOP')]
    43     )
     38SLIP_STYLE = [
     39    ('VALIGN',(0,0),(-1,-1),'TOP'),
     40    #('FONT', (0,0), (-1,-1), 'Helvetica', 11),
     41    ]
     42
     43CONTENT_STYLE = [
     44    ('VALIGN',(0,0),(-1,-1),'TOP'),
     45    #('FONT', (0,0), (-1,-1), 'Helvetica', 8),
     46    #('TEXTCOLOR',(0,0),(-1,0),colors.white),
     47    ('BACKGROUND',(0,0),(-1,0),colors.black),
     48    ]
     49
     50FONT_SIZE = 10
     51FONT_COLOR = 'black'
     52
     53def formatted_string(color=FONT_COLOR, size=FONT_SIZE):
     54    tag1 ='<font color=%s size=%d>' % (color, size)
     55    return tag1 + '%s</font>'
    4456
    4557def generate_student_id(students,letter):
     
    6880
    6981def render_student_data(studentview):
     82    """Render student table for an existing frame.
     83    """
     84    width, height = A4
    7085    set_up_widgets(studentview, ignore_request=True)
    71     data = []
     86    data_left = []
     87    data_right = []
    7288    style = getSampleStyleSheet()
    7389    img = getUtility(IExtFileStore).getFileByContext(
     
    7692        from waeup.sirp.browser import DEFAULT_PASSPORT_IMAGE_PATH
    7793        img = open(DEFAULT_PASSPORT_IMAGE_PATH, 'rb')
    78     doc_img = Image(img.name, width=4*cm, height=3*cm, kind='bound')
    79     data.append([doc_img])
    80     data.append([Spacer(1, 12)])
     94    doc_img = Image(img.name, width=4*cm, height=4*cm, kind='bound')
     95    data_left.append([doc_img])
     96    #data.append([Spacer(1, 12)])
    8197    for widget in studentview.widgets:
    8298        if widget.name == 'form.adm_code':
    8399            continue
    84         f_label = '<font size=12>%s</font>:' % (
    85             widget.label.strip())
     100        f_label = formatted_string() % widget.label.strip()
    86101        f_label = Paragraph(f_label, style["Normal"])
    87         f_text = '<font size=12>%s</font>' % widget()
     102        f_text = formatted_string() % widget()
    88103        f_text = Paragraph(f_text, style["Normal"])
    89         data.append([f_label,f_text])
    90     table = Table(data,style=SLIP_STYLE)
     104        data_right.append([f_label,f_text])
     105    table_left = Table(data_left,style=SLIP_STYLE)
     106    table_right = Table(data_right,style=SLIP_STYLE, colWidths=[4*cm, 10*cm])
     107    table = Table([[table_left, table_right],],style=SLIP_STYLE)
    91108    return table
    92109
    93 def insert_footer(pdf,width,style,text=None):
     110def render_table_data(tableheader,tabledata):
     111    """Render children table for an existing frame.
     112    """
     113    data = []
     114    #data.append([Spacer(1, 12)])
     115    line = []
     116    style = getSampleStyleSheet()
     117    for element in tableheader:
     118        field = formatted_string(color='white') % element[0]
     119        field = Paragraph(field, style["Normal"])
     120        line.append(field)
     121    data.append(line)
     122    for ticket in tabledata:
     123        line = []
     124        for element in tableheader:
     125              field = formatted_string() % str(getattr(ticket,element[1],u' '))
     126              field = Paragraph(field, style["Normal"])
     127              line.append(field)
     128        data.append(line)
     129    table = Table(data,colWidths=[
     130        element[2]*cm for element in tableheader], style=CONTENT_STYLE)
     131    return table
     132
     133
     134def insert_footer(pdf,width,style,text=None, number_of_pages=1):
     135      """Render the whole footer frame.
     136      """
    94137      story = []
    95138      frame_footer = Frame(1*cm,0,width-(2*cm),1*cm)
     
    100143      story = []
    101144      frame_footer = Frame(1*cm,0,width-(2*cm),1*cm)
    102       right_text = '<font size=10>%s Page %s</font>' % (
    103           text, pdf.getPageNumber())
     145      right_text = '<font size=10>%s Page %s of %s</font>' % (
     146          text, pdf.getPageNumber(), number_of_pages)
    104147      story.append(Paragraph(right_text, style["Right"]))
    105148      frame_footer.addFromList(story,pdf)
    106 
    107 def render_table_data(tableheader,tabledata):
    108     data = []
    109     data.append([Spacer(1, 12)])
    110     line = []
    111     style = getSampleStyleSheet()
    112     for element in tableheader:
    113         field = '<strong><font size=10>%s</font></strong>' % element[0]
    114         field = Paragraph(field, style["Normal"])
    115         line.append(field)
    116     data.append(line)
    117     for ticket in tabledata:
    118         line = []
    119         for element in tableheader:
    120               fieldcontent = getattr(ticket,element[1],u' ')
    121               field = '<font size=10>%s</font>' % fieldcontent
    122               field = Paragraph(field, style["BodyText"])
    123               line.append(field)
    124         data.append(line)
    125     # ToDo: The table does not show up if there are too many line
    126     # and it thus doesn't fit on the page
    127     table = Table(data,colWidths=[
    128         element[2]*cm for element in tableheader],style=CONTENT_STYLE)
    129     return table
    130149
    131150class StudentsUtils(grok.GlobalUtility):
     
    190209        return available_beds[0]
    191210
    192     def renderPDF(self, view, subject='', filename='slip.pdf',
     211    def renderPDF(self, view, filename='slip.pdf',
    193212        student=None, studentview=None, tableheader=None, tabledata=None):
    194213        # (0,0),(-1,-1) = whole table
     
    200219        pdf = canvas.Canvas(filename,pagesize=A4)
    201220        pdf.setTitle(view.label)
    202         pdf.setSubject(subject)
     221        pdf.setSubject(view.title)
    203222        pdf.setAuthor('%s (%s)' % (view.request.principal.title,
    204223            view.request.principal.id))
     
    218237        frame_header.addFromList(story,pdf)
    219238
     239        # Insert student data table
    220240        story = []
    221241        frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))
    222242        story.append(Paragraph(view.label, style["Heading2"]))
    223 
    224         # Insert student data table
    225243        if student is not None:
    226             story.append(Spacer(1, 12))
     244            #story.append(Spacer(1, 12))
     245            story.append(Paragraph('Student Base Data', style["Heading3"]))
    227246            studenttable = render_student_data(studentview)
    228247            story.append(studenttable)
    229248
    230         story.append(Spacer(1, 12))
    231         #story.append(PageBreak())
     249        # Insert widgets
     250        story.append(Paragraph(view.title, style["Heading3"]))
    232251        set_up_widgets(view)
    233252        data = []
    234253        for widget in view.widgets:
    235             f_label = '<font size=12>%s</font>:' % widget.label.strip()
     254            f_label = formatted_string() % widget.label.strip()
    236255            f_label = Paragraph(f_label, style["Normal"])
    237             f_text = '<font size=12>%s</font>' % widget()
     256            f_text = formatted_string() % str(widget())
    238257            f_text = Paragraph(f_text, style["Normal"])
    239258            data.append([f_label,f_text])
    240         # First, import browser components locally
     259        table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm])
     260        story.append(table)
     261
     262        # Import browser components locally
    241263        # to avoid circular import
    242264        from waeup.sirp.students.viewlets import FileManager
    243265        from waeup.sirp.browser import DEFAULT_IMAGE_PATH
     266        data = []
     267        # Collect viewlets
    244268        fm = FileManager(view.context, view.request, view)
    245         # Collect viewlets
    246269        fm.update()
    247         # Insert list of scanned documents
    248         for viewlet in fm.viewlets:
    249             f_label = '<font size=12>%s</font>' % viewlet.label
    250             f_label = Paragraph(f_label, style["Normal"])
    251             if viewlet.template.__grok_name__ == 'imagedisplay':
    252                 # In case we define FileDisplay viewlets with
    253                 # an imagedisplay template in the customization package
    254                 img = getUtility(IExtFileStore).getFileByContext(
    255                     view.context, attr=viewlet.download_name)
    256                 if img is None:
    257                     img = open(DEFAULT_IMAGE_PATH, 'rb')
    258                 doc_img = Image(img.name, width=2*cm, height=1*cm, kind='bound')
    259                 data.append([f_label,doc_img])
    260             else:
    261                 f_text = '<font size=12>%s</font>' % viewlet.title
    262                 f_text = Paragraph(f_text, style["Normal"])
    263                 data.append([f_label,f_text])
    264         table = Table(data,style=SLIP_STYLE)
    265         story.append(table)
     270        if fm.viewlets:
     271            story.append(Paragraph('Scanned Documents', style["Heading3"]))
     272            # Insert list of scanned documents
     273            for viewlet in fm.viewlets:
     274                f_label = formatted_string() % viewlet.label
     275                f_label = Paragraph(f_label, style["Normal"])
     276                if viewlet.template.__grok_name__ == 'imagedisplay':
     277                    # In case we define FileDisplay viewlets with
     278                    # an imagedisplay template in the customization package
     279                    img = getUtility(IExtFileStore).getFileByContext(
     280                        view.context, attr=viewlet.download_name)
     281                    if img is None:
     282                        img = open(DEFAULT_IMAGE_PATH, 'rb')
     283                    doc_img = Image(img.name, width=2*cm, height=1*cm, kind='bound')
     284                    data.append([f_label,doc_img])
     285                else:
     286                    f_text = formatted_string() % viewlet.title
     287                    f_text = Paragraph(f_text, style["Normal"])
     288                    data.append([f_label,f_text])
     289            table = Table(data,style=SLIP_STYLE, colWidths=[5*cm, 13.5*cm])
     290            story.append(table)
     291
    266292        try:
    267293            frame_body.addFromList(story,pdf)
     
    269295            view.flash('Error in image file.')
    270296            return view.redirect(view.url(view.context))
    271         insert_footer(pdf,width,style,footer_text)
    272         pdf.showPage()
    273         frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(1.5*cm))
    274         # Insert content table
     297
    275298        if tabledata and tableheader:
     299            insert_footer(pdf,width,style,footer_text,number_of_pages=2)
     300        else:
     301            insert_footer(pdf,width,style,footer_text)
     302
     303        # Insert content table on second page
     304        if tabledata and tableheader:
     305            pdf.showPage()
     306            frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(2*cm))
    276307            story = []
     308            story.append(Paragraph(view.content_title, style["Heading3"]))
    277309            #story.append(Spacer(1, 12))
    278310            contenttable = render_table_data(tableheader,tabledata)
    279311            story.append(contenttable)
    280312            frame_body.addFromList(story,pdf)
    281         insert_footer(pdf,width,style,footer_text)
     313            insert_footer(pdf,width,style,footer_text,number_of_pages=2)
     314
    282315        view.response.setHeader(
    283316            'Content-Type', 'application/pdf')
Note: See TracChangeset for help on using the changeset viewer.