Skip to content

Commit

Permalink
Fix linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
chloend committed Oct 3, 2024
1 parent 9f1dba4 commit ce4df31
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@


sitemaps = {
'flatpages': FlatPageSitemap,
'staticpages': StaticPageSitemap,
'wagtail': WagtailSitemap,
"flatpages": FlatPageSitemap,
"staticpages": StaticPageSitemap,
"wagtail": WagtailSitemap,
}


Expand All @@ -41,7 +41,7 @@
path("", include("lemarche.www.pages.urls")),
path("", include(wagtail_urls)),
# sitemap
path('sitemap.xml', sitemap, {'sitemaps': sitemaps}, name='sitemap'),
path("sitemap.xml", sitemap, {"sitemaps": sitemaps}, name="sitemap"),
]

if settings.DEBUG and "debug_toolbar" in settings.INSTALLED_APPS:
Expand Down
28 changes: 15 additions & 13 deletions lemarche/www/pages/sitemaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@
"2021-10-06-le-marche-fait-peau-neuve",
]


class FlatPageSitemap(Sitemap):
changefreq = "monthly"
priority = 0.6

def items(self):
return Page.objects.all()

def lastmod(self, obj):
return obj.updated_at

def location(self, obj):
return obj.get_absolute_url()


class StaticPageSitemap(Sitemap):
changefreq = "yearly"
priority = 0.4

def _is_included_view(self, callback):
"""
Check if a view has to be included in the sitemap.
Expand All @@ -32,19 +35,18 @@ def _is_included_view(self, callback):
callback: The view callable.
Returns:
bool: True if the callback is a is a ContactView,
bool: True if the callback is a is a ContactView,
HomeView, PageView, or SitemapView, False otherwise.
"""
return (
hasattr(callback, 'view_class') and
issubclass(callback.view_class, (ContactView, PageView, SitemapView))
return hasattr(callback, "view_class") and issubclass(
callback.view_class, (ContactView, PageView, SitemapView)
)

def items(self):
"""
Return a list of URL names for static pages.
This method iterates over all URL patterns, checking if a view inherits
This method iterates over all URL patterns, checking if a view inherits
from certain class views. Excluded pages defined in EXCLUDED_STATIC_PAGES
are not included in the sitemap.
"""
Expand All @@ -55,17 +57,17 @@ def items(self):
# Handle nested URLResolver patterns (e.g., 'pages' namespace)
if isinstance(pattern, URLResolver):
static_pages.extend(
f'pages:{sub_pattern.name}'
for sub_pattern in pattern.url_patterns
if isinstance(sub_pattern, URLPattern)
and self._is_included_view(sub_pattern.callback)
f"pages:{sub_pattern.name}"
for sub_pattern in pattern.url_patterns
if isinstance(sub_pattern, URLPattern)
and self._is_included_view(sub_pattern.callback)
and sub_pattern.name not in EXCLUDED_STATIC_PAGES
)
# Handle top-level URLPatterns
elif isinstance(pattern, URLPattern) and pattern.name:
if self._is_included_view(pattern.callback) and pattern.name not in EXCLUDED_STATIC_PAGES:
static_pages.append(pattern.name)

return static_pages

def location(self, item):
Expand Down
4 changes: 2 additions & 2 deletions lemarche/www/pages/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_sitemap_contains_wagtail_page(self):
def test_wagtail_page_contains_lastmod_tag(self):
"""Test the sitemap contains the lastmod tag for the wagtail page."""
response = self.client.get(reverse("sitemap"))
lastmod = self.wagtail_page.last_published_at.strftime('%Y-%m-%d')
lastmod = self.wagtail_page.last_published_at.strftime("%Y-%m-%d")

self.assertContains(response, f"<lastmod>{lastmod}</lastmod>")

Expand All @@ -218,6 +218,6 @@ def test_sitemap_contains_flatpage(self):
def test_flatpage_contains_lastmod_tag(self):
"""Test the sitemap contains the lastmod tag for the flatpage."""
response = self.client.get(reverse("sitemap"))
lastmod = self.flat_page.updated_at.strftime('%Y-%m-%d')
lastmod = self.flat_page.updated_at.strftime("%Y-%m-%d")

self.assertContains(response, f"<lastmod>{lastmod}</lastmod>")
14 changes: 7 additions & 7 deletions lemarche/www/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,24 +402,24 @@ def trigger_error(request):
class SitemapView(View):
def get(self, request):
# Get sitemap.xml content
sitemap_url = request.build_absolute_uri('/sitemap.xml')
sitemap_url = request.build_absolute_uri("/sitemap.xml")
response = requests.get(sitemap_url)

urls = []

if response.status_code == 200:
try:
# Read XML and extract URLs
root = ElementTree.fromstring(response.content)

# Define namespace to find URLs
namespace = {'ns': 'http://www.sitemaps.org/schemas/sitemap/0.9'}
namespace = {"ns": "http://www.sitemaps.org/schemas/sitemap/0.9"}

for url in root.findall('ns:url', namespaces=namespace):
loc = url.find('ns:loc', namespaces=namespace)
for url in root.findall("ns:url", namespaces=namespace):
loc = url.find("ns:loc", namespaces=namespace)
if loc is not None:
urls.append(loc.text.strip())
except ElementTree.ParseError as e:
print("Erreur d'analyse XML:", e)

return render(request, 'pages/plan_du_site.html', {'sitemap_urls': urls})
return render(request, "pages/plan_du_site.html", {"sitemap_urls": urls})

0 comments on commit ce4df31

Please sign in to comment.