Add sitemaps
This commit is contained in:
parent
062c80c413
commit
5bb0801f15
4 changed files with 47 additions and 3 deletions
|
|
@ -25,7 +25,7 @@ class Domain(models.Model):
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta information for the domain model."""
|
"""Meta information for the domain model."""
|
||||||
|
|
||||||
ordering: typing.ClassVar[list[str]] = ["name"] # Example: Orders by name alphabetically
|
ordering: typing.ClassVar[list[str]] = ["name"]
|
||||||
verbose_name: str = "Domain"
|
verbose_name: str = "Domain"
|
||||||
verbose_name_plural: str = "Domains"
|
verbose_name_plural: str = "Domains"
|
||||||
|
|
||||||
|
|
@ -34,6 +34,10 @@ class Domain(models.Model):
|
||||||
if_hidden: Literal[" (hidden)", ""] = " (hidden)" if self.hidden else ""
|
if_hidden: Literal[" (hidden)", ""] = " (hidden)" if self.hidden else ""
|
||||||
return self.name + if_hidden
|
return self.name + if_hidden
|
||||||
|
|
||||||
|
def get_absolute_url(self) -> str:
|
||||||
|
"""Return the absolute URL of the domain."""
|
||||||
|
return f"/domain/{self.pk}/"
|
||||||
|
|
||||||
|
|
||||||
class Author(models.Model):
|
class Author(models.Model):
|
||||||
"""An author of an entry."""
|
"""An author of an entry."""
|
||||||
|
|
@ -48,7 +52,7 @@ class Author(models.Model):
|
||||||
"""Meta information for the author model."""
|
"""Meta information for the author model."""
|
||||||
|
|
||||||
unique_together: typing.ClassVar[list[str]] = ["name", "email", "href"]
|
unique_together: typing.ClassVar[list[str]] = ["name", "email", "href"]
|
||||||
ordering: typing.ClassVar[list[str]] = ["name"] # Example: Orders by name alphabetically
|
ordering: typing.ClassVar[list[str]] = ["name"]
|
||||||
verbose_name: str = "Author"
|
verbose_name: str = "Author"
|
||||||
verbose_name_plural: str = "Authors"
|
verbose_name_plural: str = "Authors"
|
||||||
|
|
||||||
|
|
@ -70,7 +74,7 @@ class Generator(models.Model):
|
||||||
"""Meta information for the generator model."""
|
"""Meta information for the generator model."""
|
||||||
|
|
||||||
unique_together: typing.ClassVar[list[str]] = ["name", "version", "href"]
|
unique_together: typing.ClassVar[list[str]] = ["name", "version", "href"]
|
||||||
ordering: typing.ClassVar[list[str]] = ["name"] # Example: Orders by name alphabetically
|
ordering: typing.ClassVar[list[str]] = ["name"]
|
||||||
verbose_name: str = "Feed generator"
|
verbose_name: str = "Feed generator"
|
||||||
verbose_name_plural: str = "Feed generators"
|
verbose_name_plural: str = "Feed generators"
|
||||||
|
|
||||||
|
|
@ -216,6 +220,10 @@ class Feed(models.Model):
|
||||||
"""Return string representation of the feed."""
|
"""Return string representation of the feed."""
|
||||||
return f"{self.domain} - {self.title}"
|
return f"{self.domain} - {self.title}"
|
||||||
|
|
||||||
|
def get_absolute_url(self) -> str:
|
||||||
|
"""Return the absolute URL of the feed."""
|
||||||
|
return f"/feed/{self.pk}/"
|
||||||
|
|
||||||
|
|
||||||
class Entry(models.Model):
|
class Entry(models.Model):
|
||||||
"""Each feed has multiple entries."""
|
"""Each feed has multiple entries."""
|
||||||
|
|
|
||||||
18
feeds/sitemaps.py
Normal file
18
feeds/sitemaps.py
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from django.contrib.sitemaps import Sitemap
|
||||||
|
from django.urls import reverse
|
||||||
|
|
||||||
|
|
||||||
|
class StaticViewSitemap(Sitemap):
|
||||||
|
"""Sitemap for static views."""
|
||||||
|
|
||||||
|
changefreq: str = "daily"
|
||||||
|
priority: float = 0.5
|
||||||
|
|
||||||
|
def items(self: StaticViewSitemap) -> list[str]:
|
||||||
|
"""Return all the items in the sitemap."""
|
||||||
|
return ["feeds:index", "feeds:feeds", "feeds:domains"]
|
||||||
|
|
||||||
|
def location(self, item) -> str:
|
||||||
|
return reverse(item)
|
||||||
|
|
@ -1,14 +1,25 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from django.contrib.sitemaps import GenericSitemap
|
||||||
|
from django.contrib.sitemaps.views import sitemap
|
||||||
from django.urls import URLPattern, path
|
from django.urls import URLPattern, path
|
||||||
from django.views.decorators.cache import cache_page
|
from django.views.decorators.cache import cache_page
|
||||||
|
|
||||||
from feeds import views
|
from feeds import views
|
||||||
|
from feeds.models import Domain, Feed
|
||||||
|
from feeds.sitemaps import StaticViewSitemap
|
||||||
|
|
||||||
from .views import APIView, CustomLoginView, CustomLogoutView, ProfileView, RegisterView
|
from .views import APIView, CustomLoginView, CustomLogoutView, ProfileView, RegisterView
|
||||||
|
|
||||||
app_name: str = "feeds"
|
app_name: str = "feeds"
|
||||||
|
|
||||||
|
sitemaps = {
|
||||||
|
"static": StaticViewSitemap,
|
||||||
|
"feeds": GenericSitemap({"queryset": Feed.objects.all(), "date_field": "created_at"}),
|
||||||
|
"domains": GenericSitemap({"queryset": Domain.objects.all(), "date_field": "created_at"}),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Normal pages
|
# Normal pages
|
||||||
urlpatterns: list[URLPattern] = [
|
urlpatterns: list[URLPattern] = [
|
||||||
path(route="", view=views.IndexView.as_view(), name="index"),
|
path(route="", view=views.IndexView.as_view(), name="index"),
|
||||||
|
|
@ -17,6 +28,12 @@ urlpatterns: list[URLPattern] = [
|
||||||
path(route="add", view=views.AddView.as_view(), name="add"),
|
path(route="add", view=views.AddView.as_view(), name="add"),
|
||||||
path(route="upload", view=views.UploadView.as_view(), name="upload"),
|
path(route="upload", view=views.UploadView.as_view(), name="upload"),
|
||||||
path(route="robots.txt", view=cache_page(timeout=60 * 60 * 365)(views.RobotsView.as_view()), name="robots"),
|
path(route="robots.txt", view=cache_page(timeout=60 * 60 * 365)(views.RobotsView.as_view()), name="robots"),
|
||||||
|
path(
|
||||||
|
"sitemap.xml",
|
||||||
|
sitemap,
|
||||||
|
{"sitemaps": sitemaps},
|
||||||
|
name="django.contrib.sitemaps.views.sitemap",
|
||||||
|
),
|
||||||
path(route="domains/", view=views.DomainsView.as_view(), name="domains"),
|
path(route="domains/", view=views.DomainsView.as_view(), name="domains"),
|
||||||
path(route="domain/<int:domain_id>/", view=views.DomainView.as_view(), name="domain"),
|
path(route="domain/<int:domain_id>/", view=views.DomainView.as_view(), name="domain"),
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ INSTALLED_APPS: list[str] = [
|
||||||
"django.contrib.sessions",
|
"django.contrib.sessions",
|
||||||
"django.contrib.messages",
|
"django.contrib.messages",
|
||||||
"django.contrib.staticfiles",
|
"django.contrib.staticfiles",
|
||||||
|
"django.contrib.sitemaps",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE: list[str] = [
|
MIDDLEWARE: list[str] = [
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue