Exemple #1
0
def publish(request, category_id=None):
    if category_id:
        get_object_or_404(
            Category.objects.visible(),
            pk=category_id)

    user = request.user
    form = TopicForm(
        user=user,
        data=post_data(request),
        initial={'category': category_id})
    cform = CommentForm(
        user=user,
        data=post_data(request))
    if (is_post(request) and
            all([form.is_valid(), cform.is_valid()]) and
            not request.is_limited()):
        if not user.st.update_post_hash(form.get_topic_hash()):
            return redirect(
                request.POST.get('next', None) or
                form.get_category().get_absolute_url())
        # wrap in transaction.atomic?
        topic = form.save()
        cform.topic = topic
        comment = cform.save()
        comment_posted(comment=comment, mentions=cform.mentions)
        return redirect(topic.get_absolute_url())
    return render(
        request=request,
        template_name='spirit/topic/publish.html',
        context={'form': form, 'cform': cform})
Exemple #2
0
def publish(request, user_id=None):
    initial = None
    if user_id:  # todo: move to form
        user_to = get_object_or_404(User, pk=user_id)
        initial = {'users': [user_to.st.nickname]}

    user = request.user
    tform = TopicForPrivateForm(
        user=user, data=post_data(request))
    cform = CommentForm(
        user=user, data=post_data(request))
    tpform = TopicPrivateManyForm(
        user=user, data=post_data(request), initial=initial)

    if (is_post(request) and
            all([tform.is_valid(), cform.is_valid(), tpform.is_valid()]) and
            not request.is_limited()):
        if not user.st.update_post_hash(tform.get_topic_hash()):
            return safe_redirect(
                request, 'next', lambda: tform.category.get_absolute_url(), method='POST')

        # wrap in transaction.atomic?
        topic = tform.save()
        cform.topic = topic
        comment = cform.save()
        comment_posted(comment=comment, mentions=None)
        tpform.topic = topic
        tpform.save_m2m()
        TopicNotification.bulk_create(
            users=tpform.get_users(), comment=comment)
        return redirect(topic.get_absolute_url())

    return render(
        request=request,
        template_name='spirit/topic/private/publish.html',
        context={
            'tform': tform,
            'cform': cform,
            'tpform': tpform})
Exemple #3
0
def publish(request, category_id=None):
    title = request.POST.get("title", '')
    comment = request.POST.get("comment", '')
    title = not title.isspace()  #如果里面全是空格,不允许通过
    comment = not comment.isspace()

    # logger.warn({"action":"publish_topic", "title_comment_info":title+comment})
    # topic = Topic()
    # if not all([title,comment]):
    #     return HttpResponse(123)

    if category_id:
        get_object_or_404(Category.objects.visible(), pk=category_id)

    if request.method == 'POST':
        form = TopicForm(user=request.user, data=request.POST)
        cform = CommentForm(user=request.user, data=request.POST)

        if not request.is_limited and all([
                form.is_valid(), cform.is_valid()
        ]) and all([title, comment]):  # TODO: test!
            # wrap in transaction.atomic

            topic = form.save()
            bu = BanUser.objects.filter(username=request.user).first()
            if bu and bu.is_baned and topic.user == request.user:
                topic.is_removed = True
                topic.save()
            cform.topic = topic
            comment = cform.save()
            comment_posted(comment=comment, mentions=cform.mentions)
            return redirect(topic.get_absolute_url())
    else:
        category_id = 2  #七嘴八舌为默认选择分类
        form = TopicForm(user=request.user,
                         initial={
                             'category': category_id,
                         })
        cform = CommentForm()

    context = {'form': form, 'cform': cform}

    return render(request, 'spirit/topic/publish.html', context)
def comment_publish(request, topic_id, pk=None):
    topic = get_object_or_404(Topic.objects.opened(), pk=topic_id)

    if request.method == 'POST':
        User = get_user_model()
        user = User.objects.get(username='******')
        form = CommentForm(user=user, topic=topic, data=request.POST)

        category = topic.category

        if form.is_valid():
            comment = form.save()
            comment.user = user
            comment.save()

            author = request.POST.get('author', False)

            if author:
                comment_author = CommentAuthor(comment=comment, author=author)
                comment_author.save()

            comment_posted(comment=comment, mentions=form.mentions)

            if category.slug == settings.PUBLIC_CATEGORY:
                return redirect('pinboard:public')
            elif category.slug == settings.PRIVATE_CATEGORY:
                return redirect('pinboard:private')
    else:
        initial = None

        if pk:
            comment = get_object_or_404(Comment.objects.for_access(user=None),
                                        pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.username)
            initial = {
                'comment': quote,
            }

        form = CommentForm(initial=initial)

    context = {
        'form': form,
        'topic': topic,
    }

    return render(request, 'spirit/comment/publish.html', context)
Exemple #5
0
def update(request, pk):
    comment = Comment.objects.for_update_or_404(pk, request.user)

    if request.method == 'POST':
        form = CommentForm(data=request.POST, instance=comment)

        if form.is_valid():
            pre_comment_update(comment=Comment.objects.get(pk=comment.pk))
            comment = form.save()
            post_comment_update(comment=comment)
            return redirect(
                request.POST.get('next', comment.get_absolute_url()))
    else:
        form = CommentForm(instance=comment)

    context = {
        'form': form,
    }

    return render(request, 'spirit/comment/update.html', context)
