Skip to content

Commit

Permalink
add CategoryUpdateView
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 24, 2024
1 parent de8782f commit c73ac10
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
75 changes: 75 additions & 0 deletions lacommunaute/documentation/tests/tests_category_update_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import pytest # noqa

from django.urls import reverse

from lacommunaute.documentation.factories import CategoryFactory
from lacommunaute.documentation.models import Category
from lacommunaute.users.factories import UserFactory


@pytest.fixture(name="category")
def fixture_category(db):
return CategoryFactory(for_snapshot=True)


@pytest.fixture(name="url")
def fixture_url(category):
return reverse("documentation:category_update", kwargs={"slug": category.slug, "pk": category.pk})


@pytest.fixture(name="superuser")
def fixture_superuser(db):
return UserFactory(is_superuser=True)


@pytest.mark.parametrize(
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)]
)
def test_user_passes_test_mixin(client, db, url, user, status_code):
if user:
client.force_login(user())
response = client.get(url)
assert response.status_code == status_code


@pytest.fixture(name="expected_context")
def fixture_expected_context(category):
return {
"title": f"Modifier la catégorie {category.name}",
"back_url": reverse("documentation:category_detail", kwargs={"slug": category.slug, "pk": category.pk}),
}


def test_view(client, db, url, superuser, expected_context):
client.force_login(superuser)
response = client.get(url)
assert response.status_code == 200
assert {k: response.context[k] for k in expected_context.keys()} == expected_context
assert response.context["form"].fields.keys() == {"name", "short_description", "description", "image"}


@pytest.fixture(name="post_data")
def fixture_post_data():
return {
"name": "New Name",
"short_description": "New Short Description",
"description": "### New Description",
}


@pytest.fixture(name="expected_values")
def fixture_expected_values(post_data):
return {
"name": post_data["name"],
"short_description": post_data["short_description"],
"_description_rendered": "<h3>New Description</h3>",
"slug": "new-name",
}


def test_update(client, db, url, superuser, post_data, expected_values):
client.force_login(superuser)
response = client.post(url, post_data)
assert response.status_code == 302
category = Category.objects.get(slug=expected_values["slug"])
assert {k: getattr(category, k) for k in expected_values.keys()} == expected_values
8 changes: 7 additions & 1 deletion lacommunaute/documentation/urls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from django.urls import path

from lacommunaute.documentation.views import CategoryCreateView, CategoryDetailView, CategoryListView
from lacommunaute.documentation.views import (
CategoryCreateView,
CategoryDetailView,
CategoryListView,
CategoryUpdateView,
)


app_name = "documentation"
Expand All @@ -10,4 +15,5 @@
path("", CategoryListView.as_view(), name="category_list"),
path("<str:slug>-<int:pk>/", CategoryDetailView.as_view(), name="category_detail"),
path("category/create/", CategoryCreateView.as_view(), name="category_create"),
path("<str:slug>-<int:pk>/update/", CategoryUpdateView.as_view(), name="category_update"),
]
11 changes: 10 additions & 1 deletion lacommunaute/documentation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from django.views.generic import DetailView, ListView
from django.views.generic.edit import CreateView
from django.views.generic.edit import CreateView, UpdateView
from taggit.models import Tag

from lacommunaute.documentation.models import Category, Document
Expand Down Expand Up @@ -57,3 +57,12 @@ def get_context_data(self, **kwargs):
"back_url": reverse("documentation:category_list"),
}
return super().get_context_data(**kwargs) | additionnal_context


class CategoryUpdateView(CategoryCreateUpdateMixin, UpdateView):
def get_context_data(self, **kwargs):
additionnal_context = {
"title": f"Modifier la catégorie {self.object.name}",
"back_url": self.object.get_absolute_url(),
}
return super().get_context_data(**kwargs) | additionnal_context

0 comments on commit c73ac10

Please sign in to comment.