loko/loko/assets/migrations/0010_add_parking_thematic.py

74 lines
2.4 KiB
Python

from django.db import migrations
def create_parking_thematic(apps, schema_editor):
Thematic = apps.get_model('common', 'Thematic')
AssetCategory = apps.get_model('assets', 'AssetCategory')
ContentType = apps.get_model('contenttypes', 'ContentType')
thematic, _created = Thematic.objects.get_or_create(
code='parking',
defaults={
'name_fr': 'Stationnement & Mobilité',
'name_nl': 'Parkeren & Mobiliteit',
'description': 'Thématique dédiée aux emplacements de stationnement et zones de mobilité (vélos, trottinettes, PMR, livraison)',
'icon': 'bi-p-square',
'color': '#0d6efd',
}
)
category, _cat_created = AssetCategory.objects.get_or_create(
code='parking_spot',
defaults={
'thematic': thematic,
'name_fr': 'Emplacements de stationnement',
'name_nl': 'Parkeerplaatsen',
}
)
if category.thematic_id != thematic.id:
category.thematic = thematic
category.save()
# Link ContentTypes
try:
spot_ct = ContentType.objects.get(app_label='assets', model='parkingspot')
category.allowed_models.add(spot_ct)
except Exception:
pass
try:
loc_ct = ContentType.objects.get(app_label='assets', model='parkinglocation')
category.allowed_models.add(loc_ct)
except Exception:
pass
# Assign category to existing ParkingSpot records
ParkingSpot = apps.get_model('assets', 'ParkingSpot')
ParkingSpot.objects.filter(category__isnull=True).update(category=category)
# Grant thematic permissions only to admin user configurations
UserConfig = apps.get_model('common', 'UserConfig')
UserThematics = apps.get_model('common', 'UserThematics')
for uc in UserConfig.objects.filter(roles__name='admin').distinct():
UserThematics.objects.get_or_create(
user_config=uc,
thematic=thematic,
defaults={'can_view_assets': True, 'can_edit_assets': True}
)
def remove_parking_thematic(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
('assets', '0009_remove_assetinspection_verified_has_scooter_drop_zone_and_more'),
('common', '0001_initial'),
]
operations = [
migrations.RunPython(create_parking_thematic, remove_parking_thematic),
]