Merge pull request #20222 from edx/bmedx/course_enrollment_history

Re-adding django-simple-history for CourseEnrollment
This commit is contained in:
Brian Mesick
2019-05-09 13:02:07 -04:00
committed by GitHub
9 changed files with 116 additions and 27 deletions

View File

@@ -545,6 +545,9 @@ MIDDLEWARE_CLASSES = [
'edx_rest_framework_extensions.auth.jwt.middleware.EnsureJWTAuthSettingsMiddleware',
# Handles automatically storing user ids in django-simple-history tables when possible.
'simple_history.middleware.HistoryRequestMiddleware',
# This must be last so that it runs first in the process_response chain
'openedx.core.djangoapps.site_configuration.middleware.SessionCookieDomainOverrideMiddleware',
]

View File

@@ -3,6 +3,8 @@ Configuration for the ``student`` Django application.
"""
from __future__ import absolute_import
import os
from django.apps import AppConfig
from django.contrib.auth.signals import user_logged_in
from django.db.models.signals import pre_save
@@ -23,3 +25,11 @@ class StudentConfig(AppConfig):
from django.contrib.auth.models import User
from .signals.receivers import on_user_updated
pre_save.connect(on_user_updated, sender=User)
# The django-simple-history model on CourseEnrollment creates performance
# problems in testing, we mock it here so that the mock impacts all tests.
if os.environ.get('DISABLE_COURSEENROLLMENT_HISTORY', False):
import student.models as student_models
from mock import MagicMock
student_models.CourseEnrollment.history = MagicMock()

View File

@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-04-25 20:18
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models
import uuid
class Migration(migrations.Migration):
dependencies = [
('course_overviews', '0014_courseoverview_certificate_available_date'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('student', '0020_auto_20190227_2019'),
]
operations = [
migrations.CreateModel(
name='HistoricalCourseEnrollment',
fields=[
('id', models.IntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('created', models.DateTimeField(blank=True, db_index=True, editable=False, null=True)),
('is_active', models.BooleanField(default=True)),
('mode', models.CharField(default=b'audit', max_length=100)),
('history_id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('history_date', models.DateTimeField()),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('course', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to='course_overviews.CourseOverview')),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('user', models.ForeignKey(blank=True, db_constraint=False, null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ('-history_date', '-history_id'),
'db_table': 'student_courseenrollment_history',
'verbose_name': 'historical course enrollment',
'get_latest_by': 'history_date',
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
]

View File

@@ -47,6 +47,7 @@ from model_utils.models import TimeStampedModel
from opaque_keys.edx.django.models import CourseKeyField
from opaque_keys.edx.keys import CourseKey
from pytz import UTC
from simple_history.models import HistoricalRecords
from six import text_type
from six.moves import range
from six.moves.urllib.parse import urlencode
@@ -1130,6 +1131,14 @@ class CourseEnrollment(models.Model):
# list of possible values.
mode = models.CharField(default=CourseMode.DEFAULT_MODE_SLUG, max_length=100)
# An audit row will be created for every change to a CourseEnrollment. This
# will create a new model behind the scenes - HistoricalCourseEnrollment and a
# table named 'student_courseenrollment_history'.
history = HistoricalRecords(
history_id_field=models.UUIDField(default=uuid.uuid4),
table_name='student_courseenrollment_history'
)
objects = CourseEnrollmentManager()
# cache key format e.g enrollment.<username>.<course_key>.mode = 'honor'

View File

@@ -1680,14 +1680,8 @@ class GroupModeratorPermissionsTestCase(ModuleStoreTestCase):
# Create course, seed permissions roles, and create team
self.course = CourseFactory.create()
seed_permissions_roles(self.course.id)
verified_coursemode = CourseModeFactory.create(
course_id=self.course.id,
mode_slug=CourseMode.VERIFIED
)
audit_coursemode = CourseModeFactory.create(
course_id=self.course.id,
mode_slug=CourseMode.AUDIT
)
verified_coursemode = CourseMode.VERIFIED
audit_coursemode = CourseMode.AUDIT
# Create four users: group_moderator (who is within the verified enrollment track and in the cohort),
# verified_user (who is in the verified enrollment track but not the cohort),
@@ -1781,18 +1775,6 @@ class GroupModeratorPermissionsTestCase(ModuleStoreTestCase):
'can_vote': True,
'can_report': True
})
RequestCache.clear_all_namespaces()
set_discussion_division_settings(self.course.id, division_scheme=CourseDiscussionSettings.ENROLLMENT_TRACK)
content = {'user_id': self.verified_user.id, 'type': 'thread', 'username': self.verified_user.username}
self.assertEqual(utils.get_ability(self.course.id, content, self.group_moderator), {
'editable': True,
'can_reply': True,
'can_delete': True,
'can_openclose': True,
'can_vote': True,
'can_report': True
})
@mock.patch(
'lms.djangoapps.discussion.django_comment_client.permissions._check_condition',

View File

@@ -1324,6 +1324,9 @@ MIDDLEWARE_CLASSES = [
'edx_rest_framework_extensions.auth.jwt.middleware.EnsureJWTAuthSettingsMiddleware',
# Handles automatically storing user ids in django-simple-history tables when possible.
'simple_history.middleware.HistoryRequestMiddleware',
# This must be last
'openedx.core.djangoapps.site_configuration.middleware.SessionCookieDomainOverrideMiddleware',
]

View File

@@ -55,10 +55,12 @@ class TestPaverPytestCmd(unittest.TestCase):
else:
django_env_var_cmd = "export DJANGO_SETTINGS_MODULE='openedx.tests.settings'"
env_var_cmd = "{} DISABLE_COURSEENROLLMENT_HISTORY=1".format(django_env_var_cmd)
xdist_string = u'--tx {}*ssh="ubuntu@{} -o StrictHostKeyChecking=no"' \
'//python="source /edx/app/edxapp/edxapp_env; {}; python"' \
'//chdir="/edx/app/edxapp/edx-platform"' \
.format(processes, ip, django_env_var_cmd)
.format(processes, ip, env_var_cmd)
expected_statement.append(xdist_string)
for rsync_dir in Env.rsync_dirs():
expected_statement.append(u'--rsyncdir {}'.format(rsync_dir))

View File

@@ -70,6 +70,19 @@ __test__ = False # do not collect
dest='disable_migrations',
help="Create tables by applying migrations."
),
make_option(
'--disable_courseenrollment_history',
action='store_true',
dest='disable_courseenrollment_history',
help="Disable history on student.CourseEnrollent. Can also be used by exporting"
"DISABLE_COURSEENROLLMENT_HISTORY=1."
),
make_option(
'--enable_courseenrollment_history',
action='store_false',
dest='disable_courseenrollment_history',
help="Enable django-simple-history on student.CourseEnrollment."
),
make_option(
'--xdist_ip_addresses',
dest='xdist_ip_addresses',
@@ -230,6 +243,19 @@ def test_lib(options, passthrough_options):
dest='disable_migrations',
help="Create tables directly from apps' models. Can also be used by exporting DISABLE_MIGRATIONS=1."
),
make_option(
'--disable_courseenrollment_history',
action='store_true',
dest='disable_courseenrollment_history',
help="Disable history on student.CourseEnrollent. Can also be used by exporting"
"DISABLE_COURSEENROLLMENT_HISTORY=1."
),
make_option(
'--enable_courseenrollment_history',
action='store_false',
dest='disable_courseenrollment_history',
help="Enable django-simple-history on student.CourseEnrollment."
),
])
@PassthroughTask
@timed

View File

@@ -26,6 +26,7 @@ class PytestSuite(TestSuite):
self.django_toxenv = None
else:
self.django_toxenv = 'py27-django{}'.format(django_version.replace('.', ''))
self.disable_courseenrollment_history = kwargs.get('disable_courseenrollment_history', '1')
self.disable_capture = kwargs.get('disable_capture', None)
self.report_dir = Env.REPORT_DIR / self.root
@@ -36,6 +37,10 @@ class PytestSuite(TestSuite):
if os.environ.get("SHARD", None):
shard_str = "shard_{}".format(os.environ.get("SHARD"))
self.report_dir = self.report_dir / shard_str
if self.disable_courseenrollment_history:
os.environ['DISABLE_COURSEENROLLMENT_HISTORY'] = '1'
self.xunit_report = self.report_dir / "nosetests.xml"
self.cov_args = kwargs.get('cov_args', '')
@@ -161,13 +166,14 @@ class SystemTestSuite(PytestSuite):
else:
xdist_remote_processes = self.processes
for ip in self.xdist_ip_addresses.split(','):
# The django settings runtime command does not propagate to xdist remote workers
django_env_var_cmd = u'export DJANGO_SETTINGS_MODULE={}' \
.format('{}.envs.{}'.format(self.root, self.settings))
# Propogate necessary env vars to xdist containers
env_var_cmd = u'export DJANGO_SETTINGS_MODULE={} DISABLE_COURSEENROLLMENT_HISTORY={}'\
.format('{}.envs.{}'.format(self.root, self.settings),
self.disable_courseenrollment_history)
xdist_string = u'--tx {}*ssh="ubuntu@{} -o StrictHostKeyChecking=no"' \
'//python="source /edx/app/edxapp/edxapp_env; {}; python"' \
'//chdir="/edx/app/edxapp/edx-platform"' \
.format(xdist_remote_processes, ip, django_env_var_cmd)
.format(xdist_remote_processes, ip, env_var_cmd)
cmd.append(xdist_string)
for rsync_dir in Env.rsync_dirs():
cmd.append(u'--rsyncdir {}'.format(rsync_dir))
@@ -280,15 +286,19 @@ class LibTestSuite(PytestSuite):
else:
xdist_remote_processes = self.processes
for ip in self.xdist_ip_addresses.split(','):
# The django settings runtime command does not propagate to xdist remote workers
# Propogate necessary env vars to xdist containers
if 'pavelib/paver_tests' in self.test_id:
django_env_var_cmd = "export DJANGO_SETTINGS_MODULE='lms.envs.test'"
else:
django_env_var_cmd = "export DJANGO_SETTINGS_MODULE='openedx.tests.settings'"
env_var_cmd = u'{} DISABLE_COURSEENROLLMENT_HISTORY={}' \
.format(django_env_var_cmd, self.disable_courseenrollment_history)
xdist_string = u'--tx {}*ssh="ubuntu@{} -o StrictHostKeyChecking=no"' \
'//python="source /edx/app/edxapp/edxapp_env; {}; python"' \
'//chdir="/edx/app/edxapp/edx-platform"' \
.format(xdist_remote_processes, ip, django_env_var_cmd)
.format(xdist_remote_processes, ip, env_var_cmd)
cmd.append(xdist_string)
for rsync_dir in Env.rsync_dirs():
cmd.append(u'--rsyncdir {}'.format(rsync_dir))