|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | + |
| 3 | +from article.tasks import task_load_article_from_opac, task_load_article_from_article_meta |
| 4 | + |
| 5 | + |
| 6 | +class Command(BaseCommand): |
| 7 | + help = 'Generate task requests for loading article data from Article Meta for each year from 1900 to 2025' |
| 8 | + |
| 9 | + def add_arguments(self, parser): |
| 10 | + parser.add_argument( |
| 11 | + '--start-year', |
| 12 | + type=int, |
| 13 | + default=1990, |
| 14 | + help='Start year (default: 1990)' |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + '--end-year', |
| 18 | + type=int, |
| 19 | + default=2025, |
| 20 | + help='End year (default: 2025)' |
| 21 | + ) |
| 22 | + parser.add_argument( |
| 23 | + '--collection', |
| 24 | + type=str, |
| 25 | + default='scl', |
| 26 | + help='Collection code (default: scl)' |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + '--task', |
| 30 | + choices=['load_article_from_opac', 'load_article_from_article_meta'], |
| 31 | + default='load_article_from_opac', |
| 32 | + help='Task to execute (default: load_article_from_opac)', |
| 33 | + ) |
| 34 | + |
| 35 | + def handle(self, *args, **options): |
| 36 | + start_year = options['start_year'] |
| 37 | + end_year = options['end_year'] |
| 38 | + collection = options['collection'] |
| 39 | + |
| 40 | + self.stdout.write( |
| 41 | + self.style.SUCCESS( |
| 42 | + f'Generating task requests from {start_year} to {end_year} for collection: {collection}' |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + total_tasks = 0 |
| 47 | + |
| 48 | + for year in range(start_year, end_year + 1): |
| 49 | + from_date = f'{year}-01-01' |
| 50 | + until_date = f'{year}-12-31' |
| 51 | + |
| 52 | + self.stdout.write(f'Queuing task for year {year}...') |
| 53 | + |
| 54 | + # Queue the task for each year |
| 55 | + if options['task'] == 'load_article_from_article_meta': |
| 56 | + task_result = task_load_article_from_article_meta.delay( |
| 57 | + from_date=from_date, |
| 58 | + until_date=until_date, |
| 59 | + collection=collection |
| 60 | + ) |
| 61 | + else: |
| 62 | + task_result = task_load_article_from_opac.delay( |
| 63 | + from_date=from_date, |
| 64 | + until_date=until_date, |
| 65 | + collection=collection |
| 66 | + ) |
| 67 | + |
| 68 | + total_tasks += 1 |
| 69 | + |
| 70 | + self.stdout.write( |
| 71 | + self.style.SUCCESS( |
| 72 | + f'✓ Task queued for year {year}: {from_date} to {until_date} (Task ID: {task_result.id})' |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + self.stdout.write( |
| 77 | + self.style.SUCCESS( |
| 78 | + f'\nCompleted! {total_tasks} tasks have been queued successfully.' |
| 79 | + ) |
| 80 | + ) |
0 commit comments