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

Last change on this file since 5139 was 5132, checked in by uli, 15 years ago

Support reimport of AC batches.

File size: 5.0 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
[5129]8from waeup.sirp.browser.viewlets import (
9    AdminTask, AddActionButton, LeftSidebar, BatchOpButton)
[5104]10from waeup.sirp.interfaces import IWAeUPObject, IUniversity
[5082]11
[5088]12from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch
13from waeup.sirp.accesscodes.interfaces import (
14    IAccessCodeBatchContainer, IAccessCodeBatch,
15    )
[5082]16
17grok.context(IWAeUPObject)
18
19class BatchContainerPage(WAeUPPage):
20    grok.name('index')
21    grok.context(IAccessCodeBatchContainer)
22    grok.template('batchcontainer')
[5104]23    grok.require('waeup.manageACBatches')
[5082]24
25    title = 'Access Code Batches'
26    pnav = 0
27
[5117]28    def update(self, batches=None, archive=None, delete=None):
29        if archive is None and delete is None:
30            return
[5124]31        if not batches:
32            self.flash('No batch selected.')
33            return
[5117]34        if isinstance(batches, basestring):
35            batches = [batches]
36        for name in batches:
37            batch = self.context[name]
38            csv_file = batch.archive()
39            self.flash('Archived %s (%s)' % (name, csv_file))
40            if delete is None:
41                continue
42            del self.context[name]
43            self.flash('Deleted batch %s' % name)
[5104]44
[5084]45class AddBatchPage(WAeUPAddFormPage):
46    grok.name('add')
47    grok.context(IAccessCodeBatchContainer)
[5104]48    grok.require('waeup.manageACBatches')
[5084]49
50    title = label = 'Create a WAeUP Scratch Card Batch'
51    pnav = 0
52
[5088]53    form_fields = grok.AutoFields(IAccessCodeBatch).select(
54        'batch_prefix', 'entry_num', 'cost')
55
56    @grok.action('Cancel')
[5104]57    def cancel(self, *args, **kw):
[5088]58        self.flash('Batch creation cancelled.')
59        self.redirect(self.url(self.context))
60
[5084]61    @grok.action('Create batch')
62    def createBatch(self, **data):
[5088]63        creator = self.request.principal.id
64        creation_date = datetime.now()
65        data.update(creation_date=creation_date, creator=creator)
[5126]66        batch = self.context.createBatch(**data)
[5113]67        csv_file = batch.createCSVLogFile()
[5088]68        self.context._p_changed = True
69        self.flash('Batch created (%s entries)' % data['entry_num'])
[5113]70        self.flash('Data written to %s' % csv_file)
[5084]71        self.redirect(self.url(self.context))
72
[5130]73class ReimportBatchPage(WAeUPPage):
[5132]74    """Screen for reimporting AC batches.
75    """
[5130]76    grok.name('reimport')
77    grok.context(IAccessCodeBatchContainer)
78    grok.template('reimportbatchpage')
79    grok.require('waeup.manageACBatches')
80
81    title = 'Reimport Access Code Batches'
82    pnav = 0
83
[5132]84    def update(self, filenames=None, reimport=None, cancel=None):
85        if cancel is not None:
86            self.flash('Reimport cancelled.')
87            self.redirect(self.url(self.context))
88            return
89        if reimport is None:
90            return
91        if not filenames:
92            self.flash('No file chosen. Action cancelled.')
93            self.redirect(self.url(self.context))
94            return
95        if isinstance(filenames, basestring):
96            filenames = [filenames]
97        userid = self.request.principal.id
98        for filename in filenames:
99            try:
100                self.context.reimport(filename, userid)
101            except KeyError:
102                self.flash('This batch already exists: %s' % filename)
103                continue
104            self.flash('Successfully reimported: %s' % filename)
105        self.redirect(self.url(self.context))
[5130]106       
[5132]107   
[5082]108class BatchContainerBreadcrumb(Breadcrumb):
109    """A breadcrumb for ac batch containers.
[5104]110    """
111    grok.require('waeup.manageACBatches')
[5082]112    grok.context(IAccessCodeBatchContainer)
113    title = u'Access Code Batches'
114    parent_viewname = 'administration'
115
116class AdminTaskManageACBatches(AdminTask):
117    """Entry on administration page that links to batch container.
118    """
119    grok.order(5)
[5104]120    grok.require('waeup.manageACBatches')
[5082]121    grok.template('admintaskacbatches')
122
123    link_title = 'Manage access-code batches'
124    target_viewname = 'accesscodes'
[5084]125
126class CreateBatchButton(AddActionButton):
127    """Action button on batch container page which links to batch creation.
128    """
129    grok.context(IAccessCodeBatchContainer)
130    grok.view(BatchContainerPage)
[5104]131    grok.require('waeup.manageACBatches')
[5084]132    text = 'Add Scratch Card Batch'
[5104]133
[5129]134class ReimportBatchButton(BatchOpButton):
135    """Action button on batch container page which links to batch reimport.
136    """
137    grok.context(IAccessCodeBatchContainer)
138    grok.view(BatchContainerPage)
139    grok.require('waeup.manageACBatches')
140    target = 'reimport'
141    text = 'Reimport Scratch Card Batch'
142
[5104]143class ManageAccessCodes(grok.Viewlet):
144    """Link on front page to access code management.
145    """
146    grok.viewletmanager(LeftSidebar)
147    grok.context(IUniversity)
148    grok.view(UniversityPage)
149    grok.order(6)
150    grok.require('waeup.manageACBatches')
151
152    def render(self):
153        return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>'
Note: See TracBrowser for help on using the repository browser.