Skip to content

Commit

Permalink
setup get_absolute_url on Category model
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Sep 25, 2024
1 parent d9af09d commit a60d232
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lacommunaute/documentation/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager

from lacommunaute.forum_upvote.models import UpVote
Expand All @@ -16,6 +17,9 @@ class Meta:
def __str__(self):
return f"{self.name}"

def get_absolute_url(self):
return reverse("documentation:category_detail", kwargs={"pk": self.pk, "slug": self.slug})


class Document(Publication):
category = models.ForeignKey(Category, on_delete=models.CASCADE, related_name="documents")
Expand Down
12 changes: 2 additions & 10 deletions lacommunaute/documentation/tests/tests_category_detail_view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest # noqa

from django.urls import reverse

from lacommunaute.documentation.factories import CategoryFactory, DocumentFactory
from lacommunaute.utils.testing import parse_response_to_soup

Expand All @@ -11,11 +9,6 @@ def fixture_category():
return CategoryFactory(for_snapshot=True)


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


@pytest.mark.parametrize(
"active_tag, snapshot_name",
[
Expand All @@ -24,10 +17,9 @@ def fixture_url(category):
("unknowntag", "category_detail_view_with_unknown_tag"),
],
)
def test_category_detail_view_with_tagged_documents(client, db, url, category, active_tag, snapshot_name, snapshot):
def test_category_detail_view_with_tagged_documents(client, db, category, active_tag, snapshot_name, snapshot):
DocumentFactory(category=category, with_tags=["tag1", "tag2"], for_snapshot=True)
if active_tag:
url = f"{url}?tag={active_tag}"
url = f"{category.get_absolute_url()}?tag={active_tag}" if active_tag else category.get_absolute_url()
response = client.get(url)
assert response.status_code == 200
content = parse_response_to_soup(response, selector="main", replace_img_src=True, replace_in_href=[category])
Expand Down
4 changes: 4 additions & 0 deletions lacommunaute/documentation/tests/tests_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def test_slug_is_unique(self, db):
with pytest.raises(IntegrityError):
CategoryFactory(for_snapshot=True)

def test_get_absolute_url(self, db):
category = CategoryFactory()
assert category.get_absolute_url() == f"/documentation/{category.slug}-{category.pk}/"


class TestDocument:
def test_slug(self, db):
Expand Down

0 comments on commit a60d232

Please sign in to comment.