From de438c85e01a922dc1ef6edf90c22ba19e5eea22 Mon Sep 17 00:00:00 2001 From: Robin Tissot <tissotrobin@gmail.com> Date: Thu, 20 May 2021 15:30:49 +0200 Subject: [PATCH] Fixes creating a document without project. --- .../migrations/0046_data_share_doc_to_proj.py | 19 +++++++++++++++++++ .../migrations/0048_auto_20210520_1308.py | 19 +++++++++++++++++++ app/apps/core/models.py | 2 +- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 app/apps/core/migrations/0048_auto_20210520_1308.py diff --git a/app/apps/core/migrations/0046_data_share_doc_to_proj.py b/app/apps/core/migrations/0046_data_share_doc_to_proj.py index c1e758f3..5007dece 100644 --- a/app/apps/core/migrations/0046_data_share_doc_to_proj.py +++ b/app/apps/core/migrations/0046_data_share_doc_to_proj.py @@ -21,6 +21,7 @@ def make_slug(proj, Project): def forwards(apps, schema_editor): User = apps.get_model('users', 'User') Project = apps.get_model('core', 'Project') + Document = apps.get_model('core', 'Document') # create user projects for user in User.objects.all(): proj, created = Project.objects.get_or_create(name=user.username+"'s Project", @@ -39,6 +40,24 @@ def forwards(apps, schema_editor): # shared to draft user.document_set.filter(workflow_state=1).update(workflow_state=0) + # deal with documents without owner (shouldn't be any but let's be safe) + # move them to admin's + user = User.objects.filter(is_superuser=True).first() + proj, dummy = Project.objects.get_or_create(name=user.username+"'s Project", + owner=user) + if not proj.slug: + make_slug(proj, Project) + for doc in Document.objects.filter(owner=None): + doc.project = proj + doc.save() + # move share from docs to created projects + for doc in user.document_set.all(): + for share in doc.shared_with_users.all(): + proj.shared_with_users.add(share) + for share in doc.shared_with_groups.all(): + proj.shared_with_groups.add(share) + + def backwards(apps, schema_editor): Document = apps.get_model('core', 'Document') for doc in Document.objects.all(): diff --git a/app/apps/core/migrations/0048_auto_20210520_1308.py b/app/apps/core/migrations/0048_auto_20210520_1308.py new file mode 100644 index 00000000..31bfc704 --- /dev/null +++ b/app/apps/core/migrations/0048_auto_20210520_1308.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.20 on 2021-05-20 13:08 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('core', '0047_auto_20210510_1512'), + ] + + operations = [ + migrations.AlterField( + model_name='document', + name='project', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='documents', to='core.Project'), + ), + ] diff --git a/app/apps/core/models.py b/app/apps/core/models.py index 2b6e829a..2269c867 100644 --- a/app/apps/core/models.py +++ b/app/apps/core/models.py @@ -236,7 +236,7 @@ class Document(models.Model): metadatas = models.ManyToManyField(Metadata, through=DocumentMetadata, blank=True) - project = models.ForeignKey(Project, null=True, blank=True, + project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='documents') -- GitLab