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

Last change on this file since 6455 was 6455, checked in by Henrik Bettermann, 13 years ago

Use jquery datatable for search results.

File size: 7.2 KB
Line 
1"""UI components for accesscodes.
2"""
3import grok
4from datetime import datetime
5from hurry.workflow.interfaces import InvalidTransitionError
6from waeup.sirp.browser.resources import datatable
7from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage
8from waeup.sirp.browser.breadcrumbs import Breadcrumb
9from waeup.sirp.browser.viewlets import (
10    AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink)
11from waeup.sirp.interfaces import IWAeUPObject
12
13from waeup.sirp.accesscodes.interfaces import (
14    IAccessCodeBatchContainer, IAccessCodeBatch,
15    )
16from waeup.sirp.accesscodes.catalog import search
17
18grok.context(IWAeUPObject)
19
20class BatchContainerPage(WAeUPPage):
21    grok.name('index')
22    grok.context(IAccessCodeBatchContainer)
23    grok.template('batchcontainer')
24    grok.require('waeup.manageACBatches')
25
26    title = 'Access Code Batches'
27    pnav = 0
28
29    def update(self, batches=None, archive=None, delete=None):
30        if archive is None and delete is None:
31            return
32        if not batches:
33            self.flash('No batch selected.')
34            return
35        if isinstance(batches, basestring):
36            batches = [batches]
37        for name in batches:
38            batch = self.context[name]
39            csv_file = batch.archive()
40            self.flash('Archived %s (%s)' % (name, csv_file))
41            if delete is None:
42                continue
43            del self.context[name]
44            self.flash('Deleted batch %s' % name)
45
46class AddBatchPage(WAeUPAddFormPage):
47    grok.name('add')
48    grok.context(IAccessCodeBatchContainer)
49    grok.require('waeup.manageACBatches')
50
51    title = 'Create Access Code Batch'
52    pnav = 0
53
54    form_fields = grok.AutoFields(IAccessCodeBatch).select(
55        'prefix', 'entry_num', 'cost')
56
57    @grok.action('Cancel')
58    def cancel(self, *args, **kw):
59        self.flash('Batch creation cancelled.')
60        self.redirect(self.url(self.context))
61
62    @grok.action('Create batch')
63    def createBatch(self, **data):
64        creator = self.request.principal.id
65        creation_date = datetime.now()
66        data.update(creation_date=creation_date, creator=creator)
67        batch = self.context.createBatch(**data)
68        csv_file = batch.createCSVLogFile()
69        self.context._p_changed = True
70        self.flash('Batch created (%s entries)' % data['entry_num'])
71        self.flash('Data written to %s' % csv_file)
72        self.redirect(self.url(self.context))
73
74class ReimportBatchPage(WAeUPPage):
75    """Screen for reimporting AC batches.
76    """
77    grok.name('reimport')
78    grok.context(IAccessCodeBatchContainer)
79    grok.template('reimportbatchpage')
80    grok.require('waeup.manageACBatches')
81
82    title = 'Reimport Access Code Batches'
83    pnav = 0
84
85    def update(self, filenames=None, reimport=None, cancel=None):
86        if cancel is not None:
87            self.flash('Reimport cancelled.')
88            self.redirect(self.url(self.context))
89            return
90        if reimport is None:
91            return
92        if not filenames:
93            self.flash('No file chosen. Action cancelled.')
94            self.redirect(self.url(self.context))
95            return
96        if isinstance(filenames, basestring):
97            filenames = [filenames]
98        userid = self.request.principal.id
99        for filename in filenames:
100            try:
101                self.context.reimport(filename, userid)
102            except KeyError:
103                self.flash('This batch already exists: %s' % filename)
104                continue
105            self.flash('Successfully reimported: %s' % filename)
106        self.redirect(self.url(self.context))
107
108class BatchContainerSearchPage(WAeUPPage):
109    grok.name('search')
110    grok.context(IAccessCodeBatchContainer)
111    grok.template('searchpage')
112    grok.require('waeup.manageACBatches')
113
114    title = 'Access Codes'
115    pnav = 0
116    label = 'Search and manage access codes'
117
118    def update(self, *args, **kw):
119        datatable.need()
120        form = self.request.form
121        self.hitlist = []
122        if 'searchterm' in form and form['searchterm']:
123            self.searchterm = form['searchterm']
124            self.searchtype = form['searchtype']
125        elif 'old_searchterm' in form:
126            self.searchterm = form['old_searchterm']
127            self.searchtype = form['old_searchtype']
128        else:
129            return
130        #import pdb; pdb.set_trace()
131        if not 'entries' in form:
132            self.hitlist = search(query=self.searchterm,
133                searchtype=self.searchtype, view=self)
134            return
135        entries = form['entries']
136        if isinstance(entries, basestring):
137            entries = [entries]
138        for entry in entries:
139            if 'disable' in form:
140                try:
141                    self.context.disable(entry, self.request.principal.id)
142                    self.flash('%s disabled.' % entry)
143                except InvalidTransitionError:
144                    self.flash('%s: Disable transition not allowed.' % entry)
145            elif 'enable' in form:
146                try:
147                    self.context.enable(entry, self.request.principal.id)
148                    self.flash('%s (re-)enabled.' % entry)
149                except InvalidTransitionError:
150                    self.flash('%s: Re-enable transition not allowed.' % entry)
151        self.hitlist = search(query=self.searchterm,
152            searchtype=self.searchtype, view=self)
153        return
154
155class BatchContainerBreadcrumb(Breadcrumb):
156    """A breadcrumb for ac batch containers.
157    """
158    grok.require('waeup.manageACBatches')
159    grok.context(IAccessCodeBatchContainer)
160    title = u'Access Code Batches'
161    parent_viewname = 'administration'
162
163class BatchContainerSearchBreadcrumb(Breadcrumb):
164    """A breadcrumb for ac batch containers search page.
165    """
166    grok.require('waeup.manageACBatches')
167    grok.context(IAccessCodeBatchContainer)
168    grok.name('search')
169    title = u'Search Access Codes'
170    viewname = 'search'
171    parent_viewname = 'index'
172
173class AdminTaskManageACBatches(AdminTask):
174    """Entry on administration page that links to batch container.
175    """
176    grok.order(5)
177    grok.require('waeup.manageACBatches')
178    grok.template('admintaskacbatches')
179
180    link_title = 'Access Code Batches'
181    target_viewname = 'accesscodes'
182
183class CreateBatchButton(AddActionButton):
184    """Action button on batch container page which links to batch creation.
185    """
186    grok.context(IAccessCodeBatchContainer)
187    grok.view(BatchContainerPage)
188    grok.require('waeup.manageACBatches')
189    text = 'Add Access Code Batch'
190
191class ReimportBatchButton(BatchOpButton):
192    """Action button on batch container page which links to batch reimport.
193    """
194    grok.context(IAccessCodeBatchContainer)
195    grok.view(BatchContainerPage)
196    grok.require('waeup.manageACBatches')
197    target = 'reimport'
198    text = 'Reimport Access Code Batch'
199
200class SearchAccessCodeButton(SearchActionButton):
201    """Action button on batch container page which links to search.
202    """
203    grok.context(IAccessCodeBatchContainer)
204    grok.view(BatchContainerPage)
205    grok.require('waeup.manageACBatches')
206    text = 'Search Access Codes'
207
208class ManageAccessCodes(ManageLink):
209    """Link in upper left box to access code management.
210    """
211    grok.order(6)
212    grok.require('waeup.manageACBatches')
213
214    link = u'accesscodes'
215    text = u'Access Codes'
Note: See TracBrowser for help on using the repository browser.