@@ -265,6 +265,10 @@ def serve_path(self, request, path):
265
265
version = unresolved .version
266
266
filename = unresolved .filename
267
267
268
+ # Inject the UnresolvedURL into the HttpRequest so we can access from the middleware.
269
+ # We could resolve it again from the middleware, but we would duplicating DB queries.
270
+ request .unresolved_url = unresolved
271
+
268
272
# Check if the old language code format was used, and redirect to the new one.
269
273
# NOTE: we may have some false positives here, for example for an URL like:
270
274
# /pt-br/latest/pt_BR/index.html, but our protection for infinite redirects
@@ -391,7 +395,7 @@ def get(self, request, proxito_path):
391
395
the Docs default page (Maze Found) is rendered by Django and served.
392
396
"""
393
397
log .bind (proxito_path = proxito_path )
394
- log .debug (' Executing 404 handler.' )
398
+ log .debug (" Executing 404 handler." )
395
399
unresolved_domain = request .unresolved_domain
396
400
# We force all storage calls to use the external versions storage,
397
401
# since we are serving an external version.
@@ -420,6 +424,11 @@ def get(self, request, proxito_path):
420
424
path = proxito_path ,
421
425
append_indexhtml = False ,
422
426
)
427
+
428
+ # Inject the UnresolvedURL into the HttpRequest so we can access from the middleware.
429
+ # We could resolve it again from the middleware, but we would duplicating DB queries.
430
+ request .unresolved_url = unresolved
431
+
423
432
project = unresolved .project
424
433
version = unresolved .version
425
434
filename = unresolved .filename
@@ -708,11 +717,12 @@ def get(self, request):
708
717
# Verify if the project is marked as spam and return a custom robots.txt
709
718
if "readthedocsext.spamfighting" in settings .INSTALLED_APPS :
710
719
from readthedocsext .spamfighting .utils import is_robotstxt_denied # noqa
720
+
711
721
if is_robotstxt_denied (project ):
712
722
return render (
713
723
request ,
714
- ' robots.spam.txt' ,
715
- content_type = ' text/plain' ,
724
+ " robots.spam.txt" ,
725
+ content_type = " text/plain" ,
716
726
)
717
727
718
728
# Use the ``robots.txt`` file from the default version configured
@@ -747,33 +757,30 @@ def get(self, request):
747
757
filename = "robots.txt" ,
748
758
check_if_exists = True ,
749
759
)
750
- log .info (' Serving custom robots.txt file.' )
760
+ log .info (" Serving custom robots.txt file." )
751
761
return response
752
762
except StorageFileNotFound :
753
763
pass
754
764
755
765
# Serve default robots.txt
756
- sitemap_url = ' {scheme}://{domain}/sitemap.xml' .format (
757
- scheme = ' https' ,
766
+ sitemap_url = " {scheme}://{domain}/sitemap.xml" .format (
767
+ scheme = " https" ,
758
768
domain = project .subdomain (),
759
769
)
760
770
context = {
761
- ' sitemap_url' : sitemap_url ,
762
- ' hidden_paths' : self ._get_hidden_paths (project ),
771
+ " sitemap_url" : sitemap_url ,
772
+ " hidden_paths" : self ._get_hidden_paths (project ),
763
773
}
764
774
return render (
765
775
request ,
766
- ' robots.txt' ,
776
+ " robots.txt" ,
767
777
context ,
768
- content_type = ' text/plain' ,
778
+ content_type = " text/plain" ,
769
779
)
770
780
771
781
def _get_hidden_paths (self , project ):
772
782
"""Get the absolute paths of the public hidden versions of `project`."""
773
- hidden_versions = (
774
- Version .internal .public (project = project )
775
- .filter (hidden = True )
776
- )
783
+ hidden_versions = Version .internal .public (project = project ).filter (hidden = True )
777
784
resolver = Resolver ()
778
785
hidden_paths = [
779
786
resolver .resolve_path (project , version_slug = version .slug )
@@ -846,8 +853,8 @@ def hreflang_formatter(lang):
846
853
Use hyphen instead of underscore in language and country value.
847
854
ref: https://en.wikipedia.org/wiki/Hreflang#Common_Mistakes
848
855
"""
849
- if '_' in lang :
850
- return lang .replace ('_' , '-' )
856
+ if "_" in lang :
857
+ return lang .replace ("_" , "-" )
851
858
return lang
852
859
853
860
def changefreqs_generator ():
@@ -861,8 +868,8 @@ def changefreqs_generator():
861
868
aggressive. If the tag is removed and a branch is created with the same
862
869
name, we will want bots to revisit this.
863
870
"""
864
- changefreqs = [' weekly' , ' daily' ]
865
- yield from itertools .chain (changefreqs , itertools .repeat (' monthly' ))
871
+ changefreqs = [" weekly" , " daily" ]
872
+ yield from itertools .chain (changefreqs , itertools .repeat (" monthly" ))
866
873
867
874
project = request .unresolved_domain .project
868
875
public_versions = Version .internal .public (
@@ -879,28 +886,34 @@ def changefreqs_generator():
879
886
# We want stable with priority=1 and changefreq='weekly' and
880
887
# latest with priority=0.9 and changefreq='daily'
881
888
# More details on this: https://github.com/rtfd/readthedocs.org/issues/5447
882
- if (len (sorted_versions ) >= 2 and sorted_versions [0 ].slug == LATEST and
883
- sorted_versions [1 ].slug == STABLE ):
884
- sorted_versions [0 ], sorted_versions [1 ] = sorted_versions [1 ], sorted_versions [0 ]
889
+ if (
890
+ len (sorted_versions ) >= 2
891
+ and sorted_versions [0 ].slug == LATEST
892
+ and sorted_versions [1 ].slug == STABLE
893
+ ):
894
+ sorted_versions [0 ], sorted_versions [1 ] = (
895
+ sorted_versions [1 ],
896
+ sorted_versions [0 ],
897
+ )
885
898
886
899
versions = []
887
900
for version , priority , changefreq in zip (
888
- sorted_versions ,
889
- priorities_generator (),
890
- changefreqs_generator (),
901
+ sorted_versions ,
902
+ priorities_generator (),
903
+ changefreqs_generator (),
891
904
):
892
905
element = {
893
- ' loc' : version .get_subdomain_url (),
894
- ' priority' : priority ,
895
- ' changefreq' : changefreq ,
896
- ' languages' : [],
906
+ " loc" : version .get_subdomain_url (),
907
+ " priority" : priority ,
908
+ " changefreq" : changefreq ,
909
+ " languages" : [],
897
910
}
898
911
899
912
# Version can be enabled, but not ``built`` yet. We want to show the
900
913
# link without a ``lastmod`` attribute
901
- last_build = version .builds .order_by (' -date' ).first ()
914
+ last_build = version .builds .order_by (" -date" ).first ()
902
915
if last_build :
903
- element [' lastmod' ] = last_build .date .isoformat ()
916
+ element [" lastmod" ] = last_build .date .isoformat ()
904
917
905
918
resolver = Resolver ()
906
919
if project .translations .exists ():
@@ -915,27 +928,31 @@ def changefreqs_generator():
915
928
project = translation ,
916
929
version = translated_version ,
917
930
)
918
- element ['languages' ].append ({
919
- 'hreflang' : hreflang_formatter (translation .language ),
920
- 'href' : href ,
921
- })
931
+ element ["languages" ].append (
932
+ {
933
+ "hreflang" : hreflang_formatter (translation .language ),
934
+ "href" : href ,
935
+ }
936
+ )
922
937
923
938
# Add itself also as protocol requires
924
- element ['languages' ].append ({
925
- 'hreflang' : project .language ,
926
- 'href' : element ['loc' ],
927
- })
939
+ element ["languages" ].append (
940
+ {
941
+ "hreflang" : project .language ,
942
+ "href" : element ["loc" ],
943
+ }
944
+ )
928
945
929
946
versions .append (element )
930
947
931
948
context = {
932
- ' versions' : versions ,
949
+ " versions" : versions ,
933
950
}
934
951
return render (
935
952
request ,
936
- ' sitemap.xml' ,
953
+ " sitemap.xml" ,
937
954
context ,
938
- content_type = ' application/xml' ,
955
+ content_type = " application/xml" ,
939
956
)
940
957
941
958
def _get_project (self ):
0 commit comments