From 3d303518eb3f7c327616cb99e45ea5904fe8b23a Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Fri, 25 Jan 2013 12:51:34 -0500 Subject: [PATCH] Remove old user replication code. - we don't use it, and if we do want to change the arch, we should have a proper central user info service. --- common/djangoapps/student/models.py | 164 ---------------------------- 1 file changed, 164 deletions(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 00819d7dc4..611b6effbe 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -704,167 +704,3 @@ def update_user_information(sender, instance, created, **kwargs): log.error(unicode(e)) log.error("update user info to discussion failed for user with id: " + str(instance.id)) - -########################## REPLICATION SIGNALS ################################# -# @receiver(post_save, sender=User) -def replicate_user_save(sender, **kwargs): - user_obj = kwargs['instance'] - if not should_replicate(user_obj): - return - for course_db_name in db_names_to_replicate_to(user_obj.id): - replicate_user(user_obj, course_db_name) - - -# @receiver(post_save, sender=CourseEnrollment) -def replicate_enrollment_save(sender, **kwargs): - """This is called when a Student enrolls in a course. It has to do the - following: - - 1. Make sure the User is copied into the Course DB. It may already exist - (someone deleting and re-adding a course). This has to happen first or - the foreign key constraint breaks. - 2. Replicate the CourseEnrollment. - 3. Replicate the UserProfile. - """ - if not is_portal(): - return - - enrollment_obj = kwargs['instance'] - log.debug("Replicating user because of new enrollment") - for course_db_name in db_names_to_replicate_to(enrollment_obj.user.id): - replicate_user(enrollment_obj.user, course_db_name) - - log.debug("Replicating enrollment because of new enrollment") - replicate_model(CourseEnrollment.save, enrollment_obj, enrollment_obj.user_id) - - log.debug("Replicating user profile because of new enrollment") - user_profile = UserProfile.objects.get(user_id=enrollment_obj.user_id) - replicate_model(UserProfile.save, user_profile, enrollment_obj.user_id) - - -# @receiver(post_delete, sender=CourseEnrollment) -def replicate_enrollment_delete(sender, **kwargs): - enrollment_obj = kwargs['instance'] - return replicate_model(CourseEnrollment.delete, enrollment_obj, enrollment_obj.user_id) - - -# @receiver(post_save, sender=UserProfile) -def replicate_userprofile_save(sender, **kwargs): - """We just updated the UserProfile (say an update to the name), so push that - change to all Course DBs that we're enrolled in.""" - user_profile_obj = kwargs['instance'] - return replicate_model(UserProfile.save, user_profile_obj, user_profile_obj.user_id) - - -######### Replication functions ######### -USER_FIELDS_TO_COPY = ["id", "username", "first_name", "last_name", "email", - "password", "is_staff", "is_active", "is_superuser", - "last_login", "date_joined"] - - -def replicate_user(portal_user, course_db_name): - """Replicate a User to the correct Course DB. This is more complicated than - it should be because Askbot extends the auth_user table and adds its own - fields. So we need to only push changes to the standard fields and leave - the rest alone so that Askbot changes at the Course DB level don't get - overridden. - """ - try: - course_user = User.objects.using(course_db_name).get(id=portal_user.id) - log.debug("User {0} found in Course DB, replicating fields to {1}" - .format(course_user, course_db_name)) - except User.DoesNotExist: - log.debug("User {0} not found in Course DB, creating copy in {1}" - .format(portal_user, course_db_name)) - course_user = User() - - for field in USER_FIELDS_TO_COPY: - setattr(course_user, field, getattr(portal_user, field)) - - mark_handled(course_user) - course_user.save(using=course_db_name) - unmark(course_user) - - -def replicate_model(model_method, instance, user_id): - """ - model_method is the model action that we want replicated. For instance, - UserProfile.save - """ - if not should_replicate(instance): - return - - course_db_names = db_names_to_replicate_to(user_id) - log.debug("Replicating {0} for user {1} to DBs: {2}" - .format(model_method, user_id, course_db_names)) - - mark_handled(instance) - for db_name in course_db_names: - model_method(instance, using=db_name) - unmark(instance) - - -######### Replication Helpers ######### - - -def is_valid_course_id(course_id): - """Right now, the only database that's not a course database is 'default'. - I had nicer checking in here originally -- it would scan the courses that - were in the system and only let you choose that. But it was annoying to run - tests with, since we don't have course data for some for our course test - databases. Hence the lazy version. - """ - return course_id != 'default' - - -def is_portal(): - """Are we in the portal pool? Only Portal servers are allowed to replicate - their changes. For now, only Portal servers see multiple DBs, so we use - that to decide.""" - return len(settings.DATABASES) > 1 - - -def db_names_to_replicate_to(user_id): - """Return a list of DB names that this user_id is enrolled in.""" - return [c.course_id - for c in CourseEnrollment.objects.filter(user_id=user_id) - if is_valid_course_id(c.course_id)] - - -def marked_handled(instance): - """Have we marked this instance as being handled to avoid infinite loops - caused by saving models in post_save hooks for the same models?""" - return hasattr(instance, '_do_not_copy_to_course_db') and instance._do_not_copy_to_course_db - - -def mark_handled(instance): - """You have to mark your instance with this function or else we'll go into - an infinite loop since we're putting listeners on Model saves/deletes and - the act of replication requires us to call the same model method. - - We create a _replicated attribute to differentiate the first save of this - model vs. the duplicate save we force on to the course database. Kind of - a hack -- suggestions welcome. - """ - instance._do_not_copy_to_course_db = True - - -def unmark(instance): - """If we don't unmark a model after we do replication, then consecutive - save() calls won't be properly replicated.""" - instance._do_not_copy_to_course_db = False - - -def should_replicate(instance): - """Should this instance be replicated? We need to be a Portal server and - the instance has to not have been marked_handled.""" - if marked_handled(instance): - # Basically, avoid an infinite loop. You should - log.debug("{0} should not be replicated because it's been marked" - .format(instance)) - return False - if not is_portal(): - log.debug("{0} should not be replicated because we're not a portal." - .format(instance)) - return False - return True