Notify & subscribe user when assigned to ticket

ticket: https://todo.sr.ht/~sircmpwn/todo.sr.ht/141
This commit is contained in:
Ivan Habunek 2019-01-01 15:15:20 +01:00 committed by Drew DeVault
parent c01daeeb4e
commit 1bf2bb9090
3 changed files with 67 additions and 4 deletions

View File

@ -1,12 +1,14 @@
from srht.database import db
from todosrht.tickets import assign, unassign
from .factories import UserFactory, TicketFactory
from .factories import UserFactory, TrackerFactory, TicketFactory
def test_assignment():
ticket = TicketFactory()
assigner = UserFactory()
def test_assignment(mailbox):
owner = UserFactory(username="foo")
tracker = TrackerFactory(owner=owner, name="bar")
ticket = TicketFactory(tracker=tracker, scoped_id=1, title="Hilfe!")
assigner = UserFactory(username="assigner")
assignee1 = UserFactory()
assignee2 = UserFactory()
@ -18,6 +20,12 @@ def test_assignment():
db.session.commit()
assert set(ticket.assigned_users) == {assignee1}
assert len(mailbox) == 1
assert mailbox[0].to == assignee1.email
assert mailbox[0].subject == "~foo/bar/#1: Hilfe!"
assert mailbox[0].body.startswith(
"You were assigned to ~foo/bar/#1 by ~assigner")
# Assignment is idempotent
assign(ticket, assignee1, assigner)
db.session.commit()
@ -27,6 +35,12 @@ def test_assignment():
db.session.commit()
assert set(ticket.assigned_users) == {assignee1, assignee2}
assert len(mailbox) == 2
assert mailbox[1].to == assignee2.email
assert mailbox[1].subject == "~foo/bar/#1: Hilfe!"
assert mailbox[1].body.startswith(
"You were assigned to ~foo/bar/#1 by ~assigner")
unassign(ticket, assignee1, assigner)
db.session.commit()
assert set(ticket.assigned_users) == {assignee2}
@ -39,3 +53,17 @@ def test_assignment():
unassign(ticket, assignee2, assigner)
db.session.commit()
assert ticket.assigned_users == []
# No more emails were sent
assert len(mailbox) == 2
def test_email_not_sent_when_self_assigned(mailbox):
ticket = TicketFactory()
user = UserFactory()
db.session.commit()
assign(ticket, user, user)
db.session.commit()
assert set(ticket.assigned_users) == {user}
assert len(mailbox) == 0

View File

@ -0,0 +1,8 @@
{{! vim: set ft=email }}
You were assigned to {{ ticket_path }} by {{ assigner }}.
--
View on the web:
{{root}}{{ticket_url}}

View File

@ -167,6 +167,29 @@ def get_or_create_subscription(ticket, user):
return subscription
def notify_assignee(subscription, ticket, assigner, assignee):
"""
Sends a notification email to the person who was assigned to the issue.
"""
ticket_path = "{}/{}/#{}".format(
ticket.tracker.owner.canonical_name,
ticket.tracker.name,
ticket.scoped_id)
subject = "{}: {}".format(ticket_path, ticket.title)
headers = {
"From": "~{} <{}>".format(assigner.username, notify_from),
"Sender": smtp_user,
}
context = {
"assigner": assigner.canonical_name,
"ticket_path": ticket_path,
"ticket_url": ticket_url(ticket).replace("%7E", "~") # hack
}
notify(subscription, "ticket_assigned", subject, headers, **context)
def assign(ticket, assignee, assigner):
role = "" # Role is not yet implemented
@ -185,6 +208,10 @@ def assign(ticket, assignee, assigner):
)
db.session.add(ticket_assignee)
subscription = get_or_create_subscription(ticket, assignee)
if assigner != assignee:
notify_assignee(subscription, ticket, assigner, assignee)
event = Event()
event.event_type = EventType.assigned_user
event.user_id = assigner.id