def process(obj, field, action, state=None):
     current_state = getattr(obj, field)
     approvements = ApprovementService.get_approvements_object_waiting_for_approval(
         obj, [current_state])
     c = approvements.count()
     if c == 0:
         raise Exception(
             "There is no available state for destination for the user.")
     if c > 1:
         if state:
             approvements = approvements.filter(
                 approve_definition__transition__destination_state=state)
             if approvements.count() == 0:
                 available_states = StateService.get_available_states(
                     obj, field)
                 raise Exception(
                     "Invalid state is given(%s). Valid states is(are) %s" %
                     (state.__unicode__(), ','.join(
                         [ast.__unicode__() for ast in available_states])))
         else:
             raise Exception(
                 "State must be given when there are multiple states for destination"
             )
     approvement = approvements[0]
     approvement.status = action
     approvement.transactioner = middleware.get_user()
     approvement.transaction_date = datetime.now()
     approvement.save()
     return approvement
    def save(self, *args, **kwargs):
        user = get_user()
        self.last_updated_at = datetime.now()
        self.last_updated_by = user and user.username or None
        if not self.id:
            self.created_at = datetime.now()
            self.created_by = user and user.username or None

        super(BaseModel, self).save(*args, **kwargs)
        def get_approvement(approvements):
            min_order = approvements.aggregate(Min('approve_definition__order'))['approve_definition__order__min']
            approvements = approvements.filter(approve_definition__order=min_order)
            if include_user:
                user = middleware.get_user()
                if not user.is_superuser:
                    approvements = approvements.filter(Q(transactioner=user) | Q(approve_definition__permission__in=user.user_permissions.all()))
            if destination_state:
                approvements = approvements.filter(approve_definition__transition__destination_state=destination_state)

            return approvements
 def get_available_states(obj, field, include_user=True):
     content_type = ContentType.objects.get_for_model(obj)
     current_state = getattr(obj, field)
     approvements = TransitionApprovement.objects.filter(
         content_type=content_type,
         object_pk=obj.pk,
         approve_definition__transition__source_state=current_state,
     )
     if include_user:
         user = middleware.get_user()
         approvements = approvements.filter(
             approve_definition__permission__in=user.user_permissions.all()
         )
     destination_states = approvements.values_list('approve_definition__transition__destination_state', flat=True)
     return State.objects.filter(pk__in=destination_states)
 def process(obj, field, action, state=None):
     current_state = getattr(obj, field)
     approvements = ApprovementService.get_approvements_object_waiting_for_approval(obj, [current_state])
     c = approvements.count()
     if c == 0:
         raise Exception("There is no available state for destination for the user.")
     if c > 1:
         if state:
             approvements = approvements.filter(approve_definition__transition__destination_state=state)
             if approvements.count() == 0:
                 available_states = StateService.get_available_states(obj, field)
                 raise Exception("Invalid state is given(%s). Valid states is(are) %s" % (state.__unicode__(), ','.join([ast.__unicode__() for ast in available_states])))
         else:
             raise Exception("State must be given when there are multiple states for destination")
     approvement = approvements[0]
     approvement.status = action
     approvement.transactioner = middleware.get_user()
     approvement.transaction_date = datetime.now()
     approvement.save()
     return approvement
 def has_user_any_action(content_type, field):
     user = middleware.get_user()
     approvements = TransitionApprovement.objects.filter(Q(transactioner=user) | Q(approve_definition__permission__in=user.user_permissions.all())).filter(content_type=content_type)
     return approvements.count() != 0