Skip to content

Commit 9db883d

Browse files
committed
Move use of record_url out of models
Managing links should be the job of views or the freezer
1 parent d281625 commit 9db883d

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

naucse/models.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
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.freezer import record_url
1817
from naucse.validation import AllowedElementsParser
1918
from naucse.templates import setup_jinja_env, vars_functions
2019
from naucse.utils.markdown import convert_markdown
@@ -644,13 +643,6 @@ def render(self, page_type, *args, **kwargs):
644643
if page_type != "calendar_ics" and result.output["content"] is not None:
645644
allowed_elements_parser.reset_and_feed(result.output["content"])
646645

647-
if "urls" in result.output:
648-
# Freeze URLs generated by the code in fork, but only if
649-
# they start with the slug of the course
650-
for url in result.output["urls"]:
651-
if url.startswith(f"/{self.slug}/"):
652-
record_url(url)
653-
654646
return result.output
655647

656648
def render_course(self, **kwargs):
@@ -708,9 +700,9 @@ def validate_link(link, key):
708700
for link_type in "prev_link", "session_link", "next_link":
709701
link = result.output.get(link_type)
710702

711-
if isinstance(link, dict) and validate_link(link, "url") and validate_link(link, "title"):
712-
if link["url"].startswith(f"/{self.slug}/"):
713-
record_url(link["url"])
703+
if (isinstance(link, dict) and
704+
validate_link(link, "url") and
705+
validate_link(link, "title")):
714706
to_return.append(link)
715707
else:
716708
to_return.append(None)

naucse/views.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,13 @@ def lesson_static_generator():
276276
yield from lesson_static_generator_dir(lesson.slug, static, static)
277277

278278

279+
def record_content_urls(data_from_fork, prefix):
280+
# Freeze URLs generated by the code in fork, but only if
281+
# they start with the given prefix
282+
for url in data_from_fork.get("urls", ()):
283+
if url.startswith(prefix):
284+
record_url(url)
285+
279286
def course_content(course):
280287
def lesson_url(lesson, *args, **kwargs):
281288
if kwargs.get("page") == "index":
@@ -319,6 +326,7 @@ def course(course):
319326
root_slug=model.meta.slug,
320327
travis_build_id=os.environ.get("TRAVIS_BUILD_ID"),
321328
)
329+
record_content_urls(data_from_fork, f"/{course.slug}/")
322330
kwargs = {
323331
"course_content": content,
324332
"edit_info": edit_info,
@@ -464,7 +472,6 @@ def content_creator():
464472
subpage_url=subpage_url,
465473
vars=variables
466474
)
467-
468475
absolute_urls = [url_for(logged[0], **logged[1]) for logged in logger.logged_calls]
469476

470477
relative_urls = [get_relative_url(request.path, x) for x in absolute_urls]
@@ -529,7 +536,10 @@ def course_page(course, lesson, page, solution=None):
529536
if content_offer:
530537
fork_kwargs["content_key"] = content_key
531538

532-
data_from_fork = course.render_page(lesson_slug, page, solution, **fork_kwargs)
539+
data_from_fork = course.render_page(
540+
lesson_slug, page, solution,
541+
**fork_kwargs)
542+
record_content_urls(data_from_fork, f"/{course.slug}/")
533543

534544
content = data_from_fork["content"]
535545

@@ -572,8 +582,17 @@ def course_page(course, lesson, page, solution=None):
572582
title = '{}: {}'.format(course.title, page.title)
573583

574584
try:
575-
prev_link, session_link, next_link = course.get_footer_links(lesson.slug, page_slug,
576-
request_url=request.path)
585+
footer_links = course.get_footer_links(
586+
lesson.slug,
587+
page_slug,
588+
request_url=request.path,
589+
)
590+
for link in footer_links:
591+
_prefix = f"/{course.slug}/"
592+
if link and link["url"].startswith(_prefix):
593+
record_url(link["url"])
594+
prev_link, session_link, next_link = footer_links
595+
577596
except POSSIBLE_FORK_EXCEPTIONS as e:
578597
if raise_errors_from_forks():
579598
raise
@@ -725,7 +744,9 @@ def session_coverpage(course, session, coverpage):
725744
naucse.utils.views.forks_raise_if_disabled()
726745

727746
try:
728-
data_from_fork = course.render_session_coverpage(session, coverpage, request_url=request.path)
747+
data_from_fork = course.render_session_coverpage(
748+
session, coverpage, request_url=request.path)
749+
record_content_urls(data_from_fork, f"/{course.slug}/")
729750

730751
content = data_from_fork.get("content")
731752
if content is None:
@@ -789,6 +810,7 @@ def course_calendar(course):
789810

790811
try:
791812
data_from_fork = course.render_calendar(request_url=request.path)
813+
record_content_urls(data_from_fork, f"/{course.slug}/")
792814

793815
course = process_course_data(data_from_fork.get("course"), slug=course.slug)
794816
edit_info = links.process_edit_info(data_from_fork.get("edit_info"))
@@ -864,7 +886,9 @@ def course_calendar_ics(course):
864886
naucse.utils.views.forks_raise_if_disabled()
865887

866888
try:
867-
data_from_fork = course.render_calendar_ics(request_url=request.path)
889+
data_from_fork = course.render_calendar_ics(
890+
request_url=request.path)
891+
record_content_urls(data_from_fork, f"/{course.slug}/")
868892

869893
calendar = data_from_fork.get("calendar")
870894

0 commit comments

Comments
 (0)