Add admin command to create & sync Berufsbildner

This commit is contained in:
Christian Cueni 2024-07-31 15:27:06 +02:00
parent 5defb386bd
commit 7110b5f97c
3 changed files with 79 additions and 1 deletions

View File

@ -1,4 +1,4 @@
from django.contrib import admin
from django.contrib import admin, messages
from django.contrib.auth import admin as auth_admin, get_user_model
from django.utils.translation import gettext_lazy as _
@ -10,6 +10,9 @@ from vbv_lernwelt.core.models import (
SecurityRequestResponseLog,
)
from vbv_lernwelt.core.utils import pretty_print_json
from vbv_lernwelt.learning_mentor.services import (
create_or_sync_berufsbildner as create_or_sync_bb,
)
User = get_user_model()
@ -31,6 +34,26 @@ class LogAdmin(admin.ModelAdmin):
return pretty_print_json(json_string)
@admin.action(description="Berufsbildner: Create or Sync")
def create_or_sync_berufsbildner(modeladmin, request, queryset):
# keep it easy
success = []
for user in queryset:
success.append(create_or_sync_bb(user))
if all(success):
messages.add_message(
request,
messages.SUCCESS,
f"Berufsbildner erfolgreich erstellt oder synchronisiert",
)
else:
messages.add_message(
request,
messages.ERROR,
f"Einige Berufsbildner konnten nicht erstellt oder synchronisiert werden",
)
@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):
fieldsets = (
@ -91,6 +114,7 @@ class UserAdmin(auth_admin.UserAdmin):
"sso_id",
]
search_fields = ["first_name", "last_name", "email", "username", "sso_id"]
actions = [create_or_sync_berufsbildner]
@admin.register(JobLog)

View File

@ -0,0 +1,54 @@
import structlog
from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.consts import COURSE_UK, COURSE_UK_FR, COURSE_UK_IT
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.learning_mentor.models import (
AgentParticipantRelation,
AgentParticipantRoleType,
)
logger = structlog.get_logger(__name__)
UK_COURSES = [COURSE_UK, COURSE_UK_FR, COURSE_UK_IT]
def create_or_sync_berufsbildner(berufsbildner: User) -> bool:
logger.info(
"Creating or syncing berufsbildner",
berufsbildner=berufsbildner,
org=berufsbildner.organisation.name_de,
)
# check if it is a valid organisation
if berufsbildner.organisation and berufsbildner.organisation.organisation_id < 4:
logger.error("Invalid organisation", org=berufsbildner.organisation)
return False
# get existing connections
existing_members = set(berufsbildner.agentparticipantrelation_set.all())
# gather new relations
new_members = set(
CourseSessionUser.objects.filter(user__organisation=berufsbildner.organisation)
.filter(course_session__course__configuration__is_uk=True)
.filter(role=CourseSessionUser.Role.MEMBER.value)
.filter(course_session__course_id__in=UK_COURSES)
.exclude(course_session_id__in=[4, 5, 6])
)
# add new relations that are not in existing relations
for csu in new_members:
if csu not in existing_members:
AgentParticipantRelation.objects.get_or_create(
agent=berufsbildner,
participant=csu,
role=AgentParticipantRoleType.BERUFSBILDNER.value,
)
# remove old relations that are not in the new relations
for relation in existing_members:
if relation.participant not in new_members:
relation.delete()
return True