You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
2.1 KiB
62 lines
2.1 KiB
import datetime
|
|
from django.conf import settings
|
|
from django.utils import timezone
|
|
from apps.article.sitemaps import BaseCanonicalSitemap
|
|
from apps.video.models import Video
|
|
|
|
|
|
class VideoSitemap(BaseCanonicalSitemap):
|
|
"""
|
|
Sitemap class for Video model.
|
|
Dynamically builds alternate (hreflang) URLs for all supported languages,
|
|
formats lastmod to strict W3C / ISO 8601 with timezone offsets, and outputs directly to XML.
|
|
"""
|
|
changefreq = "weekly"
|
|
priority = 0.6
|
|
|
|
def items(self):
|
|
# Only include active/published videos
|
|
return Video.objects.filter(status=True).order_by("-updated_at")
|
|
|
|
def lastmod(self, obj):
|
|
return obj.updated_at
|
|
|
|
def location(self, obj):
|
|
return obj.share_link
|
|
|
|
def get_urls(self, page=1, site=None, protocol=None):
|
|
urls = []
|
|
paginator_page = self.paginator.page(page)
|
|
|
|
for item in paginator_page.object_list:
|
|
alternates = []
|
|
|
|
# Dynamically build alternate URLs for all supported languages
|
|
for lang_code, lang_name in settings.LANGUAGES:
|
|
localized_url = f"{settings.DOVODI_DOMAIN}/{lang_code}/videos/{item.slug}"
|
|
alternates.append({
|
|
"location": localized_url,
|
|
"lang_code": lang_code,
|
|
})
|
|
|
|
# Safely format naive/aware lastmod datetime to strict ISO 8601 / W3C format
|
|
lastmod_dt = item.updated_at
|
|
if lastmod_dt:
|
|
if timezone.is_naive(lastmod_dt):
|
|
lastmod_dt = timezone.make_aware(lastmod_dt)
|
|
lastmod_str = lastmod_dt.isoformat()
|
|
else:
|
|
lastmod_str = None
|
|
|
|
# Construct URL metadata dictionary
|
|
url_info = {
|
|
"item": item,
|
|
"location": item.share_link,
|
|
"lastmod": lastmod_str,
|
|
"changefreq": self.changefreq,
|
|
"priority": str(self.priority),
|
|
"alternates": alternates,
|
|
}
|
|
urls.append(url_info)
|
|
|
|
return urls
|