Exemple #1
0
def comment_threads(request, comment_id):

    comment = None
    if not comment_id is None:
        try:
            comment = Comment.objects.get(id=int(comment_id))
            content_object = comment.content_object
            content_id = comment.content_object.id
        except:
            pass

    initial_setup = {
        "content_type": content_object.get_instance_content_type,
        "object_id": comment.object_id
    }
    # print(comment.is_parent)
    post = comment.content_object

    form = CommentForm(data=request.POST or None, initial=initial_setup)
    if form.is_valid():
        # print(request.POST)
        cd = form.cleaned_data
        ctype = cd.get('content_type')
        content_type = ContentType.objects.get(model=ctype)
        obj_id = cd.get('object_id')
        content = cd.get('content')
        parent_obj = None
        try:
            parent_id = int(request.POST.get('parent_id'))
        except:
            parent_id = None

        if parent_id:
            parent_qry = Comment.objects.filter(id=parent_id)
            if parent_qry.exists() and parent_qry.count() == 1:
                parent_obj = parent_qry.first()

        comment_obj = Comment(
            user=request.user,
            content=content,
            content_type=content_type,
            object_id=obj_id,
            parent=parent_obj,
        )
        comment_obj.save()
        create_action(request.user, 'replied', comment_obj)
        return redirect('comment:comment-threads',
                        comment_id=comment_id)  # redirect to parent comment

    context = {}
    context['comment'] = comment
    context['post'] = post
    context['form'] = form
    template = 'comment/comment_threads.html'
    return render(request, template, context)
Exemple #2
0
def post_like_unlike(request):
    user = request.user
    post_id = request.POST.get('post_id')
    post = get_object_or_404(Post, id=post_id)
    is_liked = None
    if request.user in post.users_like.all():
        post.users_like.remove(request.user)
        is_liked = False
    else:
        is_liked = True
        post.users_like.add(request.user)
        utils.create_action(user, 'likes', post)
    likes_count = post.likes_count
    return JsonResponse({'liked': is_liked, 'likes_count': likes_count})
Exemple #3
0
def comment_add(request):
    post_id = request.POST.get('post_id')
    post = get_object_or_404(Post, id=post_id)
    user = request.user
    comment_text = request.POST.get('comment_text')
    comment_obj, added = Comment.objects.get_or_create(user=user,
                                                       post=post,
                                                       content=comment_text)
    comment_added = False
    if added:
        comment_added = True
        utils.create_action(user, 'commented on', post)
    count = post.comments_count
    data = {'comment_added': comment_added, 'comments_count': count}
    return JsonResponse(data)
Exemple #4
0
def follow_toggle(request):
    #NOTE: Refactor this !
    username = request.POST.get('to_user__username')
    from_user = request.user
    try:
        to_user = get_user_model().objects.get(username__iexact=username)
        from_user = get_user_model().objects.get(id=from_user.id)
    except get_user_model().DoesNotExist:
        raise Http404
    follow_obj, created = Follow.objects.get_or_create(to_user=to_user,
                                                       from_user=from_user)
    is_follow = True
    if not created:
        follow_obj.delete()
        is_follow = False
    else:
        utils.create_action(from_user, 'is now following', to_user)
    data = {'is_follow': is_follow}
    return JsonResponse(data)
Exemple #5
0
def post_add(request):
    if request.method == 'POST' and request.FILES['file']:
        username = request.POST.get('post_owner')
        owner = get_object_or_404(get_user_model(),
                                  username__iexact=username.strip())
        title = request.POST.get('title')
        content = request.POST.get('content')
        file = request.FILES.get('file')
        # TODO : validate user input & files( size,format,dimensions,ip(optional))
        post_instance,_ = Post.objects.\
        get_or_create(owner = owner,
        title = title,
        content = content,
        photo = file)
        utils.create_action(owner, 'added a new post', post_instance)
        return redirect(
            reverse('account:user-profile',
                    kwargs={'username': owner.username}))
    else:
        pass
Exemple #6
0
def post_like(request):
	post_id = request.POST.get('post-id')
	post = get_object_or_404(Post,id = str(post_id.strip()))

	User = get_user_model()
	username = request.POST.get('username')
	request_user = get_object_or_404(User,username__iexact=username.strip())
	
	is_liked = True
	like_obj,created = Like.objects.get_or_create(post = post,user = request_user)
	if not created:
		# already exist - delete it !
		like_obj = Like.objects.filter(post = post,user = request_user)
		like_obj.delete()
		is_liked = False
	else:
    	create_action(request_user,'Liked',post)
		count = post.likes.count()
		response = {'is_liked':is_liked,'count':count}
	return JsonResponse(response)
Exemple #7
0
def post_details(request,post_author_username,post_slug):

	is_auth = functions.is_user_authenticated(request)# user js - to  toggle auth-social-modal 
	post = get_object_or_404(Post,
							author__username = post_author_username,
							slug = post_slug)

	# print(post.__class__)
	authors_post_count = Post.authors_blog_post_count(post)

	post_url = request.build_absolute_uri(post.get_absolute_url())
	facebook_share = "https://www.facebook.com/sharer/sharer.php?u={0}".format(post_url)
	google_plus_share   = "https://plus.google.com/share?url={0}".format(post_url)

	flag = Like.user_has_liked_post(post.id,request.user)
	post_likes_count = post.likes.count()

	context = dict()
	# comment form
	initial_setup = {
	"content_type":post.get_instance_content_type,
	"object_id": post.id
	}
	# print(post.get_instance_content_type)
	form = CommentForm(data = request.POST or None,initial = initial_setup)
	if form.is_valid():
		# print(request.POST)
		cd = form.cleaned_data
		ctype = cd.get('content_type')
		content_type = ContentType.objects.get(model = ctype)
		obj_id = cd.get('object_id')
		content = cd.get('content')
		parent_obj = None
		try:
			parent_id = int(request.POST.get('parent_id'))
		except:
			parent_id = None 

		if parent_id:
			parent_qry = Comment.objects.filter(id = parent_id)
			if parent_qry.exists() and parent_qry.count() == 1:
				parent_obj = parent_qry.first()

		comment 	= Comment(
					  user = request.user,
					  content  = content,
					  content_type = content_type,
					  object_id = obj_id,
					  parent = parent_obj,
					)
		comment.save()
		create_action(request.user,'commented',comment)
		return redirect(post.get_absolute_url())


	context['form'] = form

	comments = post.post_comments

	session_key = 'viewed_post_{}'.format(post.id)
	if not request.session.get(session_key,False):
		post.views += 1
		post.save()
		request.session[session_key] = True

	context['post'] = post
	context['author_posts_count'] = authors_post_count
	context['facebook_share'] = facebook_share
	context['google_plus_share'] = google_plus_share
	context['post_url'] = post_url
	context['is_likes'] = flag
	context['likes_count'] = post_likes_count
	context['is_auth'] = is_auth
	context['comments'] = comments
	
	template = 'blog/detail.html'
	return TemplateResponse(request,template,context)