From 7cc58dc6e1a5cbc6ee6f07933b3ee7f623e6ba2f Mon Sep 17 00:00:00 2001 From: Topvennie Date: Thu, 23 May 2024 19:29:25 +0200 Subject: [PATCH] chore: notifications --- backend/api/tasks/docker_image.py | 2 +- backend/notifications/serializers.py | 5 +++-- backend/notifications/signals.py | 8 +++++--- backend/ypovoli/settings.py | 1 + test.yml | 2 ++ 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/backend/api/tasks/docker_image.py b/backend/api/tasks/docker_image.py index 246d5934..73946f60 100644 --- a/backend/api/tasks/docker_image.py +++ b/backend/api/tasks/docker_image.py @@ -21,7 +21,7 @@ def task_docker_image_build(docker_image: DockerImage): client.images.build(path=MEDIA_ROOT, dockerfile=docker_image.file.path, tag=get_docker_image_tag(docker_image), rm=True, quiet=True, forcerm=True) docker_image.state = StateEnum.READY - except (docker.errors.APIError, docker.errors.BuildError, TypeError): + except Exception: docker_image.state = StateEnum.ERROR notification_type = NotificationType.DOCKER_IMAGE_BUILD_ERROR finally: diff --git a/backend/notifications/serializers.py b/backend/notifications/serializers.py index ab719b91..351c9c16 100644 --- a/backend/notifications/serializers.py +++ b/backend/notifications/serializers.py @@ -1,6 +1,5 @@ -from django.utils.translation import gettext as _ - from authentication.models import User +from django.utils.translation import gettext as _ from notifications.models import Notification, NotificationTemplate from rest_framework import serializers @@ -20,6 +19,7 @@ class NotificationSerializer(serializers.ModelSerializer): title = serializers.SerializerMethodField() content = serializers.SerializerMethodField() + arguments = serializers.JSONField(write_only=True) def get_content(self, notification: Notification) -> str: """Get the content from the template and arguments.""" @@ -39,4 +39,5 @@ class Meta: "created_at", "is_read", "is_sent", + "arguments" ] diff --git a/backend/notifications/signals.py b/backend/notifications/signals.py index 979184bb..15cad5e0 100644 --- a/backend/notifications/signals.py +++ b/backend/notifications/signals.py @@ -4,11 +4,11 @@ from typing import Dict, List, Union from authentication.models import User -from django.db.models.query import QuerySet from django.dispatch import Signal, receiver from django.urls import reverse from notifications.logic import schedule_send_mails from notifications.serializers import NotificationSerializer +from ypovoli.settings import TESTING notification_create = Signal() @@ -21,13 +21,15 @@ def notification_creation( arguments: Dict[str, str], **kwargs, # Required by django ) -> bool: + if TESTING: + return True + data: List[Dict[str, Union[str, int, Dict[str, str]]]] = [] for user in queryset: if user: data.append( { - "template_id": type.value, "user": reverse("user-detail", kwargs={"pk": user.id}), "arguments": arguments, } @@ -38,7 +40,7 @@ def notification_creation( if not serializer.is_valid(raise_exception=False): return False - serializer.save() + serializer.save(template_id_id=type.value) schedule_send_mails() diff --git a/backend/ypovoli/settings.py b/backend/ypovoli/settings.py index 36527c9f..223fcc2f 100644 --- a/backend/ypovoli/settings.py +++ b/backend/ypovoli/settings.py @@ -27,6 +27,7 @@ MEDIA_ROOT = os.path.normpath(os.path.join("data")) # TESTING +TESTING = environ.get("DJANGO_TESTING", "False").lower() in ["true", "1", "t"] TESTING_BASE_LINK = "http://testserver" TEST_ADMIN_DATA = { "id": "0", diff --git a/test.yml b/test.yml index 24ec2d9b..71dce008 100644 --- a/test.yml +++ b/test.yml @@ -50,6 +50,8 @@ services: context: $BACKEND_DIR dockerfile: Dockerfile.dev command: sh -c "./setup.sh && python manage.py runsslserver 0.0.0.0:8000" + environment: + - DJANGO_TESTING=true volumes: - $BACKEND_DIR:/code depends_on: