Skip to content

Commit

Permalink
Rename BannerNotification type to level
Browse files Browse the repository at this point in the history
See issue with using type as a name with mutations:
graphql-python/graphene#1519
  • Loading branch information
matti-lamppu committed Aug 31, 2023
1 parent 278014f commit c84d337
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 49 deletions.
6 changes: 3 additions & 3 deletions api/graphql/banner_notification/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

class BannerNotificationOrderingFilter(CustomOrderingFilter):
@staticmethod
def order_by_type(qs: BannerNotificationQuerySet, desc: bool) -> BannerNotificationQuerySet:
return qs.order_by_type(desc)
def order_by_level(qs: BannerNotificationQuerySet, desc: bool) -> BannerNotificationQuerySet:
return qs.order_by_level(desc)

@staticmethod
def order_by_target(qs: BannerNotificationQuerySet, desc: bool) -> BannerNotificationQuerySet:
Expand All @@ -29,7 +29,7 @@ class BannerNotificationFilterSet(django_filters.FilterSet):
("active_until", "ends"),
),
custom_fields=(
"type",
"level",
"state",
"target",
),
Expand Down
2 changes: 1 addition & 1 deletion api/graphql/banner_notification/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Meta:
"message_en",
"message_sv",
"draft",
"type",
"level",
"target",
"active_from",
"active_until",
Expand Down
4 changes: 2 additions & 2 deletions common/admin/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class BannerNotificationAdmin(TranslationAdmin):
form = BannerNotificationAdminForm
list_display = [
"name",
"type",
"level",
"target",
"active_from",
"active_until",
]
list_filter = [
"type",
"level",
"target",
]
4 changes: 2 additions & 2 deletions common/admin/forms/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Meta:
fields = [
"name",
"message",
"type",
"level",
"target",
"active_from",
"active_until",
Expand All @@ -23,7 +23,7 @@ class Meta:
help_texts = {
"name": gettext_lazy("Name of the notification. Should be unique."),
"message": gettext_lazy("Notification body."),
"type": gettext_lazy("Type of the notification."),
"level": gettext_lazy("Level of the notification."),
"target": gettext_lazy("Target user interface of the notification."),
"active_from": gettext_lazy(
"Start date of the notification. If empty, 'active_until' must also be empty.",
Expand Down
4 changes: 2 additions & 2 deletions common/choices/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .banner_notification import BannerNotificationState, BannerNotificationTarget, BannerNotificationType
from .banner_notification import BannerNotificationLevel, BannerNotificationState, BannerNotificationTarget

__all__ = [
"BannerNotificationType",
"BannerNotificationLevel",
"BannerNotificationTarget",
"BannerNotificationState",
]
2 changes: 1 addition & 1 deletion common/choices/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.utils.translation import gettext_lazy


class BannerNotificationType(models.TextChoices):
class BannerNotificationLevel(models.TextChoices):
EXCEPTION = "EXCEPTION", gettext_lazy("Exception")
WARNING = "WARNING", gettext_lazy("Warning")
NORMAL = "NORMAL", gettext_lazy("Normal")
Expand Down
20 changes: 10 additions & 10 deletions common/management/commands/create_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
from tilavarauspalvelu.utils.commons import WEEKDAYS
from users.models import User

from ...choices import BannerNotificationTarget, BannerNotificationType
from ...choices import BannerNotificationLevel, BannerNotificationTarget
from ...models import BannerNotification
from ._utils import (
batched,
Expand Down Expand Up @@ -2091,56 +2091,56 @@ def _create_banner_notifications():
today = now()
banner_notifications: list[BannerNotification] = []
for target in BannerNotificationTarget.values:
for type_ in BannerNotificationType.values:
for level in BannerNotificationLevel.values:
draft_message_fi = faker_fi.sentence()
active_message_fi = faker_fi.sentence()
scheduled_message_fi = faker_fi.sentence()
past_message_fi = faker_fi.sentence()

banner_notifications += [
BannerNotification(
name=f"Draft {type_} notification for {target}",
name=f"Draft {level} notification for {target}",
message=draft_message_fi,
message_fi=draft_message_fi,
message_en=faker_en.sentence(),
message_sv=faker_sv.sentence(),
type=type_,
level=level,
target=target,
draft=True,
active_from=None,
active_until=None,
),
BannerNotification(
name=f"Active {type_} notification for {target}",
name=f"Active {level} notification for {target}",
message=active_message_fi,
message_fi=active_message_fi,
message_en=faker_en.sentence(),
message_sv=faker_sv.sentence(),
type=type_,
level=level,
target=target,
draft=False,
active_from=today - timedelta(days=1),
active_until=today + timedelta(days=7),
),
BannerNotification(
name=f"Scheduled {type_} notification for {target}",
name=f"Scheduled {level} notification for {target}",
message=scheduled_message_fi,
message_fi=scheduled_message_fi,
message_en=faker_en.sentence(),
message_sv=faker_sv.sentence(),
type=type_,
level=level,
target=target,
draft=False,
active_from=today + timedelta(days=7),
active_until=today + timedelta(days=14),
),
BannerNotification(
name=f"Past {type_} notification for {target}",
name=f"Past {level} notification for {target}",
message=past_message_fi,
message_fi=past_message_fi,
message_en=faker_en.sentence(),
message_sv=faker_sv.sentence(),
type=type_,
level=level,
target=target,
draft=False,
active_from=today - timedelta(days=7),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 4.2.4 on 2023-08-30 07:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('common', '0002_alter_bannernotification_target_and_more'),
]

operations = [
migrations.RemoveIndex(
model_name='bannernotification',
name='type_priority_index',
),
migrations.RenameField(
model_name='bannernotification',
old_name='type',
new_name='level',
),
migrations.AddIndex(
model_name='bannernotification',
index=models.Index(models.Case(models.When(level='EXCEPTION', then=models.Value(1)), models.When(level='WARNING', then=models.Value(2)), models.When(level='NORMAL', then=models.Value(3)), default=models.Value(4)), name='level_priority_index'),
),
]
10 changes: 5 additions & 5 deletions common/models/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
from django.utils import timezone
from django.utils.translation import gettext_lazy

from common.choices import BannerNotificationState, BannerNotificationTarget, BannerNotificationType
from common.choices import BannerNotificationLevel, BannerNotificationState, BannerNotificationTarget
from common.fields import ChoiceField
from common.querysets import BannerNotificationQuerySet
from common.querysets.banner_notification import BANNER_TARGET_SORT_ORDER, BANNER_TYPE_SORT_ORDER
from common.querysets.banner_notification import BANNER_LEVEL_SORT_ORDER, BANNER_TARGET_SORT_ORDER


class BannerNotification(models.Model):
name: str = models.CharField(max_length=100, null=False, blank=False, unique=True)
message: str = models.TextField(max_length=1_000, blank=True, default="")
draft: bool = models.BooleanField(default=True)
type: str = ChoiceField(choices=BannerNotificationType.choices)
level: str = ChoiceField(choices=BannerNotificationLevel.choices)
target: str = ChoiceField(choices=BannerNotificationTarget.choices)
active_from: datetime | None = models.DateTimeField(null=True, blank=True, default=None)
active_until: datetime | None = models.DateTimeField(null=True, blank=True, default=None)
Expand Down Expand Up @@ -51,8 +51,8 @@ class Meta:
base_manager_name = "objects"
indexes = [
models.Index(
BANNER_TYPE_SORT_ORDER,
name="type_priority_index",
BANNER_LEVEL_SORT_ORDER,
name="level_priority_index",
),
models.Index(
BANNER_TARGET_SORT_ORDER,
Expand Down
14 changes: 7 additions & 7 deletions common/querysets/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.db import models
from django.db.models.functions import Now

from common.choices import BannerNotificationTarget, BannerNotificationType
from common.choices import BannerNotificationLevel, BannerNotificationTarget
from permissions.helpers import can_manage_banner_notifications

from ._base import BaseQuerySet
Expand Down Expand Up @@ -48,8 +48,8 @@ def hidden(self, user: User | AnonymousUser) -> Self:

return self.none()

def order_by_type(self, desc: bool = False) -> Self:
return self.order_by_expression(alias="__type", expression=BANNER_TYPE_SORT_ORDER, desc=desc)
def order_by_level(self, desc: bool = False) -> Self:
return self.order_by_expression(alias="__level", expression=BANNER_LEVEL_SORT_ORDER, desc=desc)

def order_by_state(self, desc: bool = False) -> Self:
return self.order_by_expression(alias="__state", expression=BANNER_STATE_SORT_ORDER, desc=desc)
Expand All @@ -58,17 +58,17 @@ def order_by_target(self, desc: bool = False) -> Self:
return self.order_by_expression(alias="__target", expression=BANNER_TARGET_SORT_ORDER, desc=desc)


BANNER_TYPE_SORT_ORDER = models.Case(
BANNER_LEVEL_SORT_ORDER = models.Case(
models.When(
type=BannerNotificationType.EXCEPTION,
level=BannerNotificationLevel.EXCEPTION,
then=models.Value(1),
),
models.When(
type=BannerNotificationType.WARNING,
level=BannerNotificationLevel.WARNING,
then=models.Value(2),
),
models.When(
type=BannerNotificationType.NORMAL,
level=BannerNotificationLevel.NORMAL,
then=models.Value(3),
),
default=models.Value(4),
Expand Down
4 changes: 2 additions & 2 deletions tests/factories/banner_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.utils import timezone
from factory import fuzzy

from common.choices import BannerNotificationTarget, BannerNotificationType
from common.choices import BannerNotificationLevel, BannerNotificationTarget
from common.models import BannerNotification

from ._base import GenericDjangoModelFactory
Expand All @@ -18,7 +18,7 @@ class Meta:
name = factory.Faker("text", max_nb_chars=100)
message = factory.Faker("text", max_nb_chars=1_000)
draft = True
type = fuzzy.FuzzyChoice(BannerNotificationType.values)
level = fuzzy.FuzzyChoice(BannerNotificationLevel.values)
target = fuzzy.FuzzyChoice(BannerNotificationTarget.values)
active_from = None
active_until = None
Expand Down
28 changes: 14 additions & 14 deletions tests/test_graphql_api/test_banner_notification/test_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.utils import timezone
from freezegun import freeze_time

from common.choices import BannerNotificationTarget, BannerNotificationType
from common.choices import BannerNotificationLevel, BannerNotificationTarget
from tests.factories import BannerNotificationFactory
from tests.factories.user import UserFactory
from tests.helpers import load_content, parametrize_helper
Expand Down Expand Up @@ -368,39 +368,39 @@ def test_sort_banner_notifications_by_target(graphql, order_by, expected):
**parametrize_helper(
{
"Ascending order": OrderingParams(
order_by="type",
order_by="level",
expected=[
{"node": {"message": "3", "type": "EXCEPTION"}},
{"node": {"message": "2", "type": "WARNING"}},
{"node": {"message": "1", "type": "NORMAL"}},
{"node": {"message": "3", "level": "EXCEPTION"}},
{"node": {"message": "2", "level": "WARNING"}},
{"node": {"message": "1", "level": "NORMAL"}},
],
),
"Descending order": OrderingParams(
order_by="-type",
order_by="-level",
expected=[
{"node": {"message": "1", "type": "NORMAL"}},
{"node": {"message": "2", "type": "WARNING"}},
{"node": {"message": "3", "type": "EXCEPTION"}},
{"node": {"message": "1", "level": "NORMAL"}},
{"node": {"message": "2", "level": "WARNING"}},
{"node": {"message": "3", "level": "EXCEPTION"}},
],
),
},
)
)
def test_sort_banner_notifications_by_type(graphql, order_by, expected):
def test_sort_banner_notifications_by_level(graphql, order_by, expected):
# given:
# - There are banner notification for all types in the system
# - Notification manager user is using the system
BannerNotificationFactory.create_active(
message="1",
type=BannerNotificationType.NORMAL,
level=BannerNotificationLevel.NORMAL,
)
BannerNotificationFactory.create_active(
message="2",
type=BannerNotificationType.WARNING,
level=BannerNotificationLevel.WARNING,
)
BannerNotificationFactory.create_active(
message="3",
type=BannerNotificationType.EXCEPTION,
level=BannerNotificationLevel.EXCEPTION,
)
user = UserFactory.create_with_general_permissions(perms=["can_manage_notifications"])
graphql.force_login(user)
Expand All @@ -414,7 +414,7 @@ def test_sort_banner_notifications_by_type(graphql, order_by, expected):
edges {
node {
message
type
level
}
}
}
Expand Down

0 comments on commit c84d337

Please sign in to comment.