pretalx / pretalx-public-voting

Public community voting on talk proposals
Other
4 stars 5 forks source link

Save score only if it has changed #8

Closed vmx closed 3 years ago

vmx commented 4 years ago

I'm not sure if that's the right approach.

An alternative version I had was:

-    timestamp = models.DateTimeField(auto_now=True)
+    timestamp = models.DateTimeField(auto_now_add=True)
…
+    def save(self, *args, **kwargs):
+        # Update the timestamp only if the score has actually changed
+        if self.existing_score != self.score:
+            self.timestamp = timezone.now()
+        super(PublicVote, self).save(*args, **kwargs)
rixx commented 4 years ago

auto_now_add=True makes absolutely sure that you can't override the timestamp later on (unless you bypass the save method entirely), so that wouldn't be an option.

vmx commented 4 years ago

I think a better place for this would be in the form. form.changed_data holds a list of things that have actually changed, and we could test for those.

Thanks, I'll try to put it there.

auto_now_add=True makes absolutely sure that you can't override the timestamp later on (unless you bypass the save method entirely), so that wouldn't be an option.

The above alternative worked for me, I tried it locally. Though I'll try it the recommended way as mentioned above.

vmx commented 4 years ago

I've moved it from the model into the form. Sadly form.has_changed() didn't work as expected, hence I've added a manual comparison.