Skip to content

Commit

Permalink
fix sitemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
jperelli committed Sep 16, 2024
1 parent 7b6e86d commit c61c312
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 101 deletions.
102 changes: 29 additions & 73 deletions config/sitemaps.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
from django.conf import settings
from django.contrib.sitemaps import GenericSitemap
from django.contrib.sites.models import Site
from django.db.models.base import Model
from requests import get
from apps.core.models import Recorrido, Linea, Parada
from apps.catastro.models import Poi, AdministrativeArea
from apps.catastro.management.commands.update_osm import kings
import datetime
from calendar import timegm
from functools import wraps

from django.contrib.sites.shortcuts import get_current_site
from django.core.paginator import EmptyPage, PageNotAnInteger
from django.http import Http404
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.http import http_date
from django.contrib.sitemaps.views import x_robots_tag


def get_alternates(loc, sitemap_lang_code):
Expand All @@ -28,13 +18,13 @@ def get_alternates(loc, sitemap_lang_code):
# only add as alternate if this url is not the sitemap_lang_code
# (if this url is the sitemap_lang_code, then it goes directly in <loc></loc> (not alternate))
if language_default[:2] == lang_code[:2]:
alternates.append({'location': loc, 'lang': lang_code})
alternates.append({'location': loc, 'lang': 'x-default'})
alternates.append({'location': loc + '?lang=' + lang_code, 'lang': lang_code})
alternates.append({'location': loc, 'lang_code': lang_code})
alternates.append({'location': loc, 'lang_code': 'x-default'})
alternates.append({'location': loc + '?lang=' + lang_code, 'lang_code': lang_code})
else:
if language_default[:2] == lang_code[:2]:
alternates.append({'location': loc, 'lang': lang_code})
alternates.append({'location': loc, 'lang': 'x-default'})
alternates.append({'location': loc, 'lang_code': lang_code})
alternates.append({'location': loc, 'lang_code': 'x-default'})
return alternates


Expand All @@ -47,20 +37,14 @@ def get_loc(loc, sitemap_lang_code):
return loc
return loc + '?lang=' + sitemap_lang_code

def get_item_lang(item):
return next((v['lang'] for k,v in kings.items() if v['country_code'] == item.country_code), 'en')

class CBSitemap(GenericSitemap):

priority = None
changefreq = None
limit = 10000

def __init__(self, info_dict, priority=None, changefreq=None, protocol=None, lang='en'):
self.queryset = info_dict['queryset']
self.date_field = info_dict.get('date_field')
self.priority = priority
self.changefreq = changefreq
self.protocol = protocol
self.lang = lang
limit = 100

def __get(self, name, obj, default=None):
try:
Expand Down Expand Up @@ -89,60 +73,32 @@ def _urls(self, page, protocol, domain):
latest_lastmod = lastmod
url_info = {
'item': item,
'location': get_loc(loc, self.lang),
'location': get_loc(loc, get_item_lang(item)),
'lastmod': lastmod,
'changefreq': self.__get('changefreq', item),
'priority': str(priority if priority is not None else ''),
'alternates': get_alternates(loc, self.lang),
'alternates': get_alternates(loc, get_item_lang(item)),
}
if domain in loc:
urls.append(url_info)
if all_items_lastmod and latest_lastmod:
self.latest_lastmod = latest_lastmod
return urls


sitemaps = {}

for (lang_code, lang_name) in settings.LANGUAGES:
sitemaps['lineas_' + lang_code] = CBSitemap({
'queryset': Linea.objects.defer('envolvente'),
}, priority=0.6, lang=lang_code)
sitemaps['recorridos_' + lang_code] = CBSitemap({
'queryset': Recorrido.objects.defer('ruta', 'ruta_simple'),
}, priority=0.6, lang=lang_code)
sitemaps['paradas_' + lang_code] = CBSitemap({
'queryset': Parada.objects.defer('latlng'),
}, priority=0.4, lang=lang_code)
sitemaps['pois_' + lang_code] = CBSitemap({
'queryset': Poi.objects.defer('latlng'),
}, priority=0.6, lang=lang_code)
sitemaps['administrativeareas_' + lang_code] = CBSitemap({
'queryset': AdministrativeArea.objects.defer('geometry', 'geometry_simple'),
}, priority=0.6, lang=lang_code)


def getsitemaps(cc):
sitemaps = {}
cclang = next((v['lang'] for k,v in kings.items() if v['country_code'] == cc), '')[:2]
# print('CCLANG:' + cclang)
for (lang_code, lang_name) in settings.LANGUAGES:
suffix = ''
if cclang != lang_code[:2]:
suffix = '_' + lang_code
sitemaps['lineas' + suffix] = CBSitemap({
'queryset': Linea.objects.defer('envolvente').filter(country_code=cc),
}, priority=0.6, lang=lang_code)
sitemaps['recorridos' + suffix] = CBSitemap({
'queryset': Recorrido.objects.defer('ruta', 'ruta_simple').filter(country_code=cc),
}, priority=0.6, lang=lang_code)
sitemaps['paradas' + suffix] = CBSitemap({
'queryset': Parada.objects.defer('latlng').filter(country_code=cc),
}, priority=0.4, lang=lang_code)
sitemaps['pois' + suffix] = CBSitemap({
'queryset': Poi.objects.defer('latlng').filter(country_code=cc),
}, priority=0.6, lang=lang_code)
sitemaps['administrativeareas' + suffix] = CBSitemap({
'queryset': AdministrativeArea.objects.defer('geometry', 'geometry_simple').filter(country_code=cc),
}, priority=0.6, lang=lang_code)
return sitemaps
sitemaps = {
'lineas': CBSitemap({
'queryset': Linea.objects.order_by('id').defer('envolvente'),
}, priority=0.6),
'recorridos': CBSitemap({
'queryset': Recorrido.objects.order_by('id').defer('ruta', 'ruta_simple'),
}, priority=0.6),
'paradas': CBSitemap({
'queryset': Parada.objects.order_by('id').defer('latlng'),
}, priority=0.4),
'pois': CBSitemap({
'queryset': Poi.objects.order_by('id').defer('latlng'),
}, priority=0.6),
'administrativeareas': CBSitemap({
'queryset': AdministrativeArea.objects.order_by('id').defer('geometry', 'geometry_simple'),
}, priority=0.6),
}
10 changes: 2 additions & 8 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings

from django.contrib.sitemaps import views as sitemaps_views
from .sitemaps import sitemaps, getsitemaps
from .sitemaps import sitemaps
from apps.catastro.management.commands.update_osm import kings

from django.views.static import serve
Expand Down Expand Up @@ -34,7 +34,7 @@
# Ranking aka agradecimientos
re_path(r'^agradecimientos/$', agradecimientos, name='agradecimientos'),

path('sitemap.xml', sitemaps_views.index, {'sitemaps': sitemaps}),
path('sitemap.xml', sitemaps_views.index, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.index'),
path('sitemap-<section>.xml', sitemaps_views.sitemap, {'sitemaps': sitemaps}, name='django.contrib.sitemaps.views.sitemap'),

re_path(r'^api/v3/', include(api3Urls)),
Expand All @@ -48,12 +48,6 @@
]


for name,k in kings.items():
cc = k['country_code']
urlpatterns.append(path(f'{cc}/sitemap.xml', sitemaps_views.index, {'sitemaps': getsitemaps(cc), 'sitemap_url_name': f'django.contrib.sitemaps.views.sitemap-{cc}'}))
urlpatterns.append(path(f'{cc}/sitemap-<section>.xml', sitemaps_views.sitemap, {'sitemaps': getsitemaps(cc)}, name=f'django.contrib.sitemaps.views.sitemap-{cc}'))


if settings.DEBUG:
import debug_toolbar

Expand Down
20 changes: 0 additions & 20 deletions templates/sitemap.xml

This file was deleted.

0 comments on commit c61c312

Please sign in to comment.