Skip to content
Draft
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
40 changes: 20 additions & 20 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ repos:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v2.4.1
hooks:
- id: mypy
exclude: manage.py
additional_dependencies:
- django-stubs[compatible-mypy]
- django-stubs-ext
- types-Markdown
- types-requests
- django-bootstrap5
- django-registration
- django-crispy-forms
- crispy-bootstrap5
- django-import-export
- types-django-import-export
- django-multiselectfield
- django-tables2
- django-filter
- django-guardian
# - repo: https://github.com/pre-commit/mirrors-mypy
# rev: v2.4.1
# hooks:
# - id: mypy
# exclude: manage.py
# additional_dependencies:
# - django-stubs[compatible-mypy]
# - django-stubs-ext
# - types-Markdown
# - types-requests
# - django-bootstrap5
# - django-registration
# - django-crispy-forms
# - crispy-bootstrap5
# - django-import-export
# - types-django-import-export
# - django-multiselectfield
# - django-tables2
# - django-filter
# - django-guardian
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.50.1
hooks:
Expand Down
13 changes: 13 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ beautifulsoup4==4.15.0
# via direct-webapp (pyproject.toml)
build==1.6.1
# via pip-tools
cachetools==8.0.3
# via django-unicorn
certifi==2026.10.23
# via requests
cfgv==3.5.0
Expand All @@ -26,6 +28,8 @@ crispy-bootstrap5==2026.9
# via direct-webapp (pyproject.toml)
cssbeautifier==2.0.3
# via djlint
decorator==5.3.2
# via django-unicorn
diff-match-patch==20241021
# via django-import-export
distlib==0.4.4
Expand All @@ -44,6 +48,7 @@ django==6.1.2
# django-stubs
# django-stubs-ext
# django-tables2
# django-unicorn
django-bootstrap5==26.4
# via direct-webapp (pyproject.toml)
django-crispy-forms==2.7
Expand All @@ -70,6 +75,8 @@ django-stubs-ext==6.1.2
# django-stubs
django-tables2==3.0.1
# via direct-webapp (pyproject.toml)
django-unicorn==0.67.0
# via direct-webapp (pyproject.toml)
djlint==1.47.1
# via direct-webapp (pyproject.toml)
editorconfig==0.17.1
Expand All @@ -96,6 +103,8 @@ json5==0.17.3
# via djlint
librt==0.16.0
# via mypy
lxml==6.1.3
# via django-unicorn
markdown==3.11.2
# via direct-webapp (pyproject.toml)
mypy==2.4.1
Expand All @@ -108,6 +117,8 @@ nh3==0.3.7
# via direct-webapp (pyproject.toml)
nodeenv==1.11.0
# via pre-commit
orjson==3.13.0
# via django-unicorn
packaging==26.3
# via
# build
Expand Down Expand Up @@ -162,6 +173,8 @@ ruff==0.17.3
# via direct-webapp (pyproject.toml)
setuptools==84.2.1
# via pip-tools
shortuuid==1.0.13
# via django-unicorn
soupsieve==3.0.4
# via beautifulsoup4
sqlparse==0.6.0
Expand Down
3 changes: 3 additions & 0 deletions direct_webapp/settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
"django_tables2",
"django_filters",
"guardian",
"django_unicorn",
]
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend", # this is default
Expand All @@ -147,6 +148,8 @@

DJANGO_TABLES2_TEMPLATE = "django_tables2/bootstrap5-responsive.html"

UNICORN = {"ALLOWED_JS_CALL_LIST": ["Unicorn", "reloadSkillWheel"]}

# Ensure the logs directory exists
LOGS_DIR = BASE_DIR / "logs"
os.makedirs(LOGS_DIR, exist_ok=True)
Expand Down
1 change: 1 addition & 0 deletions direct_webapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

