def register_object(content_type_id, object_id, field_id): transition_approvements = TransitionApprovement.objects.filter(content_type__pk=content_type_id, object_id=object_id, field__pk=field_id) if transition_approvements.count() == 0: ApprovementService.init_approvements(content_type_id, object_id, field_id) initial_state = StateService.get_init_state(content_type_id, field_id) obj = Object.objects.get(object_id=object_id) obj.state = initial_state obj.save() return {'state': initial_state.details()}
def init_approvements(content_type_id, obj_id, field_id): content_type = ExternalContentType.objects.get(pk=content_type_id) field = Field.objects.get(pk=field_id) for transition_approve_definition in TransitionApproveDefinition.objects.filter(transition__content_type__pk=content_type_id, transition__field__pk=field_id): TransitionApprovement.objects.update_or_create( approve_definition=transition_approve_definition, object_id=obj_id, content_type=content_type, field=field, defaults={ 'status': PENDING } ) init_state = StateService.get_init_state(content_type_id, field_id) Object.objects.update_or_create(object_id=obj_id, content_type=content_type, field=field, defaults={'state': init_state})
def test_init(self): test_obj = TestModel.objects.get(pk=5001) ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNone(obj) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNotNone(obj) initial_state = StateService.get_init_state(ct.pk, field.pk) self.assertEqual(initial_state, obj.state) self.assertEqual(18, TransitionApprovement.objects.count())
def test_register_object(self): test_obj = TestModel.objects.get(pk=5001) ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNone(obj) self.assertEqual(0, TransitionApprovement.objects.count()) result = self.client.post(reverse('riverio.apps.web.views.restful.register_object', args=[ct.pk, test_obj.pk, field.pk]), **{settings.TOKEN_KEY: Application.objects.get(pk=501).token}) obj = Object.objects.get_object_or_None(object_id=test_obj.pk) self.assertIsNotNone(obj) self.assertEqual(OK, result.status_code) initial_state = StateService.get_init_state(ct.pk, field.pk) self.assertEqual(initial_state, obj.state) self.assertEqual(9, TransitionApprovement.objects.count())
def process(content_type_id, object_id, field_id, user_id, action, next_state_id=None): obj = Object.objects.get(content_type_id=content_type_id, object_id=object_id, field_id=field_id) current_state = obj.state next_state = None if next_state_id: next_state = State.objects.get(pk=next_state_id) approvements = ApprovementService.get_approvements_object_waiting_for_approval(content_type_id, object_id, field_id, user_id, [current_state]) c = approvements.count() if c == 0: raise Exception("There is no available state for destination for the user.") if c > 1: if next_state: approvements = approvements.filter(approve_definition__transition__destination_state=next_state) if approvements.count() == 0: available_states = StateService.get_available_states(content_type_id, object_id, field_id, user_id) raise Exception("Invalid state is given(%s). Valid states is(are) %s" % (next_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 = ExternalUser.objects.get(user_id=user_id) approvement.transaction_date = datetime.now() approvement.save() return approvement
def test_get_available_states(self): ct = ExternalContentType.objects.get(pk=1501) field = Field.objects.get(pk=3501) ObjectService.register_object(ct.pk, 5001, field.pk) ObjectService.register_object(ct.pk, 5002, field.pk) middleware.get_user = MagicMock(return_value=User.objects.get(pk=2101)) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 2) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 3) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4, include_user=True) self.assertEqual(0, available_states.count()) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 4) self.assertEqual(0, available_states.count()) obj = TestModel.objects.get(pk=5001) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1, include_user=False) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1, include_user=True) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0]) available_states = StateService.get_available_states(ct.pk, 5001, field.pk, 1) self.assertEqual(1, available_states.count()) self.assertEqual(State.objects.get(label='s2'), available_states[0])
def get_state_by_label(request, label): return StateService.get_state_by(label=label)
def get_state_by_id(request, id): return StateService.get_state_by(pk=id)