-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored signals to their own module
- Loading branch information
Dave St.Germain
committed
Jan 3, 2019
1 parent
f238f04
commit 0a8669a
Showing
4 changed files
with
120 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
"edx-proctoring signals" | ||
from django.db.models.signals import pre_save, post_save, pre_delete | ||
from django.dispatch import receiver | ||
from edx_proctoring import models | ||
from edx_proctoring.backends import get_backend_provider | ||
|
||
|
||
@receiver(post_save, sender=models.ProctoredExamReviewPolicy) | ||
@receiver(post_save, sender=models.ProctoredExam) | ||
def _save_exam_on_backend(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
""" | ||
Save the exam to the backend provider when our model changes. | ||
It also combines the review policy into the exam when saving to the backend | ||
""" | ||
if sender == models.ProctoredExam: | ||
exam_obj = instance | ||
review_policy = models.ProctoredExamReviewPolicy.get_review_policy_for_exam(instance.id) | ||
else: | ||
exam_obj = instance.proctored_exam | ||
review_policy = instance | ||
if exam_obj.is_proctored: | ||
from edx_proctoring.serializers import ProctoredExamSerializer | ||
exam = ProctoredExamSerializer(exam_obj).data | ||
if review_policy: | ||
exam['rule_summary'] = review_policy.review_policy | ||
backend = get_backend_provider(exam) | ||
external_id = backend.on_exam_saved(exam) | ||
if external_id and external_id != exam_obj.external_id: | ||
exam_obj.external_id = external_id | ||
exam_obj.save() | ||
|
||
|
||
# Hook up the pre_save signal to record creations in the ProctoredExamReviewPolicyHistory table. | ||
@receiver(pre_save, sender=models.ProctoredExamReviewPolicy) | ||
@receiver(pre_delete, sender=models.ProctoredExamReviewPolicy) | ||
def on_review_policy_changed(sender, instance, signal, **kwargs): # pylint: disable=unused-argument | ||
""" | ||
Archiving all changes made to the Review Policy. | ||
Will only archive on update/delete, and not on new entries created. | ||
""" | ||
if signal is pre_save: | ||
if instance.id: | ||
instance = sender.objects.get(id=instance.id) | ||
else: | ||
return | ||
models.archive_model(models.ProctoredExamReviewPolicyHistory, instance, id='original_id') | ||
|
||
|
||
# Hook up the post_save signal to record creations in the ProctoredExamStudentAllowanceHistory table. | ||
@receiver(pre_save, sender=models.ProctoredExamStudentAllowance) | ||
@receiver(pre_delete, sender=models.ProctoredExamStudentAllowance) | ||
def on_allowance_saved(sender, instance, signal, **kwargs): # pylint: disable=unused-argument | ||
""" | ||
Archiving all changes made to the Student Allowance. | ||
Will only archive on update/delete, and not on new entries created. | ||
""" | ||
|
||
if signal is pre_save: | ||
if instance.id: | ||
instance = sender.objects.get(id=instance.id) | ||
else: | ||
return | ||
models.archive_model(models.ProctoredExamStudentAllowanceHistory, instance, id='allowance_id') | ||
|
||
|
||
@receiver(pre_save, sender=models.ProctoredExamStudentAttempt) | ||
@receiver(pre_delete, sender=models.ProctoredExamStudentAttempt) | ||
def on_attempt_updated(sender, instance, signal, **kwargs): # pylint: disable=unused-argument | ||
""" | ||
Archive the exam attempt whenever the attempt status is about to be | ||
modified. Make a new entry with the previous value of the status in the | ||
ProctoredExamStudentAttemptHistory table. | ||
""" | ||
|
||
if signal is pre_save: | ||
if instance.id: | ||
# on an update case, get the original | ||
# and see if the status has changed, if so, then we need | ||
# to archive it | ||
original = sender.objects.get(id=instance.id) | ||
|
||
if original.status != instance.status: | ||
instance = original | ||
else: | ||
return | ||
else: | ||
return | ||
models.archive_model(models.ProctoredExamStudentAttemptHistory, instance, id='attempt_id') | ||
|
||
|
||
# Hook up the signals to record updates/deletions in the ProctoredExamStudentAllowanceHistory table. | ||
@receiver(pre_save, sender=models.ProctoredExamSoftwareSecureReview) | ||
@receiver(pre_delete, sender=models.ProctoredExamSoftwareSecureReview) | ||
def on_review_saved(sender, instance, signal, **kwargs): # pylint: disable=unused-argument | ||
""" | ||
Archiving all changes made to the Review. | ||
Will only archive on update/delete, and not on new entries created. | ||
""" | ||
if signal is pre_save: | ||
if instance.id: | ||
# only for update cases | ||
instance = sender.objects.get(id=instance.id) | ||
else: | ||
# don't archive on create | ||
return | ||
models.archive_model(models.ProctoredExamSoftwareSecureReviewHistory, instance, id='review_id') |