Repository: cdriehuys/django-simple-email-auth

Issue:
Remove `normalized_address` field
All the current use cases for the `normalized_address` field can be replaced by querying with `address__iexact`. It is also trivial to add this field back later if we feel like we need it, but for now it is another thing to keep in sync and take up space.

Discussion:

Patch:
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index bb73d7c..728d256 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -6,6 +6,12 @@ Changelog
 In Development
 **************
 
+Breaking Changes
+================
+
+* #12: Removed ``normalized_address`` field from ``EmailAddress`` model because
+  its behavior can easily be replicated by querying against ``address__iexact``.
+
 Features
 ========
 
diff --git a/email_auth/admin.py b/email_auth/admin.py
index 834d8d1..86ddcd4 100644
--- a/email_auth/admin.py
+++ b/email_auth/admin.py
@@ -31,7 +31,6 @@ class EmailAddressAdmin(admin.ModelAdmin):
     date_hierarchy = "time_created"
     fields = (
         "address",
-        "normalized_address",
         "user",
         "is_verified",
         "time_verified",
@@ -48,8 +47,8 @@ class EmailAddressAdmin(admin.ModelAdmin):
         "time_updated",
     )
     list_filter = ("is_verified",)
-    readonly_fields = ("normalized_address", "time_created", "time_updated")
-    search_fields = ("address", "normalized_address")
+    readonly_fields = ("time_created", "time_updated")
+    search_fields = ("address",)
 
 
 @admin.register(models.EmailVerification)
diff --git a/email_auth/migrations/0003_rm_emailaddress_normalizedaddress.py b/email_auth/migrations/0003_rm_emailaddress_normalizedaddress.py
new file mode 100644
index 0000000..a297578
--- /dev/null
+++ b/email_auth/migrations/0003_rm_emailaddress_normalizedaddress.py
@@ -0,0 +1,22 @@
+# Generated by Django 2.2.3 on 2019-09-14 16:45
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [("email_auth", "0002_emailverification")]
+
+    operations = [
+        migrations.AlterModelOptions(
+            name="emailaddress",
+            options={
+                "ordering": ("time_created",),
+                "verbose_name": "email address",
+                "verbose_name_plural": "email addresses",
+            },
+        ),
+        migrations.RemoveField(
+            model_name="emailaddress", name="normalized_address"
+        ),
+    ]
diff --git a/email_auth/models.py b/email_auth/models.py
index 0a59d6e..15c33c6 100644
--- a/email_auth/models.py
+++ b/email_auth/models.py
@@ -71,14 +71,6 @@ class EmailAddress(models.Model):
             "address."
         ),
     )
-    normalized_address = models.EmailField(
-        help_text=_(
-            "The normalized version of the user's email address used for "
-            "comparison with other addresses."
-        ),
-        unique=True,
-        verbose_name=_("normalized address"),
-    )
     time_created = models.DateTimeField(
         auto_now_add=True,
         help_text=_("The time that the instance was created."),
@@ -107,7 +99,7 @@ class EmailAddress(models.Model):
     )
 
     class Meta:
-        ordering = ("normalized_address",)
+        ordering = ("time_created",)
         verbose_name = _("email address")
         verbose_name_plural = _("email addresses")
 
@@ -123,7 +115,6 @@ class EmailAddress(models.Model):
                 "address",
                 "id",
                 "is_verified",
-                "normalized_address",
                 "time_created",
                 "time_updated",
                 "time_verified",
@@ -138,12 +129,6 @@ class EmailAddress(models.Model):
         """
         return self.address
 
-    def clean(self):
-        """
-        Populate derived fields.
-        """
-        self.normalized_address = self.address.lower()
-
     def send_verification_email(self):
         """
         Create and send a verification email to the address associated


Test patch:
diff --git a/email_auth/test/models/test_email_address_model.py b/email_auth/test/models/test_email_address_model.py
index 9cbda52..13c2ecb 100644
--- a/email_auth/test/models/test_email_address_model.py
+++ b/email_auth/test/models/test_email_address_model.py
@@ -14,7 +14,6 @@ def test_init_with_all_fields():
     email = models.EmailAddress(
         address="Test@Example.com",
         is_verified=True,
-        normalized_address="test@example.com",
         time_verified=timezone.now(),
         user=auth.get_user_model()(),
     )
@@ -23,18 +22,6 @@ def test_init_with_all_fields():
     assert email.pk
 
 
-def test_clean_normalize_email_address():
-    """
-    Cleaning an email address should populate the "normalized" field
-    with a lower-cased version of the address.
-    """
-    email = models.EmailAddress(address="TeSt@ExAmPlE.cOm")
-
-    email.clean()
-
-    assert email.normalized_address == email.address.lower()
-
-
 def test_repr():
     """
     The string representation of an email address should contain all
@@ -42,7 +29,6 @@ def test_repr():
     """
     email = models.EmailAddress(
         address="test@example.com",
-        normalized_address="test@example.com",
         time_created=timezone.now(),
         time_updated=timezone.now(),
         time_verified=timezone.now(),
@@ -54,7 +40,6 @@ def test_repr():
             "address",
             "id",
             "is_verified",
-            "normalized_address",
             "time_created",
             "time_updated",
             "time_verified",