Skip to content

Commit 342d6cf

Browse files
committed
state of the ui 25
1 parent e0a3110 commit 342d6cf

36 files changed

Lines changed: 288 additions & 2 deletions

manager/director/apps/sites/consumers/routing.py

Whitespace-only changes.

manager/director/apps/sites/consumers/site_status.py

Whitespace-only changes.

manager/director/apps/users/forms.py

Whitespace-only changes.

manager/director/apps/users/middleware.py

Whitespace-only changes.

manager/director/apps/users/urls.py

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from __future__ import annotations
2+
3+
from collections.abc import Callable
4+
from typing import TYPE_CHECKING, cast
5+
6+
from django.contrib.auth.decorators import user_passes_test
7+
import pydenticon
8+
from io import BytesIO
9+
from django.core.files.base import ContentFile
10+
11+
12+
if TYPE_CHECKING:
13+
from django.contrib.auth.models import AbstractBaseUser, AnonymousUser
14+
15+
from .models import User
16+
17+
18+
def map_user(
19+
test_func: Callable[[User], bool],
20+
) -> Callable[[AbstractBaseUser | AnonymousUser], bool]:
21+
"""Fix types of test functions for :meth:`.user_passes_test`, and return False if the user is anonymous."""
22+
23+
def wrapper(user: AbstractBaseUser | AnonymousUser) -> bool:
24+
if user.is_anonymous:
25+
return False
26+
return test_func(cast("User", user))
27+
28+
return wrapper
29+
30+
31+
teacher_or_superuser_required = user_passes_test(map_user(lambda u: u.is_teacher or u.is_superuser))
32+
33+
34+
def generate_avatar(username: str) -> ContentFile:
35+
foreground_colors = [
36+
"rgb(45,79,255)", # blue
37+
"rgb(254,180,44)", # orange
38+
"rgb(226,121,234)", # pink
39+
"rgb(30,179,253)", # cyan
40+
"rgb(232,77,65)", # red
41+
"rgb(49,203,115)", # green
42+
]
43+
44+
grad_year = username[:4]
45+
try:
46+
foreground_color = foreground_colors[int(grad_year) % len(foreground_colors)]
47+
except ValueError:
48+
foreground_color = "rgb(141,69,170)" # purple, fallback color
49+
50+
generator = pydenticon.Generator(5, 5, foreground=[foreground_color], background="rgb(248,244,244)")
51+
image_data = generator.generate(username, 200, 200, padding=(20, 20, 20, 20), output_format="png")
52+
53+
return ContentFile(image_data, name=f"{username}_avatar.png")

manager/director/apps/utils/decorators.py

Whitespace-only changes.

manager/director/apps/utils/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django import template
2+
from django.templatetags.static import static
3+
4+
register = template.Library()
5+
6+
@register.filter
7+
def get_static(path):
8+
return static(path)
159 KB
Loading

0 commit comments

Comments
 (0)