def task_action(context, task): user = context['user'] transitions = utils.get_allowed_transitions(task, user) next_transition = None if len(transitions) == 1: next_transition = transitions[0] return {'transitions': transitions, 'next_transition': next_transition}
def get_response(self, request, resource_type, resource_id, args): super(Block, self).get_response(request, resource_type, resource_id, args) if args == "": return self.render_details_block(request, resource_type, resource_id) elif args == EDIT: # Server-side check for permission on this view if request.user.has_perm(EDIT, obj=ObjectWithContext(request.resource)): with transaction.commit_on_success(): rv = self._edit_resource(request) return rv raise PermissionDenied elif args == "new_note": return self.add_new_note(request, resource_type, resource_id) elif args == "remove_note": return self.remove_note(request, resource_type, resource_id) elif args == "manage_roles": return self.manage_roles(request) elif args.startswith("transition"): t_name = args.split("/")[1] allowed_transitions = get_allowed_transitions( request.resource, request.user) t = Transition.objects.get(name__iexact=t_name, workflow=request.resource.workflow) if t in allowed_transitions: request.resource.do_transition(t, request.user) return self.response_success() else: return HttpResponse('')
def get_response(self, request, resource_type, resource_id, args): super(Block, self).get_response(request, resource_type, resource_id, args) if args == "": return self.render_details_block(request, resource_type, resource_id) elif args == EDIT: # Server-side check for permission on this view if request.user.has_perm(EDIT, obj=ObjectWithContext(request.resource)): with transaction.commit_on_success(): rv = self._edit_resource(request) return rv raise PermissionDenied elif args == "new_note": return self.add_new_note(request, resource_type, resource_id) elif args == "remove_note": return self.remove_note(request, resource_type, resource_id) elif args == "manage_roles": return self.manage_roles(request) elif args.startswith("transition"): t_name = args.split("/")[1] allowed_transitions = get_allowed_transitions(request.resource, request.user) t = Transition.objects.get(name__iexact=t_name, workflow=request.resource.workflow) if t in allowed_transitions: request.resource.do_transition(t, request.user) return self.response_success() else: return HttpResponse('')
def test_expense_wf(self): # Setup default workflow install_expense_workflow() ABR = Consultant.objects.get(trigramme="ABR") TCO = Consultant.objects.get(trigramme="TCO") tco = TCO.getUser() abr = ABR.getUser() fla = User.objects.get(username="******") category = ExpenseCategory.objects.create(name="repas") e = Expense.objects.create(user=tco, description="une grande bouffe", category=category, amount=123, creation_date=date.today(), expense_date=date.today()) self.assertEqual(wf.get_state(e), None) wf.set_initial_state(e) self.assertNotEqual(wf.get_state(e), None) # Now wf is setup # state = requested self.assertEqual(len(wf.get_allowed_transitions(e, tco)), 0) # No transition allowed for user self.assertEqual(len(wf.get_allowed_transitions(e, fla)), 0) # No transition allowed for paymaster self.assertEqual(len(wf.get_allowed_transitions(e, abr)), 2) # But for his manager accept/reject # Reject it reject = Transition.objects.get(name="reject") self.assertTrue(wf.do_transition(e, reject, abr)) for user in (tco, abr, fla): self.assertEqual(len(wf.get_allowed_transitions(e, user)), 0) # No transition allowed # Validate it wf.set_initial_state(e) # Returns to requested state validate = Transition.objects.get(name="validate") self.assertTrue(wf.do_transition(e, validate, abr)) for user in (tco, abr): self.assertEqual(len(wf.get_allowed_transitions(e, user)), 0) # No transition allowed self.assertEqual(len(wf.get_allowed_transitions(e, fla)), 2) # Except paymaster accept/ask info # Ask information ask = Transition.objects.get(name="ask information") self.assertTrue(wf.do_transition(e, ask, fla)) self.assertTrue(perm.has_permission(e, tco, "expense_edit")) wf.set_initial_state(e) # Returns to requested state self.assertEqual(len(wf.get_allowed_transitions(e, tco)), 0) # No transition allowed for user self.assertTrue(wf.do_transition(e, validate, abr)) # Validate it again # Check it control = Transition.objects.get(name="control") self.assertTrue(wf.do_transition(e, control, fla)) for user in (tco, abr, fla): self.assertEqual(len(wf.get_allowed_transitions(e, user)), 0) # No transition allowed # Create a payment for that expense expensePayment = ExpensePayment(payment_date=date.today()) expensePayment.save() e.expensePayment = expensePayment e.save() self.assertEqual(expensePayment.user(), tco) self.assertEqual(expensePayment.amount(), 123)
def allowed_transitions(self, user): """ Allowed transitions user can do on the managed instance :param user: a user object :type user: `django.contrib.auth.User <https://docs.djangoproject.com/en/1.4/topics/auth/#users>`_ :return: allowed transitions :rtype: a list of `workflows.models.Transition <http://packages.python.org/django-workflows/api.html#workflows.models.Transition>`_ """ return get_allowed_transitions(self, user)
def render(self, context): try: object_under_Test = self.object_under_Test.resolve(context) #resolve the actual object from the context user = context.get("user") available_transitions = [transition.name for transition in get_allowed_transitions(object_under_Test, user)] if self.transition_string in available_transitions: return self.nodelist_true.render(context) else: if isinstance(self.nodelist_false, str): return self.nodelist_false else: return self.nodelist_false.render(context) except template.VariableDoesNotExist: return ''
def _get_user_actions(self, request): user_actions = [] if request.user.has_perm(EDIT, obj=ObjectWithContext(request.resource)): # Duck-typing # Invoke an unneeded method just to check if an exception happen try: self._get_edit_form_class() except NotImplementedError as e: # If edit_form_class is not implemented, log the event log.debug(str(e)) else: klass_name = self.resource.__class__.__name__ url = None user_actions.append( ResourceBlockAction(block_name=self.BLOCK_NAME, resource=request.resource, name=EDIT, verbose_name=_("Edit"), popup_form=True, url=url)) if self._get_roles_formset_class(): user_actions.append( # Referrers assignment ResourceBlockAction( block_name=self.BLOCK_NAME, resource=request.resource, name="manage_roles", verbose_name=_("Manage roles"), popup_form=True, )) # Show actions for transition allowed for this resource for t in get_allowed_transitions(request.resource, request.user): #FIXME: related to gas/workflows_data.py ugettext_laxy FIXME local_transitions = { 'Open': 'Apri', 'Close': 'Chiudi', 'Close and send email': 'Chiudi e invia email', 'Archive': 'Archivia', 'Cancel': 'Annulla', } translated_t = local_transitions.get(t.name, t.name) #ENDFIXME action = ResourceBlockAction(block_name=self.BLOCK_NAME, resource=request.resource, name="transition/%s" % t.name.lower(), verbose_name=translated_t, popup_form=False, url=None) user_actions.append(action) return user_actions
def productionline_do_transition(user,productionline,status): # import pdb; pdb.set_trace() result = 0 #set current_operation_record status current_operation_record = productionline.current_operation_record current_operation_record.status = status current_operation_record.save() # logger.info(current_operation_record.status) transitions = get_allowed_transitions(productionline,user) # logger.info(transitions) if len(transitions) > 0: do_transition(productionline,transitions[0],user) #get current_operation_record current_state = get_state(productionline) for oper_group_record in productionline.oper_group_records.all(): for operation_record in oper_group_record.operation_records.all(): if operation_record.operation.state == current_state: current_operation_record = operation_record #set productionline current_operation_record productionline.current_operation_record = current_operation_record productionline.save() #set current_operation_record status productionline.current_operation_record.status = 2 productionline.current_operation_record.save() #set parent_operation_record status parent_operation_record = current_operation_record.parent_operation_record if parent_operation_record.status == 1: parent_operation_record.status = 2 parent_operation_record.save() #set manufacture_item.status for manufacture_item in productionline.manufacture_items.all(): manufacture_item.status = 2 manufacture_item.save() #parent_productionline do_transition parent_productionline = productionline.parent_productionline order_code = parent_productionline.current_operation_record.order_code can_do_parent_transition_tag = True for mig in parent_productionline.manu_item_groups.all(): for mi in mig.manufacture_items.all(): if mi.current_operation_record.order_code <= order_code: can_do_parent_transition_tag = False if can_do_parent_transition_tag: #set parent_current_operation_record status parent_current_operation_record = parent_productionline.current_operation_record parent_current_operation_record.status = status parent_current_operation_record.save() #do_transition parent_transitions = get_allowed_transitions(parent_productionline,user) if len(parent_transitions) > 0: do_transition(parent_productionline,parent_transitions[0],user) #get parent_current_operation_record current_state = get_state(parent_productionline) for oper_group_record in parent_productionline.oper_group_records.all(): for operation_record in oper_group_record.operation_records.all(): if operation_record.operation.state == current_state: parent_current_operation_record = operation_record #set parent_current_operation_record status parent_productionline.current_operation_record = parent_current_operation_record parent_productionline.save() #set parent_current_operation_record status parent_productionline.current_operation_record.status = 2 parent_productionline.current_operation_record.save() else: #do parent_productionline finish parent_productionline.state = 3 parent_productionline.save() return 0 else: #do parent_productionline finish productionline.state = 3 productionline.save() parent_productionline = productionline.parent_productionline can_do_parent_transition_tag = True for mig in parent_productionline.manu_item_groups.all(): for mi in mig.manufacture_items.all(): if mi.productionline.state != 3: can_do_parent_transition_tag = False if can_do_parent_transition_tag: #set parent_current_operation_record status parent_current_operation_record = parent_productionline.current_operation_record parent_current_operation_record.status = status parent_current_operation_record.save() #do parent_productionline finish parent_productionline.state = 3 parent_productionline.save() return 0 return 0 return result
def transitions(self, user): """expense allowed transitions in workflows for given user""" if self.workflow_in_progress: return wf.get_allowed_transitions(self, user) else: return []
def _get_user_actions(self, request): user_actions = [] if request.user.has_perm(EDIT, obj=ObjectWithContext(request.resource)): klass_name = self.resource.__class__.__name__ url = None user_actions.append( ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name=EDIT, verbose_name=_("Edit"), popup_form=True, url=url ) ) if self._get_roles_formset_class(): user_actions.append( # Referrers assignment ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name="manage_roles", verbose_name=_("Manage roles"), popup_form=True, ) ) # Show actions for transition allowed for this resource for t in get_allowed_transitions(request.resource, request.user): #FIXME: related to gas/workflows_data.py ugettext_laxy FIXME local_transitions = { 'Open' : 'Apri', 'Close' : 'Chiudi', 'Close and send email' : 'Chiudi e invia email', 'Archive' : 'Archivia', 'Cancel' : 'Annulla', } translated_t = local_transitions.get(t.name,t.name) #ENDFIXME action = ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name="transition/%s" % t.name.lower(), verbose_name=translated_t, popup_form=False, url=None ) user_actions.append( action ) return user_actions
def _get_user_actions(self, request): user_actions = [] if request.user.has_perm(EDIT, obj=ObjectWithContext(request.resource)): # Duck-typing # Invoke an unneeded method just to check if an exception happen try: self._get_edit_form_class() except NotImplementedError as e: # If edit_form_class is not implemented, log the event log.debug(str(e)) else: klass_name = self.resource.__class__.__name__ url = None user_actions.append( ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name=EDIT, verbose_name=_("Edit"), popup_form=True, url=url ) ) if self._get_roles_formset_class(): user_actions.append( # Referrers assignment ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name="manage_roles", verbose_name=_("Manage roles"), popup_form=True, ) ) # Show actions for transition allowed for this resource for t in get_allowed_transitions(request.resource, request.user): #FIXME: related to gas/workflows_data.py ugettext_laxy FIXME local_transitions = { 'Open' : 'Apri', 'Close' : 'Chiudi', 'Close and send email' : 'Chiudi e invia email', 'Archive' : 'Archivia', 'Cancel' : 'Annulla', } translated_t = local_transitions.get(t.name,t.name) #ENDFIXME action = ResourceBlockAction( block_name = self.BLOCK_NAME, resource = request.resource, name="transition/%s" % t.name.lower(), verbose_name=translated_t, popup_form=False, url=None ) user_actions.append( action ) return user_actions