@@ -181,7 +181,11 @@ def init_blank(self, poll):
181
181
poll .votes_for_choice = [0 for _ in poll .choices ]
182
182
183
183
def add_response (self , poll , response ):
184
- poll .votes_for_choice [int (response )] = poll .votes_for_choice [int (response )] + 1
184
+ # Make a new votes_for_choice array with the updated counts, rather than updating
185
+ # in-place, because in-place update confuses the ORM's dirty-values detection
186
+ new_votes = poll .votes_for_choice [:]
187
+ new_votes [int (response )] += 1
188
+ poll .votes_for_choice = new_votes
185
189
186
190
def validate_response (self , poll , response ):
187
191
return self ._check_range (response , int , 0 , len (poll .choices ) - 1 , 'Invalid choice' )
@@ -201,7 +205,9 @@ def init_blank(self, poll):
201
205
poll .votes_for_choice = [0 for _ in range (poll .scalesize )]
202
206
203
207
def add_response (self , poll , response ):
204
- poll .votes_for_choice [int (response )] = poll .votes_for_choice [int (response )] + 1
208
+ new_votes = poll .votes_for_choice [:]
209
+ new_votes [int (response )] += 1
210
+ poll .votes_for_choice = new_votes
205
211
206
212
def validate_response (self , poll , response ):
207
213
return self ._check_range (response , int , 0 , poll .scalesize - 1 , 'Invalid choice' )
@@ -297,10 +303,14 @@ def init_blank(self):
297
303
self .polltype_class ().init_blank (self )
298
304
299
305
def add_response (self , response ):
300
- self .num_votes = self .num_votes + 1
301
- self .polltype_class ().add_response (self , response )
302
- self ._commit ()
306
+ with g .make_lock (self .lock_key ()):
307
+ self .num_votes = self .num_votes + 1
308
+ self .polltype_class ().add_response (self , response )
309
+ self ._commit ()
303
310
311
+ def lock_key (self ):
312
+ return 'poll_lock_' + str (self ._id )
313
+
304
314
def validate_response (self , response ):
305
315
return self .polltype_class ().validate_response (self , response )
306
316
0 commit comments