Skip to content
Snippets Groups Projects
Commit 6d4791cf authored by Tomi Valentová's avatar Tomi Valentová
Browse files

auto-create person pages

parent 122da5e0
Branches
No related tags found
2 merge requests!1097Release,!1095Add basic Octopus people sync
Pipeline #19292 passed
# Generated by Django 5.0.7 on 2024-07-30 17:16
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('district', '0262_alter_districtcenterpage_content_and_more'),
]
operations = [
migrations.AddField(
model_name='districtoctopuspersonpage',
name='is_automatically_created',
field=models.BooleanField(default=True, help_text='Pokud vytváříš stránku pro osobu z Chobotnice manuálně, toto pole by nemělo být zaškrtlé. V ostatních případech ho zaškrtlé nech.', verbose_name='Profil vytvořen automaticky'),
preserve_default=False,
),
migrations.AddField(
model_name='districtoctopuspersonpage',
name='originating_group',
field=models.CharField(default='', help_text='Skupina, ze které byla tato osba importována', max_length=128, verbose_name='Skupina'),
preserve_default=False,
),
]
...@@ -321,10 +321,27 @@ class DistrictOctopusPersonPage(ExtendedMetadataPageMixin, SubpageMixin, Metadat ...@@ -321,10 +321,27 @@ class DistrictOctopusPersonPage(ExtendedMetadataPageMixin, SubpageMixin, Metadat
verbose_name="Osoba" verbose_name="Osoba"
) )
is_automatically_created = models.BooleanField(
verbose_name="Profil vytvořen automaticky",
help_text=(
"Pokud vytváříš stránku pro osobu z Chobotnice manuálně, "
"toto pole by nemělo být zaškrtlé. V ostatních případech "
"ho zaškrtlé nech."
)
)
# Shouldn't be visible in the admin interface
originating_group = models.CharField(
verbose_name="Skupina",
help_text="Skupina, ze které byla tato osba importována",
max_length=128
)
### PANELS ### PANELS
content_panels = Page.content_panels + [ content_panels = Page.content_panels + [
FieldPanel("person"), FieldPanel("person"),
FieldPanel("is_automatically_created")
] ]
### RELATIONS ### RELATIONS
......
...@@ -36,12 +36,12 @@ def import_people_from_group( ...@@ -36,12 +36,12 @@ def import_people_from_group(
group_shortcut, group_shortcut,
lock_file_name lock_file_name
): ):
from .models import DistrictPeoplePage, DistrictPersonPage from .models import DistrictPeoplePage, DistrictOctopusPersonPage
return PeopleGroupImporter( return PeopleGroupImporter(
people_parent_page_id=people_parent_page_id, people_parent_page_id=people_parent_page_id,
people_parent_page_model=DistrictPeoplePage, people_parent_page_model=DistrictPeoplePage,
person_page_model=DistrictPersonPage, person_page_model=DistrictOctopusPersonPage,
collection_id=collection_id, collection_id=collection_id,
group_shortcut=group_shortcut, group_shortcut=group_shortcut,
lock_file_name=lock_file_name, lock_file_name=lock_file_name,
......
from gql import gql, Client from gql import gql, Client
from gql.transport.aiohttp import AIOHTTPTransport from gql.transport.aiohttp import AIOHTTPTransport
from django.conf import settings from django.conf import settings
from django.db import models
from shared.models import OctopusPerson from shared.models import OctopusPerson
import os import os
import logging import logging
...@@ -36,6 +37,7 @@ class PeopleGroupImporter: ...@@ -36,6 +37,7 @@ class PeopleGroupImporter:
self.new_user_count = 0 self.new_user_count = 0
self.existing_user_count = 0 self.existing_user_count = 0
self.people_parent_page = self.people_parent_page_model.objects.get(id=self.people_parent_page_id)
self.collection = Collection.objects.get(id=self.collection_id) self.collection = Collection.objects.get(id=self.collection_id)
self.transport = AIOHTTPTransport(url=settings.OCTOPUS_API_URL) self.transport = AIOHTTPTransport(url=settings.OCTOPUS_API_URL)
...@@ -285,7 +287,28 @@ class PeopleGroupImporter: ...@@ -285,7 +287,28 @@ class PeopleGroupImporter:
people_profiles = self.get_processed_people_profiles(people_ids) people_profiles = self.get_processed_people_profiles(people_ids)
people_instances = self.create_and_update_people_models(people_profiles) people_instances = self.create_and_update_people_models(people_profiles)
for person_instance in people_instances:
person_page = self.person_page_model.objects.filter(person=person_instance).descendant_of(self.people_parent_page).first()
if person_page is None:
person_page = self.person_page_model(
person=person_instance,
is_automatically_created=True,
originating_group=self.group_shortcut,
title=person_instance.display_name,
)
self.people_parent_page.add_child(instance=person_page)
person_page.save_revision().publish()
# Delete old pages that correspond to profiles which aren't
# part of the group we are importing anymore.
self.person_page_model.objects.filter(
~models.Q(person__in=people_instances),
originating_group=self.group_shortcut,
is_automatically_created=True
).descendant_of(self.people_parent_page).delete()
finally: finally:
# No matter what happens, at least remove the lockfile. # No matter what happens, at least remove the lockfile.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment