-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #792 from kobotoolbox/update-submission-attachment…
…s-on-edit [ TASK-163 ] Keep attachments list up-to-date when editing a submission
- Loading branch information
Showing
22 changed files
with
539 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
onadata/apps/logger/management/commands/soft_delete_orphan_attachments.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from __future__ import annotations | ||
|
||
from django.core.management.base import BaseCommand | ||
|
||
from onadata.apps.logger.models import ( | ||
Attachment, | ||
Instance, | ||
) | ||
from onadata.apps.logger.signals import pre_delete_attachment | ||
from onadata.libs.utils.logger_tools import get_soft_deleted_attachments | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
help = "Soft delete orphan attachments, i.e: Hide them in API responses" | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument( | ||
'--chunks', | ||
type=int, | ||
default=2000, | ||
help="Number of records to process per query" | ||
) | ||
|
||
def handle(self, *args, **kwargs): | ||
chunks = kwargs['chunks'] | ||
verbosity = kwargs['verbosity'] | ||
|
||
queryset = Attachment.objects.values_list('instance_id', flat=True).distinct() | ||
|
||
if verbosity > 1: | ||
self.stdout.write( | ||
f'Calculating number of instance with attachments…' | ||
) | ||
instances_count = queryset.count() | ||
|
||
cpt = 1 | ||
|
||
for instance_id in queryset.iterator(chunk_size=chunks): | ||
instance = Instance.objects.get(pk=instance_id) | ||
if verbosity > 0: | ||
message = '' if verbosity <= 1 else f' - {cpt}/{instances_count}' | ||
self.stdout.write( | ||
f'Processing Instance object #{instance_id}{message}…' | ||
) | ||
soft_deleted_attachments = get_soft_deleted_attachments(instance) | ||
for soft_deleted_attachment in soft_deleted_attachments: | ||
pre_delete_attachment( | ||
soft_deleted_attachment, only_update_counters=True | ||
) | ||
|
||
cpt += 1 | ||
self.stdout.write('Done!') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
onadata/apps/logger/migrations/0033_add_deleted_at_field_to_attachment.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 3.2.15 on 2023-10-16 17:25 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('logger', '0032_alter_daily_submission_counter_user'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='attachment', | ||
name='deleted_at', | ||
field=models.DateTimeField(blank=True, db_index=True, null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.