Skip to content

Commit fea4794

Browse files
author
James Babcock
committed
(UNTESTED) fix for poll vote-total persistence
1 parent c09863d commit fea4794

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

Diff for: r2/r2/models/poll.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,11 @@ def init_blank(self, poll):
181181
poll.votes_for_choice = [0 for _ in poll.choices]
182182

183183
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
185189

186190
def validate_response(self, poll, response):
187191
return self._check_range(response, int, 0, len(poll.choices) - 1, 'Invalid choice')
@@ -201,7 +205,9 @@ def init_blank(self, poll):
201205
poll.votes_for_choice = [0 for _ in range(poll.scalesize)]
202206

203207
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
205211

206212
def validate_response(self, poll, response):
207213
return self._check_range(response, int, 0, poll.scalesize - 1, 'Invalid choice')
@@ -297,10 +303,14 @@ def init_blank(self):
297303
self.polltype_class().init_blank(self)
298304

299305
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()
303310

311+
def lock_key(self):
312+
return 'poll_lock_' + str(self._id)
313+
304314
def validate_response(self, response):
305315
return self.polltype_class().validate_response(self, response)
306316

0 commit comments

Comments
 (0)