@@ -54,9 +54,9 @@ def handle(self, *args, **options):
54
54
csv_filename = options ['csv' ]
55
55
56
56
# 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 ))
60
60
61
61
# Valid fares (conference fares only)
62
62
all_fares = cmodels .Fare .objects \
@@ -70,16 +70,18 @@ def handle(self, *args, **options):
70
70
with csv_file :
71
71
reader = csv .DictReader (csv_file )
72
72
for row in reader :
73
+ #print ('Row %r' % row)
73
74
code = row ['code' ].strip ()
74
75
if not code or code == '0' :
75
76
# Skip lines without code
76
77
continue
77
78
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
83
85
c .max_usage = int (row .get ('max_usage' , 1 ))
84
86
c .items_per_usage = int (row .get ('items_per_usage' , 1 ))
85
87
c .value = row ['value' ]
@@ -89,4 +91,3 @@ def handle(self, *args, **options):
89
91
c .fares .set (all_fares .filter (
90
92
code__in = [x .strip ()
91
93
for x in row ['fares' ].split (',' )]))
92
- print ('Coupon %r created' % c .code )
0 commit comments