示例#1
0
文件: views.py 项目: bobschi/aurora
def vote_down_on(comment, voter):
    try:
        vote = comment.votes.get(voter=voter)
        if vote.direction == Vote.UP:
            vote.delete()
            CommentList.get_by_comment(comment).increment()

        return
    except Vote.DoesNotExist:
        Vote.objects.create(direction=Vote.DOWN, voter=voter, comment=comment)
        CommentList.get_by_comment(comment).increment()
示例#2
0
def vote_down_on(comment, voter):
    try:
        vote = comment.votes.get(voter=voter)
        if vote.direction == Vote.UP:
            vote.delete()
            CommentList.get_by_comment(comment).increment()

        return
    except Vote.DoesNotExist:
        Vote.objects.create(direction=Vote.DOWN, voter=voter, comment=comment)
        CommentList.get_by_comment(comment).increment()
示例#3
0
文件: views.py 项目: bobschi/aurora
def get_comment_list_update(request, client_revision, template='Comments/comment_list.html'):
    ref_type = client_revision['ref_type']
    ref_id = client_revision['ref_id']
    user = RequestContext(request)['user']

    revision = CommentList.get_by_ref_numbers(ref_id, ref_type).revision

    if revision > int(client_revision['number']):
        comment_list = Comment.query_top_level_sorted(ref_id, ref_type, user)
        id_suffix = "_" + str(ref_id) + "_" + str(ref_type)
        context = {'comment_list': comment_list,
                   'ref_type': ref_type,
                   'ref_id': ref_id,
                   'id_suffix': id_suffix,
                   'requester': user,
                   'revision': revision,
                   'request': request,
                   'paginator': 20}

        return {
            'ref_id': ref_id,
            'ref_type': ref_type,
            'comment_list': render_to_string(template, context)
        }
    return None
示例#4
0
def get_comment_list_update(request,
                            client_revision,
                            template='Comments/comment_list.html'):
    ref_type = client_revision['ref_type']
    ref_id = client_revision['ref_id']
    user = RequestContext(request)['user']

    revision = CommentList.get_by_ref_numbers(ref_id, ref_type).revision

    if revision > int(client_revision['number']):
        comment_list = Comment.query_top_level_sorted(ref_id, ref_type, user)
        id_suffix = "_" + str(ref_id) + "_" + str(ref_type)

        context = {
            'comment_list': comment_list,
            'ref_type': ref_type,
            'ref_id': ref_id,
            'id_suffix': id_suffix,
            'requester': user,
            'revision': revision,
            'request': request
        }

        return {
            'ref_id': ref_id,
            'ref_type': ref_type,
            'comment_list': render_to_string(template, context)
        }
    return None
示例#5
0
    def render(self, context):
        try:
            ref_object = self.reference_var.resolve(context)
            ref_type = ContentType.objects.get_for_model(ref_object)

            user = context['user']

            queryset = Comment.query_top_level_sorted(ref_object.id, ref_type.id, user, newest_last=self.newest_last)
            revision = CommentList.get_or_create(ref_object).revision

            form = CommentForm()
            form.fields['reference_id'].initial = ref_object.id
            form.fields['reference_type_id'].initial = ref_type.id
            form.fields['visibility'].initial = Comment.PUBLIC
            reply_form = ReplyForm()
            reply_form.fields['reference_id'].initial = ref_object.id
            reply_form.fields['reference_type_id'].initial = ref_type.id
            reply_form.fields['parent_comment'].initial = -1
            reply_form.fields['visibility'].initial = Comment.PUBLIC

            id_suffix = "_" + str(ref_object.id) + "_" + str(ref_type.id)
            context.update({'comment_list': queryset,
                            'form': form,
                            'reply_form': reply_form,
                            'ref_type': ref_type.id,
                            'ref_id': ref_object.id,
                            'id_suffix': id_suffix,
                            'requester': user,
                            'revision': revision})

            return render_to_string(self.template, context)
        except template.VariableDoesNotExist:
            return ''
示例#6
0
def get_comment_list_update(request,
                            client_revision,
                            template='Comments/comment_list.html'):
    ref_type = client_revision['ref_type']
    ref_id = client_revision['ref_id']
    user = AuroraAuthenticationBackend.get_user(AuroraAuthenticationBackend(),
                                                request.user.id)

    revision = CommentList.get_by_ref_numbers(ref_id, ref_type).revision

    if revision > int(client_revision['number']):
        comment_list = Comment.query_top_level_sorted(ref_id, ref_type, user)
        id_suffix = "_" + str(ref_id) + "_" + str(ref_type)
        context = {
            'comment_list': comment_list,
            'ref_type': ref_type,
            'ref_id': ref_id,
            'id_suffix': id_suffix,
            'requester': user,
            'revision': revision,
            'request': request,
            'paginator': 20
        }

        return {
            'ref_id': ref_id,
            'ref_type': ref_type,
            'comment_list': render_to_string(template, context)
        }
    return None
示例#7
0
文件: views.py 项目: bobschi/aurora
def promote_comment(request):
    data = request.POST

    requester = RequestContext(request)['user']
    if not requester.is_staff:
        return HttpResponseForbidden('You shall not promote!')

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    comment.promoted = True if data['value'] == 'true' else False
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#8
0
文件: views.py 项目: martflu/aurora
def promote_comment(request):
    data = request.POST

    requester = AuroraAuthenticationBackend.get_user(AuroraAuthenticationBackend(), request.user.id)
    if not requester.is_staff:
        return HttpResponseForbidden('You shall not promote!')

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    comment.promoted = True if data['value'] == 'true' else False
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#9
0
def promote_comment(request):
    data = request.POST

    requester = RequestContext(request)['user']
    if not requester.is_staff:
        return HttpResponseForbidden('You shall not promote!')

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    comment.promoted = True if data['value'] == 'true' else False
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#10
0
文件: views.py 项目: bobschi/aurora
def delete_comment(request):
    comment_id = request.POST['comment_id']
    deleter = RequestContext(request)['user']

    comment = Comment.objects.filter(id=comment_id).select_related('parent__children')[0]

    if comment.author != deleter and not deleter.is_staff:
        return HttpResponseForbidden('You shall not delete!')

    comment.deleter = deleter
    comment.delete_date = timezone.now()
    comment.promoted = False
    comment.seen = True
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#11
0
def promote_comment(request):
    data = request.POST

    requester = AuroraAuthenticationBackend.get_user(
        AuroraAuthenticationBackend(), request.user.id)
    if not requester.is_staff:
        return HttpResponseForbidden('You shall not promote!')

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    comment.promoted = True if data['value'] == 'true' else False
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#12
0
def delete_comment(request):
    comment_id = request.POST['comment_id']
    deleter = RequestContext(request)['user']

    comment = Comment.objects.filter(
        id=comment_id).select_related('parent__children')[0]

    if comment.author != deleter and not deleter.is_staff:
        return HttpResponseForbidden('You shall not delete!')

    comment.deleter = deleter
    comment.delete_date = timezone.now()
    comment.promoted = False
    comment.seen = True
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#13
0
文件: views.py 项目: martflu/aurora
def bookmark_comment(request):
    data = request.POST

    requester = AuroraAuthenticationBackend.get_user(AuroraAuthenticationBackend(), request.user.id)

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if data['bookmark'] == 'true':
        comment.bookmarked_by.add(requester)
    else:
        comment.bookmarked_by.remove(requester)

    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#14
0
def bookmark_comment(request):
    data = request.POST

    requester = RequestContext(request)['user']

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if data['bookmark'] == 'true':
        comment.bookmarked_by.add(requester)
    else:
        comment.bookmarked_by.remove(requester)

    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#15
0
文件: views.py 项目: bobschi/aurora
def bookmark_comment(request):
    data = request.POST

    requester = RequestContext(request)['user']

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if data['bookmark'] == 'true':
        comment.bookmarked_by.add(requester)
    else:
        comment.bookmarked_by.remove(requester)

    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#16
0
def bookmark_comment(request):
    data = request.POST

    requester = AuroraAuthenticationBackend.get_user(
        AuroraAuthenticationBackend(), request.user.id)

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if data['bookmark'] == 'true':
        comment.bookmarked_by.add(requester)
    else:
        comment.bookmarked_by.remove(requester)

    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#17
0
文件: views.py 项目: bobschi/aurora
def edit_comment(request):
    data = request.POST
    context = RequestContext(request)
    requester = context['user']

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if comment.author != requester:
        return HttpResponseForbidden('You shall not edit!')

    text = data['text']
    if text == '':
        return HttpResponse('')

    comment.text = data['text']
    comment.edited_date = timezone.now()
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#18
0
def edit_comment(request):
    data = request.POST
    context = RequestContext(request)
    requester = context['user']

    try:
        comment = Comment.objects.get(id=data['comment_id'])
    except Comment.DoesNotExist:
        return HttpResponse('')

    if comment.author != requester:
        return HttpResponseForbidden('You shall not edit!')

    text = data['text']
    if text == '':
        return HttpResponse('')

    comment.text = data['text']
    comment.edited_date = timezone.now()
    comment.save()
    CommentList.get_by_comment(comment).increment()

    return HttpResponse('')
示例#19
0
文件: comments.py 项目: ralokt/aurora
    def render(self, context):
        try:
            ref_object = self.reference_var.resolve(context)
            ref_type = ContentType.objects.get_for_model(ref_object)

            user = context['user']

            queryset = Comment.query_top_level_sorted(
                ref_object.id, ref_type.id, user, newest_last=self.newest_last)
            revision = CommentList.get_or_create(ref_object).revision

            form = CommentForm()
            form.fields['reference_id'].initial = ref_object.id
            form.fields['reference_type_id'].initial = ref_type.id
            form.fields['visibility'].initial = Comment.PUBLIC
            reply_form = ReplyForm()
            reply_form.fields['reference_id'].initial = ref_object.id
            reply_form.fields['reference_type_id'].initial = ref_type.id
            reply_form.fields['parent_comment'].initial = -1
            reply_form.fields['visibility'].initial = Comment.PUBLIC

            id_suffix = "_" + str(ref_object.id) + "_" + str(ref_type.id)
            context.update({
                'comment_list': queryset,
                'form': form,
                'reply_form': reply_form,
                'ref_type': ref_type.id,
                'ref_id': ref_object.id,
                'id_suffix': id_suffix,
                'requester': user,
                'revision': revision,
                'paginator': self.paginator
            })

            return render_to_string(self.template, context)
        except template.VariableDoesNotExist:
            return ''
示例#20
0
def create_comment(form, request):
    if not form.is_valid():
        raise ValidationError('The submitted form was not valid')

    context = RequestContext(request)
    user = context['user']
    ref_type_id = form.cleaned_data['reference_type_id']
    ref_obj_id = form.cleaned_data['reference_id']
    ref_obj_model = ContentType.objects.get_for_id(ref_type_id).model_class()
    ref_obj = ref_obj_model.objects.get(id=ref_obj_id)
    visibility = form.cleaned_data['visibility']

    parent_comment_id = form.cleaned_data.get('parent_comment', None)
    if parent_comment_id is not None:
        try:
            parent_comment = Comment.objects.get(id=parent_comment_id)
        except ObjectDoesNotExist:
            parent_comment = None
    else:
        parent_comment = None

    comment = Comment.objects.create(text=form.cleaned_data['text'],
                                     author=user,
                                     content_object=ref_obj,
                                     parent=parent_comment,
                                     post_date=timezone.now(),
                                     visibility=visibility)

    if comment.author.is_staff or comment.visibility == Comment.PRIVATE:
        comment.seen = True
        comment.save()

    comment_list = CommentList.get_by_comment(comment)

    if comment_list.uri is None or 'evaluation' in comment_list.uri:
        comment_list.uri = form.cleaned_data['uri']
        comment_list.save()

    comment_list.increment()

    if comment.visibility == Comment.PRIVATE:
        return

    if parent_comment is None:
        return

    if parent_comment.author == comment.author:
        return

    if comment.visibility == Comment.STAFF and not parent_comment.author.is_staff:
        return

    course_short_title = form.cleaned_data['course_short_title']

    if course_short_title != "":
        course = Course.get_or_raise_404(course_short_title)
        link = comment_list.uri + '#comment_' + str(parent_comment.id)
    else:
        course = None
        link = ""

    text = comment.author.nickname[:15] + ': '
    if len(comment.text) > 50:
        text += comment.text[:47] + "..."
    else:
        text += comment.text[:50]

    obj, created = Notification.objects.get_or_create(
        user=parent_comment.author,
        course=course,
        text=text,
        image_url=comment.author.avatar.url,
        link=link)

    if not created:
        obj.creation_time = timezone.now()
        obj.read = False
        obj.save()
示例#21
0
文件: views.py 项目: bobschi/aurora
def create_comment(form, request):
    if not form.is_valid():
        raise ValidationError('The submitted form was not valid')
    
    homeURL = form.cleaned_data['uri']
    
    context = RequestContext(request)
    user = context['user']
    ref_type_id = form.cleaned_data['reference_type_id']
    ref_obj_id = form.cleaned_data['reference_id']
    ref_obj_model = ContentType.objects.get_for_id(ref_type_id).model_class()
    ref_obj = ref_obj_model.objects.get(id=ref_obj_id)
    visibility = form.cleaned_data['visibility']

    parent_comment_id = form.cleaned_data.get('parent_comment', None)
    if parent_comment_id is not None:
        try:
            parent_comment = Comment.objects.get(id=parent_comment_id)
        except ObjectDoesNotExist:
            parent_comment = None
    else:
        parent_comment = None

    comment = Comment.objects.create(text=form.cleaned_data['text'],
                                     author=user,
                                     content_object=ref_obj,
                                     parent=parent_comment,
                                     post_date=timezone.now(),
                                     visibility=visibility)

    if comment.author.is_staff or comment.visibility == Comment.PRIVATE:
        comment.seen = True
        comment.save()

    comment_list = CommentList.get_by_comment(comment)

    if comment_list.uri is None or 'evaluation' in comment_list.uri:
        comment_list.uri = form.cleaned_data['uri']
        comment_list.save()

    comment_list.increment()

    if comment.visibility == Comment.PRIVATE:
        return

    if parent_comment is None:
        return

    if parent_comment.author == comment.author:
        return

    if comment.visibility == Comment.STAFF and not parent_comment.author.is_staff:
        return

    course_short_title = form.cleaned_data['course_short_title']

    if course_short_title != "":
        course = Course.get_or_raise_404(course_short_title)
        link = comment_list.uri + '#comment_' + str(parent_comment.id)
    else:
        course = None
        link = ""

    text = comment.author.nickname[:15] + ': '
    if len(comment.text) > 50:
        text += comment.text[:47] + "..."
    else:
        text += comment.text[:50]

    obj, created = Notification.objects.get_or_create(
        user=parent_comment.author,
        course=course,
        text=text,
        image_url=comment.author.avatar.url,
        link=link
    )

    if not created:
        obj.creation_time = timezone.now()
        obj.read = False
        obj.save()