def view_question(request,classes,lecture_number,noteslug,qpk): lectures = Lecture.objects.filter(slug=classes, lecture_number=lecture_number) note = Note.objects.filter(slug=noteslug) question = NoteQuestion.objects.filter(pk=qpk) replies = NoteReply.objects.filter(question=question) if request.POST: replyform = ReplyForm(request.POST) if replyform.is_valid(): newreply = replyform.save(commit=False) newreply.author = User.objects.get(username=request.user) newreply.question = question[0] newreply.save() return HttpResponseRedirect('.') else: replyform = ReplyForm(request.POST) args = {} args.update(csrf(request)) isauthor = (question[0].author == request.user.username) or request.user.is_superuser isnoteauthor = (note[0].author == request.user.username) or request.user.is_superuser args['form'] = replyform args['classes'] = lectures[0].course.classes args['lectureslug'] = classes args['lecture_number'] = lecture_number args['note'] = note[0] args['question'] = question[0] args['replies'] = replies args['isauthor'] = isauthor args['isnoteauthor'] = isnoteauthor return render_to_response('view_question.html', args)
def get_thread_posts(request, thread_id): """ Returns all posts in a thread, including the 'thread' post itself. """ thread = get_object_or_404(Thread, pk=thread_id) if thread.hidden: raise Http404 # Mark thread a read for user. read = UserRead.objects.get_or_create(user=request.user, thread=thread_id)[0] read.read = True read.save() if request.method == "POST": form = ReplyForm(request.POST) if form.is_valid(): reply = form.save(commit=False) # TODO: Check if user is logged in reply.author = request.user reply.thread_id = thread_id reply.pub_date = datetime.now() reply.update = datetime.now() reply.save() thread.latest_post = reply thread.save() form = ReplyForm() # Clear the form # Mark thread as unread for other users ur = UserRead.objects.all().filter(thread__exact=thread_id).exclude(user=request.user) for u in ur: u.read = False u.save() # Focus on new post after reply. return HttpResponseRedirect("%s?page=last#post_%s" % (reverse("thread", args=[thread.id]), reply.id)) else: form = ReplyForm() post_list = Post.objects.all().filter(thread=thread_id, hidden=False) p = Paginator(post_list, 5) try: page = int(request.GET.get("page", "1")) except: page = p.num_pages try: posts = p.page(page) except (EmptyPage, InvalidPage): raise Http404 return render_to_response( "dsf/thread_detail.html", {"thread": thread, "posts": posts, "form": form}, context_instance=RequestContext(request), )
def reply(request, topic_id): if request.method == 'POST': form = ReplyForm(request.POST) if form.is_valid(): reply = form.save(commit=False) reply.topic_id = topic_id reply.save() topic = Topic.objects.get(id=topic_id) topic.reply_date = timezone.now() topic.save() return redirect("/") else: form = ReplyForm() return render_to_response('form.html',{'form': form}, context_instance=RequestContext(request))
def post(self, reply_id): reply = Reply.get(id=reply_id) if not reply or (reply.author != self.current_user and not self.current_user.is_admin): return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=reply.topic, reply=reply) reply.put_notifier() result = {'status': 'success', 'message': '评论修改成功', 'reply_url': reply.url} return self.send_result(result, reply.url) data = dict(form=form, reply=reply) return self.send_result_and_render(form.result, "reply/edit.html", data)
def post(self): if not self.has_permission: return page = int(self.get_argument('page', 1)) topic_id = int(self.get_argument('topic_id', 0)) topic = Topic.get(id=topic_id) if not topic_id: result = {'status': 'error', 'message': '无此主题,不能创建评论'} if self.is_ajax: return self.write(result) else: self.flash_message(result) return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=topic) if 'class="mention"' in reply.content: put_notifier(reply) result = { 'status': 'success', 'message': '评论创建成功', 'content': reply.content, 'name': reply.author.name, 'nickname': reply.author.nickname, 'author_avatar': reply.author.get_avatar(size=48), 'author_url': reply.author.url, 'author_name': reply.author.name, 'author_nickname': reply.author.nickname, 'reply_url': reply.url, 'created': reply.created, 'id': reply.id, 'floor': reply.floor } if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) self.flash_message(form.result) return self.render("topic/index.html", form=form, topic=topic, category='index', page=page)
def post(self, reply_id): if not self.has_permission: return reply = Reply.get(id=reply_id) if not reply or (reply.author != self.current_user and not self.current_user.is_admin): return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=reply.topic, reply=reply) result = {"status": "success", "message": "评论修改成功", "reply_url": reply.url} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(reply.url) if self.is_ajax: return self.write(form.result) return self.render("reply/edit.html", form=form, reply=reply)
def post(self, reply_id): if not self.has_permission: return reply = Reply.get(id=reply_id) if not reply: return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=reply.topic, reply=reply) result = {'status': 'success', 'message': '评论修改成功', 'reply_url': reply.url} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(reply.url) if self.is_ajax: return self.write(form.result) return self.render("reply/edit.html", form=form, reply=reply)
def post(self): if not self.has_permission: return page = int(self.get_argument("page", 1)) topic_id = int(self.get_argument("topic_id", 0)) topic = Topic.get(id=topic_id) if not topic_id: result = {"status": "error", "message": "无此主题,不能创建评论"} if self.is_ajax: return self.write(result) else: self.flash_message(result) return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=topic) if 'class="mention"' in reply.content: put_notifier(reply) result = { "status": "success", "message": "评论创建成功", "content": reply.content, "name": reply.author.name, "nickname": reply.author.nickname, "author_avatar": reply.author.get_avatar(size=48), "author_url": reply.author.url, "author_name": reply.author.name, "author_nickname": reply.author.nickname, "reply_url": reply.url, "created": reply.created, "id": reply.id, "floor": reply.floor, } if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) self.flash_message(form.result) return self.render("topic/index.html", form=form, topic=topic, category="index", page=page)
def reply_view(request, pk_forum, pk_thread): forum = get_object(Forum.objects.active(), pk=pk_forum) category = forum.category threads = forum.threads.active() thread = get_object(threads, pk=pk_thread) form = ReplyForm(request.POST or None) editor_name = MARKUP_EDITORS[MARKUP] editor_style = 'js/forum/markitup/sets/%s/style.css' % editor_name editor_set = 'js/forum/markitup/sets/%s/set.js' % editor_name if form.is_bound and form.is_valid(): new_reply = form.save(request.user.forum_profile, forum, thread) return redirect(new_reply) return render(request, 'forum/reply.html', { 'category': category, 'forum': forum, 'thread': thread, 'form': form, 'editor_name': editor_name, 'editor_style': editor_style, 'editor_set': editor_set, })
def post(self): if not self.has_permission: return page = int(self.get_argument('page', 1)) topic_id = int(self.get_argument('topic_id', 0)) topic = Topic.get(id=topic_id) if not topic_id: result = {'status': 'error', 'message': '无此主题,不能创建评论'} if self.is_ajax: return self.write(result) else: self.flash_message(result) return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=topic) if 'class="mention"' in reply.content: put_notifier(reply) result = {'status': 'success', 'message': '评论创建成功', 'content': reply.content, 'name': reply.author.name, 'nickname': reply.author.nickname, 'author_avatar': reply.author.get_avatar(size=48), 'author_url': reply.author.url, 'author_name': reply.author.name, 'author_nickname': reply.author.nickname, 'reply_url': reply.url, 'created': reply.created, 'id': reply.id, 'floor': reply.floor} if self.is_ajax: return self.write(result) self.flash_message(result) return self.redirect(topic.url) if self.is_ajax: return self.write(form.result) self.flash_message(form.result) return self.render("topic/index.html", form=form, topic=topic, category='index', page=page)
def post(self): page = int(self.get_argument('page', 1)) category = self.get_argument('category', 'index') topic_id = int(self.get_argument('topic_id', 0)) topic = Topic.get(id=topic_id) if not topic_id: result = {'status': 'error', 'message': '无此主题,不能创建评论'} if self.is_ajax: return self.write(result) else: self.flash_message(**result) return self.redirect_next_url() user = self.current_user form = ReplyForm(self.request.arguments) if form.validate(): reply = form.save(user=user, topic=topic) reply.put_notifier() result = {'status': 'success', 'message': '评论创建成功', 'content': reply.content, 'name': reply.author.name, 'nickname': reply.author.nickname, 'author_avatar': reply.author.get_avatar(size=48), 'author_url': reply.author.url, 'author_name': reply.author.name, 'author_nickname': reply.author.nickname, 'reply_url': reply.url, 'created': reply.created, 'id': reply.id, 'floor': reply.floor} if self.is_ajax: return self.write(result) self.flash_message(**result) return self.redirect(topic.url) reply_count = topic.reply_count page_count = (reply_count + config.reply_paged - 1) // config.reply_paged replies = topic.get_replies(page=page, category=category) data = dict(form=form, topic=topic, replies=replies, category=category, page=page, page_count=page_count, url=topic.url) return self.send_result_and_render(form.result, "topic/index.html", data)
def add_reply(request): form = ReplyForm(request.POST) if form.is_valid(): reply = form.save(commit=False) review = request.POST.get('review') user_rep = request.POST.get('user') r1 = reply.rate_objective r2 = reply.rate_complete repute_rev = User_Reputation.objects.get(user=user_rep) reputation = User_Reputation.objects.filter(user=request.user) if reputation: r = User_Reputation.objects.get(user=request.user) repute = r.reputation entity_alpha = r.entity_alpha entity_beta = r.entity_beta else: r = User_Reputation() r.user = request.user r.reputation = 0.5 r.entity_alpha = 0.5 r.entity_beta = 0.5 r.votes = 0 entity_alpha = 0.5 entity_beta = 0.5 repute = 0.5 r.save() #r3 = ((r1+r2)/2) * repute/1000 alpha = ((r1 + r2) / 2) beta = 1 - alpha entity_alpha += alpha entity_beta += beta entity_reputation = (entity_alpha / (entity_alpha + entity_beta)) r4 = repute_rev.reputation if r4 == 1: repute_rev.reputation = r4 repute_rev.entity_alpha = entity_alpha repute_rev.entity_beta = entity_beta repute_rev.save() else: repute_rev.reputation = (r4 + entity_reputation) / 2 repute_rev.entity_alpha = entity_alpha repute_rev.entity_beta = entity_beta repute_rev.save() review = ProductReview.objects.get(id=review) reply.review = review reply.user = request.user reply.save() template = "pdcts/reply_review.html" html = render_to_string(template, {'reply': reply}) response = simplejson.dumps({'success': 'True', 'html': html}) else: html = form.errors.as_ul() response = simplejson.dumps({'success': 'False', 'html': html}) return HttpResponse(response, content_type='application/javascript; charset=utf-8')
def post_body_view(request, school_name, post_id, post_type): form_msg = {} ctx = {} print post_type if request.method == 'POST': user = request.user ip = get_ip_address(request) post = get_post_by_id(post_id) if post_type == "reply": post_form_kwargs = { "user": user, "post": post, "ip": ip} form = ReplyForm( request.POST, **post_form_kwargs) else: reply_id = request.POST['reply_id'] print reply_id post_form_kwargs = { "user": user, "ip": ip, "reply_id": reply_id} form = CommentForm( request.POST, **post_form_kwargs) if form.is_valid(): print form.cleaned_data form.save() if post_type == "reply": context = Context({'replys': [form.reply], 'commentForm': CommentForm()}) return_str = render_block_to_string( 'discussion/post-body.html', 'replys', context) else: return_str = { "reply_id": form.reply_id, "comment": form.comment.comment_body} form_msg['message'] = return_str else: form_msg['errors'] = form.errors print form_msg json_ctx = json.dumps(form_msg) return HttpResponse(json_ctx, mimetype='application/json') else: replyForm = ReplyForm() commentForm = CommentForm() post = get_post_by_id(post_id) reply = get_post_reply(post_id) ctx = { "school_name": school_name, "post": post, "replys": reply, "commentForm": commentForm, "replyForm": replyForm} return render_to_response( 'discussion/post-body.html', ctx, context_instance=RequestContext(request))
def add_reply(request): form = ReplyForm(request.POST) if form.is_valid(): reply = form.save(commit=False) review = request.POST.get('review') user_rep = request.POST.get('user') r1 = reply.rate_objective r2 = reply.rate_complete repute_rev = User_Reputation.objects.get(user = user_rep) reputation = User_Reputation.objects.filter(user = request.user) if reputation: r = User_Reputation.objects.get(user = request.user) repute = r.reputation entity_alpha = r.entity_alpha entity_beta = r.entity_beta else: r = User_Reputation() r.user = request.user r.reputation = 0.5 r.entity_alpha = 0.5 r.entity_beta = 0.5 r.votes = 0 entity_alpha = 0.5 entity_beta = 0.5 repute = 0.5 r.save() #r3 = ((r1+r2)/2) * repute/1000 alpha = ((r1+r2)/2) beta = 1 - alpha entity_alpha += alpha entity_beta += beta entity_reputation = (entity_alpha/(entity_alpha + entity_beta)) r4 = repute_rev.reputation if r4 == 1: repute_rev.reputation = r4 repute_rev.entity_alpha = entity_alpha repute_rev.entity_beta = entity_beta repute_rev.save() else: repute_rev.reputation = (r4 + entity_reputation)/2 repute_rev.entity_alpha = entity_alpha repute_rev.entity_beta = entity_beta repute_rev.save() review = ProductReview.objects.get(id=review) reply.review = review reply.user = request.user reply.save() template = "pdcts/reply_review.html" html = render_to_string(template, {'reply': reply }) response = simplejson.dumps({'success':'True', 'html': html}) else: html = form.errors.as_ul() response = simplejson.dumps({'success':'False', 'html': html}) return HttpResponse(response, content_type='application/javascript; charset=utf-8')