Changeset 3e372da for remit/vouchers
- Timestamp:
- Jul 25, 2010, 4:31:09 AM (15 years ago)
- Branches:
- master, client
- Children:
- 0e2f379
- Parents:
- dd6edfb
- git-author:
- Alex Dehnert <adehnert@…> (07/25/10 04:31:09)
- git-committer:
- Alex Dehnert <adehnert@…> (07/25/10 04:31:09)
- Location:
- remit/vouchers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
remit/vouchers/models.py
rb1a259c r3e372da 250 250 ), 251 251 } 252 253 class BulkRequestAction: 254 def __init__(self, name, label, action, perm_predicate=None, ): 255 self.name = name 256 self.label = label 257 self.action = action 258 if perm_predicate is None: 259 perm_predicate = lambda user: True 260 elif perm_predicate == True: 261 perm_predicate = lambda user: True 262 self.perm_predicate = perm_predicate 263 def can(self, user): 264 return self.perm_predicate(user) 265 def do(self, http_request, rr, ): 266 if self.can(http_request.user): 267 return self.action(http_request, rr, ) 268 else: 269 return False, "permission denied" 270 def __str__(self): 271 return self.label 272 @classmethod 273 def filter_can_only(cls, actions, user): 274 return [ action for action in actions if action.can(user) ] 275 def bulk_action_approve(http_request, rr): 276 approver = http_request.user 277 signatory_name = http_request.user.get_full_name() 278 if rr.voucher: 279 return False, "already approved" 280 else: 281 rr.approve(approver, signatory_name) 282 return True, "request approved" 283 284 def bulk_action_email_factory(stock_email_obj): 285 assert stock_email_obj.context == 'request' 286 def inner(http_request, rr): 287 stock_email_obj.send_email_request(rr) 288 return True, "mail sent" 289 return inner 290 def perm_checker(perm): 291 def predicate(user): 292 return user.has_perm(perm) 293 return predicate 294 295 bulk_request_actions = [] 296 if settings.SIGNATORY_EMAIL: 297 bulk_request_actions.append(BulkRequestAction( 298 name='approve', 299 label='Approve Requests', 300 action=bulk_action_approve, 301 perm_predicate=perm_checker('vouchers.can_approve'), 302 )) 303 for name, stockemail in stock_emails.items(): 304 if stockemail.context == 'request': 305 bulk_request_actions.append(BulkRequestAction( 306 name='email/%s' % name, 307 label='Stock Email: %s' % stockemail.label, 308 action=bulk_action_email_factory(stockemail), 309 perm_predicate=perm_checker('vouchers.can_email'), 310 )) -
remit/vouchers/views.py
rbef7191 r3e372da 354 354 ) 355 355 356 def list_to_keys(lst): 357 dct = {} 358 for key in lst: 359 dct[key] = True 360 return dct 361 356 362 @login_required 357 363 def show_requests(http_request, ): 364 # BULK ACTIONS 365 actions = vouchers.models.BulkRequestAction.filter_can_only( 366 vouchers.models.bulk_request_actions, 367 http_request.user, 368 ) 369 apply_action_message = None 370 apply_action_errors = [] 371 if 'select' in http_request.REQUEST: 372 selected_rr_ids = [ int(item) for item in http_request.REQUEST.getlist('select') ] 373 else: 374 selected_rr_ids = [] 375 if "apply-action" in http_request.POST: 376 action_name = http_request.POST['action'] 377 if action_name == 'none': 378 apply_action_message = "No action selected." 379 else: 380 matching_actions = [ action for action in actions if action.name == action_name] 381 if(len(matching_actions) > 0): 382 action = matching_actions[0] 383 rrs = ReimbursementRequest.objects.filter(pk__in=selected_rr_ids) 384 for rr in rrs: 385 success, msg = action.do(http_request, rr) 386 if not success: 387 apply_action_errors.append((rr, msg)) 388 apply_action_message = '"%s" applied to %d request(s) (%d errors encountered)' % (action.label, len(rrs), len(apply_action_errors), ) 389 else: 390 apply_action_message = "Unknown or forbidden action requested." 391 358 392 # PERMISSION-BASED REQUEST FILTERING 359 393 if http_request.user.has_perm('vouchers.can_list'): … … 398 432 queryset=qs, 399 433 extra_context={ 434 'actions' : actions, 435 'selected_ids' : list_to_keys(selected_rr_ids), 436 'action_message': apply_action_message, 437 'action_errors' : apply_action_errors, 400 438 'useronly': useronly, 401 439 'order' : order,
Note: See TracChangeset
for help on using the changeset viewer.