def is_valid(self, bundle, request=None): errors = {} if not bundle.obj.pk: return errors try: ci = CI.objects.get(pk=bundle.obj.pk) except CI.DoesNotExist: return {"__all__": "This object does not exist."} changed_state = bundle.data.get("state") if not can_change_ci_state(ci, changed_state): message = """You can not change state because this service has linked devices.""" errors["state"] = " ".join(message.split()) return errors
def clean_state(self, *args, **kwargs): state = self.instance.state changed_state = self.cleaned_data.get('state') if ( self.instance.id and not can_change_ci_state(self.instance, changed_state) ): message = """You can not change state to {}, because this service has linked devices. Click <a href="{}" target="_blank">here</a> to see it.""".format( CI_STATE_TYPES.NameFromID(changed_state), reverse('search') + '?service_catalog=' + self.instance.name, ) raise forms.ValidationError(mark_safe(message)) return state