Merge pull request #531 from MITx/feature/arjun/fix_discussion_sync
Feature/arjun/fix discussion sync
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
# call some function from permissions so that the post_save hook is imported
|
||||
from permissions import assign_default_role
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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
|
||||
print
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
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 +7,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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user