source: main/waeup.sirp/trunk/src/waeup/sirp/students/utils.py @ 7033

Last change on this file since 7033 was 7021, checked in by Henrik Bettermann, 13 years ago

Adjust customisation and base package.

  • Property svn:keywords set to Id
File size: 6.3 KB
Line 
1"""General helper functions for the student section.
2"""
3from grok import getSite
4from 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
14from waeup.sirp.interfaces import academic_sessions_vocab
15
16SLIP_STYLE = TableStyle(
17    [('VALIGN',(0,0),(-1,-1),'TOP')]
18    )
19
20def generate_student_id(students,letter):
21    if letter == '?':
22        letter= r().choice('ABCDEFGHKLMNPQRSTUVWXY')
23    sid = u"%c%d" % (letter,r().randint(99999,1000000))
24    while sid in students.keys():
25        sid = u"%c%d" % (letter,r().randint(99999,1000000))
26    return sid
27
28def set_returning_data(student):
29    student['studycourse'].current_level += 100
30    student['studycourse'].current_session += 1
31    verdict = student['studycourse'].current_verdict
32    student['studycourse'].current_verdict = '0'
33    student['studycourse'].previous_verdict = verdict
34    return
35
36# To be specified in customization packages, see also the view which
37# calls the function.
38def getPaymentDetails(category, student):
39    d = {}
40    d['p_item'] = u''
41    d['amount'] = 0
42    d['error'] = u''
43    d['p_session'] = student['studycourse'].current_session
44    session = str(d['p_session'])
45    try:
46        academic_session = getSite()['configuration'][session]
47    except KeyError:
48        d['error'] = u'Session configuration object is not available.'
49        return d
50    d['surcharge_1'] = academic_session.surcharge_1
51    d['surcharge_2'] = academic_session.surcharge_2
52    d['surcharge_3'] = academic_session.surcharge_3
53    if category == 'schoolfee':
54        d['amount'] = academic_session.school_fee_base
55        d['p_item'] = student['studycourse'].certificate.code
56    elif category == 'clearance':
57        d['p_item'] = student['studycourse'].certificate.code
58        d['amount'] = academic_session.clearance_fee
59    elif category == 'bed_allocation':
60        d['p_item'] = getAccommodationDetails(student)['bt']
61        d['amount'] = academic_session.booking_fee
62    return d
63
64# To be specified in customization packages, see also the view which
65# calls the function.
66def getAccommodationDetails(student):
67    d = {}
68    d['error'] = u''
69    site_confoguration = getSite()['configuration']
70    d['booking_session'] = site_confoguration.accommodation_session
71    d['allowed_states'] = site_confoguration.accommodation_states
72    session = str(d['booking_session'])
73    # Determine bed type
74    studycourse = student['studycourse']
75    entry_session = studycourse.entry_session
76    current_level = studycourse.current_level
77    end_level = studycourse.certificate.end_level
78    if entry_session == getSite()['configuration'].accommodation_session:
79        bt = 'fr'
80    elif current_level >= end_level:
81        bt = 'fi'
82    else:
83        bt = 're'
84    if student.sex == 'f':
85        sex = 'female'
86    else:
87        sex = 'male'
88    special_handling = 'regular'
89    d['bt'] = u'%s_%s_%s' % (special_handling,sex,bt)
90    return d
91
92# To be specified in customization packages, see also view which
93# calls the function.
94# I the standard configuration we select the first bed found,
95# but can also randomize the selection if we like.
96def selectBed(available_beds):
97    return available_beds[0]
98
99def setUpWidgets(view, ignore_request=False):
100    view.adapters = {}
101    view.widgets = setUpEditWidgets(
102        view.form_fields, view.prefix, view.context, view.request,
103        adapters=view.adapters, for_display=True,
104        ignore_request=ignore_request
105        )
106
107def renderStudentData(student, studentview):
108    setUpWidgets(studentview, ignore_request=True)
109    data = []
110    style = getSampleStyleSheet()
111    for widget in studentview.widgets:
112        if widget.name == 'form.adm_code':
113            continue
114        f_label = '<font size=12>%s</font>:' % widget.label.strip()
115        f_label = Paragraph(f_label, style["Normal"])
116        f_text = '<font size=12>%s</font>' % widget()
117        f_text = Paragraph(f_text, style["Normal"])
118        data.append([f_label,f_text])
119    table = Table(data,style=SLIP_STYLE)
120    return table
121
122# To be specified in customization packages, see also view which
123# calls the function.
124def renderPDF(view, subject='', filename='slip.pdf', student=None, studentview=None):
125    # (0,0),(-1,-1) = whole table
126    # (0,0),(0,-1) = first column
127    # (-1,0),(-1,-1) = last column
128    # (0,0),(-1,0) = first row
129    # (0,-1),(-1,-1) = last row
130
131    pdf = canvas.Canvas(filename,pagesize=A4)
132    pdf.setTitle(view.label)
133    pdf.setSubject(subject)
134    pdf.setAuthor('%s (%s)' % (view.request.principal.title,
135        view.request.principal.id))
136    pdf.setCreator('WAeUP SIRP')
137    width, height = A4
138    style = getSampleStyleSheet()
139    pdf.line(1*cm,height-(1.8*cm),width-(1*cm),height-(1.8*cm))
140
141    story = []
142    frame_header = Frame(1*cm,1*cm,width-(1.7*cm),height-(1.7*cm))
143    header_title = getattr(getSite(), 'name', u'Sample University')
144    story.append(Paragraph(header_title, style["Heading1"]))
145    frame_header.addFromList(story,pdf)
146
147    story = []
148    frame_body = Frame(1*cm,1*cm,width-(2*cm),height-(3.5*cm))
149    story.append(Paragraph(view.label, style["Heading2"]))
150    #story.append(HRFlowable())
151    if student:
152        story.append(Spacer(1, 18))
153        studenttable = renderStudentData(student, studentview)
154        story.append(studenttable)
155    story.append(Spacer(1, 18))
156    setUpWidgets(view)
157    data = []
158    for widget in view.widgets:
159        f_label = '<font size=12>%s</font>:' % widget.label.strip()
160        f_label = Paragraph(f_label, style["Normal"])
161        f_text = '<font size=12>%s</font>' % widget()
162        f_text = Paragraph(f_text, style["Normal"])
163        data.append([f_label,f_text])
164    table = Table(data,style=SLIP_STYLE)
165    story.append(table)
166    frame_body.addFromList(story,pdf)
167
168    story = []
169    frame_footer = Frame(1*cm,0,width-(2*cm),1*cm)
170    timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
171    f_text = '<font size=10>%s</font>' % timestamp
172    story.append(Paragraph(f_text, style["Normal"]))
173    frame_footer.addFromList(story,pdf)
174
175    view.response.setHeader(
176        'Content-Type', 'application/pdf')
177    return pdf.getpdfdata()
Note: See TracBrowser for help on using the repository browser.