loko/loko/projects/migrations/0002_update_project_documents_visibility.py

68 lines
2.2 KiB
Python

from django.db import migrations
def make_project_documents_and_folders_scoped(apps, schema_editor):
DocumentFolder = apps.get_model('documents', 'DocumentFolder')
ManagedDocument = apps.get_model('documents', 'ManagedDocument')
DocumentFolderAttachment = apps.get_model('documents', 'DocumentFolderAttachment')
DocumentAttachment = apps.get_model('documents', 'DocumentAttachment')
ContentType = apps.get_model('contenttypes', 'ContentType')
try:
project_ct = ContentType.objects.get(app_label='projects', model='project')
except ContentType.DoesNotExist:
return
# Update project folders from internal -> scoped
project_folder_ids = list(
DocumentFolderAttachment.objects.filter(
content_type=project_ct
).values_list('folder_id', flat=True)
)
if project_folder_ids:
DocumentFolder.objects.filter(
pk__in=project_folder_ids,
visibility='internal'
).update(visibility='scoped')
# Also update folders with application_label='projects'
DocumentFolder.objects.filter(
application_label='projects',
visibility='internal'
).update(visibility='scoped')
# Update project documents from internal -> scoped
project_doc_ids = list(
DocumentAttachment.objects.filter(
content_type=project_ct
).values_list('document_id', flat=True)
)
if project_doc_ids:
ManagedDocument.objects.filter(
pk__in=project_doc_ids,
visibility='internal'
).update(visibility='scoped')
# Also update documents in project folders
if project_folder_ids:
ManagedDocument.objects.filter(
folders__pk__in=project_folder_ids,
visibility='internal'
).update(visibility='scoped')
class Migration(migrations.Migration):
dependencies = [
('projects', '0001_initial'),
('documents', '0003_documentfolder_visibility_manageddocument_thematics_and_more'),
]
operations = [
migrations.RunPython(
make_project_documents_and_folders_scoped,
reverse_code=migrations.RunPython.noop,
),
]