Skip to content

Commit

Permalink
Create notifications when ADSManagerRequest is created
Browse files Browse the repository at this point in the history
  • Loading branch information
brmzkw committed Jun 6, 2024
1 parent 8b9a6cf commit e323692
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions mesads/app/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .ads_manager_administrator import * # noqa
from .ads_manager_request import * # noqa
from .ads_update_file import * # noqa
from .notifications import * # noqa

# Remove "Group" administration from admin. We do not use groups in the
# application.
Expand Down
13 changes: 13 additions & 0 deletions mesads/app/admin/notifications.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin

from mesads.app.models import Notification


@admin.register(Notification)
class NotificationAdmin(admin.ModelAdmin):
autocomplete_fields = ("user",)

list_display = (
"user",
"ads_manager_requests",
)
48 changes: 48 additions & 0 deletions mesads/app/migrations/0082_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 5.0.6 on 2024-06-06 10:33

import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("app", "0081_alter_adsuser_name_alter_adsuser_status"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Notification",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"ads_manager_requests",
models.BooleanField(
default=True,
verbose_name="Recevoir une notification lorsqu'une demande pour devenir gestionnaire ADS est créée",
),
),
(
"user",
models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
verbose_name="Utilisateur",
),
),
],
options={
"verbose_name": "Notification",
"verbose_name_plural": "Notifications",
},
),
]
21 changes: 21 additions & 0 deletions mesads/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,24 @@ def get_update_filename(self, filename):
import_output = models.TextField(
blank=True, null=False, verbose_name="Output du script d'import du fichier"
)


class Notification(models.Model):
class Meta:
verbose_name = "Notification"
verbose_name_plural = "Notifications"

def __str__(self):
return f"Notifications pour l'utilisateur {self.user.email}"

user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
verbose_name="Utilisateur",
)

ads_manager_requests = models.BooleanField(
default=True,
null=False,
verbose_name="Recevoir une notification lorsqu'une demande pour devenir gestionnaire ADS est créée",
)
7 changes: 7 additions & 0 deletions mesads/app/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
ADSUser,
ADSUpdateFile,
ADSManagerDecree,
Notification,
get_legal_filename,
validate_no_ads_declared,
)
Expand Down Expand Up @@ -271,3 +272,9 @@ def test_get_update_filename(self):
self.assertIn("ADS_UPDATES", filename)
self.assertIn(self.admin_user.email, filename)
self.assertIn("superfile.txt", filename)


class TestNotification(ClientTestCase):
def test_str(self):
notification = Notification(user=self.admin_user)
self.assertIn(self.admin_user.email, str(notification))
21 changes: 13 additions & 8 deletions mesads/app/views/ads_manager_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.exceptions import SuspiciousOperation
from django.core.mail import send_mail
from django.db.models import Count
from django.db import transaction
from django.shortcuts import get_object_or_404, redirect
from django.template.loader import render_to_string
from django.urls import reverse, reverse_lazy
Expand Down Expand Up @@ -137,6 +138,7 @@ def get_context_data(self, **kwargs):
)
return ctx

@transaction.atomic
def form_valid(self, form):
_, created = ADSManagerRequest.objects.get_or_create(
user=self.request.user,
Expand Down Expand Up @@ -185,14 +187,17 @@ def form_valid(self, form):
for administrator_user in form.cleaned_data[
"ads_manager"
].administrator.users.all():
send_mail(
email_subject,
email_content,
settings.MESADS_CONTACT_EMAIL,
[administrator_user],
fail_silently=True,
html_message=email_content_html,
)
notifications = getattr(administrator_user, "notification", None)
if not notifications or notifications.ads_manager_requests:
print("send mail to:", administrator_user)
send_mail(
email_subject,
email_content,
settings.MESADS_CONTACT_EMAIL,
[administrator_user],
fail_silently=True,
html_message=email_content_html,
)

return super().form_valid(form)

Expand Down
36 changes: 36 additions & 0 deletions mesads/app/views/test_ads_manager_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ..models import (
ADSManager,
ADSManagerRequest,
Notification,
)
from ..unittest import ClientTestCase

Expand Down Expand Up @@ -147,6 +148,41 @@ def test_create_request_commune(self):
# No new email
self.assertEqual(len(mail.outbox), 1)

def test_create_commune_notification_false(self):
"""The ADSManagerAdministrator of self.commune_melesse is linked to the
user ads_manager_administrator_35_user, which has a Notification object
set to False: no email should be sent when a request is made."""
Notification.objects.create(
user=self.ads_manager_administrator_35_user,
ads_manager_requests=False,
)
self.auth_client.post(
"/registre_ads/gestion", {"commune": self.commune_melesse.id}
)
self.assertEqual(len(mail.outbox), 0)

def test_create_commune_notification_true(self):
"""The ADSManagerAdministrator of self.commune_melesse is linked to the
user ads_manager_administrator_35_user, which has a Notification object
set to False: an email should be sent when a request is made."""
Notification.objects.create(
user=self.ads_manager_administrator_35_user,
ads_manager_requests=True,
)
self.auth_client.post(
"/registre_ads/gestion", {"commune": self.commune_melesse.id}
)
self.assertEqual(len(mail.outbox), 1)

def test_create_commune_notification_empty(self):
"""The ADSManagerAdministrator of self.commune_melesse is linked to the
user ads_manager_administrator_35_user, but no Notification is linked to the user. By default, we should send an email.
"""
self.auth_client.post(
"/registre_ads/gestion", {"commune": self.commune_melesse.id}
)
self.assertEqual(len(mail.outbox), 1)

def test_create_request_epci(self):
epci = EPCI.objects.first()
resp = self.auth_client.post("/registre_ads/gestion", {"epci": epci.id})
Expand Down

0 comments on commit e323692

Please sign in to comment.