Esempio n. 1
0
def post_reply_edit(request, reply_id):
    reply = get_object_or_404(Reply, pk=reply_id)

    form = ReplyForm(request.POST)
    if not form.is_valid():
        return get_reply_edit(request, reply_id, errors=form.errors)

    user = request.user
    if reply.author_id != user.id:
        errors = {'invalid_permission': [u'没有权限修改该回复']}
        return get_reply_edit(request, reply_id, errors=errors)

    Reply.objects.filter(pk=reply.id).update(updated=timezone.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/' % reply.topic_id)
Esempio n. 2
0
def post_reply_edit(request, reply_id):
    reply = get_object_or_404(Reply, pk=reply_id)

    form = ReplyForm(request.POST)
    if not form.is_valid():
        return get_reply_edit(request, reply_id, errors=form.errors)

    user = request.user
    if reply.author_id != user.id:
        errors = {'invalid_permission': [u'没有权限修改该回复']}
        return get_reply_edit(request, reply_id, errors=errors)

    Reply.objects.filter(pk=reply.id).update(updated=timezone.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/' % reply.topic_id)
Esempio n. 3
0
def post_view(request, topic_id):
    try:
        topic = Topic.objects.select_related('author').get(pk=topic_id)
    except Topic.DoesNotExist:
        raise Http404
    form = ReplyForm(request.POST)
    if not form.is_valid():
        return get_view(request, topic_id, errors=form.errors)

    user = request.user
    try:
        last_reply = topic.reply_set.all().order_by('-created')[0]
    except IndexError:
        last_reply = None
    if last_reply:
        last_replied_fingerprint = hashlib.sha1(
            str(topic.id) + str(last_reply.author_id) +
            last_reply.content).hexdigest()
        new_replied_fingerprint = hashlib.sha1(
            str(topic.id) + str(user.id) +
            form.cleaned_data.get('content')).hexdigest()
        if last_replied_fingerprint == new_replied_fingerprint:
            errors = {'duplicated_reply': [u'回复重复提交']}
            return get_view(request, topic.id, errors=errors)

    now = timezone.now()
    reply = Reply(
        topic=topic,
        author=user,
        content=form.cleaned_data.get('content'),
        created=now,
    )
    reply.save()
    Topic.objects.filter(pk=topic.id).update(last_replied_by=user,
                                             last_replied_time=now,
                                             last_touched=now)

    notifications = []
    if user.id != topic.author.id:
        notification = Notification(
            content=form.cleaned_data.get('content'),
            status=0,
            involved_type=1,  # 0: mention, 1: reply
            involved_user=topic.author,
            involved_topic=topic,
            trigger_user=user,
            occurrence_time=now,
        )
        notifications.append(notification)

    mentions = find_mentions(form.cleaned_data.get('content'))
    if user.username in mentions:
        mentions.remove(user.username)
    if topic.author.username in mentions:
        mentions.remove(topic.author.username)
    if mentions:
        mention_users = ForumUser.objects.filter(username__in=mentions)
        if mention_users:
            for mention_user in mention_users:
                notification = Notification(
                    content=form.cleaned_data.get('content'),
                    status=0,
                    involved_type=0,  # 0: mention, 1: reply
                    involved_user=mention_user,
                    involved_topic=topic,
                    trigger_user=user,
                    occurrence_time=now,
                )
                notifications.append(notification)
    if notifications:
        Notification.objects.bulk_create(notifications)

    if user.id != topic.author.id:
        topic_time_diff = timezone.now() - topic.created
        reputation = topic.author.reputation or 0
        reputation = reputation + 2 * math.log(
            user.reputation or 0 + topic_time_diff.days + 10, 10)
        ForumUser.objects.filter(pk=topic.author.id).update(
            reputation=reputation)

    return redirect('/t/%s/#reply%s' % (topic.id, topic.reply_count + 1))
Esempio n. 4
0
def post_view(request, topic_id):
    try:
        topic = Topic.objects.select_related('author').get(pk=topic_id)
    except Topic.DoesNotExist:
        raise Http404
    form = ReplyForm(request.POST)
    if not form.is_valid():
        return get_view(request, topic_id, errors=form.errors)

    user = request.user
    try:
        last_reply = topic.reply_set.all().order_by('-created')[0]
    except IndexError:
        last_reply = None
    if last_reply:
        last_replied_fingerprint = hashlib.sha1(str(topic.id) + str(last_reply.author_id) + last_reply.content).hexdigest()
        new_replied_fingerprint = hashlib.sha1(str(topic.id) + str(user.id) + form.cleaned_data.get('content')).hexdigest()
        if last_replied_fingerprint == new_replied_fingerprint:
            errors = {'duplicated_reply': [u'回复重复提交']}
            return get_view(request, topic.id, errors=errors)

    now = timezone.now()
    reply = Reply(
        topic = topic,
        author = user,
        content = form.cleaned_data.get('content'),
        created = now,
    )
    reply.save()
    Topic.objects.filter(pk=topic.id).update(last_replied_by=user, last_replied_time=now, last_touched=now)

    notifications = []
    if user.id != topic.author.id:
        notification = Notification(
            content = form.cleaned_data.get('content'),
            status = 0,
            involved_type = 1, # 0: mention, 1: reply
            involved_user = topic.author,
            involved_topic = topic,
            trigger_user = user,
            occurrence_time = now,
        )
        notifications.append(notification)

    mentions = find_mentions(form.cleaned_data.get('content'))
    if user.username in mentions:
        mentions.remove(user.username)
    if topic.author.username in mentions:
        mentions.remove(topic.author.username)
    if mentions:
        mention_users = ForumUser.objects.filter(username__in=mentions)
        if mention_users:
            for mention_user in mention_users:
                notification = Notification(
                    content = form.cleaned_data.get('content'),
                    status = 0,
                    involved_type = 0, # 0: mention, 1: reply
                    involved_user = mention_user,
                    involved_topic = topic,
                    trigger_user = user,
                    occurrence_time = now,
                )
                notifications.append(notification)
    if notifications:
        Notification.objects.bulk_create(notifications)

    if user.id != topic.author.id:
        topic_time_diff = timezone.now() - topic.created
        reputation = topic.author.reputation or 0
        reputation = reputation + 2 * math.log(user.reputation or 0 + topic_time_diff.days + 10, 10)
        ForumUser.objects.filter(pk=topic.author.id).update(reputation=reputation)

    return redirect('/t/%s/#reply%s' % (topic.id, topic.reply_count + 1))