From 424abc030b1b7674493c10a027fa2b525224caf5 Mon Sep 17 00:00:00 2001 From: Dillon Dumesnil Date: Tue, 10 Aug 2021 13:40:32 -0400 Subject: [PATCH] feat: Add User Activity Model for Course Goals AA-903 --- .../migrations/0005_useractivity.py | 34 +++++++++++++++++++ lms/djangoapps/course_goals/models.py | 27 +++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 lms/djangoapps/course_goals/migrations/0005_useractivity.py diff --git a/lms/djangoapps/course_goals/migrations/0005_useractivity.py b/lms/djangoapps/course_goals/migrations/0005_useractivity.py new file mode 100644 index 0000000000..7a1efe3c83 --- /dev/null +++ b/lms/djangoapps/course_goals/migrations/0005_useractivity.py @@ -0,0 +1,34 @@ +# Generated by Django 2.2.24 on 2021-08-13 17:02 + +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import opaque_keys.edx.django.models + + +class Migration(migrations.Migration): + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ('course_goals', '0004_auto_20210806_0137'), + ] + + operations = [ + migrations.CreateModel( + name='UserActivity', + fields=[ + ('id', models.BigAutoField(primary_key=True, serialize=False)), + ('course_key', opaque_keys.edx.django.models.CourseKeyField(max_length=255)), + ('date', models.DateField()), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), + ], + ), + migrations.AddIndex( + model_name='useractivity', + index=models.Index(fields=['user', 'course_key'], name='user_course_index'), + ), + migrations.AddConstraint( + model_name='useractivity', + constraint=models.UniqueConstraint(fields=('user', 'course_key', 'date'), name='unique_user_course_date'), + ), + ] diff --git a/lms/djangoapps/course_goals/models.py b/lms/djangoapps/course_goals/models.py index 62a80c0483..c2347f7d8c 100644 --- a/lms/djangoapps/course_goals/models.py +++ b/lms/djangoapps/course_goals/models.py @@ -28,10 +28,10 @@ class CourseGoal(models.Model): .. no_pii: """ class Meta: - app_label = "course_goals" - unique_together = ("user", "course_key") + app_label = 'course_goals' + unique_together = ('user', 'course_key') - user = models.ForeignKey(User, blank=False, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) course_key = CourseKeyField(max_length=255, db_index=True) # The goal a user has set for the number of days they want to learn per week days_per_week = models.PositiveIntegerField(default=0) @@ -46,3 +46,24 @@ class CourseGoal(models.Model): goal=self.days_per_week, course=self.course_key, ) + + +class UserActivity(models.Model): + """ + Tracks the date a user performs an activity in a course for goal purposes. + To be used in conjunction with the CourseGoal model to establish if a learner is hitting + their desired days_per_week. + + To start, this model will only be tracking page views that count towards a learner's goal, + but could grow to tracking other types of goal achieving activities in the future. + + .. no_pii: + """ + class Meta: + constraints = [models.UniqueConstraint(fields=['user', 'course_key', 'date'], name='unique_user_course_date')] + indexes = [models.Index(fields=['user', 'course_key'], name='user_course_index')] + + id = models.BigAutoField(primary_key=True) + user = models.ForeignKey(User, on_delete=models.CASCADE) + course_key = CourseKeyField(max_length=255) + date = models.DateField()