Commit 941ae0f0 by Rocky Duan

basic error handling

parent 2869de16
......@@ -10,30 +10,13 @@ import comment_client
from django.core import exceptions
from django.contrib.auth.decorators import login_required
from django.views.decorators.http import require_POST, require_GET
from django.http import HttpResponse
from django.utils import simplejson
from django.views.decorators import csrf
from django.core.files.storage import get_storage_class
from django.utils.translation import ugettext as _
from django.conf import settings
class JsonResponse(HttpResponse):
def __init__(self, data=None):
content = simplejson.dumps(data,
indent=2,
ensure_ascii=False)
super(JsonResponse, self).__init__(content,
mimetype='application/json; charset=utf8')
class JsonError(HttpResponse):
def __init__(self, status, error_message=""):
content = simplejson.dumps({'errors': error_message},
indent=2,
ensure_ascii=False)
super(JsonError, self).__init__(content,
status=status,
mimetype='application/json; charset=utf8')
from django_comment_client.utils import JsonResponse, JsonError
def thread_author_only(fn):
def verified_fn(request, *args, **kwargs):
thread_id = args.get('thread_id', False) or \
......
......@@ -60,7 +60,7 @@ def forum_form_discussion(request, course_id, discussion_id):
search_text = request.GET.get('text', '')
if len(search_text) > 0:
threads = comment_client.search(search_text, discussion_id)
threads = comment_client.search_threads({'text': search_text, 'commentable_id': discussion_id})
else:
threads = comment_client.get_threads(discussion_id, recursive=False)
......
......@@ -3,6 +3,8 @@ from courseware.models import StudentModuleCache
from courseware.module_render import get_module
from xmodule.modulestore import Location
from xmodule.modulestore.django import modulestore
from django.http import HttpResponse
from django.utils import simplejson
from django.conf import settings
import operator
......@@ -88,3 +90,19 @@ def initialize_discussion_info(request, course):
'discussion_id': url_course_id,
'category': 'General',
}]
class JsonResponse(HttpResponse):
def __init__(self, data=None):
content = simplejson.dumps(data,
indent=2,
ensure_ascii=False)
super(JsonResponse, self).__init__(content,
mimetype='application/json; charset=utf8')
class JsonError(HttpResponse):
def __init__(self, error_message=""):
content = simplejson.dumps({'errors': error_message},
indent=2,
ensure_ascii=False)
super(JsonError, self).__init__(content,
mimetype='application/json; charset=utf8')
......@@ -273,6 +273,7 @@ TEMPLATE_LOADERS = (
MIDDLEWARE_CLASSES = (
'util.middleware.ExceptionLoggingMiddleware',
'django_comment_client.middleware.AjaxExceptionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
......
......@@ -6,7 +6,8 @@ SERVICE_HOST = 'http://localhost:4567'
PREFIX = SERVICE_HOST + '/api/v1'
class CommentClientError(Exception):
pass
def __init__(self, msg):
self.message = msg
class CommentClientUnknownError(CommentClientError):
pass
......
......@@ -168,6 +168,9 @@ Discussion =
else
editView = $("<div>").addClass("discussion-content-edit")
errorsField = $("<ul>").addClass("discussion-errors")
editView.append(errorsField)
textarea = $("<div>").addClass("comment-edit")
editView.append(textarea)
......@@ -221,7 +224,11 @@ Discussion =
autowatch = false || $local(".discussion-auto-watch").is(":checked")
$.post url, {body: body, anonymous: anonymous, autowatch: autowatch}, (response, textStatus) ->
if textStatus == "success"
if response.errors
errorsField = $local(".discussion-errors").empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
Discussion.handleAnchorAndReload(response)
, 'json'
......@@ -232,6 +239,10 @@ Discussion =
if textStatus == "success"
Discussion.handleAnchorAndReload(response)
, 'json'
handleEditThread = (elem) ->
handleEditComment = (elem) ->
$local(".discussion-reply").click ->
handleReply(this)
......@@ -245,6 +256,13 @@ Discussion =
$local(".discussion-vote-down").click ->
handleVote(this, "down")
$local(".discussion-edit").click ->
if $content.hasClass("thread")
handleEditThread(this)
else
handleEditComment(this)
initializeContent: (content) ->
$content = $(content)
$local = generateLocal($content.children(".discussion-content"))
......@@ -272,13 +290,17 @@ Discussion =
tags = $local(".new-post-tags").val()
url = Discussion.urlFor('create_thread', $local(".new-post-form").attr("_id"))
$.post url, {title: title, body: body, tags: tags}, (response, textStatus) ->
if textStatus == "success"
if response.errors
errorsField = $local(".discussion-errors").empty()
for error in response.errors
errorsField.append($("<li>").addClass("new-post-form-error").html(error))
else
Discussion.handleAnchorAndReload(response)
, 'json'
$local(".discussion-search-form").submit (event) ->
event.preventDefault()
text = $local(".discussion-search-text").val()
text = $local(".searchInput").val()
isSearchWithinBoard = $local(".discussion-search-within-board").is(":checked")
handleSearch(text, isSearchWithinBoard)
......
......@@ -7,7 +7,8 @@
</div>
${search_bar}
<form class="new-post-form" _id="${discussion_id}">
<input type="text" class="new-post-title" placeholder="Title"/>
<ul class="discussion-errors"></ul>
<input type="text" class="new-post-title" placeholder="Title"/>
<div class="new-post-body"></div>
<input class="new-post-tags" placeholder="Tags"/>
<a class="discussion-new-post" href="javascript:void(0)">New Post</a>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment