Debian Codegraph @

django/contrib/sitemaps/__init__.py

raw
1 from django.apps import apps as django_apps
2 from django.conf import settings
3 from django.core import paginator
4 from django.core.exceptions import ImproperlyConfigured
5 from django.utils import translation
6
7
8 class Sitemap:
9 # This limit is defined by Google. See the index documentation at
10 # https://www.sitemaps.org/protocol.html#index.
11 limit = 50000
12
13 # If protocol is None, the URLs in the sitemap will use the protocol
14 # with which the sitemap was requested.
15 protocol = None
16
17 # Enables generating URLs for all languages.
18 i18n = False
19
20 # Override list of languages to use.
21 languages = None
22
23 # Enables generating alternate/hreflang links.
24 alternates = False
25
26 # Add an alternate/hreflang link with value 'x-default'.
27 x_default = False
28
29 def _get(self, name, item, default=None):
30 try:
31 attr = getattr(self, name)
32 except AttributeError:
33 return default
34 if callable(attr):
35 if self.i18n:
36 # Split the (item, lang_code) tuples again for the location,
37 # priority, lastmod and changefreq method calls.
38 item, lang_code = item
39 return attr(item)
40 return attr
41
42 def get_languages_for_item(self, item):
43 """Languages for which this item is displayed."""
44 return self._languages()
45
46 def _languages(self):
47 if self.languages is not None:
48 return self.languages
49 return [lang_code for lang_code, _ in settings.LANGUAGES]
50
51 def _items(self):
52 if self.i18n:
53 # Create (item, lang_code) tuples for all items and languages.
54 # This is necessary to paginate with all languages already considered.
55 items = [
56 (item, lang_code)
57 for item in self.items()
58 for lang_code in self.get_languages_for_item(item)
59 ]
60 return items
61 return self.items()
62
63 def _location(self, item, force_lang_code=None):
64 if self.i18n:
65 obj, lang_code = item
66 # Activate language from item-tuple or forced one before calling location.
67 with translation.override(force_lang_code or lang_code):
68 return self._get("location", item)
69 return self._get("location", item)
70
71 @property
72 def paginator(self):
73 return paginator.Paginator(self._items(), self.limit)
74
75 def items(self):
76 return []
77
78 def location(self, item):
79 return item.get_absolute_url()
80
81 def get_protocol(self, protocol=None):
82 # Determine protocol
83 return self.protocol or protocol or "https"
84
85 def get_domain(self, site=None):
86 # Determine domain
87 if site is None:
88 if django_apps.is_installed("django.contrib.sites"):
89 Site = django_apps.get_model("sites.Site")
90 try:
91 site = Site.objects.get_current()
92 except Site.DoesNotExist:
93 pass
94 if site is None:
95 raise ImproperlyConfigured(
96 "To use sitemaps, either enable the sites framework or pass "
97 "a Site/RequestSite object in your view."
98 )
99 return site.domain
100
101 def get_urls(self, page=1, site=None, protocol=None):
102 protocol = self.get_protocol(protocol)
103 domain = self.get_domain(site)
104 return self._urls(page, protocol, domain)
105
106 def get_latest_lastmod(self):
107 if not hasattr(self, "lastmod"):
108 return None
109 if callable(self.lastmod):
110 try:
111 return max([self.lastmod(item) for item in self.items()], default=None)
112 except TypeError:
113 return None
114 else:
115 return self.lastmod
116
117 def _urls(self, page, protocol, domain):
118 urls = []
119 latest_lastmod = None
120 all_items_lastmod = True # track if all items have a lastmod
121
122 paginator_page = self.paginator.page(page)
123 for item in paginator_page.object_list:
124 loc = f"{protocol}://{domain}{self._location(item)}"
125 priority = self._get("priority", item)
126 lastmod = self._get("lastmod", item)
127
128 if all_items_lastmod:
129 all_items_lastmod = lastmod is not None
130 if all_items_lastmod and (
131 latest_lastmod is None or lastmod > latest_lastmod
132 ):
133 latest_lastmod = lastmod
134
135 url_info = {
136 "item": item,
137 "location": loc,
138 "lastmod": lastmod,
139 "changefreq": self._get("changefreq", item),
140 "priority": str(priority if priority is not None else ""),
141 "alternates": [],
142 }
143
144 if self.i18n and self.alternates:
145 item_languages = self.get_languages_for_item(item[0])
146 for lang_code in item_languages:
147 loc = f"{protocol}://{domain}{self._location(item, lang_code)}"
148 url_info["alternates"].append(
149 {
150 "location": loc,
151 "lang_code": lang_code,
152 }
153 )
154 if self.x_default and settings.LANGUAGE_CODE in item_languages:
155 lang_code = settings.LANGUAGE_CODE
156 loc = f"{protocol}://{domain}{self._location(item, lang_code)}"
157 loc = loc.replace(f"/{lang_code}/", "/", 1)
158 url_info["alternates"].append(
159 {
160 "location": loc,
161 "lang_code": "x-default",
162 }
163 )
164
165 urls.append(url_info)
166
167 if all_items_lastmod and latest_lastmod:
168 self.latest_lastmod = latest_lastmod
169
170 return urls
171
172
173 class GenericSitemap(Sitemap):
174 priority = None
175 changefreq = None
176
177 def __init__(self, info_dict, priority=None, changefreq=None, protocol=None):
178 self.queryset = info_dict["queryset"]
179 self.date_field = info_dict.get("date_field")
180 self.priority = self.priority or priority
181 self.changefreq = self.changefreq or changefreq
182 self.protocol = self.protocol or protocol
183
184 def items(self):
185 # Make sure to return a clone; we don't want premature evaluation.
186 return self.queryset.filter()
187
188 def lastmod(self, item):
189 if self.date_field is not None:
190 return getattr(item, self.date_field)
191 return None
192
193 def get_latest_lastmod(self):
194 if self.date_field is not None:
195 return (
196 self.queryset.order_by("-" + self.date_field)
197 .values_list(self.date_field, flat=True)
198 .first()
199 )
200 return None