From 7f3a4889b8b24dc0115b2cdd3931907d97debf43 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Thu, 23 Aug 2012 06:48:22 -0700 Subject: [PATCH 1/2] make discussions work for new users --- common/djangoapps/student/models.py | 12 +++++++++++- lms/djangoapps/django_comment_client/__init__.py | 2 -- lms/djangoapps/django_comment_client/base/views.py | 2 +- lms/djangoapps/django_comment_client/forum/views.py | 6 +++--- .../commands/create_roles_for_existing.py | 6 ++---- lms/djangoapps/django_comment_client/permissions.py | 13 ++----------- lms/lib/comment_client/user.py | 10 ++++++++++ 7 files changed, 29 insertions(+), 22 deletions(-) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index b5243f9ab7..55204f19f6 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -51,6 +51,7 @@ from django.dispatch import receiver from functools import partial import comment_client as cc +from django_comment_client.models import Role, Permission import logging @@ -174,7 +175,6 @@ class PendingEmailChange(models.Model): new_email = models.CharField(blank=True, max_length=255, db_index=True) activation_key = models.CharField(('activation key'), max_length=32, unique=True, db_index=True) - class CourseEnrollment(models.Model): user = models.ForeignKey(User) course_id = models.CharField(max_length=255, db_index=True) @@ -184,6 +184,16 @@ class CourseEnrollment(models.Model): class Meta: unique_together = (('user', 'course_id'), ) +@receiver(post_save, sender=CourseEnrollment) +def assign_default_role(sender, instance, **kwargs): + if instance.user.is_staff: + role = Role.objects.get_or_create(course_id=instance.course_id, name="Moderator")[0] + else: + role = Role.objects.get_or_create(course_id=instance.course_id, name="Student")[0] + + logging.info("assign_default_role: adding %s as %s" % (instance.user, role)) + instance.user.roles.add(role) + #cache_relation(User.profile) #### Helper methods for use from python manage.py shell. diff --git a/lms/djangoapps/django_comment_client/__init__.py b/lms/djangoapps/django_comment_client/__init__.py index 61f59d6f9b..e69de29bb2 100644 --- a/lms/djangoapps/django_comment_client/__init__.py +++ b/lms/djangoapps/django_comment_client/__init__.py @@ -1,2 +0,0 @@ -# call some function from permissions so that the post_save hook is imported -from permissions import assign_default_role diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index dd9da857c6..280e6d2780 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -51,7 +51,7 @@ def ajax_content_response(request, course_id, content, template_name): 'content': content, } html = render_to_string(template_name, context) - user_info = cc.User.from_django_user(request.user).to_dict() + user_info = cc.User.from_django_user(request.user).safe_attributes() annotated_content_info = utils.get_annotated_content_info(course_id, content, request.user, user_info) return JsonResponse({ 'html': html, diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index eda574cb6e..38f8fa985d 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -64,7 +64,7 @@ def render_discussion(request, course_id, threads, *args, **kwargs): 'user': (lambda: reverse('django_comment_client.forum.views.user_profile', args=[course_id, user_id])), }[discussion_type]() - user_info = cc.User.from_django_user(request.user).to_dict() + user_info = cc.User.from_django_user(request.user).safe_attributes() def infogetter(thread): return utils.get_annotated_content_infos(course_id, thread, request.user, user_info) @@ -176,7 +176,7 @@ def render_single_thread(request, discussion_id, course_id, thread_id): thread = cc.Thread.find(thread_id).retrieve(recursive=True).to_dict() - user_info = cc.User.from_django_user(request.user).to_dict() + user_info = cc.User.from_django_user(request.user).safe_attributes() annotated_content_info = utils.get_annotated_content_infos(course_id, thread=thread, user=request.user, user_info=user_info) @@ -194,7 +194,7 @@ def single_thread(request, course_id, discussion_id, thread_id): if request.is_ajax(): - user_info = cc.User.from_django_user(request.user).to_dict() + user_info = cc.User.from_django_user(request.user).safe_attributes() thread = cc.Thread.find(thread_id).retrieve(recursive=True) annotated_content_info = utils.get_annotated_content_infos(course_id, thread, request.user, user_info=user_info) context = {'thread': thread.to_dict(), 'course_id': course_id} diff --git a/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py b/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py index 148c204acf..d1244a6690 100644 --- a/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py +++ b/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py @@ -6,9 +6,7 @@ Enrollments. """ from django.core.management.base import BaseCommand, CommandError -from student.models import CourseEnrollment -from django_comment_client.permissions import assign_default_role - +from student.models import CourseEnrollment, assign_default_role class Command(BaseCommand): args = 'course_id' @@ -23,4 +21,4 @@ class Command(BaseCommand): assign_default_role(None, enrollment) if i % 1000 == 0: print "{0}...".format(i), - print \ No newline at end of file + print diff --git a/lms/djangoapps/django_comment_client/permissions.py b/lms/djangoapps/django_comment_client/permissions.py index ffb676d2c2..fca363220a 100644 --- a/lms/djangoapps/django_comment_client/permissions.py +++ b/lms/djangoapps/django_comment_client/permissions.py @@ -1,5 +1,6 @@ +import comment_client as cc + from .models import Role, Permission -from django.contrib.auth.models import User from django.db.models.signals import post_save from django.dispatch import receiver from student.models import CourseEnrollment @@ -8,16 +9,6 @@ import logging from util.cache import cache -@receiver(post_save, sender=CourseEnrollment) -def assign_default_role(sender, instance, **kwargs): - if instance.user.is_staff: - role = Role.objects.get_or_create(course_id=instance.course_id, name="Moderator")[0] - else: - role = Role.objects.get_or_create(course_id=instance.course_id, name="Student")[0] - - logging.info("assign_default_role: adding %s as %s" % (instance.user, role)) - instance.user.roles.add(role) - def cached_has_permission(user, permission, course_id=None): """ Call has_permission if it's not cached. A change in a user's role or diff --git a/lms/lib/comment_client/user.py b/lms/lib/comment_client/user.py index ae4abf91b7..1f61e9d625 100644 --- a/lms/lib/comment_client/user.py +++ b/lms/lib/comment_client/user.py @@ -33,6 +33,16 @@ class User(models.Model): params = {'source_type': source.type, 'source_id': source.id} response = perform_request('delete', _url_for_subscription(self.id), params) + # TODO this is a hack to compensate for the fact that synchronization isn't + # happening properly. + def safe_attributes(self): + try: + return self.to_dict() + except: + self.save() + self._retrieve() + return self.to_dict() + def vote(self, voteable, value): if voteable.type == 'thread': url = _url_for_vote_thread(voteable.id) From a075cc79fd7d2954539722249adadd636782ff51 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Thu, 23 Aug 2012 06:50:16 -0700 Subject: [PATCH 2/2] remove stray import --- lms/djangoapps/django_comment_client/permissions.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/lms/djangoapps/django_comment_client/permissions.py b/lms/djangoapps/django_comment_client/permissions.py index fca363220a..9b064a7fef 100644 --- a/lms/djangoapps/django_comment_client/permissions.py +++ b/lms/djangoapps/django_comment_client/permissions.py @@ -1,5 +1,3 @@ -import comment_client as cc - from .models import Role, Permission from django.db.models.signals import post_save from django.dispatch import receiver