From 07e02b56f70a51049e9f4bf578334f52c513c5dd Mon Sep 17 00:00:00 2001 From: Scot Hacker Date: Sat, 24 Mar 2018 12:24:49 -0700 Subject: [PATCH] Consolidate forms for task creation and editing --- todo/forms.py | 23 +--- todo/templates/todo/include/task_edit.html | 54 +++++++++ todo/templates/todo/list_detail.html | 48 +------- todo/templates/todo/list_lists.html | 1 + todo/templates/todo/task_detail.html | 129 ++++++--------------- todo/views.py | 50 ++++---- 6 files changed, 128 insertions(+), 177 deletions(-) create mode 100644 todo/templates/todo/include/task_edit.html diff --git a/todo/forms.py b/todo/forms.py index bca5925..756e5c8 100644 --- a/todo/forms.py +++ b/todo/forms.py @@ -19,16 +19,17 @@ class AddTaskListForm(ModelForm): exclude = [] -class AddItemForm(ModelForm): +class AddEditItemForm(ModelForm): """The picklist showing the users to which a new task can be assigned must find other members of the groups the current list belongs to.""" - def __init__(self, task_list, *args, **kwargs): + def __init__(self, user, *args, **kwargs): super().__init__(*args, **kwargs) - self.fields['assigned_to'].queryset = get_user_model().objects.filter(groups__in=[task_list.group]) + self.fields['assigned_to'].queryset = get_user_model().objects.filter(groups__in=user.groups.all()) self.fields['assigned_to'].label_from_instance = lambda obj: "%s (%s)" % (obj.get_full_name(), obj.username) self.fields['assigned_to'].widget.attrs = { 'id': 'id_assigned_to', 'class': "custom-select mb-3", 'name': 'assigned_to'} + self.fields['task_list'].value = kwargs['initial']['task_list'].id due_date = forms.DateField( widget=forms.DateInput(attrs={'type': 'date'}), required=False) @@ -44,22 +45,6 @@ class AddItemForm(ModelForm): exclude = [] -class EditItemForm(ModelForm): - """The picklist showing the users to which a new task can be assigned - must find other members of the groups the current list belongs to.""" - - def __init__(self, *args, **kwargs): - super(EditItemForm, self).__init__(*args, **kwargs) - self.fields['assigned_to'].queryset = get_user_model().objects.filter( - groups__in=[self.instance.task_list.group]) - - due_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}), required=False) - - class Meta: - model = Item - exclude = ('created_date', 'created_by',) - - class AddExternalItemForm(ModelForm): """Form to allow users who are not part of the GTD system to file a ticket.""" diff --git a/todo/templates/todo/include/task_edit.html b/todo/templates/todo/include/task_edit.html new file mode 100644 index 0000000..8139c16 --- /dev/null +++ b/todo/templates/todo/include/task_edit.html @@ -0,0 +1,54 @@ +{# Form used by both Add Task and Edit Task views #} + +
+ {% csrf_token %} + +
+
+ + +
+ +
+ + + + Describe the task or bug. Provide steps to reproduce the issue. + +
+ +
+ + +
+ +
+ + {# See todo.forms.AddItemForm #} + {{form.assigned_to}} +
+ +
+ + + + Email notifications will only be sent if task is assigned to someone other than yourself. + +
+ + + + + + +

+ +

+ +
+
diff --git a/todo/templates/todo/list_detail.html b/todo/templates/todo/list_detail.html index 9b4e499..e8937f2 100644 --- a/todo/templates/todo/list_detail.html +++ b/todo/templates/todo/list_detail.html @@ -6,49 +6,11 @@ {% block content %} {% if list_slug != "mine" %} -
- {% csrf_token %} + - - -
-
- - -
- -
- - - Describe the task or bug. Provide steps to reproduce the issue. -
- -
- - -
- -
- - {# See todo.forms.AddItemForm #} - {{form.assigned_to}} -
- -
- - - Email notifications will only be sent if task is assigned to someone other than yourself. -
- - - - - -

-
-
+ {# Task edit / new task form #} + {% include 'todo/include/task_edit.html' %}
{% endif %} @@ -74,6 +36,7 @@ Comm Del + {% for task in items %} @@ -112,6 +75,7 @@ + {% include 'todo/include/toggle_delete.html' %} diff --git a/todo/templates/todo/list_lists.html b/todo/templates/todo/list_lists.html index 92bcac3..88c0a09 100644 --- a/todo/templates/todo/list_lists.html +++ b/todo/templates/todo/list_lists.html @@ -1,6 +1,7 @@ {% extends "todo/base.html" %} {% block title %}{{ list_title }} Todo Lists{% endblock %} + {% block content %}

Todo Lists

diff --git a/todo/templates/todo/task_detail.html b/todo/templates/todo/task_detail.html index c150b6d..8986da0 100644 --- a/todo/templates/todo/task_detail.html +++ b/todo/templates/todo/task_detail.html @@ -4,6 +4,9 @@ {% block content %} + +
@@ -15,88 +18,44 @@
-
    -
  • - Assigned to: - {% if task.assigned_to %} - {{ task.assigned_to.get_full_name }} - {% else %} - Anyone - {% endif %} -
  • -
  • - Reported by: {{ task.created_by.get_full_name }} -
  • -
  • - Due date: {{ task.due_date }} -
  • -
  • - Completed: {{ task.completed|yesno:"Yes,No" }} -
  • -
  • - In list: - - {{ task.task_list }} - -
  • -
-
-
- - - - {% comment %} Needs form tag {% endcomment %} -
-

Edit Task

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Title:{{ form.title }} -
List:{{ form.task_list }} -
Due:{{ form.due_date }} -
Assigned to:{{ form.assigned_to }} -
Note:{{ form.note }} -
Priority:{{ form.priority }} -
-

+
    +
  • + Assigned to: + {% if task.assigned_to %} {{ task.assigned_to.get_full_name }} {% else %} Anyone {% endif %} +
  • +
  • + Reported by: {{ task.created_by.get_full_name }} +
  • +
  • + Due date: {{ task.due_date }} +
  • +
  • + Completed: {{ task.completed|yesno:"Yes,No" }} +
  • +
  • + In list: + + {{ task.task_list }} + +
  • +
-
+
-

Add comment

- -

+
+ {# Task edit / new task form #} + {% include 'todo/include/task_edit.html' %} +
- +

Add comment

+
+ {% csrf_token %} +
+ +
+ +
{% if comment_list %} @@ -116,17 +75,3 @@
{% endblock %} - -{% block extra_js %} - -{% endblock extra_js %} diff --git a/todo/views.py b/todo/views.py index 0214c27..a79e437 100644 --- a/todo/views.py +++ b/todo/views.py @@ -14,7 +14,7 @@ from django.template.loader import render_to_string from django.views.decorators.csrf import csrf_exempt from todo import settings -from todo.forms import AddTaskListForm, AddItemForm, EditItemForm, AddExternalItemForm, SearchForm +from todo.forms import AddTaskListForm, AddEditItemForm, AddExternalItemForm, SearchForm from todo.models import Item, TaskList, Comment from todo.utils import ( toggle_done, @@ -126,10 +126,11 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False): # Add New Task Form # ###################### - if request.POST.getlist('add_task'): - form = AddItemForm(task_list, request.POST, initial={ + if request.POST.getlist('add_edit_task'): + form = AddEditItemForm(request.user, request.POST, initial={ 'assigned_to': request.user.id, 'priority': 999, + 'task_list': task_list }) if form.is_valid(): @@ -144,9 +145,10 @@ def list_detail(request, list_id=None, list_slug=None, view_completed=False): else: # Don't allow adding new tasks on some views if list_slug not in ["mine", "recent-add", "recent-complete", ]: - form = AddItemForm(task_list=task_list, initial={ + form = AddEditItemForm(request.user, initial={ 'assigned_to': request.user.id, 'priority': 999, + 'task_list': task_list }) context = { @@ -174,33 +176,33 @@ def task_detail(request, task_id: int) -> HttpResponse: if task.task_list.group not in request.user.groups.all() and not request.user.is_staff: raise PermissionDenied - if request.POST: - form = EditItemForm(request.POST, instance=task) + # Save submitted comments + if request.POST.get('add_comment'): + Comment.objects.create( + author=request.user, + task=task, + body=request.POST['comment-body'], + ) + + send_email_to_thread_participants(request, task) + messages.success(request, "Comment posted. Notification email sent to thread participants.") + + # Save task edits + if request.POST.get('add_edit_task'): + form = AddEditItemForm(request.user, request.POST, instance=task, initial={'task_list': task.task_list}) if form.is_valid(): form.save() - - # Also save submitted comment, if non-empty - if request.POST['comment-body']: - c = Comment( - author=request.user, - task=task, - body=request.POST['comment-body'], - ) - c.save() - - send_email_to_thread_participants(request, task) - messages.success(request, "Notification email sent to thread participants.") - messages.success(request, "The task has been edited.") return redirect('todo:list_detail', list_id=task.task_list.id, list_slug=task.task_list.slug) else: - form = EditItemForm(instance=task) - if task.due_date: - thedate = task.due_date - else: - thedate = datetime.datetime.now() + form = AddEditItemForm(request.user, instance=task, initial={'task_list': task.task_list}) + + if task.due_date: + thedate = task.due_date + else: + thedate = datetime.datetime.now() context = { "task": task,