def add_reply (request): #if request.method == "POST": post_id = request.POST['post_id'] #print("post_id= "+post_id) r=Reply(reply_date=timezone.now(), reply_by_id=request.user.id, reply_post_id=post_id, reply_content=request.POST['reply_content']) r.save() p = r.reply_post t = p.post_topic if not request.user in t.topic_subscribers.all(): t.topic_subscribers.add(request.user) # Topic subscriptions t.subscribes += 1 t.save() content = str(request.user) + " replied: "+ r.reply_content +" - on post "+ p.post_content url = "http://test.archilane.com/forum/topic/" + str(t.id) topic_subscribers = t.topic_subscribers.all() Notification.save_notification(content,url,'forum','Post','private',topic_subscribers) if t.topic_cat.subscribes > 0: cat_subscribers = t.topic_cat.cat_subscribers.all() Notification.save_notification(content,url,'forum','Category','private',cat_subscribers) #topic_id = Post.objects.get(pk=post_id).post_topic_id #return HttpResponseRedirect ("/forum/topic/%s" % topic_id) #print(r.reply_post.pk) #print(r.reply_post.post_topic.pk) return HttpResponse(r.reply_post.post_topic.pk)
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}))
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))
def reply_create(request, t_id): ''' 创建回复 ''' if request.method == "POST": if not request.user.is_authenticated(): return redirect(reverse('forum:login')) try: topic = Topic.objects.select_related('author').get(pk=t_id) except Topic.DoesNotExist: return Http404 form = ReplyForm(request.POST) if form.is_valid(): user = request.user now = timezone.now() notifications = [] if user.id != topic.author_id: notification = Notification(content=form.cleaned_data['content'], status=0, involved_type=1, involved_user=topic.author, involved_topic=topic, trigger_user=user, occurrence_time=now) notifications.append(notification) metions = find_mentions(form.cleaned_data['content']) if user.username in metions: metions.remove(user.username) if topic.author.username in metions: metions.remove(topic.author.username) if metions: metion_names = User.objects.filter(username__in=metions) for username in metion_names: notification = Notification(content=form.cleaned_data['content'], involved_topic=topic, involved_user=user, involved_type=0, trigger_user=user, occurrence_time=now) notifications.append(notification) if notifications: # 批量插入 Notification.objects.bulk_create(notifications) reply = Reply(content=form.cleaned_data['content'], created=timezone.now(), author=user, topic=topic, last_touched=now) reply.save() Topic.objects.filter(pk=topic.id).update(last_replied_by=user,last_replied_time=now, last_touched=now,reply_count=topic.reply_count+1) topic.reply_count=topic.reply_count+1 return redirect(reverse('forum:reply_create', args=[topic.id]) + '#reply' + str(topic.reply_count)) user = request.user topic = Topic.objects.get(pk=t_id) if request.user.is_authenticated(): counter, notifications_count = user_info(request.user) topic_favorited = Favorite.objects.filter(involved_topic=topic, owner_user=user).exists() reply_last_page = (topic.reply_count // 20 + (topic.reply_count % 20 and 1)) or 1 try: current_page = int(request.GET.get('p', reply_last_page)) except ValueError: current_page = reply_last_page replies, reply_page = Reply.objects.get_all_replies(t_id, current_page, 20) return render(request, 'topic/view.html', locals())
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))