urlpatterns = [
path("admin/", admin.site.urls),
path("unicorn/", include("django_unicorn.urls")),
path("", include("main.urls")),
]
13 changes: 13 additions & 0 deletions doc-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ babel==2.18.0
# via mkdocs-material
backrefs==8.1
# via mkdocs-material
cachetools==8.0.3
# via django-unicorn
certifi==2026.10.23
# via requests
charset-normalizer==3.5.2
Expand All @@ -20,6 +22,8 @@ confusable-homoglyphs==3.3.1
# via django-registration
crispy-bootstrap5==2026.9
# via direct-webapp (pyproject.toml)
decorator==5.3.2
# via django-unicorn
diff-match-patch==20241021
# via django-import-export
django==6.1.2
Expand All @@ -35,6 +39,7 @@ django==6.1.2
# django-registration
# django-stubs-ext
# django-tables2
# django-unicorn
django-bootstrap5==26.4
# via direct-webapp (pyproject.toml)
django-crispy-forms==2.7
Expand All @@ -55,6 +60,8 @@ django-stubs-ext==6.1.2
# via direct-webapp (pyproject.toml)
django-tables2==3.0.1
# via direct-webapp (pyproject.toml)
django-unicorn==0.67.0
# via direct-webapp (pyproject.toml)
ghp-import==2.1.0
# via
# mkdocs
Expand All @@ -71,6 +78,8 @@ jinja2==3.1.6
# mkdocs-material
# mkdocstrings
# properdocs
lxml==6.1.3
# via django-unicorn
markdown==3.11.2
# via
# direct-webapp (pyproject.toml)
Expand Down Expand Up @@ -124,6 +133,8 @@ mkdocstrings-python==2.0.10
# via direct-webapp (pyproject.toml)
nh3==0.3.7
# via direct-webapp (pyproject.toml)
orjson==3.13.0
# via django-unicorn
packaging==26.3
# via
# mkdocs
Expand Down Expand Up @@ -166,6 +177,8 @@ requests==2.34.2
# via
# direct-webapp (pyproject.toml)
# mkdocs-material
shortuuid==1.0.13
# via django-unicorn
six==1.17.0
# via python-dateutil
sqlparse==0.6.0
Expand Down
9 changes: 9 additions & 0 deletions docs/dev-guide/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ the shared view-test helpers described in [Testing](testing.md).
Use Django URL reversing for application links and the `static` template tag for
static assets, following nearby templates.

## Django Unicorn

