Add avatar images to dashboar persons page

This commit is contained in:
Daniel Egger 2024-04-25 11:26:35 +02:00
parent 85007a17c9
commit bb57591387
5 changed files with 77 additions and 64 deletions

View File

@ -161,9 +161,12 @@ watch(selectedCourse, () => {
<div class="w-full flex-auto md:w-1/3">
<div class="flex items-center space-x-2">
<img
class="inline-block h-11 w-11 rounded-full"
:src="
person.avatar_url_small ||
'/static/avatars/myvbv-default-avatar.png'
"
:alt="person.last_name"
class="h-11 w-11 rounded-full"
:src="'/static/avatars/myvbv-default-avatar.png'"
/>
<div>
<div class="text-bold">

View File

@ -56,6 +56,8 @@ export type DashboardPersonType = {
last_name: string;
email: string;
course_sessions: DashboardPersonCourseSessionType[];
avatar_url: string;
avatar_url_small: string;
};
export type DashboardCourseConfigType = {

View File

@ -0,0 +1,11 @@
# pylint: disable=unused-wildcard-import,wildcard-import,wrong-import-position
import os
from environs import Env
script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
env = Env()
env.read_env(f"{script_dir}/../../../env_secrets/local_daniel.env", recurse=False)
from .base import * # noqa

View File

@ -1,10 +1,13 @@
import uuid
import structlog
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import JSONField
from django.urls import reverse
logger = structlog.get_logger(__name__)
class Organisation(models.Model):
organisation_id = models.IntegerField(primary_key=True)
@ -107,15 +110,24 @@ class User(AbstractUser):
blank=True,
)
def create_avatar_url(self, size=400):
try:
if self.avatar:
filter_spec = f"fill-{size}x{size}"
self.avatar.get_rendition(filter_spec)
url = reverse("user_image", kwargs={"image_id": self.avatar.id})
return f"{url}?filter={filter_spec}"
except Exception:
logger.warn("could not create avatar url", label="security", exc_info=True)
return "/static/avatars/myvbv-default-avatar.png"
@property
def avatar_url(self):
if self.avatar:
filter_spec = "fill-400x400"
self.avatar.get_rendition(filter_spec)
url = reverse("user_image", kwargs={"image_id": self.avatar.id})
return f"{url}?filter={filter_spec}"
else:
return "/static/avatars/myvbv-default-avatar.png"
return self.create_avatar_url()
@property
def avatar_url_small(self):
return self.create_avatar_url(size=96)
class SecurityRequestResponseLog(models.Model):

View File

@ -126,6 +126,29 @@ def user_role(roles: Set[str]) -> str:
@api_view(["GET"])
def get_dashboard_persons(request):
def create_user_dict(user_object):
return {
"user_id": user_object.id,
"first_name": user_object.first_name,
"last_name": user_object.last_name,
"email": user_object.email,
"avatar_url_small": user_object.avatar_url_small,
"avatar_url": user_object.avatar_url,
}
def create_course_session_dict(course_session_object, user_role, my_role):
return {
"id": str(course_session_object.id),
"session_title": course_session_object.title,
"course_id": str(course_session_object.course.id),
"course_title": course_session_object.course.title,
"course_slug": course_session_object.course.slug,
"user_role": user_role,
"my_role": my_role,
"is_uk": course_session_object.course.configuration.is_uk,
"is_vv": course_session_object.course.configuration.is_vv,
}
try:
course_sessions = get_course_sessions_with_roles_for_user(request.user)
@ -137,25 +160,11 @@ def get_dashboard_persons(request):
)
my_role = user_role(cs.roles)
for csu in course_session_users:
result_persons[csu.user.id] = {
"user_id": csu.user.id,
"first_name": csu.user.first_name,
"last_name": csu.user.last_name,
"email": csu.user.email,
"course_sessions": [
{
"id": str(cs.id),
"session_title": cs.title,
"course_id": str(cs.course.id),
"course_title": cs.course.title,
"course_slug": cs.course.slug,
"user_role": csu.role,
"my_role": my_role,
"is_uk": cs.course.configuration.is_uk,
"is_vv": cs.course.configuration.is_vv,
}
],
}
person_data = create_user_dict(csu.user)
person_data["course_sessions"] = [
create_course_session_dict(cs, csu.role, my_role)
]
result_persons[csu.user.id] = person_data
# add persons where request.user is mentor
for cs in course_sessions:
@ -165,26 +174,14 @@ def get_dashboard_persons(request):
).first()
for participant in lm.participants.all():
course_session_entry = {
"id": str(cs.id),
"session_title": cs.title,
"course_id": str(cs.course.id),
"course_title": cs.course.title,
"course_slug": cs.course.slug,
"user_role": "LEARNING_MENTEE",
"my_role": "LEARNING_MENTOR",
"is_uk": cs.course.configuration.is_uk,
"is_vv": cs.course.configuration.is_vv,
}
course_session_entry = create_course_session_dict(
cs, "LEARNING_MENTEE", "LEARNING_MENTOR"
)
if participant.user.id not in result_persons:
result_persons[participant.user.id] = {
"user_id": participant.user.id,
"first_name": participant.user.first_name,
"last_name": participant.user.last_name,
"email": participant.user.email,
"course_sessions": [course_session_entry],
}
person_data = create_user_dict(participant.user)
person_data["course_sessions"] = [course_session_entry]
result_persons[participant.user.id] = person_data
else:
# user is already in result_persons
result_persons[participant.user.id]["course_sessions"].append(
@ -197,26 +194,14 @@ def get_dashboard_persons(request):
).prefetch_related("mentor", "course_session")
for mentor_relation in mentor_relation_qs:
cs = mentor_relation.course_session
course_session_entry = {
"id": str(cs.id),
"session_title": cs.title,
"course_id": str(cs.course.id),
"course_title": cs.course.title,
"course_slug": cs.course.slug,
"user_role": "LEARNING_MENTOR",
"my_role": "LEARNING_MENTEE",
"is_uk": cs.course.configuration.is_uk,
"is_vv": cs.course.configuration.is_vv,
}
course_session_entry = create_course_session_dict(
cs, "LEARNING_MENTOR", "LEARNING_MENTEE"
)
if mentor_relation.mentor.id not in result_persons:
result_persons[mentor_relation.mentor.id] = {
"user_id": mentor_relation.mentor.id,
"first_name": mentor_relation.mentor.first_name,
"last_name": mentor_relation.mentor.last_name,
"email": mentor_relation.mentor.email,
"course_sessions": [course_session_entry],
}
person_data = create_user_dict(mentor_relation.mentor)
person_data["course_sessions"] = [course_session_entry]
result_persons[mentor_relation.mentor.id] = person_data
else:
# user is already in result_persons
result_persons[mentor_relation.mentor.id]["course_sessions"].append(