Changeset 5149 for main/waeup.sirp/trunk
- Timestamp:
- 13 Apr 2010, 15:47:46 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/accesscodes.py
r5132 r5149 19 19 20 20 def __init__(self, batch_serial, random_num, invalidation_date=None, 21 student_id=None ):21 student_id=None, disabled=False): 22 22 self.batch_serial = batch_serial 23 23 self.random_num = random_num 24 24 self._invalidation_date = invalidation_date 25 25 self.student_id = student_id 26 self._disabled = disabled 26 27 27 28 @property … … 57 58 # This attribute should be set by the surrounding batch only. 58 59 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 60 67 class AccessCodeBatch(grok.Model): 61 68 """A batch of access codes. … … 73 80 self.num = num 74 81 self.invalidated_num = 0 82 self.disabled_num = 0 75 83 self._entries = list() 76 84 self._acids = OIBTree() 85 self._studids = OIBTree() 77 86 self._createEntries() 78 87 … … 121 130 return self._entries[self._acids[ac_id]] 122 131 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 123 137 def addAccessCode(self, num, pin): 124 138 """Add an access-code. … … 137 151 ac._invalidation_date = datetime.now() 138 152 ac.student_id = student_id 153 if student_id is not None: 154 self._studids.update({student_id: num}) 139 155 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 140 190 141 191 def createCSVLogFile(self): … … 195 245 return os.path.basename(csv_path) 196 246 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 197 267 class AccessCodeBatchContainer(grok.Container): 198 268 grok.implements(IAccessCodeBatchContainer) … … 269 339 batch.createCSVLogFile() 270 340 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 272 356 class AccessCodePlugin(grok.GlobalUtility): 273 357 grok.name('accesscodes')
Note: See TracChangeset for help on using the changeset viewer.