Exemplo n.º 1
0
def update_todo_list_view(request: HttpRequest, pk: int) -> Union[HttpResponse, HttpResponseRedirect]:
    """
    View to update a todo list
    :param request: wsgi request
    :param pk: pk of todo list
    :return: template with form to edit todo list, or redirect to details page
    """
    todo_list = TodoListModel.objects.get(id=pk)

    context = {
        'todo_list': todo_list
    }

    if request.method == 'GET':
        context['form'] = TodoListForm(instance=todo_list)

        return render(request, 'todo/update_todo_list.html', context)
    elif request.method == 'POST':
        form = TodoListForm(data=deepcopy(request.POST), instance=todo_list)

        if form.is_valid():
            todo_list = form.save()

            return redirect(todo_list.get_absolute_url())
        else:
            context['form'] = form
            return render(request, 'todo/update_todo_list.html', {'form': form})
Exemplo n.º 2
0
 def test_todo_list_made(self):
     """Lists with a user assigned to them should pass."""
     data = {'name': 'Test'}
     form = TodoListForm(data=data)
     todo_list = form.save(commit=False)
     todo_list.user = self.user
     todo_list.save()
     self.assertEquals(todo_list.name, 'Test')
Exemplo n.º 3
0
def home(request):
    todos=Todo.objects.order_by('-created')
    if request.method == 'POST':
        action = request.POST['action'].lower()
        todo_list_form = TodoListForm(data=request.POST, todos=todos)
        if todo_list_form.is_valid():
            selected = Todo.objects.filter(id__in=todo_list_form.cleaned_data)
            actions = {'done': lambda items: items.update(is_done=True),
                       'delete': lambda items: items.delete()}
            actions.get(action)(selected)
            messages.add_message(request, messages.SUCCESS,
                                 'Items has been updated.')
        else:
            messages.add_message(request, messages.ERROR,
                                 ''.join(todo_list_form.non_field_errors()))
        return redirect('home')
    else:
        todo_form = TodoForm()
        todo_list_form = TodoListForm(todos=todos)
    context = {'todo_list_form': todo_list_form,
               'todo_form': todo_form}
    return render_to_response('todo/home.html',
                              context,
                              RequestContext(request))
Exemplo n.º 4
0
def create_list(request: HttpRequest):
    context = {'todo_lists': TodoList.objects.filter(user=request.user).all()}
    if request.method == 'POST':
        form = TodoListForm(request.POST)
        if not form.is_valid():
            context['form'] = form
            return render(request, 'create_list.html', context)
        todo_list = form.save(commit=False)
        todo_list.user = request.user
        todo_list.save()
        return redirect('view_list', todo_list.id)
    form = TodoListForm()
    context['form'] = form
    return render(request, 'create_list.html', context)
Exemplo n.º 5
0
def create_todo_list_view(request: HttpRequest) -> Union[HttpResponse, HttpResponseRedirect]:
    """
    View to create new todo lists
    :param request: wsgi request
    :return: template with form to create new list, or redirect to detail page of list
    """
    if request.method == 'GET':
        form = TodoListForm()

        return render(request, 'todo/create_todo_list.html', {'form': form})
    elif request.method == 'POST':
        form = TodoListForm(data=deepcopy(request.POST))

        if form.is_valid():
            todo_list = form.save()

            return redirect(todo_list.get_absolute_url())
        else:
            return render(request, 'todo/create_todo_list.html', {'form': form})
Exemplo n.º 6
0
 def test_todo_list_requires_user(self):
     """Lists require a user to be assigned to them."""
     data = {'name': 'Test'}
     form = TodoListForm(data=data)
     self.assertRaises(IntegrityError, lambda: form.save())