mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-05-02 10:44:42 +00:00
Converted the level field from a CharField to an IntegerField
This commit is contained in:
parent
7915c66079
commit
c2ec8ac800
8 changed files with 137 additions and 6 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@ from django.db.models.query import QuerySet
|
|||
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
|
||||
|
|
@ -144,6 +142,13 @@ class NotificationQuerySet(models.query.QuerySet):
|
|||
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
|
||||
|
|
@ -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,
|
||||
|
|
@ -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
|
||||
|
|
|
|||
19
notifications/migrations/0011_notification_new_level.py
Normal file
19
notifications/migrations/0011_notification_new_level.py
Normal file
|
|
@ -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
notifications/migrations/0012_auto_20230601_1905.py
Normal file
21
notifications/migrations/0012_auto_20230601_1905.py
Normal file
|
|
@ -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
notifications/migrations/0013_remove_notification_level.py
Normal file
16
notifications/migrations/0013_remove_notification_level.py
Normal file
|
|
@ -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",
|
||||
),
|
||||
]
|
||||
0
notifications/tests/test_migrations/__init__.py
Normal file
0
notifications/tests/test_migrations/__init__.py
Normal file
53
notifications/tests/test_migrations/test_level_migration.py
Normal file
53
notifications/tests/test_migrations/test_level_migration.py
Normal file
|
|
@ -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
|
||||
Loading…
Reference in a new issue