Refactor utils to NOT take request

For testability / separation of concerns
This commit is contained in:
Scot Hacker 2018-03-27 23:17:01 -07:00
parent 5b2820df37
commit 97e596f463
2 changed files with 33 additions and 18 deletions

View file

@ -1,6 +1,5 @@
import datetime
from django.contrib import messages
from django.contrib.sites.models import Site
from django.core.mail import send_mail
from django.template.loader import render_to_string
@ -8,30 +7,38 @@ from django.template.loader import render_to_string
from todo.models import Item, Comment
def toggle_done(request, items):
def toggle_done(item_ids):
"""Check for items in the mark_done POST array. If present, change status to complete.
Takes a list of task IDs. No return value.
Takes a list of task IDs. Returns list of status change strings.
"""
for item in items:
i = Item.objects.get(id=item)
_ret = []
for item_id in item_ids:
i = Item.objects.get(id=item_id)
old_state = "completed" if i.completed else "incomplete"
i.completed = not i.completed # Invert the done state, either way
new_state = "completed" if i.completed else "incomplete"
i.completed_date = datetime.datetime.now()
i.save()
messages.success(request, "Item \"{i}\" changed from {o} to {n}.".format(i=i.title, o=old_state, n=new_state))
_ret.append("Item \"{i}\" changed from {o} to {n}.".format(i=i.title, o=old_state, n=new_state))
return _ret
def toggle_deleted(request, deleted_items):
# Delete selected items
for item_id in deleted_items:
def toggle_deleted(deleted_item_ids):
"""Delete selected items. Returns list of status change strings.
"""
_ret = []
for item_id in deleted_item_ids:
i = Item.objects.get(id=item_id)
messages.success(request, "Item \"{i}\" deleted.".format(i=i.title))
_ret.append("Item \"{i}\" deleted.".format(i=i.title))
i.delete()
return _ret
def send_notify_mail(request, new_task):
def send_notify_mail(new_task):
# Send email to assignee if task is assigned to someone other than submittor.
# Unassigned tasks should not try to notify.
@ -47,14 +54,14 @@ def send_notify_mail(request, new_task):
[new_task.assigned_to.email], fail_silently=False)
def send_email_to_thread_participants(request, task):
def send_email_to_thread_participants(task, msg_body, user):
# Notify all previous commentors on a Task about a new comment.
current_site = Site.objects.get_current()
email_subject = render_to_string("todo/email/assigned_subject.txt", {'task': task})
email_body = render_to_string(
"todo/email/newcomment_body.txt",
{'task': task, 'body': request.POST['comment-body'], 'site': current_site, 'user': request.user}
{'task': task, 'body': msg_body, 'site': current_site, 'user': user}
)
# Get list of all thread participants - everyone who has commented, plus task creator.

View file

@ -137,8 +137,13 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False):
if request.POST:
# Process completed and deleted items on each POST
toggle_done(request, request.POST.getlist('toggle_done_tasks'))
toggle_deleted(request, request.POST.getlist('toggle_deleted_tasks'))
results_changed = toggle_done(request.POST.getlist('toggle_done_tasks'))
for res in results_changed:
messages.success(request, res)
results_changed = toggle_deleted(request, request.POST.getlist('toggle_deleted_tasks'))
for res in results_changed:
messages.success(request, res)
# ######################
# Add New Task Form
@ -156,7 +161,7 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False):
# Send email alert only if Notify checkbox is checked AND assignee is not same as the submitter
if "notify" in request.POST and new_task.assigned_to != request.user:
send_notify_mail(request, new_task)
send_notify_mail(new_task)
messages.success(request, "New task \"{t}\" has been added.".format(t=new_task.title))
return redirect(request.path)
@ -202,7 +207,7 @@ def task_detail(request, task_id: int) -> HttpResponse:
body=request.POST['comment-body'],
)
send_email_to_thread_participants(request, task)
send_email_to_thread_participants(task, request.POST['comment-body'], request.user)
messages.success(request, "Comment posted. Notification email sent to thread participants.")
# Save task edits
@ -218,7 +223,10 @@ def task_detail(request, task_id: int) -> HttpResponse:
# Mark complete
if request.POST.get('toggle_done'):
toggle_done(request, [task.id, ])
results_changed = toggle_done([task.id, ])
for res in results_changed:
messages.success(request, res)
return redirect('todo:task_detail', task_id=task.id,)
if task.due_date: