def post(self, request, *args, **kwargs): json = YpSerialiser() form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user.get_profile() comment.save() photo_id = kwargs.get("id") if photo_id: photo = get_object_or_404(PhotosModels.Photos, pk=photo_id) photo.comments.add(comment) photo.save() else: return HttpResponse( simplejson.dumps({"id": 0, "status": 2, "txt": "комментарий не добавлен к изображению"}), mimetype="application/json", ) return HttpResponse( json.serialize( [comment], excludes=("object_id", "content_type"), relations={"author": {"fields": ("first_name", "last_name", "avatar")}}, ), mimetype="application/json", ) return HttpResponse(simplejson.dumps({"id": 0, "status": form._errors}), mimetype="application/json")
def post(self, request): form = CommentForm(request.POST or None) if form.is_valid() and request.user.is_authenticated(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get('object_id') title_data = form.cleaned_data.get("title") content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, title=title_data, ) return redirect(new_comment.content_object.get_absolute_url())
def comment_edit(request, object_id, template_name='comments/edit.html'): comment = get_object_or_404(Comment, pk=object_id, user=request.user) if DELTA > comment.submit_date: return comment_error(request) if request.method == 'POST': form = CommentForm(request.POST, instance=comment) if form.is_valid(): form.save() return redirect(request, comment.content_object) else: form = CommentForm(instance=comment) return render(request, template_name, { 'form': form, 'comment': comment, })
def add_comment(request, article): """Helper-function for add comment""" success = None if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): success = True comment = form.save(commit=False) # Setting fields forms for save comment.article_id = article comment.approved = False comment.author = form.cleaned_data['author'] comment.email_author = form.cleaned_data['email_author'] comment.text = form.cleaned_data['text'] comment.save() form = CommentForm() else: form = CommentForm() return [form, success]
def post(self, request, *args, **kwargs): json = YpSerialiser() form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user.get_profile() comment.save() photo_id = kwargs.get('id') if photo_id: photo = get_object_or_404(PhotosModels.Photos, pk=photo_id) photo.comments.add(comment) photo.save() else: return HttpResponse(simplejson.dumps({ 'id': 0, 'status': 2, 'txt': 'комментарий не добавлен к изображению' }), mimetype="application/json") return HttpResponse(json.serialize( [comment], excludes=("object_id", "content_type"), relations={ 'author': { 'fields': ('first_name', 'last_name', 'avatar') } }), mimetype="application/json") return HttpResponse(simplejson.dumps({ 'id': 0, 'status': form._errors }), mimetype="application/json")
def post_detail(request, slug=None): """ Displays details of Post. """ # taggit # tag = Post.objects.filter( # tags__name__in=list(self.objects.tags.values_list('name', flat=True)) # ).exclude(id=self.object.id) # tag = Post..filter(pk=pk) # tag_names = [tag.name for tag in Tag.objects.all()] # print(tag_names) # import pdb; pdb.set_trace() instance = get_object_or_404(Post, slug=slug) recent_post = Post.objects.all().order_by( '-id')[:3] # 3 top list from descending order to time recent_comment = Comment.objects.all().order_by( '-id')[:3] # 3 top list from descending order to time top_tag = Tag.objects.all().order_by('-id')[:4] # for sidbar top tag # print(t) tag = instance.tags.all() num_tags = tag.count() print(tag.count()) # tag = instance if instance.publish > timezone.now().date() or instance.draft: if not request.user.is_staff or not request.user.is_superuser: raise Http404 share_string = quote_plus(instance.content) initial_data = { "content_type": instance.get_content_type, "object_id": instance.id } form = CommentForm(request.POST or None, initial=initial_data) if form.is_valid() and request.user.is_authenticated(): c_type = form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = form.cleaned_data.get('object_id') content_data = form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return HttpResponseRedirect( new_comment.content_object.get_absolute_url()) comments = instance.comments context = { "title": instance.title, "instance": instance, "share_string": share_string, "comments": comments, "comment_form": form, "tag": tag, "num_tags": num_tags, "recent_post": recent_post, "recent_comment": recent_comment, "top_tag": top_tag } # authentication if not request.user.is_authenticated(): return render(request, "user/post_detail.html", context) else: return render(request, "user/post_detail.html", context)