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

Last change on this file since 6157 was 5918, checked in by Henrik Bettermann, 14 years ago

Change some titles.

File size: 7.3 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 (
[5381]9    AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink)
[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
[5918]50    title = 'Create Scratch Card Batch'
[5084]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       
[5155]107class BatchContainerSearchPage(WAeUPPage):
108    grok.name('search')
109    grok.context(IAccessCodeBatchContainer)
110    grok.template('searchpage')
111    grok.require('waeup.manageACBatches')
112
113    title = 'Search Scratch Cards'
114    pnav = 0
115
116    def update(self, search=None, searchterm=None, searchtype=None,
117               entries=None, disable=None, enable=None):
118        self.searchresults = None
119        if search is not None:
120            searchresults = self.context.search(searchterm, searchtype)
121            if len(searchresults) == 0:
122                return
123            self.searchresults = []
124            for result in searchresults:
125                status = u'unused'
126                if result.disabled is True:
127                    status = u'disabled by %s on %s' % (
128                        result.student_id,
129                        result.invalidation_date.strftime('%c')
130                        )
131                elif result.invalidation_date is not None:
132                    status = u'invalidated by %s on %s' % (
133                        result.student_id,
134                        result.invalidation_date.strftime('%c')
135                        )
136                entry = dict(
137                    serial = result.batch_serial,
138                    code = result.representation,
139                    status = status)
140                self.searchresults.append(entry)
141        if entries is None:
142            return
143        if isinstance(entries, basestring):
144            entries = [entries]
145        for entry in entries:
146            if disable is not None:
147                self.context.disable(entry, self.request.principal.id)
148                self.flash('disabled %s' % entry)
149            elif enable is not None:
150                self.context.enable(entry)
151                self.flash('(re-)enabled %s' % entry)
152        return
153
[5082]154class BatchContainerBreadcrumb(Breadcrumb):
155    """A breadcrumb for ac batch containers.
[5104]156    """
157    grok.require('waeup.manageACBatches')
[5082]158    grok.context(IAccessCodeBatchContainer)
159    title = u'Access Code Batches'
160    parent_viewname = 'administration'
161
[5155]162class BatchContainerSearchBreadcrumb(Breadcrumb):
163    """A breadcrumb for ac batch containers search page.
164    """
165    grok.require('waeup.manageACBatches')
166    grok.context(IAccessCodeBatchContainer)
167    grok.name('search')
168    title = u'Search Scratch Cards'
169    viewname = 'search'
170    parent_viewname = 'index'
171
[5082]172class AdminTaskManageACBatches(AdminTask):
173    """Entry on administration page that links to batch container.
174    """
175    grok.order(5)
[5104]176    grok.require('waeup.manageACBatches')
[5082]177    grok.template('admintaskacbatches')
178
[5412]179    link_title = 'Access Code Batches'
[5082]180    target_viewname = 'accesscodes'
[5084]181
182class CreateBatchButton(AddActionButton):
183    """Action button on batch container page which links to batch creation.
184    """
185    grok.context(IAccessCodeBatchContainer)
186    grok.view(BatchContainerPage)
[5104]187    grok.require('waeup.manageACBatches')
[5084]188    text = 'Add Scratch Card Batch'
[5104]189
[5129]190class ReimportBatchButton(BatchOpButton):
191    """Action button on batch container page which links to batch reimport.
192    """
193    grok.context(IAccessCodeBatchContainer)
194    grok.view(BatchContainerPage)
195    grok.require('waeup.manageACBatches')
196    target = 'reimport'
197    text = 'Reimport Scratch Card Batch'
198
[5155]199class SearchAccessCodeButton(SearchActionButton):
200    """Action button on batch container page which links to search.
201    """
202    grok.context(IAccessCodeBatchContainer)
203    grok.view(BatchContainerPage)
204    grok.require('waeup.manageACBatches')
205    text = 'Search Scratch Cards'   
206   
[5381]207class ManageAccessCodes(ManageLink):
208    """Link in upper left box to access code management.
[5104]209    """
210    grok.order(6)
211    grok.require('waeup.manageACBatches')
212
[5381]213    link = u'accesscodes'
[5412]214    text = u'Access Codes'
Note: See TracBrowser for help on using the repository browser.