forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy_collection_submission_metadata_to_cedar.py
More file actions
73 lines (60 loc) · 2.39 KB
/
Copy pathcopy_collection_submission_metadata_to_cedar.py
File metadata and controls
73 lines (60 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import logging
from django.core.management.base import BaseCommand
from osf.models import CollectionSubmission
logger = logging.getLogger(__name__)
def copy_collection_submission_metadata_to_cedar(dry_run=False, batch_size=100, provider_id=None):
qs = CollectionSubmission.objects.filter(
collection__provider__required_metadata_template__isnull=False,
).select_related(
'guid',
'collection__provider__required_metadata_template',
)
if provider_id:
qs = qs.filter(collection__provider___id=provider_id)
total = qs.count()
logger.info(f'{"[DRY RUN] " if dry_run else ""}Found {total} collection submissions to process')
processed = errors = 0
for submission in qs.iterator(chunk_size=batch_size):
if dry_run:
logger.info(f'[DRY RUN] Would sync cedar metadata for submission {submission._id}')
continue
try:
submission.sync_cedar_metadata()
processed += 1
except Exception as e:
logger.error(f'Failed to sync cedar metadata for submission {submission._id}: {e}')
errors += 1
logger.info(
f'{"[DRY RUN] " if dry_run else ""}'
f'Done. Processed {processed}/{total} submissions'
f'{f", {errors} error(s)" if errors else ""}'
)
class Command(BaseCommand):
help = 'Copy CollectionSubmission custom metadata fields to CedarMetadataRecord for providers with a required cedar template.'
def add_arguments(self, parser):
super().add_arguments(parser)
parser.add_argument(
'--dry-run',
action='store_true',
dest='dry_run',
help='Preview what would be synced without making any changes',
)
parser.add_argument(
'--batch-size',
type=int,
default=100,
dest='batch_size',
help='Number of submissions to process per iteration (default: 100)',
)
parser.add_argument(
'--provider',
type=str,
dest='provider_id',
help='Optional collection provider _id to limit processing to a single provider',
)
def handle(self, *args, **options):
copy_collection_submission_metadata_to_cedar(
dry_run=options['dry_run'],
batch_size=options['batch_size'],
provider_id=options.get('provider_id'),
)