12589 fix for Objectchange

This commit is contained in:
Arthur 2023-06-20 17:12:43 -07:00
parent 39608f6d83
commit 8267ca5840
2 changed files with 40 additions and 15 deletions

View File

@ -201,6 +201,11 @@ class UserForm(BootstrapMixin, forms.ModelForm):
del self.fields['date_joined']
del self.fields['last_login']
# def is_valid(self):
# ret = super().is_valid()
# breakpoint()
# return ret
def save(self, *args, **kwargs):
instance = super().save(*args, **kwargs)
instance.object_permissions.set(self.cleaned_data['object_permissions'])
@ -215,9 +220,27 @@ class UserForm(BootstrapMixin, forms.ModelForm):
if password != confirm_password:
raise forms.ValidationError(
"password and confirm_password does not match"
_("password and confirm_password does not match")
)
def clean_username(self):
"""Reject usernames that differ only in case."""
instance = getattr(self, 'instance', None)
if instance:
qs = self._meta.model.objects.exclude(pk=instance.pk)
else:
qs = self._meta.model.objects.all()
username = self.cleaned_data.get("username")
if (
username and qs.filter(username__iexact=username).exists()
):
raise forms.ValidationError(
_("user with this username already exists")
)
return username
class GroupForm(BootstrapMixin, forms.ModelForm):
users = DynamicModelMultipleChoiceField(

View File

@ -170,6 +170,7 @@ class ViewTestCases:
instance = self._get_queryset().order_by('pk').last()
self.assertInstanceEqual(instance, self.form_data)
if hasattr(self.model, "to_objectchange"):
# Verify ObjectChange creation
objectchanges = ObjectChange.objects.filter(
changed_object_type=ContentType.objects.get_for_model(instance),
@ -263,6 +264,7 @@ class ViewTestCases:
self.assertHttpStatus(self.client.post(**request), 302)
self.assertInstanceEqual(self._get_queryset().get(pk=instance.pk), self.form_data)
if hasattr(self.model, "to_objectchange"):
# Verify ObjectChange creation
objectchanges = ObjectChange.objects.filter(
changed_object_type=ContentType.objects.get_for_model(instance),