stephenmcd / mezzanine

CMS framework for Django
http://mezzanine.jupo.org
BSD 2-Clause "Simplified" License
4.75k stars 1.64k forks source link

[BUG] SitePermissionUserAdminForm: Verifying that a User with the same email already exists, use exists instead of get. #2018

Closed ytyng closed 2 years ago

ytyng commented 2 years ago

Is there an existing issue for this?

Current Behavior

https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/admin.py#L412

in SitePermissionUserAdminForm, If there are multiple records for users with the same email, Raises MultipleObjectsReturned, and no catched.

Expected Behavior

Raises ValidationError(_("This email is already registered"))

Steps To Reproduce

  1. Multiple records for users with the same email exists,
  2. Save user admin page.

MultipleObjectsReturned_at__admin_user_user_42_change_

Environment

- Operating System: MacOS, Ubuntu
- Python version: 3.8
- Django version: 3.2
- Database engine and version: MySQL 5.7
- Mezzanine version: 5.0.0 (85564606d4594f027b9ad7ff38c576a9bdd3096d)

Anything else?

https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/core/admin.py#L412

I think

        try:
            User.objects.exclude(id=form.instance.id).get(email=email)
        except User.DoesNotExist:
            return email
        raise ValidationError(_("This email is already registered"))

Change to

        if User.objects.exclude(id=form.instance.id).filter(email=email).exists():
            raise ValidationError(_("This email is already registered"))
        return email
github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version 5.1.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: