Ignore:
Timestamp:
22 Jun 2011, 09:47:22 (14 years ago)
Author:
Henrik Bettermann
Message:

Implement search page for access codes (work in progress).

Location:
main/waeup.sirp/trunk/src/waeup/sirp/accesscodes
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py

    r6449 r6450  
    5252
    5353    @property
     54    def status(self):
     55        return IWorkflowState(self).getState()
     56
     57    @property
    5458    def disabled(self):
    5559        return IWorkflowState(self).getState() == DISABLED
     
    6266    def history(self):
    6367        history = IObjectHistory(self)
    64         return '\n'.join(history.messages)
     68        return '<br />'.join(history.messages)
    6569
    6670class AccessCodeBatch(grok.Container):
     
    288292        return
    289293
    290     def search(self, searchterm, searchtype, ):
    291         """Look for access-codes that comply with the given params.
    292         """
    293         results = []
    294         if searchtype == 'serial':
    295             try:
    296                 searchterm = int(searchterm)
    297             except:
    298                 return []
    299         for batchname in self.keys():
    300             part_result = self[batchname].search(searchterm, searchtype)
    301             results.extend(part_result)
    302         return results
     294    #def search(self, searchterm, searchtype):
     295    #    """Look for access-codes that comply with the given params.
     296    #    """
     297    #    results = []
     298    #    return results
    303299
    304300    def getAccessCode(self, ac_id):
     
    322318        if ac is None:
    323319            return
    324         ac.__parent__.disable(ac_id, user_id)
    325         return
    326 
    327     def enable(self, ac_id):
     320        disable_accesscode(ac_id, user_id)
     321        return
     322
     323    def enable(self, ac_id, user_id):
    328324        """(Re-)enable the AC with ID ``ac_id``.
    329325
     
    334330        if ac is None:
    335331            return
    336         ac.__parent__.enable(ac_id)
    337         return
    338 
    339     def invalidate(self, ac_id):
    340         """Invalidate the AC with ID ``ac_id``.
    341         """
    342         ac = self.getAccessCode(ac_id)
    343         if ac is None:
    344             return
    345         ac.__parent__.invalidate(ac_id)
    346         return
     332        reenable_accesscode(ac_id, user_id)
     333        return
     334
     335    #def invalidate(self, ac_id):
     336    #    """Invalidate the AC with ID ``ac_id``.
     337    #    """
     338    #    ac = self.getAccessCode(ac_id)
     339    #    if ac is None:
     340    #        return
     341    #    ac.__parent__.invalidate(ac_id)
     342    #    return
    347343
    348344
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/browser.py

    r6417 r6450  
    33import grok
    44from datetime import datetime
     5from hurry.workflow.interfaces import InvalidTransitionError
    56from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage
    67from waeup.sirp.browser.breadcrumbs import Breadcrumb
     
    1213    IAccessCodeBatchContainer, IAccessCodeBatch,
    1314    )
     15from waeup.sirp.accesscodes.catalog import search
    1416
    1517grok.context(IWAeUPObject)
     
    4648    grok.require('waeup.manageACBatches')
    4749
    48     title = 'Create Scratch Card Batch'
     50    title = 'Create Access Code Batch'
    4951    pnav = 0
    5052
     
    109111    grok.require('waeup.manageACBatches')
    110112
    111     title = 'Search Scratch Cards'
    112     pnav = 0
    113 
    114     def update(self, search=None, searchterm=None, searchtype=None,
    115                entries=None, disable=None, enable=None):
    116         self.searchresults = None
    117         if search is not None:
    118             searchresults = self.context.search(searchterm, searchtype)
    119             if len(searchresults) == 0:
    120                 return
    121             self.searchresults = []
    122             for result in searchresults:
    123                 status = u'unused'
    124                 if result.disabled is True:
    125                     status = u'disabled by %s on %s' % (
    126                         result.student_id,
    127                         result.invalidation_date.strftime('%c')
    128                         )
    129                 elif result.invalidation_date is not None:
    130                     status = u'invalidated by %s on %s' % (
    131                         result.student_id,
    132                         result.invalidation_date.strftime('%c')
    133                         )
    134                 entry = dict(
    135                     serial = result.batch_serial,
    136                     code = result.representation,
    137                     status = status)
    138                 self.searchresults.append(entry)
    139         if entries is None:
    140             return
     113    title = 'Access Codes'
     114    pnav = 0
     115    label = 'Search and manage access codes'
     116
     117    def update(self, *args, **kw):
     118        form = self.request.form
     119        self.hitlist = []
     120        if 'searchterm' in form and form['searchterm']:
     121            self.searchterm = form['searchterm']
     122            self.searchtype = form['searchtype']
     123        elif 'old_searchterm' in form:
     124            self.searchterm = form['old_searchterm']
     125            self.searchtype = form['old_searchtype']
     126        else:
     127            return
     128        #import pdb; pdb.set_trace()
     129        if not 'entries' in form:
     130            self.hitlist = search(query=self.searchterm,
     131                searchtype=self.searchtype, view=self)
     132            return
     133        entries = form['entries']
    141134        if isinstance(entries, basestring):
    142135            entries = [entries]
    143136        for entry in entries:
    144             if disable is not None:
    145                 self.context.disable(entry, self.request.principal.id)
    146                 self.flash('disabled %s' % entry)
    147             elif enable is not None:
    148                 self.context.enable(entry)
    149                 self.flash('(re-)enabled %s' % entry)
     137            if 'disable' in form:
     138                try:
     139                    self.context.disable(entry, self.request.principal.id)
     140                    self.flash('%s disabled.' % entry)
     141                except InvalidTransitionError:
     142                    self.flash('Transition not allowed.')
     143            elif 'enable' in form:
     144                try:
     145                    self.context.enable(entry, self.request.principal.id)
     146                    self.flash('%s (re-)enabled.' % entry)
     147                except InvalidTransitionError:
     148                    self.flash('Transition not allowed.')
     149        self.hitlist = search(query=self.searchterm,
     150            searchtype=self.searchtype, view=self)
    150151        return
    151152
     
    164165    grok.context(IAccessCodeBatchContainer)
    165166    grok.name('search')
    166     title = u'Search Scratch Cards'
     167    title = u'Search Access Codes'
    167168    viewname = 'search'
    168169    parent_viewname = 'index'
     
    184185    grok.view(BatchContainerPage)
    185186    grok.require('waeup.manageACBatches')
    186     text = 'Add Scratch Card Batch'
     187    text = 'Add Access Code Batch'
    187188
    188189class ReimportBatchButton(BatchOpButton):
     
    193194    grok.require('waeup.manageACBatches')
    194195    target = 'reimport'
    195     text = 'Reimport Scratch Card Batch'
     196    text = 'Reimport Access Code Batch'
    196197
    197198class SearchAccessCodeButton(SearchActionButton):
     
    201202    grok.view(BatchContainerPage)
    202203    grok.require('waeup.manageACBatches')
    203     text = 'Search Scratch Cards'
     204    text = 'Search Access Codes'
    204205
    205206class ManageAccessCodes(ManageLink):
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/browser.txt

    r6439 r6450  
    7070action bar:
    7171
    72     >>> browser.getLink('Add Scratch Card Batch').click()
     72    >>> browser.getLink('Add Access Code Batch').click()
    7373
    7474The add screen shows a form where we have to enter a prefix, the
     
    9292'Create batch' in the form:
    9393
    94     >>> browser.getLink('Add Scratch Card Batch').click()
     94    >>> browser.getLink('Add Access Code Batch').click()
    9595    >>> browser.getControl(name='form.prefix').value = 'APP'
    9696    >>> browser.getControl(name='form.entry_num').value = '5'
     
    126126works:
    127127
    128     >>> browser.getLink('Add Scratch Card Batch').click()
     128    >>> browser.getLink('Add Access Code Batch').click()
    129129    >>> browser.getControl(name='form.prefix').value = 'APP'
    130130    >>> browser.getControl(name='form.entry_num').value = '5'
     
    134134And a third one that can be deleted afterwards:
    135135
    136     >>> browser.getLink('Add Scratch Card Batch').click()
     136    >>> browser.getLink('Add Access Code Batch').click()
    137137    >>> browser.getControl(name='form.prefix').value = 'BLA'
    138138    >>> browser.getControl(name='form.entry_num').value = '3'
     
    192192list of importable files is empty:
    193193
    194     >>> browser.getLink('Reimport Scratch Card Batch').click()
     194    >>> browser.getLink('Reimport Access Code Batch').click()
    195195    >>> print browser.contents
    196196    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     
    218218Now the file will be presented as import source:
    219219
    220     >>> browser.getLink('Reimport Scratch Card Batch').click()
     220    >>> browser.getLink('Reimport Access Code Batch').click()
    221221    >>> filename in browser.contents
    222222    True
     
    232232Now let's really reimport the batch:
    233233
    234     >>> browser.getLink('Reimport Scratch Card Batch').click()
     234    >>> browser.getLink('Reimport Access Code Batch').click()
    235235    >>> ctrl = browser.getControl(name='filenames')
    236236    >>> ctrl.getControl(value=filename).selected = True
     
    246246If we try to reimport an existing batch, that won't work:
    247247
    248     >>> browser.getLink('Reimport Scratch Card Batch').click()
     248    >>> browser.getLink('Reimport Access Code Batch').click()
    249249    >>> ctrl = browser.getControl(name='filenames')
    250250    >>> ctrl.getControl(value=filename).selected = True
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/browser_templates/searchpage.pt

    r5918 r6450  
    11<form method="POST">
     2
     3<br />
     4
    25  <input type="submit" name="search" value="Search" />
    3   for scratch cards
     6  for access codes
    47
    58  <select name="searchtype">
    6     <option value="stud_id">
    7       used by student with ID
    8     </option>
    9     <option value="pin">with PIN</option>
    10     <option value="serial">with serial</option>
    11   </select>:
     9    <option value="code">with PIN</option>
     10    <option value="batch_serial">with serial</option>
     11    <option value="history">with message term</option>
     12  </select>
    1213
    1314  <input type="text" name="searchterm" />
    14   <br /><br />
    15   <b>complete numbers only</b>
    16   (Student ID means registration number for APP SCs)
    17   <br />
    1815
    1916  <p>&nbsp;</p>
    20   <div tal:condition="view/searchresults">
     17  <div tal:condition="view/hitlist">
    2118    <h3>Search Results</h3>
    22     <table>
     19    <input type="hidden" name="old_searchterm"
     20         tal:attributes="value view/searchterm" />
     21    <input type="hidden" name="old_searchtype"
     22         tal:attributes="value view/searchtype" />
     23    <table class = "zebra">
    2324      <thead>
    24         <tr>
    25           <th>&nbsp;</th>
    26           <th>Serial</th><th>AC</th><th>Status</th>
    27         </tr>
     25        <tr>
     26          <th>&nbsp;</th>
     27          <th>Serial</th><th>AC</th><th>Status</th><th>History</th>
     28        </tr>
    2829      </thead>
    2930      <tbody>
    30         <tr tal:repeat="item view/searchresults"
    31             tal:attributes="class python: repeat['item'].odd() and 'even' or 'odd'">
    32           <td><input type="checkbox" name="entries"
    33                      tal:attributes="value item/code" /></td>
    34           <td tal:content="item/serial">1</td>
    35           <td tal:content="item/code">APP-1-1234567890</td>
    36           <td tal:content="item/status">unused</td>
    37         </tr>
     31        <tr tal:repeat="item view/hitlist">
     32          <td><input type="checkbox" name="entries"
     33                     tal:attributes="value item/code" /></td>
     34          <td tal:content="item/batch_serial">1</td>
     35          <td tal:content="item/code">APP-1-1234567890</td>
     36          <td tal:content="item/status">unused</td>
     37          <td tal:content="structure item/history">history</td>
     38        </tr>
    3839      </tbody>
    3940    </table>
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/catalog.py

    r6446 r6450  
    22"""
    33import grok
    4 from waeup.sirp.interfaces import IUniversity
     4from hurry.query import Eq, Text
     5from hurry.query.query import Query
     6from zope.index.text.parsetree import ParseError
     7from waeup.sirp.interfaces import IUniversity, IQueryResultItem
    58from waeup.sirp.accesscodes.interfaces import IAccessCode
    69
     
    1619    disabled = grok.index.Field(attribute='disabled')
    1720    used = grok.index.Field(attribute='used')
     21    batch_serial = grok.index.Field(attribute='batch_serial')
     22    status = grok.index.Field(attribute='status')
     23
     24class AccessCodeQueryResultItem(object):
     25    grok.implements(IQueryResultItem)
     26
     27    def __init__(self, context, view):
     28        self.context = context
     29        #self.url = view.url(context)
     30        self.code = context.representation
     31        self.history = context.history
     32        self.status = context.status
     33        self.batch_serial = context.batch_serial
     34
     35def search(query=None, searchtype=None, view=None):
     36    if not query:
     37        view.flash('Empty search string.')
     38        return
     39    hitlist = []
     40    try:
     41        if searchtype == 'history':
     42            results = Query().searchResults(
     43                Text(('accesscodes_catalog', searchtype), query))
     44        elif searchtype == 'batch_serial':
     45            results = Query().searchResults(
     46                Eq(('accesscodes_catalog', searchtype), int(query)))
     47        else:
     48            results = Query().searchResults(
     49                Eq(('accesscodes_catalog', searchtype), query))
     50        for result in results:
     51            hitlist.append(AccessCodeQueryResultItem(result, view=view))
     52    except ParseError:
     53        view.flash('Search string not allowed.')
     54        return
     55    return hitlist
     56
     57
  • main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/interfaces.py

    r6425 r6450  
    2323        title = u'Cost of access code',
    2424        default = 0.0, min = 0.0,
     25        )
     26    status = schema.TextLine(
     27        title = u'Workflow status',
    2528        )
    2629    disabled = schema.Bool(
Note: See TracChangeset for help on using the changeset viewer.