Skip to content

Commit

Permalink
adding enrollment change tracking (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav authored Mar 6, 2024
1 parent 26fa0c5 commit afdd7e1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
1 change: 1 addition & 0 deletions courses/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class CourseRunEnrollmentAuditInline(admin.TabularInline):
"enrollment_id",
"created_on",
"acting_user",
"call_stack",
"data_before",
"data_after",
]
Expand Down
28 changes: 28 additions & 0 deletions courses/migrations/0045_audit_modified_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.23 on 2024-02-29 18:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("courses", "0044_alter_courserun_live"),
]

operations = [
migrations.AddField(
model_name="courserunenrollmentaudit",
name="modified_by",
field=models.CharField(blank=True, default="", max_length=750, null=True),
),
migrations.AddField(
model_name="courserungradeaudit",
name="modified_by",
field=models.CharField(blank=True, default="", max_length=750, null=True),
),
migrations.AddField(
model_name="programenrollmentaudit",
name="modified_by",
field=models.CharField(blank=True, default="", max_length=750, null=True),
),
]
28 changes: 28 additions & 0 deletions courses/migrations/0046_audit_rename_call_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 3.2.23 on 2024-03-06 13:41

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("courses", "0045_audit_modified_by"),
]

operations = [
migrations.RenameField(
model_name="courserunenrollmentaudit",
old_name="modified_by",
new_name="call_stack",
),
migrations.RenameField(
model_name="courserungradeaudit",
old_name="modified_by",
new_name="call_stack",
),
migrations.RenameField(
model_name="programenrollmentaudit",
old_name="modified_by",
new_name="call_stack",
),
]
9 changes: 4 additions & 5 deletions ecommerce/management/commands/refund_fulfilled_order.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Looks up a fulfilled order in the system, sets it to Refunded, and then adjusts
the enrollments accordingly.
the enrollments accordingly.
- If --unenroll is specified, the learner will be unenrolled from the course run
associated with the order.
Expand All @@ -9,8 +9,8 @@
This does not make any sort of call to CyberSource or any other payment gateway
to perform a refund - you're expected to have refunded the learner's money
manually already. (At time of writing, PayPal transactions can't be refunded
using the normal means, so they get refunded manually via CyberSource and then
manually already. (At time of writing, PayPal transactions can't be refunded
using the normal means, so they get refunded manually via CyberSource and then
this command comes in to clean up afterwards.)
"""
Expand Down Expand Up @@ -83,8 +83,7 @@ def handle(self, *args, **kwargs):
f"Changing enrollment for {order.purchaser.username} in {enrollment.run} to 'audit'"
)

enrollment.enrollment_mode = "audit"
enrollment.save()
enrollment.update_mode_and_save("audit")

enroll_in_edx_course_runs(
order.purchaser, [enrollment.run], mode="audit"
Expand Down
11 changes: 10 additions & 1 deletion main/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""
Common model classes
"""
import traceback

from django.conf import settings
from django.db import transaction
from django.db.models import (
CASCADE,
CharField,
DateTimeField,
ForeignKey,
JSONField,
Expand All @@ -18,6 +21,7 @@ class AuditModel(TimestampedModel):
"""An abstract base class for audit models"""

acting_user = ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=CASCADE)
call_stack = CharField(default="", max_length=750, null=True, blank=True)
data_before = JSONField(blank=True, null=True)
data_after = JSONField(blank=True, null=True)

Expand Down Expand Up @@ -84,8 +88,13 @@ def save_and_log(self, acting_user, *args, **kwargs):
if before_obj is not None:
before_dict = before_obj.to_dict()

call_stack = "".join(traceback.format_stack()[-6:-2])

audit_kwargs = dict(
acting_user=acting_user, data_before=before_dict, data_after=self.to_dict()
acting_user=acting_user,
call_stack=call_stack,
data_before=before_dict,
data_after=self.to_dict(),
)
audit_class = self.get_audit_class()
audit_kwargs[audit_class.get_related_field_name()] = self
Expand Down
1 change: 1 addition & 0 deletions main/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_get_field_names():
"acting_user",
"created_on",
"updated_on",
"call_stack",
}


Expand Down

0 comments on commit afdd7e1

Please sign in to comment.