@@ -1019,7 +1019,7 @@ class ServeSitemapXMLBase(CDNCacheControlMixin, CDNCacheTagsMixin, View):
1019
1019
# Extra cache tag to invalidate only this view if needed.
1020
1020
project_cache_tag = "sitemap.xml"
1021
1021
1022
- def get (self , request ):
1022
+ def get (self , request , subproject_slug = None ):
1023
1023
"""
1024
1024
Generate and serve a ``sitemap.xml`` for a particular ``project``.
1025
1025
@@ -1078,6 +1078,12 @@ def changefreqs_generator():
1078
1078
yield from itertools .chain (changefreqs , itertools .repeat ('monthly' ))
1079
1079
1080
1080
project = request .unresolved_domain .project
1081
+
1082
+ if subproject_slug :
1083
+ project = get_object_or_404 (
1084
+ project .subprojects , alias = subproject_slug
1085
+ ).child
1086
+
1081
1087
public_versions = Version .internal .public (
1082
1088
project = project ,
1083
1089
only_active = True ,
@@ -1164,6 +1170,59 @@ class ServeSitemapXML(SettingsOverrideObject):
1164
1170
_default_class = ServeSitemapXMLBase
1165
1171
1166
1172
1173
+ class ServeSitemapIndexXMLBase (CDNCacheControlMixin , CDNCacheTagsMixin , View ):
1174
+
1175
+ """Serve sitemap_index.xml from the domain's root."""
1176
+
1177
+ cache_response = True
1178
+ project_cache_tag = "sitemap.xml"
1179
+
1180
+ def get (self , request ):
1181
+ """
1182
+ Generate and serve a ``sitemap_index.xml`` for a ``project``.
1183
+
1184
+ The sitemap index is generated from the project and all sub-projects.
1185
+ """
1186
+
1187
+ project = request .unresolved_domain .project
1188
+
1189
+ locations = [
1190
+ "{scheme}://{domain}/sitemap.xml" .format (
1191
+ scheme = "https" ,
1192
+ domain = project .subdomain (),
1193
+ )
1194
+ ]
1195
+ for subproject in project .related_projects .all ():
1196
+ locations .append (
1197
+ "{scheme}://{domain}/projects/{subproject}/sitemap.xml" .format (
1198
+ scheme = "https" ,
1199
+ domain = project .subdomain (),
1200
+ subproject = subproject .slug ,
1201
+ )
1202
+ )
1203
+ context = {
1204
+ "locations" : locations ,
1205
+ }
1206
+ return render (
1207
+ request ,
1208
+ "sitemap_index.xml" ,
1209
+ context ,
1210
+ content_type = "application/xml" ,
1211
+ )
1212
+
1213
+ def _get_project (self ):
1214
+ # Method used by the CDNCacheTagsMixin class.
1215
+ return self .request .unresolved_domain .project
1216
+
1217
+ def _get_version (self ):
1218
+ # This view isn't attached to a version.
1219
+ return None
1220
+
1221
+
1222
+ class ServeSitemapIndexXML (SettingsOverrideObject ):
1223
+ _default_class = ServeSitemapIndexXMLBase
1224
+
1225
+
1167
1226
class ServeStaticFiles (CDNCacheControlMixin , CDNCacheTagsMixin , ServeDocsMixin , View ):
1168
1227
1169
1228
"""
0 commit comments