Ejemplo n.º 1
0
def CommentView(request, id):
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            content = form.cleaned_data.get('content')
            post = Post.objects.filter(id=id).first()
            user = request.user
            Comment.objects.create(post=post, user=user, content=content)
            post.set_comment_count()
            post.save()
    return redirect(reverse('post:post-detail', kwargs={'id': id}))
Ejemplo n.º 2
0
def CommentEditView(request,id,c_id):
    if request.method=='POST':
        try:
            post=Post.objects.get(id=id)
            comment=Comment.objects.get(id=c_id)
            form=CommentForm(request.POST)
            if form.is_valid():
                content=form.cleaned_data.get('content')
                comment.content=content
                comment.save()
                return redirect(reverse('post:post-detail',kwargs={'id':id}))
            return render(request,'mainapp/commentedit.html',{'form':form})
        except:
            return render(request,'accounts/pagenotfound.html',{'title':'HTTP 404 ERROR'})
    else:
        return redirect(reverse('post:post-comment-view',kwargs={'id':id,'c_id':c_id}))
Ejemplo n.º 3
0
def CommentDisplayView(request,id,c_id):
    if request.method=='POST':
        try:
            post=Post.objects.get(id=id)
            comment=Comment.objects.get(id=c_id)
            form=CommentForm(instance=comment)
            return render(request,'mainapp/commentedit.html',{'form':form})
        except:
            return render(request,'accounts/pagenotfound.html',{'title':'HTTP 404 ERROR'})
    else:
        return render(request,'accounts/pagenotfound.html',{'title':'HTTP 404 ERROR'})
Ejemplo n.º 4
0
def new_comment(request):
	form = CommentForm()
	if request.method == 'POST':
		form = CommentForm(request.POST, request.FILES)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect('/photos')
	else:
		form = CommentForm()
	return render_to_response('commentform.html',{'form':form}, context_instance=RequestContext(request))
Ejemplo n.º 5
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['categories_list'] = Category.objects.all()
        context['comment_form'] = CommentForm()
        context['like_count'] = Article.objects.filter(
            status='published', created_at__gt=now() -
            timedelta(days=7)).annotate(number_of_likes=Count(
                'liked_by')).order_by('-number_of_likes')[:3]
        context['comment_count'] = Article.objects.filter(
            status='published', created_at__gt=now() -
            timedelta(days=7)).annotate(number_of_comments=Count(
                'comment')).order_by('-number_of_comments')[:2]

        # Проверка, поставил ли текущий пользователь "лайк" статье.
        article = self.get_object()
        if self.request.user in article.liked_by.all():
            context['user_add_like'] = True
        else:
            context['user_add_like'] = False

        return context
Ejemplo n.º 6
0
def add_comment_to_post(request,slug):
    post = get_object_or_404(Post,slug=slug)
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.post = post
            comment.author = request.user # add this line
            comment.slug = post.slug
            comment.save()
            messages.success(request, 'Comment added successfully')
            return redirect('mainapp:post_detail',slug=post.slug)
           
    else:
        form = CommentForm()
    return render(request,'mainapp/comment_form.html',{'form':form})
Ejemplo n.º 7
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['form'] = CommentForm() # Inject CommentForm
     return context
