Ignore:
Timestamp:
7 Nov 2011, 13:03:42 (13 years ago)
Author:
Henrik Bettermann
Message:

Implement download page for pdf payment receipts (ExportPDFPaymentSlipPage) including student base data.

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

Legend:

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

    r7017 r7019  
    5252from waeup.sirp.students.vocabularies import StudyLevelSource
    5353from waeup.sirp.students.utils import (
    54     getPaymentDetails, getAccommodationDetails, selectBed)
     54    getPaymentDetails, getAccommodationDetails, selectBed, renderPDF)
    5555from waeup.sirp.browser.resources import toggleall
    5656from waeup.sirp.authentication import get_principal_role_manager
     
    957957    def label(self):
    958958        return '%s: Online Payment Ticket %s' % (
    959             self.context.__parent__.__parent__.fullname,self.context.p_id)
     959            self.context.getStudent().fullname,self.context.p_id)
     960
     961class PaymentReceiptActionButton(ManageActionButton):
     962    grok.context(IStudentOnlinePayment)
     963    grok.require('waeup.viewStudent')
     964    icon = 'actionicon_pdf.png'
     965    text = 'Download payment receipt'
     966    target = 'payment_receipt.pdf'
    960967
    961968class OnlinePaymentCallbackPage(grok.View):
     
    10051012        return
    10061013
     1014class ExportPDFPaymentSlipPage(grok.View):
     1015    """Deliver a PDF slip of the context.
     1016    """
     1017    grok.context(IStudentOnlinePayment)
     1018    grok.name('payment_receipt.pdf')
     1019    grok.require('waeup.viewStudent')
     1020    form_fields = grok.AutoFields(IStudentOnlinePayment)
     1021    form_fields['creation_date'].custom_widget = FriendlyDateDisplayWidget('le')
     1022    form_fields['payment_date'].custom_widget = FriendlyDateDisplayWidget('le')
     1023    prefix = 'form'
     1024
     1025    @property
     1026    def label(self):
     1027        return 'Online Payment Receipt %s' % self.context.p_id
     1028
     1029    def render(self):
     1030        studentview = StudentBaseDisplayFormPage(self.context.getStudent(),
     1031            self.request)
     1032        return renderPDF(self,'Payment', 'payment_receipt.pdf',
     1033            self.context.getStudent, studentview)
     1034
    10071035# We don't need the display form page yet
    10081036#class AccommodationDisplayFormPage(WAeUPDisplayFormPage):
  • main/waeup.sirp/trunk/src/waeup/sirp/students/tests/test_browser.py

    r7018 r7019  
    696696        self.assertMatches('...Amount Authorized...',
    697697                           self.browser.contents)
    698 
     698        payment_url = self.browser.url
    699699        # Managers can open the callback view which simulates a valid callback
    700700        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
    701         self.browser.open(self.browser.url + '/callback')
     701        self.browser.open(payment_url + '/callback')
    702702        self.assertMatches('...Valid callback received...',
    703703                          self.browser.contents)
     704
     705        # Managers can open the pdf payment receipt
     706        self.browser.open(payment_url + '/payment_receipt.pdf')
     707        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     708        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
    704709
    705710        # Managers can remove online school fee payment tickets
     
    769774        value = ctrl.options[0]
    770775        self.browser.getLink(value).click()
    771         self.browser.open(self.browser.url + '/callback')
     776        payment_url = self.browser.url
     777        self.browser.open(payment_url + '/callback')
    772778        self.assertMatches('...Valid callback received...',
    773779                          self.browser.contents)
     
    782788        ac = self.app['accesscodes']['CLR-0'][pin]
    783789        ac.owner = self.student_id
     790
     791        # Students can open the pdf payment receipt
     792        self.browser.open(payment_url + '/payment_receipt.pdf')
     793        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     794        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
     795
    784796        # The new CLR-0 pin can be used for starting clearance
    785797        IWorkflowInfo(self.student).fireTransition('admit')
  • main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py

    r7004 r7019  
    33from grok import getSite
    44from random import SystemRandom as r
     5from datetime import date, datetime
     6from reportlab.pdfgen import canvas
     7from reportlab.lib.units import cm
     8from reportlab.lib.pagesizes import A4
     9from reportlab.lib.styles import getSampleStyleSheet
     10from reportlab.platypus import (Frame, Paragraph, Image,
     11    Table, Spacer)
     12from reportlab.platypus.tables import TableStyle
     13from zope.formlib.form import setUpEditWidgets
    514from waeup.sirp.interfaces import academic_sessions_vocab
     15
     16SLIP_STYLE = TableStyle(
     17    [('VALIGN',(0,0),(-1,-1),'TOP')]
     18    )
    619
    720def generate_student_id(students,letter):
     
    8598def selectBed(available_beds):
    8699    return available_beds[0]
     100
     101def setUpWidgets(view, ignore_request=False):
     102    view.adapters = {}
     103    view.widgets = setUpEditWidgets(
     104        view.form_fields, view.prefix, view.context, view.request,
     105        adapters=view.adapters, for_display=True,
     106        ignore_request=ignore_request
     107        )
     108
     109def renderStudentData(student, studentview):
     110    setUpWidgets(studentview, ignore_request=True)
     111    data = []
     112    style = getSampleStyleSheet()
     113    for widget in studentview.widgets:
     114        if widget.name == 'form.adm_code':
     115            continue
     116        f_label = '<font size=12>%s</font>:' % widget.label.strip()
     117        f_label = Paragraph(f_label, style["Normal"])
     118        f_text = '<font size=12>%s</font>' % widget()
     119        f_text = Paragraph(f_text, style["Normal"])
     120        data.append([f_label,f_text])
     121    table = Table(data,style=SLIP_STYLE)
     122    return table
     123
     124# To be specified in customization packages, see also view which
     125# calls the function.
     126def renderPDF(view, subject='', filename='slip.pdf', student=None, studentview=None):
     127    # (0,0),(-1,-1) = whole table
     128    # (0,0),(0,-1) = first column
     129    # (-1,0),(-1,-1) = last column
     130    # (0,0),(-1,0) = first row
     131    # (0,-1),(-1,-1) = last row
     132
     133    pdf = canvas.Canvas(filename,pagesize=A4)
     134    pdf.setTitle(view.label)
     135    pdf.setSubject(subject)
     136    pdf.setAuthor('%s (%s)' % (view.request.principal.title,
     137        view.request.principal.id))
     138    pdf.setCreator('WAeUP SIRP')
     139    width, height = A4
     140    style = getSampleStyleSheet()
     141    pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm))
     142
     143    story = []
     144    frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm))
     145    header_title = getattr(getSite(), 'name', u'Sample University')
     146    story.append(Paragraph(header_title, style["Heading1"]))
     147    frame_header.addFromList(story,pdf)
     148
     149    story = []
     150    frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))
     151    story.append(Paragraph(view.label, style["Heading2"]))
     152    #story.append(HRFlowable())
     153    if student:
     154        story.append(Spacer(1, 18))
     155        studenttable = renderStudentData(student, studentview)
     156        story.append(studenttable)
     157    story.append(Spacer(1, 18))
     158    setUpWidgets(view)
     159    data = []
     160    for widget in view.widgets:
     161        f_label = '<font size=12>%s</font>:' % widget.label.strip()
     162        f_label = Paragraph(f_label, style["Normal"])
     163        f_text = '<font size=12>%s</font>' % widget()
     164        f_text = Paragraph(f_text, style["Normal"])
     165        data.append([f_label,f_text])
     166    table = Table(data,style=SLIP_STYLE)
     167    story.append(table)
     168    frame_body.addFromList(story,pdf)
     169
     170    story = []
     171    frame_footer = Frame(1*cm,0,width-(2*cm),1*cm)
     172    timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
     173    f_text = '<font size=10>%s</font>' % timestamp
     174    story.append(Paragraph(f_text, style["Normal"]))
     175    frame_footer.addFromList(story,pdf)
     176
     177    view.response.setHeader(
     178        'Content-Type', 'application/pdf')
     179    return pdf.getpdfdata()
Note: See TracChangeset for help on using the changeset viewer.