@@ -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,60 @@ 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 particular
1183
+ ``project``.
1184
+
1185
+ The sitemap index is generated from the project and all sub-projects.
1186
+ """
1187
+
1188
+ project = request .unresolved_domain .project
1189
+
1190
+ locations = [
1191
+ "{scheme}://{domain}/sitemap.xml" .format (
1192
+ scheme = "https" ,
1193
+ domain = project .subdomain (),
1194
+ )
1195
+ ]
1196
+ for subproject in project .related_projects .all ():
1197
+ locations .append (
1198
+ "{scheme}://{domain}/projects/{subproject}/sitemap.xml" .format (
1199
+ scheme = "https" ,
1200
+ domain = project .subdomain (),
1201
+ subproject = subproject .slug ,
1202
+ )
1203
+ )
1204
+ context = {
1205
+ "locations" : locations ,
1206
+ }
1207
+ return render (
1208
+ request ,
1209
+ "sitemap_index.xml" ,
1210
+ context ,
1211
+ content_type = "application/xml" ,
1212
+ )
1213
+
1214
+ def _get_project (self ):
1215
+ # Method used by the CDNCacheTagsMixin class.
1216
+ return self .request .unresolved_domain .project
1217
+
1218
+ def _get_version (self ):
1219
+ # This view isn't attached to a version.
1220
+ return None
1221
+
1222
+
1223
+ class ServeSitemapIndexXML (SettingsOverrideObject ):
1224
+ _default_class = ServeSitemapIndexXMLBase
1225
+
1226
+
1167
1227
class ServeStaticFiles (CDNCacheControlMixin , CDNCacheTagsMixin , ServeDocsMixin , View ):
1168
1228
1169
1229
"""
0 commit comments