Add create_or_sync_ausbildungsverantwortlicher action
This commit is contained in:
parent
ff96cba446
commit
636e3ee95a
|
|
@ -11,6 +11,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_ausbildungsverantwortlicher as create_or_sync_av,
|
||||
)
|
||||
from vbv_lernwelt.learning_mentor.services import (
|
||||
create_or_sync_berufsbildner as create_or_sync_bb,
|
||||
)
|
||||
|
|
@ -55,6 +58,25 @@ def create_or_sync_berufsbildner(modeladmin, request, queryset):
|
|||
)
|
||||
|
||||
|
||||
@admin.action(description="Ausbildungsverantwortlicher: Create or Sync")
|
||||
def create_or_sync_ausbildungsverantwortlicher(modeladmin, request, queryset):
|
||||
success = []
|
||||
for user in queryset:
|
||||
success.append(create_or_sync_av(user))
|
||||
if all(success):
|
||||
messages.add_message(
|
||||
request,
|
||||
messages.SUCCESS,
|
||||
"Ausbildungsverantwortlicher erfolgreich erstellt oder synchronisiert",
|
||||
)
|
||||
else:
|
||||
messages.add_message(
|
||||
request,
|
||||
messages.ERROR,
|
||||
"Einige Ausbildungsverantwortliche konnten nicht erstellt oder synchronisiert werden",
|
||||
)
|
||||
|
||||
|
||||
@admin.register(User)
|
||||
class UserAdmin(auth_admin.UserAdmin):
|
||||
fieldsets = (
|
||||
|
|
@ -117,7 +139,7 @@ class UserAdmin(auth_admin.UserAdmin):
|
|||
]
|
||||
list_filter = ("is_staff", "is_superuser", "is_active", "groups", "organisation")
|
||||
search_fields = ["first_name", "last_name", "email", "username", "sso_id"]
|
||||
actions = [create_or_sync_berufsbildner]
|
||||
actions = [create_or_sync_berufsbildner, create_or_sync_ausbildungsverantwortlicher]
|
||||
|
||||
|
||||
@admin.register(JobLog)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,12 @@
|
|||
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.consts import (
|
||||
COURSE_UK,
|
||||
COURSE_UK_FR,
|
||||
COURSE_UK_IT,
|
||||
VV_COURSE_IDS,
|
||||
)
|
||||
from vbv_lernwelt.course.models import CourseSessionUser
|
||||
from vbv_lernwelt.learning_mentor.models import (
|
||||
AgentParticipantRelation,
|
||||
|
|
@ -14,21 +19,6 @@ 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)
|
||||
|
|
@ -36,12 +26,45 @@ def create_or_sync_berufsbildner(berufsbildner: User) -> bool:
|
|||
.filter(course_session__course_id__in=UK_COURSES)
|
||||
.exclude(course_session_id__in=[4, 5, 6])
|
||||
)
|
||||
return create_or_sync_learning_mentor(berufsbildner, new_members)
|
||||
|
||||
|
||||
def create_or_sync_ausbildungsverantwortlicher(
|
||||
ausbildungsverantwortlicher: User,
|
||||
) -> bool:
|
||||
new_members = set(
|
||||
CourseSessionUser.objects.filter(
|
||||
user__organisation=ausbildungsverantwortlicher.organisation
|
||||
)
|
||||
.filter(course_session__course__configuration__is_uk=False)
|
||||
.filter(role=CourseSessionUser.Role.MEMBER.value)
|
||||
.filter(course_session__course_id__in=VV_COURSE_IDS)
|
||||
)
|
||||
return create_or_sync_learning_mentor(ausbildungsverantwortlicher, new_members)
|
||||
|
||||
|
||||
def create_or_sync_learning_mentor(
|
||||
agent: User, new_members: set[CourseSessionUser]
|
||||
) -> bool:
|
||||
logger.info(
|
||||
"Creating or syncing berufsbildner",
|
||||
berufsbildner=agent,
|
||||
org=agent.organisation.name_de,
|
||||
)
|
||||
|
||||
# check if it is a valid organisation
|
||||
if agent.organisation and agent.organisation.organisation_id < 4:
|
||||
logger.error("Invalid organisation", org=agent.organisation)
|
||||
return False
|
||||
|
||||
# get existing connections
|
||||
existing_members = set(agent.agentparticipantrelation_set.all())
|
||||
|
||||
# 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,
|
||||
agent=agent,
|
||||
participant=csu,
|
||||
role=AgentParticipantRoleType.BERUFSBILDNER.value,
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in New Issue