Add avatar images to dashboar persons page
This commit is contained in:
parent
85007a17c9
commit
bb57591387
|
|
@ -161,9 +161,12 @@ watch(selectedCourse, () => {
|
||||||
<div class="w-full flex-auto md:w-1/3">
|
<div class="w-full flex-auto md:w-1/3">
|
||||||
<div class="flex items-center space-x-2">
|
<div class="flex items-center space-x-2">
|
||||||
<img
|
<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"
|
:alt="person.last_name"
|
||||||
class="h-11 w-11 rounded-full"
|
|
||||||
:src="'/static/avatars/myvbv-default-avatar.png'"
|
|
||||||
/>
|
/>
|
||||||
<div>
|
<div>
|
||||||
<div class="text-bold">
|
<div class="text-bold">
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,8 @@ export type DashboardPersonType = {
|
||||||
last_name: string;
|
last_name: string;
|
||||||
email: string;
|
email: string;
|
||||||
course_sessions: DashboardPersonCourseSessionType[];
|
course_sessions: DashboardPersonCourseSessionType[];
|
||||||
|
avatar_url: string;
|
||||||
|
avatar_url_small: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DashboardCourseConfigType = {
|
export type DashboardCourseConfigType = {
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
@ -1,10 +1,13 @@
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
|
import structlog
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import JSONField
|
from django.db.models import JSONField
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
|
logger = structlog.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class Organisation(models.Model):
|
class Organisation(models.Model):
|
||||||
organisation_id = models.IntegerField(primary_key=True)
|
organisation_id = models.IntegerField(primary_key=True)
|
||||||
|
|
@ -107,16 +110,25 @@ class User(AbstractUser):
|
||||||
blank=True,
|
blank=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@property
|
def create_avatar_url(self, size=400):
|
||||||
def avatar_url(self):
|
try:
|
||||||
if self.avatar:
|
if self.avatar:
|
||||||
filter_spec = "fill-400x400"
|
filter_spec = f"fill-{size}x{size}"
|
||||||
self.avatar.get_rendition(filter_spec)
|
self.avatar.get_rendition(filter_spec)
|
||||||
url = reverse("user_image", kwargs={"image_id": self.avatar.id})
|
url = reverse("user_image", kwargs={"image_id": self.avatar.id})
|
||||||
return f"{url}?filter={filter_spec}"
|
return f"{url}?filter={filter_spec}"
|
||||||
else:
|
except Exception:
|
||||||
|
logger.warn("could not create avatar url", label="security", exc_info=True)
|
||||||
return "/static/avatars/myvbv-default-avatar.png"
|
return "/static/avatars/myvbv-default-avatar.png"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def avatar_url(self):
|
||||||
|
return self.create_avatar_url()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def avatar_url_small(self):
|
||||||
|
return self.create_avatar_url(size=96)
|
||||||
|
|
||||||
|
|
||||||
class SecurityRequestResponseLog(models.Model):
|
class SecurityRequestResponseLog(models.Model):
|
||||||
label = models.CharField(max_length=255, blank=True, default="")
|
label = models.CharField(max_length=255, blank=True, default="")
|
||||||
|
|
|
||||||
|
|
@ -126,6 +126,29 @@ def user_role(roles: Set[str]) -> str:
|
||||||
|
|
||||||
@api_view(["GET"])
|
@api_view(["GET"])
|
||||||
def get_dashboard_persons(request):
|
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:
|
try:
|
||||||
course_sessions = get_course_sessions_with_roles_for_user(request.user)
|
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)
|
my_role = user_role(cs.roles)
|
||||||
for csu in course_session_users:
|
for csu in course_session_users:
|
||||||
result_persons[csu.user.id] = {
|
person_data = create_user_dict(csu.user)
|
||||||
"user_id": csu.user.id,
|
person_data["course_sessions"] = [
|
||||||
"first_name": csu.user.first_name,
|
create_course_session_dict(cs, csu.role, my_role)
|
||||||
"last_name": csu.user.last_name,
|
]
|
||||||
"email": csu.user.email,
|
result_persons[csu.user.id] = person_data
|
||||||
"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,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
# add persons where request.user is mentor
|
# add persons where request.user is mentor
|
||||||
for cs in course_sessions:
|
for cs in course_sessions:
|
||||||
|
|
@ -165,26 +174,14 @@ def get_dashboard_persons(request):
|
||||||
).first()
|
).first()
|
||||||
|
|
||||||
for participant in lm.participants.all():
|
for participant in lm.participants.all():
|
||||||
course_session_entry = {
|
course_session_entry = create_course_session_dict(
|
||||||
"id": str(cs.id),
|
cs, "LEARNING_MENTEE", "LEARNING_MENTOR"
|
||||||
"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,
|
|
||||||
}
|
|
||||||
|
|
||||||
if participant.user.id not in result_persons:
|
if participant.user.id not in result_persons:
|
||||||
result_persons[participant.user.id] = {
|
person_data = create_user_dict(participant.user)
|
||||||
"user_id": participant.user.id,
|
person_data["course_sessions"] = [course_session_entry]
|
||||||
"first_name": participant.user.first_name,
|
result_persons[participant.user.id] = person_data
|
||||||
"last_name": participant.user.last_name,
|
|
||||||
"email": participant.user.email,
|
|
||||||
"course_sessions": [course_session_entry],
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
# user is already in result_persons
|
# user is already in result_persons
|
||||||
result_persons[participant.user.id]["course_sessions"].append(
|
result_persons[participant.user.id]["course_sessions"].append(
|
||||||
|
|
@ -197,26 +194,14 @@ def get_dashboard_persons(request):
|
||||||
).prefetch_related("mentor", "course_session")
|
).prefetch_related("mentor", "course_session")
|
||||||
for mentor_relation in mentor_relation_qs:
|
for mentor_relation in mentor_relation_qs:
|
||||||
cs = mentor_relation.course_session
|
cs = mentor_relation.course_session
|
||||||
course_session_entry = {
|
course_session_entry = create_course_session_dict(
|
||||||
"id": str(cs.id),
|
cs, "LEARNING_MENTOR", "LEARNING_MENTEE"
|
||||||
"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,
|
|
||||||
}
|
|
||||||
|
|
||||||
if mentor_relation.mentor.id not in result_persons:
|
if mentor_relation.mentor.id not in result_persons:
|
||||||
result_persons[mentor_relation.mentor.id] = {
|
person_data = create_user_dict(mentor_relation.mentor)
|
||||||
"user_id": mentor_relation.mentor.id,
|
person_data["course_sessions"] = [course_session_entry]
|
||||||
"first_name": mentor_relation.mentor.first_name,
|
result_persons[mentor_relation.mentor.id] = person_data
|
||||||
"last_name": mentor_relation.mentor.last_name,
|
|
||||||
"email": mentor_relation.mentor.email,
|
|
||||||
"course_sessions": [course_session_entry],
|
|
||||||
}
|
|
||||||
else:
|
else:
|
||||||
# user is already in result_persons
|
# user is already in result_persons
|
||||||
result_persons[mentor_relation.mentor.id]["course_sessions"].append(
|
result_persons[mentor_relation.mentor.id]["course_sessions"].append(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue