Example #1
0
def add_post(request, id):
    """添加一个Post(wiki 形式)"""

    if not request.user.is_authenticated():
        return HttpResponseForbidden('<h1>您没有这个权限!请 <a href="/account/login">登录</a></h1>')

    topic = get_object_or_404(Topic, pk=id)
    if not topic.catalog.has_access(request.user):
        return HttpResponseForbidden("<h1>您没有权限回复此帖!</h1>")
    # 如果 topic 已锁,则重定向到显示 topic
    if topic and topic.closed:
        return HttpResponseRedirect(topic.get_absolute_url())

    ip = ylinux_get_ip(request)

    form = build_form(AddPostForm, request, topic=topic, user=request.user, ip=ip)

    if form.is_valid():
        post = form.save()
        # set last post
        topic.last_post = Post.objects.filter(topic=topic).latest("updated")
        topic.save()
        url = reverse("wiki:show_topic", args=[post.topic.id])
        return HttpResponseRedirect(url)

    posts = topic.posts.all().select_related()
    return {"form": form, "posts": posts, "topic": topic}
Example #2
0
def edit_topic(request, id):

    topic = get_object_or_404(Topic, pk=id)

    if topic.user != request.user:
        return HttpResponseForbidden('<h1>您没有这个权限!</h1>')

    parents = get_parents(Catalog, topic.catalog.id)
    ip = ylinux_get_ip(request)

    form = build_form(EditTopicForm,
                      request,
                      text=topic.body,
                      user_ip=ip,
                      instance=topic)

    if form.is_valid():
        topic = form.save()
        url = reverse('wiki:show_topic', args=[id])
        return HttpResponseRedirect(url)

    attachments = Attachment.objects.filter(topic=topic)
    return {
        'form': form,
        'parents': parents,
        'topic': topic,
        'attachments': attachments
    }
Example #3
0
def add_topic(request, id):
    ''' id 是 Catalog '''

    if not id:
        return {'errors': '没有指定 Catalog'}

    catalog = get_object_or_404(Catalog, pk=id)
    if not catalog.has_access(request.user):
        return HttpResponseForbidden(u'<h2>你没有权限在此组中发贴!</h2>')
    parents = get_parents(Catalog, id)
    ip = ylinux_get_ip(request)

    form = build_form(AddTopicForm,
                      request,
                      catalog=catalog,
                      user=request.user,
                      user_ip=ip)

    if form.is_valid():
        topic = form.save()
        # set last topic
        catalog.last_topic = topic
        catalog.save()
        url = reverse('wiki:show_topic', args=[topic.id])
        return HttpResponseRedirect(url)

    return {'parents': parents, 'form': form}
Example #4
0
def add_post(request, id):
    """添加一个Post(wiki 形式)"""

    if not request.user.is_authenticated():
        return HttpResponseForbidden(
            '<h1>您没有这个权限!请 <a href="/account/login">登录</a></h1>')

    topic = get_object_or_404(Topic, pk=id)
    if not topic.catalog.has_access(request.user):
        return HttpResponseForbidden('<h1>您没有权限回复此帖!</h1>')
    # 如果 topic 已锁,则重定向到显示 topic
    if topic and topic.closed:
        return HttpResponseRedirect(topic.get_absolute_url())

    ip = ylinux_get_ip(request)

    form = build_form(AddPostForm,
                      request,
                      topic=topic,
                      user=request.user,
                      ip=ip)

    if form.is_valid():
        post = form.save()
        # set last post
        topic.last_post = Post.objects.filter(topic=topic).latest('updated')
        topic.save()
        url = reverse('wiki:show_topic', args=[post.topic.id])
        return HttpResponseRedirect(url)

    posts = topic.posts.all().select_related()
    return {'form': form, 'posts': posts, 'topic': topic}
Example #5
0
def replayAJAX(request, topicID=None, postID=None):
    """ 通过 AJAX 方式回复 """

    if not request.user.is_authenticated():
        return HttpResponse(
            u'<h1>您没有权限!请 <a href="/account/login">登录</a></h1>')

    replayID = None  # 需要回复的 id
    parent_post = None

    if topicID:
        topic = get_object_or_404(Topic, pk=topicID)
        replayID = topicID
    elif postID:
        parent_post = get_object_or_404(Post, pk=postID)
        topic = get_object_or_404(Topic, pk=parent_post.topic.id)
        replayID = postID
    else:
        return HttpResponse(u'topicID 和 postID 必需要指定一个!')

    if request.method == 'GET':
        m = 'POST' if postID else 'TOPIC'
        ajaxFunc = 'ajax_replay(this, "%s", "%s")' % (m, replayID)
        return {'method': 'GET', 'ajaxFunc': ajaxFunc}

    else:

        if not topic.catalog.has_access(request.user):
            return HttpResponse(u'<h1>您没有权限回复此帖!</h1>')

        # 如果 topic 已锁,则重定向到显示 topic
        if topic and topic.closed:
            return HttpResponse(u'主题已锁定,不可回复!')

        ip = ylinux_get_ip(request)

        post_body = request.POST.get("body", None)
        if len(post_body) < 2:
            return {'error': u'您的回复太短,至少2个字符!'}
        post = Post(topic=topic,
                    user=request.user,
                    user_ip=ip,
                    markup='none',
                    body=post_body,
                    parent=parent_post)
        post.save()
        topic.post_count += 1
        topic.last_post = post
        topic.save()
        topic.catalog.post_count += 1
        topic.catalog.last_post = post
        topic.catalog.save()

        return {'method': 'POST', 'topic': topic, 'post': post}
