Formatting

This commit is contained in:
Scot Hacker 2019-04-06 16:39:34 -07:00
parent 276ead54e7
commit 9a5c794c41

View file

@ -1,14 +1,16 @@
import email.utils import email.utils
import functools
import time import time
import logging
from django.conf import settings from django.conf import settings
from django.contrib.sites.models import Site from django.contrib.sites.models import Site
from django.core import mail from django.core import mail
from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.template.loader import render_to_string from django.template.loader import render_to_string
from todo.models import Comment, Task from todo.models import Comment, Task
log = logging.getLogger(__name__)
def staff_check(user): def staff_check(user):
"""If TODO_STAFF_ONLY is set to True, limit view access to staff users only. """If TODO_STAFF_ONLY is set to True, limit view access to staff users only.
@ -28,7 +30,7 @@ def user_can_read_task(task, user):
def todo_get_backend(task): def todo_get_backend(task):
'''returns a mail backend for some task''' """Returns a mail backend for some task"""
mail_backends = getattr(settings, "TODO_MAIL_BACKENDS", None) mail_backends = getattr(settings, "TODO_MAIL_BACKENDS", None)
if mail_backends is None: if mail_backends is None:
return None return None
@ -41,7 +43,7 @@ def todo_get_backend(task):
def todo_get_mailer(user, task): def todo_get_mailer(user, task):
"""a mailer is a (from_address, backend) pair""" """A mailer is a (from_address, backend) pair"""
task_backend = todo_get_backend(task) task_backend = todo_get_backend(task)
if task_backend is None: if task_backend is None:
return (None, mail.get_connection) return (None, mail.get_connection)
@ -52,19 +54,13 @@ def todo_get_mailer(user, task):
def todo_send_mail(user, task, subject, body, recip_list): def todo_send_mail(user, task, subject, body, recip_list):
'''Send an email attached to task, triggered by user''' """Send an email attached to task, triggered by user"""
references = Comment.objects.filter(task=task).only('email_message_id') references = Comment.objects.filter(task=task).only("email_message_id")
references = (ref.email_message_id for ref in references) references = (ref.email_message_id for ref in references)
references = ' '.join(filter(bool, references)) references = " ".join(filter(bool, references))
from_address, backend = todo_get_mailer(user, task) from_address, backend = todo_get_mailer(user, task)
message_hash = hash(( message_hash = hash((subject, body, from_address, frozenset(recip_list), references))
subject,
body,
from_address,
frozenset(recip_list),
references,
))
message_id = ( message_id = (
# the task_id enables attaching back notification answers # the task_id enables attaching back notification answers
@ -76,14 +72,14 @@ def todo_send_mail(user, task, subject, body, recip_list):
task_id=task.pk, task_id=task.pk,
# avoid the -hexstring case (hashes can be negative) # avoid the -hexstring case (hashes can be negative)
message_hash=abs(message_hash), message_hash=abs(message_hash),
epoch=int(time.time()) epoch=int(time.time()),
) )
# the thread message id is used as a common denominator between all # the thread message id is used as a common denominator between all
# notifications for some task. This message doesn't actually exist, # notifications for some task. This message doesn't actually exist,
# it's just there to make threading possible # it's just there to make threading possible
thread_message_id = "<thread-{}@django-todo>".format(task.pk) thread_message_id = "<thread-{}@django-todo>".format(task.pk)
references = '{} {}'.format(references, thread_message_id) references = "{} {}".format(references, thread_message_id)
with backend() as connection: with backend() as connection:
message = mail.EmailMessage( message = mail.EmailMessage(
@ -91,12 +87,12 @@ def todo_send_mail(user, task, subject, body, recip_list):
body, body,
from_address, from_address,
recip_list, recip_list,
[], # Bcc [], # Bcc
headers={ headers={
**getattr(backend, 'headers', {}), **getattr(backend, "headers", {}),
'Message-ID': message_id, "Message-ID": message_id,
'References': references, "References": references,
'In-reply-to': thread_message_id, "In-reply-to": thread_message_id,
}, },
connection=connection, connection=connection,
) )
@ -104,10 +100,10 @@ def todo_send_mail(user, task, subject, body, recip_list):
def send_notify_mail(new_task): def send_notify_mail(new_task):
''' """
Send email to assignee if task is assigned to someone other than submittor. Send email to assignee if task is assigned to someone other than submittor.
Unassigned tasks should not try to notify. Unassigned tasks should not try to notify.
''' """
if new_task.assigned_to == new_task.created_by: if new_task.assigned_to == new_task.created_by:
return return
@ -123,15 +119,12 @@ def send_notify_mail(new_task):
def send_email_to_thread_participants(task, msg_body, user, subject=None): def send_email_to_thread_participants(task, msg_body, user, subject=None):
'''Notify all previous commentors on a Task about a new comment.''' """Notify all previous commentors on a Task about a new comment."""
current_site = Site.objects.get_current() current_site = Site.objects.get_current()
email_subject = subject email_subject = subject
if not subject: if not subject:
subject = render_to_string( subject = render_to_string("todo/email/assigned_subject.txt", {"task": task})
"todo/email/assigned_subject.txt",
{"task": task}
)
email_body = render_to_string( email_body = render_to_string(
"todo/email/newcomment_body.txt", "todo/email/newcomment_body.txt",
@ -140,11 +133,7 @@ def send_email_to_thread_participants(task, msg_body, user, subject=None):
# Get all thread participants # Get all thread participants
commenters = Comment.objects.filter(task=task) commenters = Comment.objects.filter(task=task)
recip_list = set( recip_list = set(ca.author.email for ca in commenters if ca.author is not None)
ca.author.email
for ca in commenters
if ca.author is not None
)
for related_user in (task.created_by, task.assigned_to): for related_user in (task.created_by, task.assigned_to):
if related_user is not None: if related_user is not None:
recip_list.add(related_user.email) recip_list.add(related_user.email)
@ -154,12 +143,13 @@ def send_email_to_thread_participants(task, msg_body, user, subject=None):
def toggle_task_completed(task_id: int) -> bool: def toggle_task_completed(task_id: int) -> bool:
"""Toggle the `completed` bool on Task from True to False or vice versa."""
try: try:
task = Task.objects.get(id=task_id) task = Task.objects.get(id=task_id)
task.completed = not task.completed task.completed = not task.completed
task.save() task.save()
return True return True
except Task.DoesNotExist: except Task.DoesNotExist:
# FIXME proper log message log.info(f"Task {task_id} not found.")
print("task not found")
return False return False