chore: refactor frontend url parts handling

This commit is contained in:
Livio Bieri 2024-02-01 19:02:33 +01:00
parent 3f40e2fe07
commit 3cbea17f39
1 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,6 @@
import re
from enum import Enum
from typing import Tuple
from django.db import models
from django.utils.text import slugify
@ -219,21 +220,29 @@ class LearningUnit(CourseBasePage):
)
super(LearningUnit, self).save(clean, user, log_action, **kwargs)
def get_frontend_url(self):
def get_frontend_url_parts(self) -> Tuple[str, str, str]:
"""
Extracts the course, circle and learning unit part from the slug.
:return: Tuple of course, circle and learning unit part
"""
r = re.compile(
r"^(?P<coursePart>.+?)-lp-circle-(?P<circlePart>.+?)-lu-(?P<luPart>.+?)$"
)
m = r.match(self.slug)
if m is None:
return "ERROR: could not parse slug"
return f"/course/{m.group('coursePart')}/learn/{m.group('circlePart')}#lu-{m.group('luPart')}"
ValueError(f"Could not parse slug: {self.slug}")
return m.group("coursePart"), m.group("circlePart"), m.group("luPart")
def get_frontend_url(self):
course, circle, learning_unit = self.get_frontend_url_parts()
return f"/course/{course}/learn/{circle}#lu-{learning_unit}"
def get_evaluate_url(self):
r = re.compile(
r"^(?P<coursePart>.+?)-lp-circle-(?P<circlePart>.+?)-lu-(?P<luPart>.+?)$"
)
m = r.match(self.slug)
return f"/course/{m.group('coursePart')}/learn/{m.group('circlePart')}/evaluate/{m.group('luPart')}"
course, circle, learning_unit = self.get_frontend_url_parts()
return f"/course/{course}/learn/{circle}/evaluate/{learning_unit}"
def get_admin_display_title(self):
return f"LE: {self.draft_title}"