The shared page and authentication shells provide the Unicorn scripts and CSRF
token. Load `{% load unicorn %}` in templates that render components, then use
the `{% unicorn %}` tag. Create components in `main` as described in the
[Unicorn installation guide](https://www.django-unicorn.com/docs/installation/).
Component actions must enforce the same authentication, terms acceptance and
object permissions as the corresponding application views.

## Forms

Use the existing Django forms and crispy-forms integration with Bootstrap 5.
Expand Down
1 change: 1 addition & 0 deletions main/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Django Unicorn Components."""
76 changes: 76 additions & 0 deletions main/components/skill_wheel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Unicorn component for rendering a competency wheel."""

import json
from pathlib import Path
from typing import Any

from django_unicorn.components import UnicornView

from ..models import (
SkillLevel,
)


def _extract_and_combine_roles(
sample_data_files: list[str | Path],
) -> list[dict[str, Any]]:
"""Read and combine multiple JSON files with sample user data."""
combined_sample_data = []
for file_path in sample_data_files:
json_path = Path(file_path)
if json_path.exists():
with json_path.open() as f:
data = json.load(f)
if isinstance(data, list):
combined_sample_data.extend(data)
else:
combined_sample_data.append(data)
return combined_sample_data


class SkillWheelView(UnicornView):
"""Render skill profile chart data in the browser."""

chart_data: list[dict[str, Any]]
skill_levels: list[dict[str, Any]]

def mount(self) -> None:
"""Load chart data and skill levels passed by the parent template."""
if "profile_sample_id" in self.component_kwargs:
self._load_sample(self.component_kwargs["profile_sample_id"])
else:
self.chart_data = self._load_list("chart_data")
self.skill_levels = self._load_list("skill_levels")
self.call("reloadSkillWheel")

def _load_sample(self, sample_id: str):
sample_data = _extract_and_combine_roles(
[f"main/static/assets/sample_data/{sample_id}.json"]
)
self.chart_data = sample_data
self.skill_levels = list(SkillLevel.objects.values("level", "name"))

def _load_list(self, key: str) -> list[dict[str, Any]]:
"""Read a list-valued component argument, decoding JSON strings."""
try:
value: Any = self.component_kwargs[key]
except KeyError as exc:
raise ValueError(f"Missing required component argument: {key}") from exc

if isinstance(value, str):
value = json.loads(value)

if value is None:
return []

if not isinstance(value, list) or not all(
isinstance(item, dict) for item in value
):
raise TypeError(f"Component argument {key} must be a list of objects")

return value

def set_profile_id(self, profile_id: str) -> None:
"""Set the profile ID and reload the sample data."""
self._load_sample(profile_id)
self.call("reloadSkillWheel")
3 changes: 2 additions & 1 deletion main/templates/main/base.auth.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{% load static %}
{% load static unicorn %}
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
<title>{% block title %}{{ site_title }}{% endblock title %} | {{ site_title }}</title>
{% include "main/snippets/head.html" %}
</head>
<body>
{% csrf_token %}
{% include "main/snippets/page-loading.html" %}
<main class="page-wrapper">
<div class="d-flex flex-column flex-lg-row position-relative h-100">
Expand Down
2 changes: 2 additions & 0 deletions main/templates/main/base.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{% load unicorn %}
<!DOCTYPE html>
<html lang="en" data-bs-theme="light">
<head>
<title>{% block title %}{{ site_title }}{% endblock title %} | {{ site_title }}</title>
{% include "main/snippets/head.html" %}
</head>
<body class="{% block body_class %}{% endblock body_class %}">
{% csrf_token %}
{% include "main/snippets/page-loading.html" %}
{% include "main/snippets/navbar.html" %}
<main class="page-wrapper">
Expand Down
1 change: 1 addition & 0 deletions main/templates/main/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "main/base.html" %}
{% load unicorn %}
{% load static %}
{% load django_bootstrap5 %}
{% block content %}
Expand Down
3 changes: 2 additions & 1 deletion main/templates/main/pages/roles.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends "main/base.page.html" %}
{% load static %}
{% load unicorn %}
{% block title %}
Roles
{% endblock title %}
Expand Down Expand Up @@ -43,5 +44,5 @@ <h2 class="mb-xl-4">{{ profile.role }}</h2>
No profiles
{% endif %}
</section>
{% include "main/snippets/skill-wheel.html" %}
{% unicorn 'skill-wheel' chart_data=chart_data skill_levels=skill_levels %}
{% endblock content %}
3 changes: 2 additions & 1 deletion main/templates/main/shared-skills-profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% load static %}
{% load crispy_forms_tags %}
{% load django_bootstrap5 %}
{% load unicorn %}
{% block title %}
Skill Profile (Anonymous)
{% endblock title %}
Expand Down Expand Up @@ -34,7 +35,7 @@ <h1>Shared Skills profile</h1>
<div class="alert alert-danger"
role="alert">{{ error_message }}</div>
{% else %}
{% include "main/snippets/skill-wheel.html" %}
{% unicorn 'skill-wheel' chart_data=chart_data skill_levels=skill_levels %}
{% endif %}
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions main/templates/main/snippets/head.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
{% load unicorn %}
<meta charset="utf-8">
<meta name="viewport"
content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover">
Expand All @@ -8,6 +9,7 @@
content="skills, competencies, professional, development, research, software, career, community, training">
<meta name="author" content="DIRECT project team">
<meta name="referrer" content="strict-origin-when-cross-origin" />
{% unicorn_scripts %}
<!-- Webmanifest + Favicon / App icons -->
<link rel="manifest" href="{% static 'manifest.json' %}">
<link rel="icon"
Expand Down
3 changes: 2 additions & 1 deletion main/templates/main/snippets/hero.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load static %}
{% load unicorn %}
<section class="position-relative pt-5 pb-4">
<div class="position-absolute top-0 start-0 w-100" style="height: 79%;">
<div class="bg-primary position-absolute top-0 start-0 w-100 h-100"
Expand Down Expand Up @@ -75,4 +76,4 @@ <h3>Understand and develop your skills</h3>
</div>
</div>
</section>
{% include "main/snippets/skill-wheel.html" %}
{% unicorn 'skill-wheel' profile_sample_id=profile_sample_id %}
3 changes: 2 additions & 1 deletion main/templates/main/user-skills-profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{% load static %}
{% load crispy_forms_tags %}
{% load django_bootstrap5 %}
{% load unicorn %}
{% block breadcrumb_items %}
<li class="breadcrumb-item">
<a href="{% url 'profile' %}">Profile</a>
Expand Down Expand Up @@ -38,7 +39,7 @@ <h1>Skills profile</h1>
with size according to your self-assessed level.
</p>
<div id="dataviz_root" style="width:100%"></div>
{% include "main/snippets/skill-wheel.html" %}
{% unicorn 'skill-wheel' chart_data=chart_data skill_levels=skill_levels %}
</div>
</div>
</section>
Expand Down
Loading
Loading