|
| 1 | +import attrs |
1 | 2 | from django import template |
2 | 3 |
|
3 | | -from cbv.models import Klass, ProjectVersion |
| 4 | +from cbv.models import Klass, Module, ProjectVersion |
4 | 5 |
|
5 | 6 |
|
6 | 7 | register = template.Library() |
@@ -28,31 +29,80 @@ def namesake_methods(parent_klass, name): |
28 | 29 | return result |
29 | 30 |
|
30 | 31 |
|
| 32 | +@attrs.frozen |
| 33 | +class OtherVersion: |
| 34 | + name: str |
| 35 | + url: str |
| 36 | + |
| 37 | + |
| 38 | +@attrs.frozen |
| 39 | +class ModuleData: |
| 40 | + @attrs.frozen |
| 41 | + class KlassData: |
| 42 | + name: str |
| 43 | + url: str |
| 44 | + active: bool |
| 45 | + |
| 46 | + source_name: str |
| 47 | + short_name: str |
| 48 | + classes: list[KlassData] |
| 49 | + active: bool |
| 50 | + |
| 51 | + @classmethod |
| 52 | + def from_module( |
| 53 | + cls, module: Module, active_module: Module | None, active_klass: Klass | None |
| 54 | + ) -> "ModuleData": |
| 55 | + return ModuleData( |
| 56 | + source_name=module.source_name(), |
| 57 | + short_name=module.short_name(), |
| 58 | + classes=[ |
| 59 | + ModuleData.KlassData( |
| 60 | + name=klass.name, |
| 61 | + url=klass.get_absolute_url(), |
| 62 | + active=klass == active_klass, |
| 63 | + ) |
| 64 | + for klass in module.klass_set.all() |
| 65 | + ], |
| 66 | + active=module == active_module, |
| 67 | + ) |
| 68 | + |
| 69 | + |
31 | 70 | @register.inclusion_tag("cbv/includes/nav.html") |
32 | 71 | def nav(version, module=None, klass=None): |
33 | 72 | other_versions = ProjectVersion.objects.filter(project=version.project).exclude( |
34 | 73 | pk=version.pk |
35 | 74 | ) |
36 | | - context = { |
| 75 | + if klass: |
| 76 | + other_versions_of_klass = Klass.objects.filter( |
| 77 | + name=klass.name, |
| 78 | + module__project_version__in=other_versions, |
| 79 | + ) |
| 80 | + other_versions_of_klass_dict = { |
| 81 | + x.module.project_version: x for x in other_versions_of_klass |
| 82 | + } |
| 83 | + version_switcher = [] |
| 84 | + for other_version in other_versions: |
| 85 | + try: |
| 86 | + other_klass = other_versions_of_klass_dict[other_version] |
| 87 | + except KeyError: |
| 88 | + url = other_version.get_absolute_url() |
| 89 | + else: |
| 90 | + url = other_klass.get_absolute_url() |
| 91 | + |
| 92 | + version_switcher.append(OtherVersion(name=str(other_version), url=url)) |
| 93 | + else: |
| 94 | + version_switcher = [ |
| 95 | + OtherVersion(name=str(other_version), url=other_version.get_absolute_url()) |
| 96 | + for other_version in other_versions |
| 97 | + ] |
| 98 | + |
| 99 | + modules = [ |
| 100 | + ModuleData.from_module(module=m, active_module=module, active_klass=klass) |
| 101 | + for m in version.module_set.prefetch_related("klass_set") |
| 102 | + ] |
| 103 | + |
| 104 | + return { |
37 | 105 | "version": version, |
| 106 | + "other_versions": version_switcher, |
| 107 | + "modules": modules, |
38 | 108 | } |
39 | | - if module: |
40 | | - context["this_module"] = module |
41 | | - if klass: |
42 | | - context["this_klass"] = klass |
43 | | - other_versions_of_klass = Klass.objects.filter( |
44 | | - name=klass.name, |
45 | | - module__project_version__in=other_versions, |
46 | | - ) |
47 | | - other_versions_of_klass_dict = { |
48 | | - x.module.project_version: x for x in other_versions_of_klass |
49 | | - } |
50 | | - for other_version in other_versions: |
51 | | - try: |
52 | | - other_klass = other_versions_of_klass_dict[other_version] |
53 | | - except KeyError: |
54 | | - pass |
55 | | - else: |
56 | | - other_version.url = other_klass.get_absolute_url() |
57 | | - context["other_versions"] = other_versions |
58 | | - return context |
|
0 commit comments