Ejemplo n.º 8
0
def home():




    if not current_user.is_authenticated:
        return redirect(url_for('login'))
    title = 'Hello'
    localtimes = {}
    mytasklocaltimes = {}

    if current_user.is_authenticated:
        if not List.query.filter_by(user =current_user.id).all():
            l = List(name='To-do', user=current_user.id, privacy=False)
            db.session.add(l)
            db.session.commit()
        title = 'Wacky Schemes'
        tasks=Post.query.filter(Post.user_id != current_user.id).all()
        mytasks = Post.query.filter_by(user_id=current_user.id).all()
    else:
        tasks = []
        mytasks=[]

    tasklikes = {}

    if tasks:
        for task in tasks:
            #likelist = Likes.query.filter(Likes.post_id==task.id).all()
            likecount = len(Likes.query.filter_by(post_id=task.id).all()) - len(Dislikes.query.filter_by(post_id=task.id).all())
            tasklikes.update({task.id : likecount})
            localtimes.update({task.id : datetime_from_utc_to_local(task.date_posted)})
    if mytasks:
        for task in mytasks:
            likecount = len(Likes.query.filter_by(post_id=task.id).all()) - len(Dislikes.query.filter_by(post_id=task.id).all())
            tasklikes.update({task.id : likecount})
            mytasklocaltimes.update({task.id : datetime_from_utc_to_local(task.date_posted) })

    comment = CommentForm()
    form = CreateTask()

    likes = {}
    if tasks:
        for task in tasks:
            if Likes.query.filter(Likes.user_id==current_user.id).filter(Likes.post_id==task.id).first():
                likes.update({task.id : 1})
            elif Dislikes.query.filter(Dislikes.user_id==current_user.id).filter(Dislikes.post_id==task.id).first():
                likes.update({task.id :-1})
            else:
                likes.update({task.id : 0})

    if mytasks:
        for task in mytasks:
            if Likes.query.filter(Likes.user_id==current_user.id).filter(Likes.post_id==task.id).first():
                likes.update({task.id : 1})
            elif Dislikes.query.filter(Dislikes.user_id==current_user.id).filter(Dislikes.post_id==task.id).first():
                likes.update({task.id :-1})
            else:
                likes.update({task.id : 0})

    lists = List.query.filter_by(user=current_user.id).all()

    if form.validate_on_submit():
        if not current_user.is_authenticated:
            flash(f'You must be logged in to do that')
            return redirect(url_for('home'))
        post = Post(title=form.title.data, content=form.content.data, user_id=current_user.id)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('home'))
    return render_template('home.html',lists=lists, home='home', tasklikes=tasklikes, title=title, likes=likes, account=current_user, tasks=tasks, mytasklocaltimes=mytasklocaltimes, mytasks=mytasks, form=form,comment=comment, localtimes=localtimes)
Ejemplo n.º 9
0
 def post(self, request, pk):
     form = CommentForm(data=request.POST)
     if form.is_valid():
         form.save()
     return HttpResponseRedirect(reverse("mainapp:post", kwargs={"pk": pk}))
Ejemplo n.º 10
0
 def get_context_data(self, *args, **kwargs):
     context = super().get_context_data(**kwargs)
     context["comment_form"] = CommentForm()
     context["comments"] = kwargs['object'].comments_set.all(
     ).select_related("user")
     return context
Ejemplo n.º 11
0
def PostDetail(request, id):
    try:
        page = 1
        f_type = 1
        if request.GET.get('page') != None:
            page = request.GET.get('page')
        if request.GET.get('filter') != None:
            f_type = request.GET.get('filter')
        post = Post.objects.get(pk=id)
        f_type = int(f_type)
        if f_type == 2:
            comment_list = Comment.objects.filter(post=post).order_by(
                '-comment_upvote_count', '-date_commented')
        else:
            comment_list = Comment.objects.filter(
                post=post).order_by('-date_commented')
        paginator = Paginator(comment_list, 5)
        page_obj = paginator.get_page(page)
        page_range = paginator.page_range
        total = len(comment_list)
        page_list = []
        for i in page_range:
            if abs(page_obj.number - i) <= 3:
                page_list.append(i)
        last = math.ceil(paginator.count / 5)
        upvoted_set = {
            -1,
        }
        downvoted_set = {
            -1,
        }
        if request.user.is_authenticated:
            upvote = False
            downvote = False
            user = request.user
            if post.upvoted_users.filter(id=user.id).exists():
                upvote = True
            elif post.downvoted_users.filter(id=user.id).exists():
                downvote = True
            form = CommentForm()
            for comment in comment_list:
                if comment.comment_upvoted_users.filter(id=user.id).exists():
                    upvoted_set.add(comment.id)
                if comment.comment_downvoted_users.filter(id=user.id).exists():
                    downvoted_set.add(comment.id)
            return render(
                request, 'mainapp/postdetail.html', {
                    'post': post,
                    'upvote': upvote,
                    'downvote': downvote,
                    'comment_write': True,
                    'form': form,
                    'page_obj': page_obj,
                    'page_list': page_list,
                    'last': last,
                    'total': total,
                    'upvoted_set': upvoted_set,
                    'downvoted_set': downvoted_set,
                })
        else:
            return render(
                request, 'mainapp/postdetail.html', {
                    'post': post,
                    'comment_write': False,
                    'page_obj': page_obj,
                    'page_list': page_list,
                    'upvoted_set': upvoted_set,
                    'downvoted_set': downvoted_set,
                })
    except:
        return render(request, 'accounts/pagenotfound.html',
                      {'title': 'HTTP 404 ERROR'})