Skip to content
This repository was archived by the owner on Apr 29, 2022. It is now read-only.

Commit ad14175

Browse files
authored
Add update functionality to the bulk coupon script (#1395)
1 parent 3ad73b5 commit ad14175

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

p3/management/commands/create_bulk_coupons.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def handle(self, *args, **options):
5454
csv_filename = options['csv']
5555

5656
# Get set of existing coupon codes
57-
all_codes = set(c['code'] for c in Coupon.objects\
58-
.filter(conference=conference.code)\
59-
.values('code'))
57+
all_codes = dict((c.code, c)
58+
for c in Coupon.objects\
59+
.filter(conference=conference.code))
6060

6161
# Valid fares (conference fares only)
6262
all_fares = cmodels.Fare.objects\
@@ -70,16 +70,18 @@ def handle(self, *args, **options):
7070
with csv_file:
7171
reader = csv.DictReader(csv_file)
7272
for row in reader:
73+
#print ('Row %r' % row)
7374
code = row['code'].strip()
7475
if not code or code == '0':
7576
# Skip lines without code
7677
continue
7778
if code in all_codes:
78-
# Skip coupons which already exist
79-
print ('Coupon %r already exists - skipping' % code)
80-
continue
81-
c = Coupon(conference=conference)
82-
c.code = code
79+
print ('Coupon %r already exists - updating' % code)
80+
c = all_codes[code]
81+
else:
82+
print ('New coupon %r will be created' % c.code)
83+
c = Coupon(conference=conference)
84+
c.code = code
8385
c.max_usage = int(row.get('max_usage', 1))
8486
c.items_per_usage = int(row.get('items_per_usage', 1))
8587
c.value = row['value']
@@ -89,4 +91,3 @@ def handle(self, *args, **options):
8991
c.fares.set(all_fares.filter(
9092
code__in = [x.strip()
9193
for x in row['fares'].split(',')]))
92-
print ('Coupon %r created' % c.code)

0 commit comments

Comments
 (0)