source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/browser.py @ 5127

Last change on this file since 5127 was 5126, checked in by uli, 15 years ago
File size: 3.5 KB
RevLine 
[5082]1"""UI components for accesscodes.
2"""
3import grok
[5088]4from datetime import datetime
[5084]5from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage
[5082]6from waeup.sirp.browser.breadcrumbs import Breadcrumb
[5104]7from waeup.sirp.browser.pages import UniversityPage
8from waeup.sirp.browser.viewlets import AdminTask, AddActionButton, LeftSidebar
9from waeup.sirp.interfaces import IWAeUPObject, IUniversity
[5082]10
[5088]11from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch
12from waeup.sirp.accesscodes.interfaces import (
13    IAccessCodeBatchContainer, IAccessCodeBatch,
14    )
[5082]15
16grok.context(IWAeUPObject)
17
18class BatchContainerPage(WAeUPPage):
19    grok.name('index')
20    grok.context(IAccessCodeBatchContainer)
21    grok.template('batchcontainer')
[5104]22    grok.require('waeup.manageACBatches')
[5082]23
24    title = 'Access Code Batches'
25    pnav = 0
26
[5117]27    def update(self, batches=None, archive=None, delete=None):
28        if archive is None and delete is None:
29            return
[5124]30        if not batches:
31            self.flash('No batch selected.')
32            return
[5117]33        if isinstance(batches, basestring):
34            batches = [batches]
35        for name in batches:
36            batch = self.context[name]
37            csv_file = batch.archive()
38            self.flash('Archived %s (%s)' % (name, csv_file))
39            if delete is None:
40                continue
41            del self.context[name]
42            self.flash('Deleted batch %s' % name)
[5104]43
[5084]44class AddBatchPage(WAeUPAddFormPage):
45    grok.name('add')
46    grok.context(IAccessCodeBatchContainer)
[5104]47    grok.require('waeup.manageACBatches')
[5084]48
49    title = label = 'Create a WAeUP Scratch Card Batch'
50    pnav = 0
51
[5088]52    form_fields = grok.AutoFields(IAccessCodeBatch).select(
53        'batch_prefix', 'entry_num', 'cost')
54
55    @grok.action('Cancel')
[5104]56    def cancel(self, *args, **kw):
[5088]57        self.flash('Batch creation cancelled.')
58        self.redirect(self.url(self.context))
59
[5084]60    @grok.action('Create batch')
61    def createBatch(self, **data):
[5088]62        creator = self.request.principal.id
63        creation_date = datetime.now()
64        data.update(creation_date=creation_date, creator=creator)
[5126]65        batch = self.context.createBatch(**data)
[5113]66        csv_file = batch.createCSVLogFile()
[5088]67        self.context._p_changed = True
68        self.flash('Batch created (%s entries)' % data['entry_num'])
[5113]69        self.flash('Data written to %s' % csv_file)
[5084]70        self.redirect(self.url(self.context))
71
[5082]72class BatchContainerBreadcrumb(Breadcrumb):
73    """A breadcrumb for ac batch containers.
[5104]74    """
75    grok.require('waeup.manageACBatches')
[5082]76    grok.context(IAccessCodeBatchContainer)
77    title = u'Access Code Batches'
78    parent_viewname = 'administration'
79
80class AdminTaskManageACBatches(AdminTask):
81    """Entry on administration page that links to batch container.
82    """
83    grok.order(5)
[5104]84    grok.require('waeup.manageACBatches')
[5082]85    grok.template('admintaskacbatches')
86
87    link_title = 'Manage access-code batches'
88    target_viewname = 'accesscodes'
[5084]89
90class CreateBatchButton(AddActionButton):
91    """Action button on batch container page which links to batch creation.
92    """
93    grok.context(IAccessCodeBatchContainer)
94    grok.view(BatchContainerPage)
[5104]95    grok.require('waeup.manageACBatches')
[5084]96    text = 'Add Scratch Card Batch'
[5104]97
98class ManageAccessCodes(grok.Viewlet):
99    """Link on front page to access code management.
100    """
101    grok.viewletmanager(LeftSidebar)
102    grok.context(IUniversity)
103    grok.view(UniversityPage)
104    grok.order(6)
105    grok.require('waeup.manageACBatches')
106
107    def render(self):
108        return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>'
Note: See TracBrowser for help on using the repository browser.