100 lines
3 KiB
Python
100 lines
3 KiB
Python
# Generated by hand
|
|
|
|
from django.db import migrations
|
|
from django.utils.text import slugify
|
|
|
|
|
|
def create_giep_folders(apps, schema_editor):
|
|
DocumentFolder = apps.get_model('documents', 'DocumentFolder')
|
|
DocumentFolderAttachment = apps.get_model('documents', 'DocumentFolderAttachment')
|
|
NatureRWIZ = apps.get_model('assets', 'NatureRWIZ')
|
|
Thematic = apps.get_model('common', 'Thematic')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
|
|
# 1. Get or create 'assets' folder
|
|
assets_folder, _ = DocumentFolder.objects.get_or_create(
|
|
slug='assets',
|
|
defaults={'name': 'Assets', 'is_predefined': True}
|
|
)
|
|
|
|
# 2. Get water thematic
|
|
water_thematic = Thematic.objects.filter(code='water').first()
|
|
|
|
# Create 'other' document tag
|
|
DocumentTag = apps.get_model('documents', 'DocumentTag')
|
|
other_tag, _ = DocumentTag.objects.get_or_create(
|
|
slug='other',
|
|
defaults={
|
|
'name_fr': 'Autre',
|
|
'name_nl': 'Andere',
|
|
'show_in_asset_detail': True
|
|
}
|
|
)
|
|
if water_thematic:
|
|
other_tag.thematics.add(water_thematic)
|
|
|
|
# 3. Create 'water' folder as child of 'assets'
|
|
water_folder, _ = DocumentFolder.objects.get_or_create(
|
|
slug='water',
|
|
defaults={
|
|
'name': 'Water',
|
|
'is_predefined': True,
|
|
}
|
|
)
|
|
if water_thematic:
|
|
water_folder.thematics.add(water_thematic)
|
|
water_folder.parent_folders.add(assets_folder)
|
|
|
|
# 4. Create subfolder for each GIEP zone (NatureRWIZ)
|
|
nature_rwiz_content_type = ContentType.objects.get_for_model(NatureRWIZ)
|
|
|
|
for zone in NatureRWIZ.objects.all():
|
|
# Avoid creating duplicate folders for the same GIEP zone
|
|
exists = DocumentFolderAttachment.objects.filter(
|
|
content_type=nature_rwiz_content_type,
|
|
object_id=zone.id,
|
|
is_default=True
|
|
).exists()
|
|
if exists:
|
|
continue
|
|
|
|
folder_name = zone.code
|
|
base_slug = slugify(folder_name) or f"giep-{zone.id}"
|
|
slug = base_slug
|
|
counter = 1
|
|
while DocumentFolder.objects.filter(slug=slug).exists():
|
|
slug = f"{base_slug}-{counter}"
|
|
counter += 1
|
|
|
|
zone_folder = DocumentFolder.objects.create(
|
|
name=folder_name,
|
|
slug=slug,
|
|
is_predefined=True
|
|
)
|
|
if water_thematic:
|
|
zone_folder.thematics.add(water_thematic)
|
|
zone_folder.parent_folders.add(water_folder)
|
|
|
|
# Attach folder to NatureRWIZ
|
|
DocumentFolderAttachment.objects.create(
|
|
folder=zone_folder,
|
|
content_type=nature_rwiz_content_type,
|
|
object_id=zone.id,
|
|
is_default=True
|
|
)
|
|
|
|
|
|
def remove_giep_folders(apps, schema_editor):
|
|
pass
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('documents', '0001_initial'),
|
|
('assets', '0020_alter_trafficlightradarpole_model'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(create_giep_folders, remove_giep_folders),
|
|
]
|