diff --git a/district/blocks.py b/district/blocks.py index fd22b761b5d3b4358732ad49b3bf659cb193050b..25e8e50ba73cdf615a0d1f5bad1aab9e7ddc17c5 100644 --- a/district/blocks.py +++ b/district/blocks.py @@ -130,17 +130,9 @@ class PersonCustomPositionBlock(PersonCustomPositionBlockMixin): ) -class OctopusGroupBlock(blocks.StructBlock): +class OctopusMixin(blocks.StructBlock): title = CharBlock(label="Titulek", required=True) - slug = blocks.CharBlock( - label="Slug skupiny", - required=False, - help_text="Není třeba vyplňovat, bude automaticky vyplněno", - ) - - group_shortcut = CharBlock(label="Zkratka skupiny", required=True) - def get_prep_value(self, value): value = super().get_prep_value(value) value["slug"] = slugify(value["title"]) @@ -162,11 +154,46 @@ class OctopusGroupBlock(blocks.StructBlock): return context class Meta: - label = "Skupina osob z Chobotnice" + abstract = True + label = "Osoby z Chobotnice" icon = "group" template = "styleguide2/includes/organisms/cards/people_card_list.html" +class OctopusGroupBlock(OctopusMixin): + slug = blocks.CharBlock( + label="Slug bloku", + required=False, + help_text="Není třeba vyplňovat, bude automaticky vyplněno", + ) + + group_shortcut = CharBlock( + label="Zkratka skupiny", + help_text="Např. cen_to_ved", + required=True + ) + + class Meta: + label = "Skupina osob z Chobotnice" + + +class OctopusTeamBlock(OctopusMixin): + slug = blocks.CharBlock( + label="Slug bloku", + required=False, + help_text="Není třeba vyplňovat, bude automaticky vyplněno", + ) + + team_shortcut = CharBlock( + label="Zkratka týmu", + help_text="Např. TO-Admin", + required=True + ) + + class Meta: + label = "Tým osob z Chobotnice" + + class PeopleGroupBlock(PeopleGroupBlockMixin): person_list = blocks.ListBlock( blocks.PageChooserBlock( diff --git a/district/management/commands/octopus_people_import.py b/district/management/commands/octopus_people_import.py index 2bdf633d9df5ccc92c18726ef92ff6a11af6d363..014f2fe3347285e1e6cedd9aee000f905eef54fa 100644 --- a/district/management/commands/octopus_people_import.py +++ b/district/management/commands/octopus_people_import.py @@ -2,7 +2,7 @@ from django.core.management.base import BaseCommand from wagtail.models.media import Collection from district.models import DistrictPeoplePage -from district.tasks import import_people_from_group +from district.tasks import import_people_from_group, import_people_from_team class Command(BaseCommand): @@ -21,3 +21,16 @@ class Command(BaseCommand): collection_id, shortcut, ) + + for people_page in DistrictPeoplePage.objects.all(): + for shortcut in people_page.get_syncable_octopus_teams(): + collection_id = people_page.root_page.image_collection_id + + if collection_id is None: + collection_id = Collection.objects.first().id + + import_people_from_team.delay( + people_page.id, + collection_id, + shortcut, + ) \ No newline at end of file diff --git a/district/migrations/0289_alter_districtpeoplepage_content.py b/district/migrations/0289_alter_districtpeoplepage_content.py new file mode 100644 index 0000000000000000000000000000000000000000..31e7e11217323fb5ca0a5232d48c3a9a8b8addeb --- /dev/null +++ b/district/migrations/0289_alter_districtpeoplepage_content.py @@ -0,0 +1,21 @@ +# Generated by Django 5.0.7 on 2024-10-02 10:56 + +import wagtail.blocks +import wagtail.fields +import wagtail.images.blocks +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('district', '0288_alter_districtarticlepage_content_and_more'), + ] + + operations = [ + migrations.AlterField( + model_name='districtpeoplepage', + name='content', + field=wagtail.fields.StreamField([('octopus_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug bloku', required=False)), ('group_shortcut', wagtail.blocks.CharBlock(help_text='Např. cen_to_ved', label='Zkratka skupiny', required=True))], label='Skupina z Chobotnice')), ('octopus_team', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug bloku', required=False)), ('team_shortcut', wagtail.blocks.CharBlock(help_text='Např. TO-Admin', label='Zkratka týmu', required=True))], label='Tým z Chobotnice')), ('people_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug skupiny', required=False)), ('person_list', wagtail.blocks.ListBlock(wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictPersonPage', 'district.DistrictOctopusPersonPage']), default=[], help_text='S pozicemi z jejich podstránek', label='Osoby')), ('person_list_with_custom_positions', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('page', wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictOctopusPersonPage', 'district.DistrictPersonPage'])), ('position', wagtail.blocks.CharBlock(help_text='Pokud není pozice vyplněná, použije se pozice ze stránky osoby.', label='Pozice', required=False))]), default=[], help_text='S nastavitelnými pozicemi', label='Osoby'))], group='', label='Seznam osob')), ('team_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Název sekce týmů')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug sekce', required=False)), ('team_list', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('headline', wagtail.blocks.CharBlock(label='Titulek bloku', required=False)), ('card_items', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek')), ('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('text', wagtail.blocks.RichTextBlock(label='Krátký text pod nadpisem', required=False)), ('page', wagtail.blocks.PageChooserBlock(label='Stránka', page_type=['district.DistrictArticlesPage', 'district.DistrictCenterPage', 'district.DistrictContactPage', 'district.DistrictCrossroadPage', 'district.DistrictCustomPage', 'district.DistrictPeoplePage', 'district.DistrictGeoFeatureCollectionPage', 'district.DistrictCalendarPage', 'district.DistrictPdfPage', 'district.DistrictNewProgramPage'], required=False)), ('link', wagtail.blocks.URLBlock(label='Odkaz', required=False))], template='styleguide2/includes/molecules/boxes/card_box_block.html'), label='Karty s odkazy'))], label='Karta týmu'), label='Týmy'))]))], blank=True, verbose_name='Lidé a týmy'), + ), + ] diff --git a/district/models.py b/district/models.py index d74ed4722101a5e9eebde687f26915e2f600425a..bccf37da3fc88452a2ec8aaf53a7bdb78ed3c583 100644 --- a/district/models.py +++ b/district/models.py @@ -594,6 +594,8 @@ class DistrictPeoplePage(MainPeoplePageMixin): content = StreamField( [ ("octopus_group", blocks.OctopusGroupBlock(label="Skupina z Chobotnice")), + ("octopus_team", blocks.OctopusTeamBlock(label="Tým z Chobotnice")), + ("octopus_team", blocks.OctopusTeamBlock(label="Tým z Chobotnice")), ("people_group", blocks.PeopleGroupBlock(label="Seznam osob", group="")), ("team_group", blocks.TeamBlock()), ], @@ -656,6 +658,16 @@ class DistrictPeoplePage(MainPeoplePageMixin): # Don't duplicate groups needlessly return list(set(group_shortcuts)) + def get_syncable_octopus_teams(self): + team_shortcuts = [] + + for block in self.content: + if block.block_type == "octopus_team": + team_shortcuts.append(block.value["team_shortcut"]) + + # Don't duplicate groups needlessly + return list(set(team_shortcuts)) + class DistrictCalendarPage(SubpageMixin, MetadataPageMixin, CalendarMixin, Page): ### PANELS diff --git a/district/tasks.py b/district/tasks.py index 73604eebeb1eca41806fe0e995c3541475992a81..8fad447d94ae4e2059247301d9c6ab8ae55e8738 100644 --- a/district/tasks.py +++ b/district/tasks.py @@ -57,3 +57,31 @@ def import_people_from_group( group_shortcut=group_shortcut, lock_file_name=lock_file_name, ).perform_import() + + +@shared_task() +def import_people_from_team( + people_parent_page_id, + collection_id, + team_shortcut, +): + from .models import DistrictOctopusPersonPage, DistrictPeoplePage + + lock_file_name = os.path.join( + tempfile.gettempdir(), + f"{people_parent_page_id}-{team_shortcut}.people-from-team-import-lock", + ) + + if os.path.isfile(lock_file_name): + return + + open(lock_file_name, "w").close() + + return PeopleTeamImporter( + people_parent_page_id=people_parent_page_id, + people_parent_page_model=DistrictPeoplePage, + person_page_model=DistrictOctopusPersonPage, + collection_id=collection_id, + team_shortcut=team_shortcut, + lock_file_name=lock_file_name, + ).perform_import() \ No newline at end of file