-
Notifications
You must be signed in to change notification settings - Fork 17
Changed indexing structure and logic to use a user's program enrollments #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,27 @@ | ||
| """ | ||
| Signals for user profiles | ||
| """ | ||
| from django.db.models.signals import pre_save, post_save | ||
| from django.db.models.signals import pre_save, post_save, post_delete | ||
| from django.dispatch import receiver | ||
|
|
||
| from dashboard.models import CachedEnrollment, ProgramEnrollment | ||
| from dashboard.models import CachedEnrollment, CachedCertificate, ProgramEnrollment | ||
| from search.tasks import index_program_enrolled_users, index_users, remove_program_enrolled_user | ||
|
|
||
|
|
||
| @receiver(post_save, sender=ProgramEnrollment, dispatch_uid="programenrollment_post_save") | ||
| def handle_create_programenrollment(sender, instance, created, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| When a ProgramEnrollment model is created/updated, update index. | ||
| """ | ||
| index_program_enrolled_users.delay([instance]) | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=ProgramEnrollment, dispatch_uid="programenrollment_post_delete") | ||
| def handle_delete_programenrollment(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| When a ProgramEnrollment model is deleted, update index. | ||
| """ | ||
| remove_program_enrolled_user.delay(instance) | ||
|
|
||
|
|
||
| @receiver(pre_save, sender=CachedEnrollment, dispatch_uid="preupdate_programenrollment") | ||
|
|
@@ -25,27 +42,65 @@ def precreate_programenrollment(sender, instance, **kwargs): # pylint: disable= | |
| instance_in_db = CachedEnrollment.objects.filter(id=instance.id).exclude(data__isnull=True).count() | ||
| # if the count is 1, it means the student unenrolled from the course run | ||
| if instance_in_db == 1: | ||
| active_enrollment_count = CachedEnrollment.objects.filter( | ||
| user=user, | ||
| course_run__course__program=program | ||
| ).exclude(data__isnull=True).count() | ||
| # if there is only one enrollment with data non None, it means that it is the | ||
| # current instance is the only one for the program, so the program enrollment | ||
| # needs to be deleted | ||
| if active_enrollment_count <= 1: # theoretically this cannot be <1, but just in case | ||
| if CachedEnrollment.active_count(user, program) <= 1: # theoretically this cannot be <1, but just in case | ||
| ProgramEnrollment.objects.filter( | ||
| user=user, | ||
| program=program | ||
| ).delete() | ||
|
|
||
|
|
||
| @receiver(post_save, sender=CachedEnrollment, dispatch_uid="update_programenrollment") | ||
| def create_programenrollment(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| @receiver(post_save, sender=CachedEnrollment, dispatch_uid="cachedenrollment_post_save") | ||
| def handle_update_enrollment(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand why you changed the name of this signal
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's doing a few different things now. it's creating (or getting) a program enrollment, and it's reindexing based on that enrollment. it's not just creating a ProgramEnrollment anymore, so it makes sense to change the name
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please make sure that all the |
||
| """ | ||
| Create ProgramEnrollment when a CachedEnrollment is created/updated, and update the index. | ||
| """ | ||
| if instance.data is not None: | ||
| program_enrollment, _ = ProgramEnrollment.objects.get_or_create( | ||
| user=instance.user, | ||
| program=instance.course_run.course.program | ||
| ) | ||
| index_program_enrolled_users.delay([program_enrollment]) | ||
|
|
||
|
|
||
| @receiver(post_save, sender=CachedCertificate, dispatch_uid="cachedcertificate_post_save") | ||
| def handle_update_certificate(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| Signal handler to create Program enrollment when the CachedEnrollment table is updated | ||
| When a CachedCertificate model is updated, update index. | ||
| """ | ||
| if instance.data is not None: | ||
| ProgramEnrollment.objects.get_or_create( | ||
| program_enrollment, _ = ProgramEnrollment.objects.get_or_create( | ||
| user=instance.user, | ||
| program=instance.course_run.course.program | ||
| ) | ||
| index_program_enrolled_users.delay([program_enrollment]) | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=CachedEnrollment, dispatch_uid="cachedenrollment_post_delete") | ||
| def handle_delete_enrollment(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| Update index when CachedEnrollment model instance is deleted. | ||
| """ | ||
| user = instance.user | ||
| program = instance.course_run.course.program | ||
| program_enrollment = ProgramEnrollment.objects.filter(user=user, program=program).first() | ||
| if program_enrollment is not None: | ||
| if CachedEnrollment.active_count(user, program) == 0: | ||
| program_enrollment.delete() | ||
| index_users.delay([user]) | ||
| else: | ||
| index_program_enrolled_users.delay([program_enrollment]) | ||
|
|
||
|
|
||
| @receiver(post_delete, sender=CachedCertificate, dispatch_uid="cachedcertificate_post_delete") | ||
| def handle_delete_certificate(sender, instance, **kwargs): # pylint: disable=unused-argument | ||
| """ | ||
| Update index when CachedCertificate model instance is deleted. | ||
| """ | ||
| user = instance.user | ||
| program = instance.course_run.course.program | ||
| program_enrollment = ProgramEnrollment.objects.filter(user=user, program=program).first() | ||
| if program_enrollment is not None: | ||
| index_program_enrolled_users.delay([program_enrollment]) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,15 @@ def webpack_dev_server_url(request): | |
| return 'http://{}:{}'.format(webpack_dev_server_host(request), settings.WEBPACK_DEV_SERVER_PORT) | ||
|
|
||
|
|
||
| def dict_without_key(dictionary, key): | ||
| """ | ||
| Helper method to remove a key from a dict and return the dict. This can be used in cases like a list comprehension | ||
| where the actual dictionary is needed once the key is deleted ('del' does not return anything) | ||
| """ | ||
| del dictionary[key] | ||
| return dictionary | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the benefit of doing this instead of just calling
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, returning the dictionary here is useless, because the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this used to be a part of search/api_test.py (line). its being used in a list comprehension, so the function has to return the actual dict after removing the key for it to work correctly. this definitely could use a better name -
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, I understand now... maybe try to add what you just said in the docstring.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring looks good to me ("Helper method to remove a key from a dict and return the dict"). describes exactly what it's doing. function name is now
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is to put some description of why someone would use this function instead of calling directly |
||
|
|
||
|
|
||
| def load_json_from_file(project_rel_filepath): | ||
| """ | ||
| Loads JSON data from a file | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically you do not need to create a certificate for the user.
edit: I mean for the sole purpose of the
ProgramEnrollmentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically no, but it seems that in other test cases, we prefer to have an enrollment and a certificate. i put this in for simple convenience
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok