From 0daf9336c012572aa5270e26308b16d44c4419f3 Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Wed, 14 Mar 2018 23:54:58 -0700 Subject: [PATCH] Replace locals() with context dicts --- todo/templates/todo/list_lists.html | 2 +- todo/templates/todo/task_detail.html | 10 ++-- todo/urls.py | 10 ++-- todo/utils.py | 16 ++++++ todo/views.py | 73 +++++++++++++++++++--------- 5 files changed, 80 insertions(+), 31 deletions(-) diff --git a/todo/templates/todo/list_lists.html b/todo/templates/todo/list_lists.html index 979e3f1..3256b70 100644 --- a/todo/templates/todo/list_lists.html +++ b/todo/templates/todo/list_lists.html @@ -8,7 +8,7 @@

{{ item_count }} items in {{ list_count }} lists

- {% regroup list_list by group as section_list %} + {% regroup lists by group as section_list %} {% for group in section_list %}

{{ group.grouper }}

diff --git a/todo/templates/todo/task_detail.html b/todo/templates/todo/task_detail.html index e09dde3..5037849 100644 --- a/todo/templates/todo/task_detail.html +++ b/todo/templates/todo/task_detail.html @@ -22,8 +22,12 @@
Assigned to: - {% if task.assigned_to %}{{ task.assigned_to.get_full_name }} - {% else %}Anyone{% endif %}
+ {% if task.assigned_to %} + {{ task.assigned_to.get_full_name }} + {% else %} + Anyone + {% endif %} +
Created by: {{ task.created_by.first_name }} @@ -105,7 +109,7 @@

{{ comment.body|safe|urlize|linebreaks }} - {% endfor %} + {% endfor %} {% else %}

No comments (yet).

{% endif %} diff --git a/todo/urls.py b/todo/urls.py index 41ef340..e637528 100644 --- a/todo/urls.py +++ b/todo/urls.py @@ -14,17 +14,17 @@ urlpatterns = [ {'list_slug': 'mine'}, name="mine"), - path( - '//', - views.list_detail, - name='list_detail'), - path( '//completed/', views.list_detail, {'view_completed': True}, name='list_detail_completed'), + path( + '//', + views.list_detail, + name='list_detail'), + path( '//delete/', views.del_list, diff --git a/todo/utils.py b/todo/utils.py index e476e14..342e426 100644 --- a/todo/utils.py +++ b/todo/utils.py @@ -1,13 +1,29 @@ import datetime +from django.conf import settings from django.contrib import messages +from django.contrib.auth.models import User from django.contrib.sites.models import Site from django.core.mail import send_mail +from django.http import HttpResponse from django.template.loader import render_to_string + from todo.models import Item, TaskList, Comment +def check_user_allowed(user: User) -> HttpResponse: + """ + Verifies user is logged in, and in staff if that setting is enabled. + Per-object permission checks (e.g. to view a particular list) are in the views that handle those objects. + """ + + if hasattr(settings, "STAFF_ONLY") and getattr(settings, "STAFF_ONLY"): + return user.is_authenticated and user.is_staff + else: + return user.is_authenticated + + def toggle_done(request, items): # Check for items in the mark_done POST array. If present, change status to complete. for item in items: diff --git a/todo/views.py b/todo/views.py index 82051ff..c61cc84 100644 --- a/todo/views.py +++ b/todo/views.py @@ -16,19 +16,12 @@ from django.views.decorators.csrf import csrf_exempt from todo import settings from todo.forms import AddTaskListForm, AddItemForm, EditItemForm, AddExternalItemForm, SearchForm from todo.models import Item, TaskList, Comment -from todo.utils import toggle_done, toggle_deleted, send_notify_mail, send_email_to_thread_participants - - -def check_user_allowed(user: User) -> HttpResponse: - """ - Verifies user is logged in, and in staff if that setting is enabled. - Per-object permission checks (e.g. to view a particular list) are in the views that handle those objects. - """ - - if settings.STAFF_ONLY: - return user.is_authenticated and user.is_staff - else: - return user.is_authenticated +from todo.utils import ( + toggle_done, + toggle_deleted, + send_notify_mail, + send_email_to_thread_participants, + check_user_allowed) @user_passes_test(check_user_allowed) @@ -45,11 +38,11 @@ def list_lists(request) -> HttpResponse: # Superusers see all lists if request.user.is_superuser: - list_list = TaskList.objects.all().order_by('group', 'name') + lists = TaskList.objects.all().order_by('group', 'name') else: - list_list = TaskList.objects.filter(group__in=request.user.groups.all()).order_by('group', 'name') + lists = TaskList.objects.filter(group__in=request.user.groups.all()).order_by('group', 'name') - list_count = list_list.count() + list_count = lists.count() # superusers see all lists, so count shouldn't filter by just lists the admin belongs to if request.user.is_superuser: @@ -57,7 +50,15 @@ def list_lists(request) -> HttpResponse: else: item_count = Item.objects.filter(completed=0).filter(task_list__group__in=request.user.groups.all()).count() - return render(request, 'todo/list_lists.html', locals()) + context = { + "lists": lists, + "thedate": thedate, + "searchform": searchform, + "list_count": list_count, + "item_count": item_count, + } + + return render(request, 'todo/list_lists.html', context) @user_passes_test(check_user_allowed) @@ -80,13 +81,24 @@ def del_list(request, list_id: int, list_slug: str) -> HttpResponse: item_count_undone = Item.objects.filter(task_list=task_list.id, completed=False).count() item_count_total = Item.objects.filter(task_list=task_list.id).count() - return render(request, 'todo/del_list.html', locals()) + context = { + "task_list": task_list, + "item_count_done": item_count_done, + "item_count_undone": item_count_undone, + "item_count_total": item_count_total, + } + + return render(request, 'todo/del_list.html', context) def list_detail(request, list_id=None, list_slug=None, view_completed=False): """Display and manage items in a todo list. """ + # Defaults + task_list = None + form = None + if not list_slug == "mine": task_list = get_object_or_404(TaskList, id=list_id, slug=list_slug) @@ -138,7 +150,16 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False): 'priority': 999, }) - return render(request, 'todo/list_detail.html', locals()) + context = { + "list_id": list_id, + "list_slug": list_slug, + "task_list": task_list, + "form": form, + "items": items, + "view_completed": view_completed, + } + + return render(request, 'todo/list_detail.html', context) @user_passes_test(check_user_allowed) @@ -169,7 +190,7 @@ def task_detail(request, task_id: int) -> HttpResponse: ) c.save() - send_email_to_thread_participants(request, task): + send_email_to_thread_participants(request, task) messages.success(request, "Notification email sent to thread participants.") messages.success(request, "The task has been edited.") @@ -237,7 +258,11 @@ def add_list(request) -> HttpResponse: else: form = AddTaskListForm(request.user) - return render(request, 'todo/add_list.html', locals()) + context = { + "form": form, + } + + return render(request, 'todo/add_list.html', context) @user_passes_test(check_user_allowed) @@ -323,4 +348,8 @@ def external_add(request) -> HttpResponse: else: form = AddExternalItemForm(initial={'priority': 999}) - return render(request, 'todo/add_task_external.html', locals()) + context = { + "form": form, + } + + return render(request, 'todo/add_task_external.html', context)