Exemple #6
0
def publish(request, topic_id, pk=None):
    topic = get_object_or_404(Topic.objects.opened().for_access(request.user),
                              pk=topic_id)

    if request.method == 'POST':
        form = CommentForm(user=request.user, topic=topic, data=request.POST)
        banuser = BanUser.objects.filter(user=request.user).first()
        if not request.is_limited and form.is_valid():
            comment = form.save()
            comment_posted(comment=comment, mentions=form.mentions)
            Comment.objects.for_access(user=request.user)
            comment_da = comment.comment
            if comment_da.find("JPG") != -1 or comment_da.find("PNG") != -1:
                comment_da = comment_da.strip()
                comment_da = comment_da.split('\r')[0]  #获取url图片链接
                comment_img = '<p><img src="%s"></p>'
                comment_img = comment_img % comment_da  #把图片链接放入<imag >中
                href_url = comment.comment_html.split("</a><br>")[
                    0]  #获取数据库中图片是链接<a>不是<image>的字符串
                # print comment.comment_html
                # print href_url
                # print comment.comment_html.find(href_url)
                comment_word = comment.comment_html.replace(
                    href_url,
                    comment_img.split('</p>')[0])  #用<img>替换<a>
                comment.comment_html = comment_word
                comment.save()
                # print comment.comment_html
            if banuser and banuser.is_baned:
                comment.is_removed = 1
                comment.save()
            if pk:

                comment_push = get_object_or_404(
                    Comment.objects.for_access(user=request.user), pk=pk)
                # at_push(comment_push.user,request.user)
                # print request.user.first_name+"给"+comment_push.user.first_name+"发了一条消息"
                # msg = request.user.first_name + "给" + comment_push.user.first_name + "回复一条评论"
                customer_id = SocialAccount.objects.filter(
                    user_id=comment_push.user.id).first().extra_data['id']
                login_url = settings.PUSH_LOGIN_URL
                push_url = settings.PUSH_URL + str(customer_id) + '/at_push'
                admin_info = settings.PUSH_ADMIN_INFO
                push_info = {
                    'back_nickname': request.user.first_name,
                    'comment_nickname': comment_push.user.first_name
                }
                t1 = threading.Thread(
                    target=push_by_xiaolusys,
                    args=(login_url, push_url, admin_info,
                          push_info))  #把推送接口函数启动多线程运行,防止调用失败程序不运行下去
                t1.setDaemon(True)
                t1.start()

                # print customer_id
                # app_push = AppPush()
                # app_push.push(customer_id,"com.jimei.xlmm://app/v1/vip_forum",msg)
            return redirect(
                request.POST.get('next', comment.get_absolute_url()))
    else:
        initial = None

        if pk:
            comment = get_object_or_404(
                Comment.objects.for_access(user=request.user), pk=pk)
            quote = markdown.quotify(comment.comment, comment.user.first_name)
            initial = {
                'comment': quote,
            }
        form = CommentForm(initial=initial)

    context = {'form': form, 'topic': topic}

    return render(request, 'spirit/comment/publish.html', context)
def render_comments_form_user(topic, user, next=None):
    form = CommentForm()
    return {'form': form, 'topic_id': topic.pk, 'user': user, 'next': next}
def topic_publish(request, category_id=None):
    if category_id:
        get_object_or_404(Category.objects.visible(), pk=category_id)

    if request.method == 'POST':

        # no longer using user auth so set user to anonymous
        # author name entered later from form
        User = get_user_model()
        user = User.objects.get(username='******')

        form = TopicForm(user=user, data=request.POST)
        cform = CommentForm(user=user, data=request.POST)
        photo_id = request.POST.get('photo', 0)

        if all([form.is_valid(), cform.is_valid()]):  # TODO: test!

            # wrap in transaction.atomic?
            topic = form.save()
            topic.user = user
            topic.save()
            cform.topic = topic
            comment = cform.save()
            comment.user = user
            comment.save()
            if photo_id:
                photo = get_object_or_404(PinboardPhoto, pk=photo_id)
                comment_photo = CommentPhoto(comment=comment, photo=photo)
                comment_photo.save()

            author = request.POST.get('author', False)

            if author:
                comment_author = CommentAuthor(comment=comment, author=author)
                comment_author.save()

            comment_posted(comment=comment, mentions=cform.mentions)

            if topic.category.slug == settings.PUBLIC_CATEGORY:
                return redirect('pinboard:public')
            elif topic.category.slug == settings.PRIVATE_CATEGORY:
                return redirect('pinboard:private')
    else:
        if not category_id:
            slug = request.GET.get('category', settings.PUBLIC_CATEGORY)
            category_id = Category.objects.get(slug=slug).id
        form = TopicForm(user=None, initial={
            'category': category_id,
        })
        cform = CommentForm()

    context = {
        'form': form,
        'cform': cform,
        'category': category_id,
    }

    photo_id = request.GET.get('photo', 0)
    if photo_id:
        context['photo'] = get_object_or_404(PinboardPhoto, pk=photo_id)

    return render(request, 'spirit/topic/publish.html', context)