Example #6
0
def ajax_add_topic(request, id):
    if not id:
        return {"errors": "没有指定 Catalog"}

    catalog = get_object_or_404(Catalog, pk=id)
    if not catalog.has_access(request.user):
        return HttpResponseForbidden(u"<h2>你没有权限在此组中发贴!</h2>")
    parents = get_parents(Catalog, id)
    ip = ylinux_get_ip(request)

    form = build_form(AddTopicForm, request, catalog=catalog, user=request.user, user_ip=ip)

    return {"parents": parents, "form": form, "id": id}
Example #7
0
def replayAJAX(request, topicID=None, postID=None):
    """ 通过 AJAX 方式回复 """

    if not request.user.is_authenticated():
        return HttpResponse(u'<h1>您没有权限!请 <a href="/account/login">登录</a></h1>')

    replayID = None  # 需要回复的 id
    parent_post = None

    if topicID:
        topic = get_object_or_404(Topic, pk=topicID)
        replayID = topicID
    elif postID:
        parent_post = get_object_or_404(Post, pk=postID)
        topic = get_object_or_404(Topic, pk=parent_post.topic.id)
        replayID = postID
    else:
        return HttpResponse(u"topicID 和 postID 必需要指定一个!")

    if request.method == "GET":
        m = "POST" if postID else "TOPIC"
        ajaxFunc = 'ajax_replay(this, "%s", "%s")' % (m, replayID)
        return {"method": "GET", "ajaxFunc": ajaxFunc}

    else:

        if not topic.catalog.has_access(request.user):
            return HttpResponse(u"<h1>您没有权限回复此帖!</h1>")

        # 如果 topic 已锁,则重定向到显示 topic
        if topic and topic.closed:
            return HttpResponse(u"主题已锁定,不可回复!")

        ip = ylinux_get_ip(request)

        post_body = request.POST.get("body", None)
        if len(post_body) < 2:
            return {"error": u"您的回复太短,至少2个字符!"}
        post = Post(topic=topic, user=request.user, user_ip=ip, markup="none", body=post_body, parent=parent_post)
        post.save()
        topic.post_count += 1
        topic.last_post = post
        topic.save()
        topic.catalog.post_count += 1
        topic.catalog.last_post = post
        topic.catalog.save()

        return {"method": "POST", "topic": topic, "post": post}
Example #8
0
def ajax_add_topic(request, id):
    if not id:
        return {'errors': '没有指定 Catalog'}

    catalog = get_object_or_404(Catalog, pk=id)
    if not catalog.has_access(request.user):
        return HttpResponseForbidden(u'<h2>你没有权限在此组中发贴!</h2>')
    parents = get_parents(Catalog, id)
    ip = ylinux_get_ip(request)

    form = build_form(AddTopicForm,
                      request,
                      catalog=catalog,
                      user=request.user,
                      user_ip=ip)

    return {'parents': parents, 'form': form, 'id': id}
Example #9
0
def edit_topic(request, id):

    topic = get_object_or_404(Topic, pk=id)

    if topic.user != request.user:
        return HttpResponseForbidden("<h1>您没有这个权限!</h1>")

    parents = get_parents(Catalog, topic.catalog.id)
    ip = ylinux_get_ip(request)

    form = build_form(EditTopicForm, request, text=topic.body, user_ip=ip, instance=topic)

    if form.is_valid():
        topic = form.save()
        url = reverse("wiki:show_topic", args=[id])
        return HttpResponseRedirect(url)

    attachments = Attachment.objects.filter(topic=topic)
    return {"form": form, "parents": parents, "topic": topic, "attachments": attachments}
Example #10
0
def add_topic(request, id):
    """ id 是 Catalog """

    if not id:
        return {"errors": "没有指定 Catalog"}

    catalog = get_object_or_404(Catalog, pk=id)
    if not catalog.has_access(request.user):
        return HttpResponseForbidden(u"<h2>你没有权限在此组中发贴!</h2>")
    parents = get_parents(Catalog, id)
    ip = ylinux_get_ip(request)

    form = build_form(AddTopicForm, request, catalog=catalog, user=request.user, user_ip=ip)

    if form.is_valid():
        topic = form.save()
        # set last topic
        catalog.last_topic = topic
        catalog.save()
        url = reverse("wiki:show_topic", args=[topic.id])
        return HttpResponseRedirect(url)

    return {"parents": parents, "form": form}