Skip to content

Commit

Permalink
Converted the level field from a CharField to an IntegerField
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroLQueiroz committed Jun 16, 2023
1 parent 7915c66 commit c2ec8ac
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -3,7 +3,8 @@
## 2.0.0
- Added docker environment and migrated to Poetry and pyproject.toml
- Added verbose_name migration
- Migrated from jsonfield to Django JSONField
- Migrated from `jsonfield` to Django `JSONField`
- Converted the `level` field from a `CharField` to an `IntegerField`

## 1.8.0

Expand Down
14 changes: 9 additions & 5 deletions notifications/base/models.py
Expand Up @@ -13,8 +13,6 @@
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django.utils.html import format_html

from model_utils import Choices
from notifications import settings as notifications_settings
from notifications.signals import notify
from notifications.utils import id2slug
Expand Down Expand Up @@ -144,6 +142,13 @@ def mark_as_sent(self, recipient=None):
return qset.update(emailed=True)


class NotificationLevel(models.IntegerChoices):
SUCCESS = 1
INFO = 2
WARNING = 3
ERROR = 4


class AbstractNotification(models.Model):
"""
Action model describing the actor acting out a verb (on an optional
Expand Down Expand Up @@ -173,8 +178,7 @@ class AbstractNotification(models.Model):
<a href="http://oebfare.com/">brosner</a> commented on <a href="http://github.com/pinax/pinax">pinax/pinax</a> 2 hours ago # noqa
"""
LEVELS = Choices('success', 'info', 'warning', 'error')
level = models.CharField(_('level'), choices=LEVELS, default=LEVELS.info, max_length=20)
level = models.IntegerField(_('level'), choices=NotificationLevel.choices, default=NotificationLevel.INFO)

recipient = models.ForeignKey(
settings.AUTH_USER_MODEL,
Expand Down Expand Up @@ -322,7 +326,7 @@ def notify_handler(verb, **kwargs):
description = kwargs.pop('description', None)
timestamp = kwargs.pop('timestamp', timezone.now())
Notification = load_model('notifications', 'Notification')
level = kwargs.pop('level', Notification.LEVELS.info)
level = kwargs.pop('level', NotificationLevel.INFO)
actor_for_concrete_model = kwargs.pop('actor_for_concrete_model', True)

# Check if User or Group
Expand Down
19 changes: 19 additions & 0 deletions notifications/migrations/0011_notification_new_level.py
@@ -0,0 +1,19 @@
# Generated by Django 4.2.1 on 2023-06-02 00:05

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("notifications", "0010_alter_notification_data"),
]

operations = [
migrations.AddField(
model_name="notification",
name="new_level",
field=models.IntegerField(
choices=[(1, "Success"), (2, "Info"), (3, "Warning"), (4, "Error")], default=2, verbose_name="level"
),
),
]
21 changes: 21 additions & 0 deletions notifications/migrations/0012_auto_20230601_1905.py
@@ -0,0 +1,21 @@
# Generated by Django 4.2.1 on 2023-06-02 00:05

from django.db import migrations
from ..base.models import NotificationLevel

def copy_level(apps, schema_editor):
Notification = apps.get_model("notifications", "Notification")
Notification.objects.filter(level="success").update(new_level=NotificationLevel.SUCCESS)
Notification.objects.filter(level="info").update(new_level=NotificationLevel.INFO)
Notification.objects.filter(level="warning").update(new_level=NotificationLevel.WARNING)
Notification.objects.filter(level="error").update(new_level=NotificationLevel.ERROR)


class Migration(migrations.Migration):
dependencies = [
("notifications", "0011_notification_new_level"),
]

operations = [
migrations.RunPython(copy_level),
]
16 changes: 16 additions & 0 deletions notifications/migrations/0013_remove_notification_level.py
@@ -0,0 +1,16 @@
# Generated by Django 4.2.1 on 2023-06-16 01:04

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("notifications", "0012_auto_20230601_1905"),
]

operations = [
migrations.RemoveField(
model_name="notification",
name="level",
),
]
@@ -0,0 +1,17 @@
# Generated by Django 4.2.1 on 2023-06-16 01:04

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("notifications", "0013_remove_notification_level"),
]

operations = [
migrations.RenameField(
model_name="notification",
old_name="new_level",
new_name="level",
),
]
Empty file.
53 changes: 53 additions & 0 deletions notifications/tests/test_migrations/test_level_migration.py
@@ -0,0 +1,53 @@
import factory

from ...base.models import NotificationLevel
from ..factories import users as user_factory


def test_main_migration0002(migrator):
"""Ensures that the second migration works."""
old_state = migrator.apply_initial_migration(("notifications", "0011_notification_new_level"))

OldUser = old_state.apps.get_model("auth", "User")
OldNotification = old_state.apps.get_model("notifications", "Notification")
OldContentType = old_state.apps.get_model("contenttypes", "ContentType")

mark_follower = factory.create(OldUser, FACTORY_CLASS=user_factory.Recipient)
guido = factory.create(OldUser, FACTORY_CLASS=user_factory.Target)
mark = factory.create(OldUser, FACTORY_CLASS=user_factory.Actor)

user_type = OldContentType.objects.get_for_model(mark)
notification_base = {
"recipient": mark_follower,
"actor_content_type": user_type,
"actor_object_id": mark.pk,
"verb": "start follow",
"target_content_type": user_type,
"target_object_id": guido.pk,
}
OldNotification(level="success", **notification_base).save()
OldNotification(level="info", **notification_base).save()
OldNotification(level="warning", **notification_base).save()
OldNotification(level="error", **notification_base).save()

assert OldNotification.objects.count() == 4
assert OldNotification.objects.filter(level="info").count() == 1
assert OldNotification.objects.filter(new_level=NotificationLevel.INFO).count() == 4

new_state = migrator.apply_tested_migration(("notifications", "0012_auto_20230601_1905"))
NewNotification = new_state.apps.get_model("notifications", "Notification")

assert NewNotification.objects.count() == 4
assert NewNotification.objects.filter(new_level=NotificationLevel.SUCCESS).count() == 1
assert NewNotification.objects.filter(new_level=NotificationLevel.INFO).count() == 1
assert NewNotification.objects.filter(new_level=NotificationLevel.WARNING).count() == 1
assert NewNotification.objects.filter(new_level=NotificationLevel.ERROR).count() == 1

new_state_2 = migrator.apply_tested_migration(("notifications", "0014_rename_new_level_notification_level"))
NewNotification2 = new_state_2.apps.get_model("notifications", "Notification")

assert NewNotification2.objects.count() == 4
assert NewNotification2.objects.filter(level=NotificationLevel.SUCCESS).count() == 1
assert NewNotification2.objects.filter(level=NotificationLevel.INFO).count() == 1
assert NewNotification2.objects.filter(level=NotificationLevel.WARNING).count() == 1
assert NewNotification2.objects.filter(level=NotificationLevel.ERROR).count() == 1

0 comments on commit c2ec8ac

Please sign in to comment.