From 117c0b692710d5bff637df97338b2b659eecd311 Mon Sep 17 00:00:00 2001 From: Arjun Singh Date: Thu, 20 Sep 2012 00:24:36 -0700 Subject: [PATCH] Mark threads as read once they are fetched; change viewed to read --- .../django_comment_client/forum/views.py | 29 +++++-------------- lms/djangoapps/django_comment_client/utils.py | 2 +- lms/lib/comment_client/models.py | 4 +-- lms/lib/comment_client/thread.py | 7 +++-- lms/lib/comment_client/user.py | 10 ------- 5 files changed, 16 insertions(+), 36 deletions(-) diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index 7a80de194b..4d02d94903 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -84,6 +84,7 @@ def inline_discussion(request, course_id, discussion_id): # TODO (vshnayder): since none of this code seems to be aware of the fact that # sometimes things go wrong, I suspect that the js client is also not # checking for errors on request. Check and fix as needed. + log.error("Error loading inline discussion threads.") raise Http404 def infogetter(thread): @@ -117,6 +118,7 @@ def forum_form_discussion(request, course_id): unsafethreads, query_params = get_threads(request, course_id) # This might process a search query threads = [utils.safe_content(thread) for thread in unsafethreads] except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err: + log.error("Error loading forum discussion threads: %s" % str(err)) raise Http404 user_info = cc.User.from_django_user(request.user).to_dict() @@ -167,21 +169,16 @@ def forum_form_discussion(request, course_id): @login_required def single_thread(request, course_id, discussion_id, thread_id): - if request.is_ajax(): - course = get_course_with_access(request.user, course_id, 'load') - cc_user = cc.User.from_django_user(request.user) - user_info = cc_user.to_dict() + course = get_course_with_access(request.user, course_id, 'load') + cc_user = cc.User.from_django_user(request.user) + user_info = cc_user.to_dict() - try: - last_read_time = datetime.datetime.utcnow().replace(tzinfo=utc).strftime('%Y-%m-%dT%H:%M:%S%z') - cc_user.update_read_states(course_id, thread_id, last_read_time) - except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err: - # TODO log error - pass + if request.is_ajax(): try: thread = cc.Thread.find(thread_id).retrieve(recursive=True, user_id=request.user.id) except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err: + log.error("Error loading single thread.") raise Http404 courseware_context = get_courseware_context(thread, course) @@ -200,24 +197,14 @@ def single_thread(request, course_id, discussion_id, thread_id): }) else: - course = get_course_with_access(request.user, course_id, 'load') category_map = utils.get_discussion_category_map(course) - cc_user = cc.User.from_django_user(request.user) - user_info = cc_user.to_dict() - - try: - last_read_time = datetime.datetime.utcnow().replace(tzinfo=utc).strftime('%Y-%m-%dT%H:%M:%S%z') - cc_user.update_read_states(course_id, thread_id, last_read_time) - except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err: - # TODO log error - pass - try: threads, query_params = get_threads(request, course_id) thread = cc.Thread.find(thread_id).retrieve(recursive=True, user_id=request.user.id) threads.append(thread.to_dict()) except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err: + log.error("Error loading single thread.") raise Http404 course = get_course_with_access(request.user, course_id, 'load') diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index 76a784a8dc..cd323e56c1 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -337,7 +337,7 @@ def safe_content(content): 'updated_at', 'depth', 'type', 'commentable_id', 'comments_count', 'at_position_list', 'children', 'highlighted_title', 'highlighted_body', 'courseware_title', 'courseware_url', 'tags', 'unread_comments_count', - 'viewed', + 'read', ] if (content.get('anonymous') is False) and (content.get('anonymous_to_peers') is False): diff --git a/lms/lib/comment_client/models.py b/lms/lib/comment_client/models.py index 3f1ad35cd7..3ce3858d2d 100644 --- a/lms/lib/comment_client/models.py +++ b/lms/lib/comment_client/models.py @@ -72,8 +72,8 @@ class Model(object): for k, v in kwargs.items(): if k in self.accessible_fields: self.__setattr__(k, v) - #else: - # raise AttributeError("Field {0} does not exist".format(k)) + else: + raise AttributeError("Field {0} does not exist".format(k)) def updatable_attributes(self): return extract(self.attributes, self.updatable_fields) diff --git a/lms/lib/comment_client/thread.py b/lms/lib/comment_client/thread.py index 0e0aa97744..2f118374d6 100644 --- a/lms/lib/comment_client/thread.py +++ b/lms/lib/comment_client/thread.py @@ -10,7 +10,7 @@ class Thread(models.Model): 'closed', 'tags', 'votes', 'commentable_id', 'username', 'user_id', 'created_at', 'updated_at', 'comments_count', 'unread_comments_count', 'at_position_list', 'children', 'type', 'highlighted_title', - 'highlighted_body', 'endorsed', 'unread' + 'highlighted_body', 'endorsed', 'read' ] updatable_fields = [ @@ -60,7 +60,10 @@ class Thread(models.Model): else: return super(Thread, cls).url(action, params) + # TODO: This is currently overriding Model._retrieve only to add parameters + # for the request. Model._retrieve should be modified to handle this such + # that subclasses don't need to override for this. def _retrieve(self, *args, **kwargs): url = self.url(action='get', params=self.attributes) - response = perform_request('get', url, {'recursive': kwargs.get('recursive'), 'user_id': kwargs.get('user_id')}) + response = perform_request('get', url, {'recursive': kwargs.get('recursive'), 'user_id': kwargs.get('user_id'), 'mark_as_read': kwargs.get('mark_as_read', True)}) self.update_attributes(**response) diff --git a/lms/lib/comment_client/user.py b/lms/lib/comment_client/user.py index 91b570c10e..101a7f2ff6 100644 --- a/lms/lib/comment_client/user.py +++ b/lms/lib/comment_client/user.py @@ -74,13 +74,6 @@ class User(models.Model): response = perform_request('get', url, retrieve_params) self.update_attributes(**response) - def update_read_states(self, course_id, thread_id, last_read_time): - url = _url_for_read_states(self.id) - response = perform_request('put', url, { "course_id": course_id, - "thread_id": thread_id, - "last_read_time": last_read_time, - }) - def _url_for_vote_comment(comment_id): return "{prefix}/comments/{comment_id}/votes".format(prefix=settings.PREFIX, comment_id=comment_id) @@ -90,8 +83,5 @@ def _url_for_vote_thread(thread_id): def _url_for_subscription(user_id): return "{prefix}/users/{user_id}/subscriptions".format(prefix=settings.PREFIX, user_id=user_id) -def _url_for_read_states(user_id): - return "{prefix}/users/{user_id}/read_states".format(prefix=settings.PREFIX, user_id=user_id) - def _url_for_user_active_threads(user_id): return "{prefix}/users/{user_id}/active_threads".format(prefix=settings.PREFIX, user_id=user_id)