Skip to content

Commit eb771b1

Browse files
committed
Replace absolute_urls_to_freeze with a utility function
1 parent 30db68d commit eb771b1

File tree

4 files changed

+21
-17
lines changed

4 files changed

+21
-17
lines changed

naucse/freezer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import contextlib
2+
from collections import deque
23

34
from flask_frozen import UrlForLogger, Freezer
45

5-
from naucse.utils.views import absolute_urls_to_freeze
6+
absolute_urls_to_freeze = deque()
7+
8+
def record_url(url):
9+
"""Logs that `url` should be included in the resulting static site"""
10+
absolute_urls_to_freeze.append(url)
611

712

813
class AllLinksLogger(UrlForLogger):

naucse/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import naucse.utils.views
1515
from naucse.utils.models import Model, YamlProperty, DataProperty, DirProperty, MultipleModelDirProperty, ForkProperty
1616
from naucse.utils.models import reify, arca
17-
from naucse.utils.views import absolute_urls_to_freeze
17+
from naucse.freezer import record_url
1818
from naucse.validation import AllowedElementsParser
1919
from naucse.templates import setup_jinja_env, vars_functions
2020
from naucse.utils.markdown import convert_markdown
@@ -647,7 +647,9 @@ def render(self, page_type, *args, **kwargs):
647647
if "urls" in result.output:
648648
# Freeze URLs generated by the code in fork, but only if
649649
# they start with the slug of the course
650-
absolute_urls_to_freeze.extend([url for url in result.output["urls"] if url.startswith(f"/{self.slug}/")])
650+
for url in result.output["urls"]:
651+
if url.startswith(f"/{self.slug}/"):
652+
record_url(url)
651653

652654
return result.output
653655

@@ -708,7 +710,7 @@ def validate_link(link, key):
708710

709711
if isinstance(link, dict) and validate_link(link, "url") and validate_link(link, "title"):
710712
if link["url"].startswith(f"/{self.slug}/"):
711-
absolute_urls_to_freeze.append(link["url"])
713+
record_url(link["url"])
712714
to_return.append(link)
713715
else:
714716
to_return.append(None)

naucse/utils/views.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
import hashlib
33
import json
44
import os
5-
from collections import deque, defaultdict
5+
from collections import defaultdict
66
from pathlib import Path
77

88
from arca.exceptions import PullError, BuildError, RequirementsMismatch
99
from arca.utils import get_hash_for_file
1010

11-
absolute_urls_to_freeze = deque()
12-
1311

1412
def get_recent_runs(course):
1513
"""Build a list of "recent" runs based on a course.

naucse/views.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from werkzeug.local import LocalProxy
1717

1818
from naucse import models
19-
from naucse.freezer import temporary_url_for_logger
19+
from naucse.freezer import temporary_url_for_logger, record_url
2020
from naucse.models import allowed_elements_parser
2121
from naucse.templates import setup_jinja_env, vars_functions
2222
from naucse.urlconverters import register_url_converters
@@ -26,7 +26,7 @@
2626
from naucse.utils.models import arca
2727
from naucse.utils.views import get_recent_runs, list_months
2828
from naucse.utils.views import does_course_return_info
29-
from naucse.utils.views import absolute_urls_to_freeze, raise_errors_from_forks
29+
from naucse.utils.views import raise_errors_from_forks
3030
from naucse.utils.views import page_content_cache_key, get_edit_info
3131
from naucse.validation import DisallowedStyle, DisallowedElement, InvalidHTML
3232

@@ -485,11 +485,10 @@ def content_creator():
485485
content_key = page_content_cache_key(Repo("."), lesson.slug, page.slug, solution, variables)
486486
cached = arca.region.get_or_create(content_key, content_creator)
487487

488-
# The urls are added twice to ``absolute_urls_to_freeze``
489-
# when the content is created.
488+
# The urls are recorded twice when the content is created.
490489
# But it doesn't matter, duplicate URLs are skipped.
491-
absolute_urls = [urljoin(request.path, x) for x in cached["urls"]]
492-
absolute_urls_to_freeze.extend(absolute_urls)
490+
for x in cached["urls"]:
491+
record_url(urljoin(request.path, x))
493492

494493
return cached
495494

@@ -537,13 +536,13 @@ def course_page(course, lesson, page, solution=None):
537536
if content is None:
538537
# the offer was accepted
539538
content = content_offer["content"]
540-
absolute_urls_to_freeze.extend([urljoin(request.path, x)
541-
for x in content_offer["urls"]])
539+
for x in content_offer["urls"]:
540+
record_url(urljoin(request.path, x))
542541
else:
543542
# the offer was rejected or the the fragment was not in cache
544543
arca.region.set(content_key, {"content": content, "urls": data_from_fork["content_urls"]})
545-
absolute_urls_to_freeze.extend([urljoin(request.path, x)
546-
for x in data_from_fork["content_urls"]])
544+
for x in data_from_fork["content_urls"]:
545+
record_url(urljoin(request.path, x))
547546

548547
# compatibility
549548
page = process_page_data(data_from_fork.get("page"))

0 commit comments

Comments
 (0)