Skip to content

Lint: run black against all our Python files #11145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 26 additions & 40 deletions readthedocs/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def sync_versions_to_db(project, versions, type):
:returns: set of versions' slug added
"""
old_version_values = project.versions.filter(type=type).values_list(
'verbose_name',
'identifier',
"verbose_name",
"identifier",
)
old_versions = dict(old_version_values)

Expand All @@ -48,8 +48,8 @@ def sync_versions_to_db(project, versions, type):
has_user_stable = False
has_user_latest = False
for version in versions:
version_id = version['identifier']
version_name = version['verbose_name']
version_id = version["identifier"]
version_name = version["verbose_name"]
if version_name == STABLE_VERBOSE_NAME:
has_user_stable = True
created_version, created = _set_or_create_version(
Expand Down Expand Up @@ -90,7 +90,7 @@ def sync_versions_to_db(project, versions, type):
)

log.info(
'Re-syncing versions: version updated.',
"Re-syncing versions: version updated.",
version_verbose_name=version_name,
version_id=version_id,
)
Expand All @@ -101,26 +101,22 @@ def sync_versions_to_db(project, versions, type):
added.update(_create_versions(project, type, versions_to_create))

if not has_user_stable:
stable_version = (
project.versions.filter(slug=STABLE, type=type).first()
)
stable_version = project.versions.filter(slug=STABLE, type=type).first()
if stable_version:
# Put back the RTD's stable version
stable_version.machine = True
stable_version.save()
if not has_user_latest:
latest_version = (
project.versions.filter(slug=LATEST, type=type).first()
)
latest_version = project.versions.filter(slug=LATEST, type=type).first()
if latest_version:
# Put back the RTD's latest version
latest_version.machine = True
latest_version.save()
if added:
log.info(
'Re-syncing versions: versions added.',
"Re-syncing versions: versions added.",
count=len(added),
versions=','.join(itertools.islice(added, 100)),
versions=",".join(itertools.islice(added, 100)),
)
return added

Expand Down Expand Up @@ -174,14 +170,8 @@ def _set_or_create_version(project, slug, version_id, verbose_name, type_):
def _get_deleted_versions_qs(project, tags_data, branches_data):
# We use verbose_name for tags
# because several tags can point to the same identifier.
versions_tags = [
version['verbose_name']
for version in tags_data
]
versions_branches = [
version['identifier']
for version in branches_data
]
versions_tags = [version["verbose_name"] for version in tags_data]
versions_branches = [version["identifier"] for version in branches_data]

to_delete_qs = (
project.versions(manager=INTERNAL)
Expand All @@ -206,32 +196,28 @@ def delete_versions_from_db(project, tags_data, branches_data):

:returns: The slug of the deleted versions from the database.
"""
to_delete_qs = (
_get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
)
.exclude(active=True)
)
to_delete_qs = _get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
).exclude(active=True)
_, deleted = to_delete_qs.delete()
versions_count = deleted.get('builds.Version', 0)
versions_count = deleted.get("builds.Version", 0)
log.info(
'Re-syncing versions: versions deleted.', project_slug=project.slug, count=versions_count,
"Re-syncing versions: versions deleted.",
project_slug=project.slug,
count=versions_count,
)


def get_deleted_active_versions(project, tags_data, branches_data):
"""Return the slug of active versions that were deleted from the repository."""
to_delete_qs = (
_get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
)
.filter(active=True)
)
return set(to_delete_qs.values_list('slug', flat=True))
to_delete_qs = _get_deleted_versions_qs(
project=project,
tags_data=tags_data,
branches_data=branches_data,
).filter(active=True)
return set(to_delete_qs.values_list("slug", flat=True))


def run_automation_rules(project, added_versions, deleted_active_versions):
Expand Down
86 changes: 40 additions & 46 deletions readthedocs/api/v2/views/footer_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,8 @@ def get_version_compare_data(project, base_version=None, user=None):
:param base_version: We assert whether or not the base_version is also the
highest version in the resulting "is_highest" value.
"""
if (
not project.show_version_warning or
(base_version and base_version.is_external)
):
return {'is_highest': False}
if not project.show_version_warning or (base_version and base_version.is_external):
return {"is_highest": False}

versions_qs = Version.internal.public(project=project, user=user).filter(
built=True, active=True
Expand All @@ -49,21 +46,21 @@ def get_version_compare_data(project, base_version=None, user=None):
versions_qs = versions_qs.filter(type=TAG)

# Optimization
versions_qs = versions_qs.select_related('project')
versions_qs = versions_qs.select_related("project")

highest_version_obj, highest_version_comparable = highest_version(
versions_qs,
)
ret_val = {
'project': str(highest_version_obj),
'version': str(highest_version_comparable),
'is_highest': True,
"project": str(highest_version_obj),
"version": str(highest_version_comparable),
"is_highest": True,
}
if highest_version_obj:
# Never link to the dashboard,
# users reading the docs may don't have access to the dashboard.
ret_val['url'] = highest_version_obj.get_absolute_url()
ret_val['slug'] = highest_version_obj.slug
ret_val["url"] = highest_version_obj.get_absolute_url()
ret_val["slug"] = highest_version_obj.slug
if base_version and base_version.slug != LATEST:
try:
base_version_comparable = parse_version_failsafe(
Expand All @@ -72,13 +69,13 @@ def get_version_compare_data(project, base_version=None, user=None):
if base_version_comparable:
# This is only place where is_highest can get set. All error
# cases will be set to True, for non- standard versions.
ret_val['is_highest'] = (
ret_val["is_highest"] = (
base_version_comparable >= highest_version_comparable
)
else:
ret_val['is_highest'] = True
ret_val["is_highest"] = True
except (Version.DoesNotExist, TypeError):
ret_val['is_highest'] = True
ret_val["is_highest"] = True
return ret_val


Expand All @@ -105,24 +102,24 @@ class BaseFooterHTML(CDNCacheTagsMixin, APIView):
are called many times, so a basic cache is implemented.
"""

http_method_names = ['get']
http_method_names = ["get"]
permission_classes = [IsAuthorizedToViewVersion]
renderer_classes = [JSONRenderer, JSONPRenderer]
project_cache_tag = 'rtd-footer'
project_cache_tag = "rtd-footer"

@lru_cache(maxsize=1)
def _get_project(self):
project_slug = self.request.GET.get('project', None)
project_slug = self.request.GET.get("project", None)
project = get_object_or_404(Project, slug=project_slug)
return project

@lru_cache(maxsize=1)
def _get_version(self):
version_slug = self.request.GET.get('version', None)
version_slug = self.request.GET.get("version", None)

# Hack in a fix for missing version slug deploy
# that went out a while back
if version_slug == '':
if version_slug == "":
version_slug = LATEST

project = self._get_project()
Expand All @@ -142,23 +139,23 @@ def _get_active_versions_sorted(self):
return versions

def _get_context(self):
theme = self.request.GET.get('theme', False)
docroot = self.request.GET.get('docroot', '')
source_suffix = self.request.GET.get('source_suffix', '.rst')
theme = self.request.GET.get("theme", False)
docroot = self.request.GET.get("docroot", "")
source_suffix = self.request.GET.get("source_suffix", ".rst")

new_theme = (theme == 'sphinx_rtd_theme')
new_theme = theme == "sphinx_rtd_theme"

project = self._get_project()
main_project = project.main_language_project or project
version = self._get_version()

page_slug = self.request.GET.get('page', '')
path = ''
if page_slug and page_slug != 'index':
page_slug = self.request.GET.get("page", "")
path = ""
if page_slug and page_slug != "index":
if version.documentation_type in {SPHINX_HTMLDIR, MKDOCS}:
path = re.sub('/index$', '', page_slug) + '/'
path = re.sub("/index$", "", page_slug) + "/"
else:
path = page_slug + '.html'
path = page_slug + ".html"

context = {
"project": project,
Expand All @@ -176,27 +173,27 @@ def _get_context(self):
docroot,
page_slug,
source_suffix,
'edit',
"edit",
),
'github_view_url': version.get_github_url(
"github_view_url": version.get_github_url(
docroot,
page_slug,
source_suffix,
'view',
"view",
),
'gitlab_edit_url': version.get_gitlab_url(
"gitlab_edit_url": version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'edit',
"edit",
),
'gitlab_view_url': version.get_gitlab_url(
"gitlab_view_url": version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'view',
"view",
),
'bitbucket_url': version.get_bitbucket_url(
"bitbucket_url": version.get_bitbucket_url(
docroot,
page_slug,
source_suffix,
Expand All @@ -214,22 +211,19 @@ def get(self, request, format=None):
)

context = self._get_context()
html = template_loader.get_template('restapi/footer.html').render(
html = template_loader.get_template("restapi/footer.html").render(
context,
request,
)

show_version_warning = (
project.show_version_warning and
not version.is_external
)
show_version_warning = project.show_version_warning and not version.is_external

resp_data = {
'html': html,
'show_version_warning': show_version_warning,
'version_active': version.active,
'version_compare': version_compare_data,
'version_supported': version.supported,
"html": html,
"show_version_warning": show_version_warning,
"version_active": version.active,
"version_compare": version_compare_data,
"version_supported": version.supported,
}

return Response(resp_data)
Expand Down
Loading