Changeset 5149


Ignore:
Timestamp:
13 Apr 2010, 15:47:46 (14 years ago)
Author:
uli
Message:
  • Add disabled flag for AccessCode? and add related methods to batches.
  • Enable searching batches and batchcontainers for ACs, serials and student IDs.
File:
1 edited

Legend:

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

    r5132 r5149  
    1919
    2020    def __init__(self, batch_serial, random_num, invalidation_date=None,
    21                  student_id=None):
     21                 student_id=None, disabled=False):
    2222        self.batch_serial = batch_serial
    2323        self.random_num = random_num
    2424        self._invalidation_date = invalidation_date
    2525        self.student_id = student_id
     26        self._disabled = disabled
    2627
    2728    @property
     
    5758        # This attribute should be set by the surrounding batch only.
    5859        return self._invalidation_date
    59    
     60
     61    @property
     62    def disabled(self):
     63        # We define this as a property to make it unwritable.
     64        # This attribute should be set by the surrounding batch only.
     65        return self._disabled
     66
    6067class AccessCodeBatch(grok.Model):
    6168    """A batch of access codes.
     
    7380        self.num = num
    7481        self.invalidated_num = 0
     82        self.disabled_num = 0
    7583        self._entries = list()
    7684        self._acids = OIBTree()
     85        self._studids = OIBTree()
    7786        self._createEntries()
    7887
     
    121130        return self._entries[self._acids[ac_id]]
    122131
     132    def getAccessCodeForStudentId(self, stud_id):
     133        """Get any AccessCode invalidated for ``stud_id`` or ``KeyError``.
     134        """
     135        return self._entries[self._studids[stud_id]]
     136
    123137    def addAccessCode(self, num, pin):
    124138        """Add an access-code.
     
    137151        ac._invalidation_date = datetime.now()
    138152        ac.student_id = student_id
     153        if student_id is not None:
     154            self._studids.update({student_id: num})
    139155        self.invalidated_num += 1
     156
     157    def disable(self, ac_id, user_id):
     158        """Disable the AC with ID ``ac_id``.
     159
     160        ``user_id`` is the user ID of the user triggering the
     161        process. Already disabled ACs are left untouched.
     162        """
     163        num = self._acids[ac_id]
     164        ac = self.getAccessCode(ac_id)
     165        if ac._disabled == True:
     166            return
     167        ac._disabled = True
     168        old_student_id = ac.student_id
     169        if old_student_id is not None:
     170            del self._studids[old_student_id]
     171            self._studids.update({user_id: num})
     172        ac.student_id = user_id
     173        ac._invalidation_date = datetime.now()
     174        self.disabled_num += 1
     175       
     176    def enable(self, ac_id):
     177        """(Re-)enable the AC with ID ``ac_id``.
     178
     179        This leaves the given AC in state ``unused``. Already enabled
     180        ACs are left untouched.
     181        """
     182        num = self._acids[ac_id]
     183        ac = self.getAccessCode(ac_id)
     184        if ac._disabled == False:
     185            return
     186        ac.student_id = None
     187        ac._disabled = False
     188        ac._invalidation_date = None
     189        self.disabled_num -= 1
    140190
    141191    def createCSVLogFile(self):
     
    195245        return os.path.basename(csv_path)
    196246
     247    def search(self, searchterm, searchtype):
     248        if searchtype == 'serial':
     249            if len(self._entries) < searchterm + 1:
     250                return []
     251            return [self._entries[searchterm]]
     252        if searchtype == 'pin':
     253            try:
     254                entry = self.getAccessCode(searchterm)
     255                return [entry]
     256            except KeyError:
     257                return []
     258        if searchtype != 'stud_id':
     259            return []
     260        try:
     261            entry = self.getAccessCodeForStudentId(searchterm)
     262            return [entry]
     263        except KeyError:
     264            pass
     265        return []
     266
    197267class AccessCodeBatchContainer(grok.Container):
    198268    grok.implements(IAccessCodeBatchContainer)
     
    269339        batch.createCSVLogFile()
    270340        return
    271    
     341
     342    def search(self, searchterm, searchtype, ):
     343        """Look for access-codes that comply with the given params.
     344        """
     345        results = []
     346        if searchtype == 'serial':
     347            try:
     348                searchterm = int(searchterm)
     349            except:
     350                return []
     351        for batchname in self.keys():
     352            part_result = self[batchname].search(searchterm, searchtype)
     353            results.extend(part_result)
     354        return results
     355
    272356class AccessCodePlugin(grok.GlobalUtility):
    273357    grok.name('accesscodes')
Note: See TracChangeset for help on using the changeset viewer.