Refactor utils to NOT take request
For testability / separation of concerns
This commit is contained in:
parent
5b2820df37
commit
97e596f463
2 changed files with 33 additions and 18 deletions
|
@ -1,6 +1,5 @@
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
from django.contrib import messages
|
|
||||||
from django.contrib.sites.models import Site
|
from django.contrib.sites.models import Site
|
||||||
from django.core.mail import send_mail
|
from django.core.mail import send_mail
|
||||||
from django.template.loader import render_to_string
|
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
|
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.
|
"""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:
|
_ret = []
|
||||||
i = Item.objects.get(id=item)
|
for item_id in item_ids:
|
||||||
|
i = Item.objects.get(id=item_id)
|
||||||
old_state = "completed" if i.completed else "incomplete"
|
old_state = "completed" if i.completed else "incomplete"
|
||||||
i.completed = not i.completed # Invert the done state, either way
|
i.completed = not i.completed # Invert the done state, either way
|
||||||
new_state = "completed" if i.completed else "incomplete"
|
new_state = "completed" if i.completed else "incomplete"
|
||||||
i.completed_date = datetime.datetime.now()
|
i.completed_date = datetime.datetime.now()
|
||||||
i.save()
|
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):
|
def toggle_deleted(deleted_item_ids):
|
||||||
# Delete selected items
|
"""Delete selected items. Returns list of status change strings.
|
||||||
for item_id in deleted_items:
|
"""
|
||||||
|
|
||||||
|
_ret = []
|
||||||
|
for item_id in deleted_item_ids:
|
||||||
i = Item.objects.get(id=item_id)
|
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()
|
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.
|
# 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.
|
||||||
|
|
||||||
|
@ -47,14 +54,14 @@ def send_notify_mail(request, new_task):
|
||||||
[new_task.assigned_to.email], fail_silently=False)
|
[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.
|
# 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 = render_to_string("todo/email/assigned_subject.txt", {'task': task})
|
email_subject = render_to_string("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",
|
||||||
{'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.
|
# Get list of all thread participants - everyone who has commented, plus task creator.
|
||||||
|
|
|
@ -137,8 +137,13 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False):
|
||||||
|
|
||||||
if request.POST:
|
if request.POST:
|
||||||
# Process completed and deleted items on each POST
|
# Process completed and deleted items on each POST
|
||||||
toggle_done(request, request.POST.getlist('toggle_done_tasks'))
|
results_changed = toggle_done(request.POST.getlist('toggle_done_tasks'))
|
||||||
toggle_deleted(request, request.POST.getlist('toggle_deleted_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
|
# 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
|
# 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:
|
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))
|
messages.success(request, "New task \"{t}\" has been added.".format(t=new_task.title))
|
||||||
return redirect(request.path)
|
return redirect(request.path)
|
||||||
|
@ -202,7 +207,7 @@ def task_detail(request, task_id: int) -> HttpResponse:
|
||||||
body=request.POST['comment-body'],
|
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.")
|
messages.success(request, "Comment posted. Notification email sent to thread participants.")
|
||||||
|
|
||||||
# Save task edits
|
# Save task edits
|
||||||
|
@ -218,7 +223,10 @@ def task_detail(request, task_id: int) -> HttpResponse:
|
||||||
|
|
||||||
# Mark complete
|
# Mark complete
|
||||||
if request.POST.get('toggle_done'):
|
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,)
|
return redirect('todo:task_detail', task_id=task.id,)
|
||||||
|
|
||||||
if task.due_date:
|
if task.due_date:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue