Beispiel #1
0
    def post(self, request, *args, **kwargs):
        topic_id = self.kwargs['topic_id']
        topic = get_object_or_404(Topic, id=topic_id)
        form = ReplyForm(self.request.POST)
        if form.is_valid():
            md = form.cleaned_data['content']
            rendered = render_markdown(md)
            reply = Reply(topic=topic, author=request.user, markdown=md, content=rendered)

            mentioned = get_metioned_user(request.user, md)
            self._commit_changes(topic, reply, mentioned)

        return HttpResponseRedirect(reverse('topic_view', kwargs={'topic_id': topic_id}))
Beispiel #2
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))