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

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