Skip to content

Commit 094ef20

Browse files
committed
automatically assign campaign's key and number
1 parent a19b454 commit 094ef20

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

Campaign/management/commands/StartNewCampaign.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.core.files.base import ContentFile
1212
from django.core.management.base import BaseCommand
1313
from django.core.management.base import CommandError
14+
from django.db import models
1415

1516
from Campaign.management.commands.init_campaign import _create_context
1617
from Campaign.management.commands.init_campaign import _init_campaign
@@ -42,6 +43,7 @@ def add_arguments(self, parser):
4243

4344
# TODO: support adding multiple batches
4445
parser.add_argument(
46+
'-b',
4547
'--batches-json',
4648
type=str,
4749
default=[],
@@ -51,6 +53,7 @@ def add_arguments(self, parser):
5153
)
5254

5355
parser.add_argument(
56+
'-o',
5457
'--csv-output',
5558
type=str,
5659
default=None,
@@ -74,6 +77,7 @@ def add_arguments(self, parser):
7477
)
7578

7679
parser.add_argument(
80+
'-t',
7781
'--task-confirmation-tokens',
7882
action='store_true',
7983
default=False,
@@ -89,6 +93,15 @@ def add_arguments(self, parser):
8993
help='Defines maximum number of batches to be processed',
9094
)
9195

96+
parser.add_argument(
97+
'-a',
98+
'--auto-campaign-id',
99+
action='store_true',
100+
default=False,
101+
help='Automatically set CAMPAIGN_KEY to CAMPAIGN_NAME and determine '
102+
'CAMPAIGN_NO as the next available Campaign ID.',
103+
)
104+
92105
def handle(self, *args, **options):
93106
manifest_json = options['manifest_json']
94107
self.stdout.write('JSON manifest path: {0!r}'.format(manifest_json))
@@ -109,6 +122,11 @@ def handle(self, *args, **options):
109122

110123
# Load manifest data, this may raise CommandError
111124
manifest_data = _load_campaign_manifest(manifest_json)
125+
126+
# Handle auto-campaign-id flag
127+
if options['auto_campaign_id']:
128+
manifest_data = _apply_auto_campaign_id(manifest_data, self.stdout)
129+
112130
context = _create_context(manifest_data, stdout=self.stdout)
113131

114132
# By default, we only include activated tasks into agenda creation.
@@ -265,3 +283,61 @@ def _create_campaign(
265283
campaign.batches.add(_campaign_data)
266284
campaign.save()
267285
return campaign
286+
287+
288+
def _apply_auto_campaign_id(manifest_data, stdout=None):
289+
"""Automatically set CAMPAIGN_KEY and CAMPAIGN_NO if requested.
290+
291+
When this function is called, it will override any existing CAMPAIGN_KEY
292+
and CAMPAIGN_NO values in the manifest data.
293+
294+
Parameters:
295+
- manifest_data:dict[str]->any dictionary containing manifest data;
296+
- stdout: output stream for logging.
297+
298+
Returns:
299+
- manifest_data:dict[str]->any updated manifest data with CAMPAIGN_KEY
300+
and CAMPAIGN_NO set.
301+
"""
302+
# Set CAMPAIGN_KEY to CAMPAIGN_NAME
303+
campaign_name = manifest_data.get('CAMPAIGN_NAME')
304+
if not campaign_name:
305+
raise CommandError('CAMPAIGN_NAME must be defined in manifest file')
306+
307+
# Check if campaign with this name already exists
308+
if Campaign.objects.filter(campaignName=campaign_name).exists():
309+
raise CommandError(
310+
'Campaign with name {0!r} already exists. '
311+
'Please use a different CAMPAIGN_NAME or remove the -a flag '
312+
'to use an existing campaign.'.format(campaign_name)
313+
)
314+
315+
# Check if values already exist and warn about override
316+
if 'CAMPAIGN_KEY' in manifest_data and stdout is not None:
317+
stdout.write(
318+
'Warning: Overriding existing CAMPAIGN_KEY {0!r} with {1!r}'.format(
319+
manifest_data['CAMPAIGN_KEY'], campaign_name
320+
)
321+
)
322+
323+
if 'CAMPAIGN_NO' in manifest_data and stdout is not None:
324+
stdout.write(
325+
'Warning: Overriding existing CAMPAIGN_NO {0}'.format(
326+
manifest_data['CAMPAIGN_NO']
327+
)
328+
)
329+
330+
manifest_data['CAMPAIGN_KEY'] = campaign_name
331+
if stdout is not None:
332+
stdout.write('Auto-set CAMPAIGN_KEY to: {0!r}'.format(campaign_name))
333+
334+
# Determine next available CAMPAIGN_NO
335+
# Get the maximum ID from existing campaigns
336+
max_id = Campaign.objects.aggregate(models.Max('id'))['id__max']
337+
next_campaign_no = (max_id or 0) + 1
338+
339+
manifest_data['CAMPAIGN_NO'] = next_campaign_no
340+
if stdout is not None:
341+
stdout.write('Auto-set CAMPAIGN_NO to: {0}'.format(next_campaign_no))
342+
343+
return manifest_data

0 commit comments

Comments
 (0)