Fix: update new runtime's runtime.anonymous_student_id to store in DB

This addresses a longstanding TODO item to make runtime.anonymous_student_id for content libraries v2 work the same way as it does for XBlocks in regular courses, persisting the "context ID" (equivalent to course ID) to the database. This way, if SECRET KEY is changed, existing anonymous IDs will continue to work unchanged.

This is a potentially breaking change, but should mostly affect capa problems using external code graders or Matlab code input, and I'm not aware of any such usage of the new runtime / libraries v2.
This commit is contained in:
Braden MacDonald
2021-01-21 15:38:53 -08:00
parent 812cd504de
commit ae16394ee5
3 changed files with 35 additions and 17 deletions

View File

@@ -0,0 +1,30 @@
# Convert the student.models.AnonymousUserId.course_id field from CourseKey to
# the more generic LearningContextKey.
#
# This migration does not produce any changes at the database level.
from django.db import migrations
import opaque_keys.edx.django.models
class Migration(migrations.Migration):
dependencies = [
('student', '0038_auto_20201021_1256'),
]
operations = [
migrations.SeparateDatabaseAndState(
# Do not actually make any changes to the database; the fields are identical string fields with the same
# database properties.
database_operations=[],
# But update the migrator's view of the field to reflect the new field type.
state_operations=[
migrations.AlterField(
model_name='anonymoususerid',
name='course_id',
field=opaque_keys.edx.django.models.LearningContextKeyField(blank=True, db_index=True, max_length=255),
),
],
),
]

View File

@@ -46,7 +46,7 @@ from edx_django_utils.cache import RequestCache
from edx_rest_api_client.exceptions import SlumberBaseException
from eventtracking import tracker
from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField
from opaque_keys.edx.django.models import CourseKeyField, LearningContextKeyField
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from simple_history.models import HistoricalRecords
@@ -143,7 +143,7 @@ class AnonymousUserId(models.Model):
user = models.ForeignKey(User, db_index=True, on_delete=models.CASCADE)
anonymous_user_id = models.CharField(unique=True, max_length=32)
course_id = CourseKeyField(db_index=True, max_length=255, blank=True)
course_id = LearningContextKeyField(db_index=True, max_length=255, blank=True)
def anonymous_id_for_user(user, course_id, save=True):