Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
chore(notifications): move notification commands to notifications app (
Browse files Browse the repository at this point in the history
  • Loading branch information
trowik authored Dec 13, 2022
1 parent e1c94c5 commit 903594e
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django.utils import timezone

from timed.employment.models import Employment
from timed.notifications.models import Notification

template = get_template("mail/notify_changed_employments.txt", using="text")

Expand Down Expand Up @@ -57,3 +58,7 @@ def handle(self, *args, **options):
headers=settings.EMAIL_EXTRA_HEADERS,
)
message.send()
Notification.objects.create(
notification_type=Notification.CHANGED_EMPLOYMENT,
sent_at=timezone.now(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from django.core.management.base import BaseCommand
from django.db.models import Q
from django.template.loader import get_template
from django.utils.timezone import now

from timed.notifications.models import Notification
from timed.projects.models import CustomerAssignee, ProjectAssignee, TaskAssignee
from timed.tracking.models import Report

Expand Down Expand Up @@ -177,3 +179,6 @@ def _notify_reviewers(self, start, end, reports, optional_message, cc):
messages.append(message)
if len(messages) > 0:
connection.send_messages(messages)
Notification.objects.create(
notification_type=Notification.REVIEWER_UNVERIFIED, sent_at=now()
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from django.core.mail import EmailMessage, get_connection
from django.core.management.base import BaseCommand
from django.template.loader import get_template
from django.utils.timezone import now

from timed.notifications.models import Notification

template = get_template("mail/notify_supervisor_shorttime.txt", using="text")

Expand Down Expand Up @@ -145,3 +148,6 @@ def _notify_supervisors(self, start, end, ratio, supervisees):
if len(mails) > 0:
connection = get_connection()
connection.send_messages(mails)
Notification.objects.create(
notification_type=Notification.SUPERVISORS_SHORTTIME, sent_at=now()
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 3.2.16 on 2022-12-12 10:08

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("notifications", "0001_initial"),
]

operations = [
migrations.AlterField(
model_name="notification",
name="notification_type",
field=models.CharField(
choices=[
("budget_check_30", "project budget exceeded 30%"),
("budget_check_70", "project budget exceeded 70%"),
("changed_employment", "recently changed employment"),
("reviewers_unverified", "reviewer has reports to verify"),
(
"supervisors_shorttime",
"supervisor has supervisees with short time",
),
("notify_accountants", "notify accountats"),
],
max_length=50,
),
),
]
8 changes: 8 additions & 0 deletions timed/notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@
class Notification(models.Model):
BUDGET_CHECK_30 = "budget_check_30"
BUDGET_CHECK_70 = "budget_check_70"
CHANGED_EMPLOYMENT = "changed_employment"
REVIEWER_UNVERIFIED = "reviewers_unverified"
SUPERVISORS_SHORTTIME = "supervisors_shorttime"
NOTIFY_ACCOUNTANTS = "notify_accountants"

NOTIFICATION_TYPE_CHOICES = [
(BUDGET_CHECK_30, "project budget exceeded 30%"),
(BUDGET_CHECK_70, "project budget exceeded 70%"),
(CHANGED_EMPLOYMENT, "recently changed employment"),
(REVIEWER_UNVERIFIED, "reviewer has reports to verify"),
(SUPERVISORS_SHORTTIME, "supervisor has supervisees with short time"),
(NOTIFY_ACCOUNTANTS, "notify accountats"),
]

NOTIFICATION_TYPES = [n for n, _ in NOTIFICATION_TYPE_CHOICES]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from django.conf import settings
from django.core.mail import EmailMultiAlternatives, get_connection
from django.template.loader import get_template, render_to_string
from django.utils.timezone import now

from timed.notifications.models import Notification


def prepare_and_send_email(project, order_duration):
Expand Down Expand Up @@ -54,3 +57,6 @@ def prepare_and_send_email(project, order_duration):

messages.append(message)
connection.send_messages(messages)
Notification.objects.create(
notification_type=Notification.REVIEWER_UNVERIFIED, sent_at=now()
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.core.management import call_command

from timed.employment.factories import EmploymentFactory
from timed.notifications.models import Notification


def test_notify_changed_employments(db, mailoutbox, freezer):
Expand All @@ -27,3 +28,4 @@ def test_notify_changed_employments(db, mailoutbox, freezer):
print(mail.body)
assert "80% {0}".format(finished.user.get_full_name()) in mail.body
assert "None 100% {0}".format(new.user.get_full_name()) in mail.body
assert Notification.objects.all().count() == 1
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.management import call_command

from timed.employment.factories import UserFactory
from timed.notifications.models import Notification
from timed.projects.factories import (
ProjectAssigneeFactory,
ProjectFactory,
Expand Down Expand Up @@ -87,6 +88,7 @@ def test_notify_reviewers(db, mailoutbox):
"toDate=2017-07-31&reviewer=%d&editable=1"
) % reviewer_work.id
assert url in mail.body
assert Notification.objects.count() == 1


@pytest.mark.freeze_time("2017-8-4")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.core.management import call_command

from timed.employment.factories import EmploymentFactory, UserFactory
from timed.notifications.models import Notification
from timed.projects.factories import TaskFactory
from timed.tracking.factories import ReportFactory

Expand Down Expand Up @@ -44,6 +45,7 @@ def test_notify_supervisors(db, mailoutbox):
supervisee.get_full_name()
)
assert expected in body
assert Notification.objects.count() == 1


def test_notify_supervisors_no_employment(db, mailoutbox):
Expand All @@ -55,3 +57,4 @@ def test_notify_supervisors_no_employment(db, mailoutbox):
call_command("notify_supervisors_shorttime")

assert len(mailoutbox) == 0
assert Notification.objects.count() == 0
Empty file.
Empty file.
2 changes: 2 additions & 0 deletions timed/subscription/tests/test_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.urls import reverse
from rest_framework import status

from timed.notifications.models import Notification
from timed.projects.factories import CustomerAssigneeFactory, ProjectFactory
from timed.subscription import factories

Expand Down Expand Up @@ -204,6 +205,7 @@ def test_order_create(
assert str(project.name) in mail.body
assert "0:30:00" in mail.body
assert url in mail.alternatives[0][0]
assert Notification.objects.count() == 1


@pytest.mark.parametrize(
Expand Down
3 changes: 2 additions & 1 deletion timed/subscription/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import decorators, exceptions, response, status, viewsets
from rest_framework_json_api.serializers import ValidationError

from timed.notifications import notify_admin
from timed.permissions import (
IsAccountant,
IsAuthenticated,
Expand All @@ -13,7 +14,7 @@
from timed.projects.filters import ProjectFilterSet
from timed.projects.models import CustomerAssignee, Project

from . import filters, models, notify_admin, serializers
from . import filters, models, serializers


class SubscriptionProjectViewSet(viewsets.ReadOnlyModelViewSet):
Expand Down

0 comments on commit 903594e

Please sign in to comment.