コード例 #1
0
ファイル: topic.py プロジェクト: Andy10101/forum
def post_create(request, slug=None):
    node = get_object_or_404(Node, slug=slug)

    form = CreateForm(request.POST)
    if not form.is_valid():
        return get_create(request, slug=slug, errors=form.errors)

    user = request.user
    try:
        last_created = user.topic_author.all().order_by('-created')[0]
    except IndexError:
        last_created = None

    if last_created: # 如果用户最后一篇的标题内容与提交的相同
        last_created_fingerprint = hashlib.sha1(last_created.title + \
            last_created.content + str(last_created.node_id)).hexdigest()
        new_created_fingerprint = hashlib.sha1(form.cleaned_data.get('title') + \
            form.cleaned_data.get('content') + str(node.id)).hexdigest()

        if last_created_fingerprint == new_created_fingerprint:
            errors = {'duplicated_topic': [u'帖子重复提交']}
            return get_create(request, slug=slug, errors=errors)

    now = timezone.now()
    topic = Topic(
        title = form.cleaned_data.get('title'),
        content = form.cleaned_data.get('content'),
        created = now,
        node = node,
        author = user,
        reply_count = 0,
        last_touched = now,
    )
    topic.save()

    reputation = user.reputation or 0
    reputation = reputation - 5 # 每次发布话题扣除用户威望5点
    reputation = 0 if reputation < 0 else reputation
    ForumUser.objects.filter(pk=user.id).update(reputation=reputation)

    return redirect('/')
コード例 #2
0
ファイル: topic.py プロジェクト: Andy10101/forum
def post_edit(request, topic_id):
    topic = get_object_or_404(Topic, pk=topic_id)

    form = CreateForm(request.POST)
    if not form.is_valid():
        return get_edit(request, topic_id, errors=form.errors)

    user = request.user
    if topic.author_id != user.id:
        errors = {'invalid_permission': [u'没有权限修改该主题']}
        return get_edit(request, topic_id, errors=errors)

    now = timezone.now()
    Topic.objects.filter(pk=topic.id).update(updated=now, last_touched=now, **form.cleaned_data)

    reputation = user.reputation or 0
    reputation = reputation - 2 # 每次修改回复扣除用户威望2点
    reputation = 0 if reputation < 0 else reputation
    ForumUser.objects.filter(pk=user.id).update(reputation=reputation)

    return redirect('/t/%s/' % topic.id)