diff --git a/completion/models.py b/completion/models.py index d09ea81..23bca04 100644 --- a/completion/models.py +++ b/completion/models.py @@ -120,10 +120,13 @@ def submit_completion(self, user, block_key, completion): block_key=block_key, ) is_new = False + if not is_new and obj.completion != completion: obj.completion = completion obj.full_clean() obj.save(update_fields={'completion', 'modified'}) + + obj.emit_tracking_log() else: # If the feature is not enabled, this method should not be called. # Error out with a RuntimeError. @@ -132,19 +135,6 @@ def submit_completion(self, user, block_key, completion): called when the feature is disabled." ) - tracker.emit( - str(BLOCK_COMPLETION_CHANGED_EVENT_TYPE), - { - 'user_id': user.id, - 'course_id': str(block_key.course_key), - 'context_key': str(context_key), - 'block_key': str(block_key), - 'block_type': block_type, - 'completion': completion, - 'is_new': is_new, - } - ) - return obj, is_new @transaction.atomic() @@ -340,3 +330,18 @@ class Meta: def __unicode__(self): return f'BlockCompletion: {self.user.username}, {self.context_key}, {self.block_key}: {self.completion}' + + def emit_tracking_log(self): + """ + Emit a tracking log when a block completion is created or updated. + """ + tracker.emit( + BLOCK_COMPLETION_CHANGED_EVENT_TYPE, + { + 'user_id': self.user.id, + 'course_id': str(self.context_key), + 'block_id': str(self.block_key), + 'block_type': self.block_type, + 'completion': self.completion, + } + ) diff --git a/completion/tests/test_models.py b/completion/tests/test_models.py index 624f2fe..70533a2 100644 --- a/completion/tests/test_models.py +++ b/completion/tests/test_models.py @@ -41,7 +41,7 @@ def setUp(self): self.set_up_completion() def test_changed_value(self): - with self.assertNumQueries(6): # Get, update, 2 * savepoints, 2 * exists checks + with self.assertNumQueries(7): # 2 * Get, update, 2 * savepoints, 2 * exists checks completion, isnew = models.BlockCompletion.objects.submit_completion( user=self.user, block_key=self.block_key, @@ -53,7 +53,7 @@ def test_changed_value(self): self.assertEqual(models.BlockCompletion.objects.count(), 1) def test_unchanged_value(self): - with self.assertNumQueries(3): # Get + 2 * savepoints + with self.assertNumQueries(4): # 2 * Get + 2 * savepoints completion, isnew = models.BlockCompletion.objects.submit_completion( user=self.user, block_key=self.block_key,