diff --git a/quackscape/users/templates/users/category.html b/quackscape/users/templates/users/category.html index 41497e6..23cc7f7 100644 --- a/quackscape/users/templates/users/category.html +++ b/quackscape/users/templates/users/category.html @@ -6,10 +6,10 @@ {% endif %} {% if request.user.is_superuser or request.user == category.owner %}
- Edit category - Delete category
diff --git a/quackscape/users/templates/users/generic_delete.html b/quackscape/users/templates/users/generic_delete.html new file mode 100644 index 0000000..0301f82 --- /dev/null +++ b/quackscape/users/templates/users/generic_delete.html @@ -0,0 +1,24 @@ +{% extends "users/base.html" %} +{% load crispy_forms_tags %} + +{% block content %} +
+
+
+
+
+

{{ title }}

+
+
+
+ {% csrf_token %} +

Are you sure you want to delete {{ object }}?

+ Cancel + +
+
+
+
+
+
+{% endblock %} diff --git a/quackscape/users/urls.py b/quackscape/users/urls.py index 0fdfe45..2285273 100644 --- a/quackscape/users/urls.py +++ b/quackscape/users/urls.py @@ -1,15 +1,39 @@ -from .views import UserAreaMainView, CategoriesView, CategoryView, FileUploadView, Login, Logout, CategoryCreateView +from .views import ( + UserAreaMainView, + CategoriesView, + CategoryView, + FileUploadView, + Login, + Logout, + CategoryCreateView, + CategoryDeleteView, + CategoryUpdateView, +) from django.urls import path -app_name = 'quackscape.users' +app_name = "quackscape.users" urlpatterns = [ - path('', UserAreaMainView.as_view(), name='user-area-main'), - path('categories/', CategoriesView.as_view(), name='categories'), - path('category/create/', CategoryCreateView.as_view(), name='category-create'), - path('category//', CategoryView.as_view(), name='category'), - path('category//upload/', FileUploadView.as_view(), name='media-upload'), - path('login/', Login.as_view(), name='login'), - path('logout/', Logout.as_view(), name='logout'), -] \ No newline at end of file + path("", UserAreaMainView.as_view(), name="user-area-main"), + path("categories/", CategoriesView.as_view(), name="categories"), + path("category/create/", CategoryCreateView.as_view(), name="category-create"), + path("category//", CategoryView.as_view(), name="category"), + path( + "category//update/", + CategoryUpdateView.as_view(), + name="category-update", + ), + path( + "category//delete/", + CategoryDeleteView.as_view(), + name="category-delete", + ), + path( + "category//upload/", + FileUploadView.as_view(), + name="media-upload", + ), + path("login/", Login.as_view(), name="login"), + path("logout/", Logout.as_view(), name="logout"), +] diff --git a/quackscape/users/views.py b/quackscape/users/views.py index f1b5419..7db789c 100644 --- a/quackscape/users/views.py +++ b/quackscape/users/views.py @@ -88,6 +88,43 @@ class CategoryCreateView(LoginRequiredMixin, TitleMixin, CreateView): ) +class CategoryUpdateView(LoginRequiredMixin, TitleMixin, UpdateView): + template_name = "users/generic_form.html" + title = "Update Category" + form_class = CategoryForm + model = Category + + def get_object(self): + try: + category = Category.objects.get(id=self.kwargs["category"]) + assert category.user_has_permission(self.request.user) + return category + except (Category.DoesNotExist, AssertionError): + raise Http404() + + def get_success_url(self): + return reverse_lazy( + "quackscape.users:category", kwargs={"category": self.object.id} + ) + + +class CategoryDeleteView(LoginRequiredMixin, TitleMixin, DeleteView): + template_name = "users/generic_delete.html" + title = "Delete Category" + model = Category + + def get_object(self): + try: + category = Category.objects.get(id=self.kwargs["category"]) + assert category.user_has_permission(self.request.user) + return category + except (Category.DoesNotExist, AssertionError): + raise Http404() + + def get_success_url(self): + return reverse_lazy("quackscape.users:categories") + + class FileUploadView(LoginRequiredMixin, GenericAPIView): parser_classes = (MultiPartParser, FormParser)