Refactor to make it simpler
@@ -20,7 +20,6 @@ from readthedocs.projects.models import Domain, Project
log = logging.getLogger(__name__)
ALLOWED_URLS = [
- '/api/v2/embed',
'/api/v2/footer_html',
'/api/v2/search',
'/api/v2/docsearch',
@@ -59,31 +58,43 @@ def decide_if_cors(sender, request, **kwargs): # pylint: disable=unused-argumen
if domain.exists():
return True
- for url in ALLOWED_URLS:
- if request.path_info.startswith(url):
- project_slug = request.GET.get('project', None)
- try:
- project = Project.objects.get(slug=project_slug)
- except Project.DoesNotExist:
# Check for Embed API, allowing CORS on public projects
# since they are already public
if request.path_info.startswith('/api/v2/embed'):
- unresolved = unresolve(request.GET.get('url'))
- if unresolved:
- # This is from IsAuthorizedToViewVersion, we should copy it here..
+ url = request.GET.get('url')
+ if url:
+ unresolved = unresolve(url)
+ project = unresolved.project
+ version_slug = unresolved.version_slug
+ else:
+ project_slug = request.GET.get('project', None)
+ version_slug = request.GET.get('version', None)
+ project = Project.objects.filter(slug=project_slug).first()
+
+ if project and version_slug:
+ # This is from IsAuthorizedToViewVersion,
+ # we should abstract is a bit perhaps?
has_access = (
Version.objects
.public(
user=request.user,
- project=unresolved.project,
+ project=project,
only_active=False,
)
- .filter(slug=unresolved.version_slug)
+ .filter(slug=version_slug)
.exists()
)
if has_access:
return True
+ return False
+
+ for url in ALLOWED_URLS:
+ if request.path_info.startswith(url):
+ project_slug = request.GET.get('project', None)
+ try:
+ project = Project.objects.get(slug=project_slug)
+ except Project.DoesNotExist:
log.warning(
'Invalid project passed to domain. [%s:%s]',
project_slug,
