Skip to content

Commit

Permalink
tech(partner): reference in sitemap.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Aug 29, 2024
1 parent 306fe2a commit 6d3f9e8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
12 changes: 12 additions & 0 deletions lacommunaute/pages/sitemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from lacommunaute.forum.models import Forum
from lacommunaute.forum_conversation.models import Topic
from lacommunaute.partner.models import Partner


class PagesSitemap(Sitemap):
Expand Down Expand Up @@ -35,3 +36,14 @@ def items(self):

def lastmod(self, obj: Model) -> str:
return obj.last_post_on


class PartnerSitemap(Sitemap):
def items(self):
return Partner.objects.all()

def location(self, obj: Model) -> str:
return reverse("partner:detail", kwargs={"slug": obj.slug, "pk": obj.pk})

def lastmod(self, obj: Model) -> str:
return obj.updated
28 changes: 14 additions & 14 deletions lacommunaute/pages/tests/test_sitemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from lacommunaute.forum.factories import ForumFactory
from lacommunaute.forum_conversation.factories import TopicFactory
from lacommunaute.partner.factories import PartnerFactory


def test_sitemap(client, db):
Expand All @@ -14,22 +15,21 @@ def test_sitemap(client, db):
assert "sitemap.xml" in response.templates[0].name


def test_topic_is_in_sitemap(client, db):
topic = TopicFactory(with_post=True)
url = reverse("pages:django.contrib.sitemaps.views.sitemap")
response = client.get(url)
assert response.status_code == 200
assert topic.get_absolute_url() in response.content.decode()
assert f"<lastmod>{topic.last_post_on.strftime('%Y-%m-%d')}</lastmod>" in response.content.decode()
@pytest.mark.parametrize(
"factory, factory_kwargs,lastmod_field",
[
(TopicFactory, {"with_post": True}, "last_post_on"),
(ForumFactory, {}, "updated"),
(PartnerFactory, {}, "updated"),
],
)
def test_objects_are_in_sitemap(client, db, factory, factory_kwargs, lastmod_field):
obj = factory(**factory_kwargs)
response = client.get(reverse("pages:django.contrib.sitemaps.views.sitemap"))


def test_forum_is_in_sitemap(client, db):
forum = ForumFactory()
url = reverse("pages:django.contrib.sitemaps.views.sitemap")
response = client.get(url)
assert response.status_code == 200
assert reverse("forum_extension:forum", kwargs={"pk": forum.pk, "slug": forum.slug}) in response.content.decode()
assert f"<lastmod>{forum.updated.strftime('%Y-%m-%d')}</lastmod>" in response.content.decode()
assert obj.get_absolute_url() in response.content.decode()
assert f"<lastmod>{getattr(obj, lastmod_field).strftime('%Y-%m-%d')}</lastmod>" in response.content.decode()


def test_flatpage_is_in_sitemap(client, db):
Expand Down
1 change: 1 addition & 0 deletions lacommunaute/pages/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"pages": sitemaps.PagesSitemap,
"forum": sitemaps.ForumSitemap,
"topic": sitemaps.TopicSitemap,
"partner": sitemaps.PartnerSitemap,
}

app_name = "pages"
Expand Down

0 comments on commit 6d3f9e8

Please sign in to comment.