source: main/waeup.sirp/branches/ulif-fasttables/src/waeup/sirp/accesscodes/browser.py @ 5263

Last change on this file since 5263 was 5155, checked in by uli, 15 years ago

Add UI components for searching access codes.

File size: 7.4 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 (
9    AdminTask, AddActionButton, SearchActionButton, LeftSidebar, BatchOpButton)
10from waeup.sirp.interfaces import IWAeUPObject, IUniversity
11
12from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch
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 archive is None and delete is None:
30            return
31        if not batches:
32            self.flash('No batch selected.')
33            return
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)
44
45class AddBatchPage(WAeUPAddFormPage):
46    grok.name('add')
47    grok.context(IAccessCodeBatchContainer)
48    grok.require('waeup.manageACBatches')
49
50    title = label = 'Create a WAeUP Scratch Card Batch'
51    pnav = 0
52
53    form_fields = grok.AutoFields(IAccessCodeBatch).select(
54        'batch_prefix', 'entry_num', 'cost')
55
56    @grok.action('Cancel')
57    def cancel(self, *args, **kw):
58        self.flash('Batch creation cancelled.')
59        self.redirect(self.url(self.context))
60
61    @grok.action('Create batch')
62    def createBatch(self, **data):
63        creator = self.request.principal.id
64        creation_date = datetime.now()
65        data.update(creation_date=creation_date, creator=creator)
66        batch = self.context.createBatch(**data)
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 ReimportBatchPage(WAeUPPage):
74    """Screen for reimporting AC batches.
75    """
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
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))
106       
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
154class BatchContainerBreadcrumb(Breadcrumb):
155    """A breadcrumb for ac batch containers.
156    """
157    grok.require('waeup.manageACBatches')
158    grok.context(IAccessCodeBatchContainer)
159    title = u'Access Code Batches'
160    parent_viewname = 'administration'
161
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
172class AdminTaskManageACBatches(AdminTask):
173    """Entry on administration page that links to batch container.
174    """
175    grok.order(5)
176    grok.require('waeup.manageACBatches')
177    grok.template('admintaskacbatches')
178
179    link_title = 'Manage access-code batches'
180    target_viewname = 'accesscodes'
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)
187    grok.require('waeup.manageACBatches')
188    text = 'Add Scratch Card Batch'
189
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
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   
207class ManageAccessCodes(grok.Viewlet):
208    """Link on front page to access code management.
209    """
210    grok.viewletmanager(LeftSidebar)
211    grok.context(IUniversity)
212    grok.view(UniversityPage)
213    grok.order(6)
214    grok.require('waeup.manageACBatches')
215
216    def render(self):
217        return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>'
Note: See TracBrowser for help on using the repository browser.