Replace locals() with context dicts

This commit is contained in:
Scot Hacker 2018-03-14 23:54:58 -07:00
parent 35cfd2eb39
commit 0daf9336c0
5 changed files with 80 additions and 31 deletions

View file

@ -8,7 +8,7 @@
<p>{{ item_count }} items in {{ list_count }} lists</p> <p>{{ item_count }} items in {{ list_count }} lists</p>
{% regroup list_list by group as section_list %} {% regroup lists by group as section_list %}
{% for group in section_list %} {% for group in section_list %}
<h3>{{ group.grouper }}</h3> <h3>{{ group.grouper }}</h3>

View file

@ -22,8 +22,12 @@
</a><br/> </a><br/>
<strong>Assigned to:</strong> <strong>Assigned to:</strong>
{% if task.assigned_to %}{{ task.assigned_to.get_full_name }} {% if task.assigned_to %}
{% else %}Anyone{% endif %}<br/> {{ task.assigned_to.get_full_name }}
{% else %}
Anyone
{% endif %}
<br/>
<strong>Created by:</strong> <strong>Created by:</strong>
{{ task.created_by.first_name }} {{ task.created_by.first_name }}
@ -105,7 +109,7 @@
</strong> </strong>
</p> </p>
{{ comment.body|safe|urlize|linebreaks }} {{ comment.body|safe|urlize|linebreaks }}
{% endfor %} {% endfor %}
{% else %} {% else %}
<h3>No comments (yet).</h3> <h3>No comments (yet).</h3>
{% endif %} {% endif %}

View file

@ -14,17 +14,17 @@ urlpatterns = [
{'list_slug': 'mine'}, {'list_slug': 'mine'},
name="mine"), name="mine"),
path(
'<int:list_id>/<str:list_slug>/',
views.list_detail,
name='list_detail'),
path( path(
'<int:list_id>/<str:list_slug>/completed/', '<int:list_id>/<str:list_slug>/completed/',
views.list_detail, views.list_detail,
{'view_completed': True}, {'view_completed': True},
name='list_detail_completed'), name='list_detail_completed'),
path(
'<int:list_id>/<str:list_slug>/',
views.list_detail,
name='list_detail'),
path( path(
'<int:list_id>/<str:list_slug>/delete/', '<int:list_id>/<str:list_slug>/delete/',
views.del_list, views.del_list,

View file

@ -1,13 +1,29 @@
import datetime import datetime
from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import User
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.http import HttpResponse
from django.template.loader import render_to_string from django.template.loader import render_to_string
from todo.models import Item, TaskList, Comment 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): def toggle_done(request, items):
# 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.
for item in items: for item in items:

View file

@ -16,19 +16,12 @@ from django.views.decorators.csrf import csrf_exempt
from todo import settings from todo import settings
from todo.forms import AddTaskListForm, AddItemForm, EditItemForm, AddExternalItemForm, SearchForm from todo.forms import AddTaskListForm, AddItemForm, EditItemForm, AddExternalItemForm, SearchForm
from todo.models import Item, TaskList, Comment from todo.models import Item, TaskList, Comment
from todo.utils import toggle_done, toggle_deleted, send_notify_mail, send_email_to_thread_participants from todo.utils import (
toggle_done,
toggle_deleted,
def check_user_allowed(user: User) -> HttpResponse: send_notify_mail,
""" send_email_to_thread_participants,
Verifies user is logged in, and in staff if that setting is enabled. check_user_allowed)
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
@user_passes_test(check_user_allowed) @user_passes_test(check_user_allowed)
@ -45,11 +38,11 @@ def list_lists(request) -> HttpResponse:
# Superusers see all lists # Superusers see all lists
if request.user.is_superuser: if request.user.is_superuser:
list_list = TaskList.objects.all().order_by('group', 'name') lists = TaskList.objects.all().order_by('group', 'name')
else: 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 # superusers see all lists, so count shouldn't filter by just lists the admin belongs to
if request.user.is_superuser: if request.user.is_superuser:
@ -57,7 +50,15 @@ def list_lists(request) -> HttpResponse:
else: else:
item_count = Item.objects.filter(completed=0).filter(task_list__group__in=request.user.groups.all()).count() 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) @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_undone = Item.objects.filter(task_list=task_list.id, completed=False).count()
item_count_total = Item.objects.filter(task_list=task_list.id).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): def list_detail(request, list_id=None, list_slug=None, view_completed=False):
"""Display and manage items in a todo list. """Display and manage items in a todo list.
""" """
# Defaults
task_list = None
form = None
if not list_slug == "mine": if not list_slug == "mine":
task_list = get_object_or_404(TaskList, id=list_id, slug=list_slug) 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, '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) @user_passes_test(check_user_allowed)
@ -169,7 +190,7 @@ def task_detail(request, task_id: int) -> HttpResponse:
) )
c.save() 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, "Notification email sent to thread participants.")
messages.success(request, "The task has been edited.") messages.success(request, "The task has been edited.")
@ -237,7 +258,11 @@ def add_list(request) -> HttpResponse:
else: else:
form = AddTaskListForm(request.user) 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) @user_passes_test(check_user_allowed)
@ -323,4 +348,8 @@ def external_add(request) -> HttpResponse:
else: else:
form = AddExternalItemForm(initial={'priority': 999}) 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)