source: main/waeup.sirp/trunk/src/waeup/sirp/students/viewlets.py @ 7104

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

File name choosers generally don't know the extension of the file stored. So we have to feed the chooseName method with the extension (ext) of the file stored in the file system.

This may conflict somehow with the idea behind the file name chooser. I think you (Uli) intended to use only the attr parameter to select a proper extension. In my opinion it's better to use attr and ext.

  • Property svn:keywords set to Id
File size: 8.4 KB
Line 
1import grok
2from zope.component import getUtility
3from zope.interface import Interface
4from waeup.sirp.interfaces import (
5    IWAeUPObject, IExtFileStore, IFileStoreNameChooser)
6from waeup.sirp.utils.helpers import string_from_bytes, file_size
7from waeup.sirp.browser import DEFAULT_IMAGE_PATH
8from waeup.sirp.students.browser import (
9    StudentClearanceDisplayFormPage, StudentClearanceManageFormPage)
10from waeup.sirp.students.interfaces import IStudentClearance
11
12grok.context(IWAeUPObject) # Make IWAeUPObject the default context
13grok.templatedir('browser_templates')
14
15class StudentManageSidebar(grok.ViewletManager):
16    grok.name('left_studentmanage')
17
18class StudentManageLink(grok.Viewlet):
19    """A link displayed in the student box which shows up for StudentNavigation
20    objects.
21
22    """
23    grok.baseclass()
24    grok.viewletmanager(StudentManageSidebar)
25    grok.context(IWAeUPObject)
26    grok.view(Interface)
27    grok.order(5)
28    grok.require('waeup.viewStudent')
29
30    link = 'index'
31    text = u'Base Data'
32
33    def render(self):
34        url = self.view.url(self.context.getStudent(), self.link)
35        return u'<div class="portlet"><a href="%s">%s</a></div>' % (
36                url, self.text)
37
38class StudentManageBaseLink(StudentManageLink):
39    grok.order(1)
40    link = 'index'
41    text = u'Base Data'
42
43class StudentManageClearanceLink(StudentManageLink):
44    grok.order(2)
45    link = 'view_clearance'
46    text = u'Clearance Data'
47
48class StudentManagePersonalLink(StudentManageLink):
49    grok.order(2)
50    link = 'view_personal'
51    text = u'Personal Data'
52
53class StudentManageStudyCourseLink(StudentManageLink):
54    grok.order(3)
55    link = 'studycourse'
56    text = u'Study Course'
57
58class StudentManagePaymentsLink(StudentManageLink):
59    grok.order(4)
60    link = 'payments'
61    text = u'Payments'
62
63class StudentManageAccommodationLink(StudentManageLink):
64    grok.order(5)
65    link = 'accommodation'
66    text = u'Accommodation Data'
67
68class StudentManageHistoryLink(StudentManageLink):
69    grok.order(6)
70    link = 'history'
71    text = u'History'
72
73
74class StudentMenu(grok.ViewletManager):
75    grok.name('top_student')
76
77class StudentLink(grok.Viewlet):
78    """A link displayed in the student box which shows up for StudentNavigation
79    objects.
80
81    """
82    grok.baseclass()
83    grok.viewletmanager(StudentMenu)
84    grok.context(IWAeUPObject)
85    grok.view(Interface)
86    grok.order(5)
87    grok.require('waeup.viewStudent')
88    template = grok.PageTemplateFile('browser_templates/plainactionbutton.pt')
89
90    link = 'index'
91    text = u'Base Data'
92
93    @property
94    def alt(self):
95        """Alternative text for icon.
96        """
97        return self.text
98
99    @property
100    def target_url(self):
101        """Get a URL to the target...
102        """
103        return self.view.url(self.context.getStudent(), self.link)
104
105class StudentBaseLink(StudentLink):
106    grok.order(1)
107    link = 'index'
108    text = u'Base Data'
109
110class StudentClearanceLink(StudentLink):
111    grok.order(2)
112    link = 'view_clearance'
113    text = u'Clearance Data'
114
115class StudentPersonalLink(StudentLink):
116    grok.order(2)
117    link = 'view_personal'
118    text = u'Personal Data'
119
120class StudentStudyCourseLink(StudentLink):
121    grok.order(3)
122    link = 'studycourse'
123    text = u'Study Course'
124
125class StudentPaymentsLink(StudentLink):
126    grok.order(4)
127    link = 'payments'
128    text = u'Payments'
129
130class StudentAccommodationLink(StudentLink):
131    grok.order(5)
132    link = 'accommodation'
133    text = u'Accommodation'
134
135class StudentHistoryLink(StudentLink):
136    grok.order(6)
137    link = 'history'
138    text = u'History'
139
140class PrimaryStudentNavManager(grok.ViewletManager):
141    """Viewlet manager for the primary navigation tab.
142    """
143    grok.name('primary_nav_student')
144
145class PrimaryStudentNavTab(grok.Viewlet):
146    """Base for primary student nav tabs.
147    """
148    grok.baseclass()
149    grok.viewletmanager(PrimaryStudentNavManager)
150    grok.template('primarynavtab')
151    grok.order(1)
152    grok.require('waeup.View')
153    pnav = 0
154    tab_title = u'Some Text'
155
156    @property
157    def link_target(self):
158        return self.view.application_url()
159
160    @property
161    def active(self):
162        view_pnav = getattr(self.view, 'pnav', 0)
163        if view_pnav == self.pnav:
164            return 'active'
165        return ''
166
167class HomeTab(PrimaryStudentNavTab):
168    """Home-tab in primary navigation.
169    """
170    grok.order(1)
171    grok.require('waeup.Public')
172    pnav = 0
173    tab_title = u'Home'
174
175class ProspectusTab(PrimaryStudentNavTab):
176    """Faculties-tab in primary navigation.
177    """
178    grok.order(2)
179    grok.require('waeup.View')
180    pnav = 1
181    tab_title = u'Prospectus'
182
183    @property
184    def link_target(self):
185        return self.view.application_url('faculties')
186
187class MyDataTab(PrimaryStudentNavTab):
188    """MyData-tab in primary navigation.
189    """
190    grok.order(3)
191    grok.require('waeup.Public')
192    pnav = 4
193    tab_title = u'My Data'
194
195    @property
196    def link_target(self):
197        rel_link = '/students/%s' % self.request.principal.id
198        return self.view.application_url() + rel_link
199
200def handle_file_upload(upload, context, view, max_size, attr=None):
201    """Handle upload of applicant image.
202
203    Returns `True` in case of success or `False`.
204
205    Please note that file pointer passed in (`upload`) most probably
206    points to end of file when leaving this function.
207    """
208    size = file_size(upload)
209    if size > max_size:
210        view.flash('Uploaded image is too big!')
211        return False
212    upload.seek(0) # file pointer moved when determining size
213    store = getUtility(IExtFileStore)
214    file_id = IFileStoreNameChooser(
215        context).chooseName(attr=attr, name=upload.filename)
216    store.createFile(file_id, upload)
217    return True
218
219class FileManager(grok.ViewletManager):
220    """Viewlet manager for uploading files, preferably scanned images.
221    """
222    grok.name('files')
223
224class FileDisplay(grok.Viewlet):
225    """Base file display viewlet.
226    """
227    grok.baseclass()
228    grok.context(IStudentClearance)
229    grok.viewletmanager(FileManager)
230    grok.view(StudentClearanceDisplayFormPage)
231    grok.template('filedisplay')
232    grok.order(1)
233    grok.require('waeup.viewStudent')
234    label = u'Some Text:'
235    img_src = u'filename.jpg'
236    attr = None
237
238class FileUpload(FileDisplay):
239    """Base upload viewlet.
240    """
241    grok.baseclass()
242    grok.context(IStudentClearance)
243    grok.viewletmanager(FileManager)
244    grok.view(StudentClearanceManageFormPage)
245    grok.template('fileupload')
246    grok.require('waeup.manageStudents')
247    mus = 1024 * 150
248    input_name = u'form.filename'
249
250    def update(self):
251        self.max_upload_size = string_from_bytes(self.mus)
252        upload = self.request.form.get(self.input_name, None)
253        if upload:
254            # We got a fresh upload
255            file_changed = handle_file_upload(
256                upload, self.context, self.view, self.mus, self.attr)
257            if file_changed is False:  # False is not None!
258                self.view.redirect(self.view.url(self.context))
259                return # error during image upload. Ignore other values
260            else:
261                self.view.files_changed += self.img_src
262        return
263
264class BirthCertificateDisplay(FileDisplay):
265    """Birth Certificate upload viewlet.
266    """
267    grok.order(1)
268    label = u'Birth Certificate:'
269    img_src = u'birth_certificate.jpg'
270    attr = u'birth_certificate'
271
272class BirthCertificateUpload(FileUpload):
273    """Birth Certificate upload viewlet.
274    """
275    grok.order(1)
276    label = u'Birth Certificate:'
277    mus = 1024 * 150
278    img_src = u'birth_certificate.jpg'
279    input_name = u'form.birth_certificate'
280    attr = u'birth_certificate'
281
282class Image(grok.View):
283    """Renders image for students.
284    """
285    grok.name('none.jpg')
286    grok.view(StudentClearanceManageFormPage)
287    grok.require('waeup.viewStudent')
288    grok.baseclass()
289    attr = None
290    ext = u'.jpg'
291
292    def render(self):
293        # A filename chooser turns a context into a filename suitable
294        # for file storage.
295        image = getUtility(IExtFileStore).getFileByContext(
296            self.context, attr=self.attr, ext=self.ext)
297        self.response.setHeader(
298            'Content-Type', 'image/jpeg')
299        if image is None:
300            # show placeholder image
301            return open(DEFAULT_IMAGE_PATH, 'rb').read()
302        return image
303
304class BirthCertificateImage(Image):
305    """Renders birth certificate.
306    """
307    grok.name('birth_certificate.jpg')
308    attr = u'birth_certificate'
309    ext = u'.jpg'
Note: See TracBrowser for help on using the repository browser.