def folder_update(request, object_id): """Update a folder. If current user doesn't own the folder, it returns 403 error. :param object_id: id of the :class:`favorites.models.Folder` that will be updated. :template favorites/folder_add.html: - ``form`` is a :class:`favorites.forms.FolderForm`. - ``next`` value returned by :func:`favorites.views._get_next`. - ``folder`` updated :class:`favorites.models.Folder` object.""" folder = get_object_or_404(Folder, pk=object_id) # check credentials if folder.user != request.user: return HttpResponseForbidden() if request.method == 'POST': form = FolderForm(request.POST) if form.is_valid(): folder.name = form.cleaned_data['name'] folder.save() return redirect(_get_next(request)) else: form = FolderForm(initial={'name': folder.name}) next_url = _get_next(request) ctx = {'form': form, 'next': next_url, 'folder': folder} return render(request, 'favorites/folder_update.html', ctx)
def folder_add(request): """Add a folder :template favorites/folder_add.html: - ``form`` is a :class:`favorites.forms.FolderForm`. - ``next_url`` value returned by :func:`favorites.views._get_next` """ if request.method == 'POST': form = FolderForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] Folder(name=name, user=request.user).save() return redirect(_get_next(request)) else: form = FolderForm() next_url = _get_next(request) ctx = {'form': form, 'next': next_url} return render(request, 'favorites/folder_add.html', ctx)