Exemple #1
0
def add_comment(request, id):
    errors = []
    if not 'item' in request.POST or not request.POST['item']:
        # print("nothing")
        message = 'You must enter an item to add.'
        json_error = '{ "error": "' + message + '" }'
        return HttpResponse(json_error, content_type='application/json')

    new_form = CommentForm(request.POST)
    if not new_form.is_valid():
        # print("comment form is not valid")
        return redirect(reverse('home'))

    # pic = Entry.objects.select_for_update().get(created_by = request.user)
    new_comment = Comments(comment=request.POST['item'],
                           user=request.user,
                           timestamp=datetime.now(),
                           postid=id)

    new_comment.save()
    post = get_object_or_404(Posts, id=id)
    post.comments.add(new_comment)
    comments = post.comments.all()
    all_list = []
    for item in comments:
        comment = item
        user = User.objects.get(username=comment.user.username)
        entry = Entry.objects.get(created_by=comment.user)
        all_list.append(comment)
        all_list.append(user)
        all_list.append(entry)

    response_text = serializers.serialize('json', all_list)
    return HttpResponse(response_text, content_type='application/json')
Exemple #2
0
def follower(request):
    my_follows = request.user.profile.follows.all()
    entries = Post.objects.filter(user_profile__in=my_follows).order_by('-creation_time')
    comment_form = CommentForm()
    if request.method == "POST":
        comment_form = CommentForm(request.POST)
        if not comment_form.is_valid():
            messages.error(request, "Your comment has errors")
        else:
            messages.success(request, "Your comment has been posted")
            return redirect(reverse('follower'))   
    context = { 'entries': entries, 'form': comment_form }
    return render(request, 'socialnetwork/stream.html', context)
def comment(request):
    context={}
    if request.method == 'GET':
        return redirect(reverse('stream'))

    form = CommentForm(request.POST)

    if not form.is_valid():
        return redirect(reverse('stream')) 

    new_comment = Comment(content=form.cleaned_data['content'], user=request.user, post=form.cleaned_data['post'])
    new_comment.save()

    on_comment(new_comment)

    return redirect(reverse('stream'))
Exemple #4
0
def home(request):
    entries = Post.objects.order_by('-creation_time')
    comment_form = CommentForm()
    post_form = PostForm()
    if request.method == "POST":
        if request.POST.get("wall_post"):
            new_post = Post(user_profile=request.user.profile, creation_time=timezone.now())
            post_form = PostForm(request.POST, instance=new_post)
            if not post_form.is_valid():
                messages.error(request, "Your post has errors")
            else:
                post_form.save()
                messages.success(request, "Your post has been made")
                return redirect(reverse('home'))
        elif request.POST.get("comment"):
            comment_form = CommentForm(request.POST)
            if not comment_form.is_valid():
                messages.error(request, "Your comment has errors")
            else:
                messages.success(request, "Your comment has been made")
                return redirect(reverse('home'))
    context = {'entries': entries, 'form': comment_form, 'post_form': post_form}
    return render(request, 'socialnetwork/stream.html', context)
Exemple #5
0
def comment_action(request, id):
    context = {}
    if not 'comment' in request.POST or not request.POST['comment']:
        message = 'You must enter an item to add.'
        json_error = '{ "error": "' + message + '" }'
        return HttpResponse(json_error, content_type='application/json')

    form = CommentForm(request.POST)
    post = Post.objects.get(id=id)
    time_str = timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
    # print("dd")
    newtime = parse_datetime(time_str)
    # print(time_str)
    # if not form.is_valid():
    #     context['commentform'] = form
    # else:
    #     new_comment = Comment(text = form.cleaned_data['text'],
    #                           user = request.user,
    #                           time = timezone.localtime(),
    #                           post = post
    #                           )
    #     new_comment.save()
    new_comment = Comment(comment_text=request.POST['comment'],
                          username=request.user.first_name + " " +
                          request.user.last_name,
                          user=request.user,
                          time=newtime,
                          post=post)
    new_comment.save()
    # print(new_comment.time)
    # context['commentform'] = form
    comments = Comment.objects.all().order_by('-time', 'id')
    context['comments'] = comments
    context['last_time'] = new_comment.time

    response_text = serializers.serialize('json', [
        new_comment,
    ])

    # print("comment added")
    return HttpResponse(response_text, content_type='application/json')
def handle_response(response, user, errorlogger):

	if 'data' in response:

		new_data = response['data']
		script = user.userprofile.script
		script.data = new_data
		script.save()


	if 'posts' in response:
		for post_info in response['posts']:
			if 'content' not in post_info:
				errorlogger.log_error("Error: Invalid post: "+json.dumps(post_info))
				break

			content = post_info['content']

			post_form = PostForm({'content':content})
			if not post_form.is_valid():
				errorlogger.log_error("Error: Invalid post: "+json.dumps(post_info))
				break

			new_post = Post(content=post_form.cleaned_data['content'], user=user)
			new_post.save()
	
	if 'comments' in response:
		for comment_info in response['comments']:
			if ('content' not in comment_info) or ('post_id' not in comment_info):
				errorlogger.log_error("Error: Invalid comment: "+json.dumps(comment_info))
				break

			post_id = comment_info['post_id']
			content = comment_info['content']
			comment_form = CommentForm({'post':post_id,'content':content})
			if not comment_form.is_valid():
				errorlogger.log_error("Error: Invalid comment: "+json.dumps(comment_info))
				break

			new_comment = Comment(content=comment_form.cleaned_data['content'],
									post=comment_form.cleaned_data['post'],
									user=user)
			new_comment.save()

	if 'follow' in response:
		for followee_username in response['follow']:
			follow_form = FollowForm({'username':followee_username})
			if not follow_form.is_valid():
				errorlogger.log_error("Error: Invalid followee: "+str(followee_username))
				break

			followee = User.objects.get(username=follow_form.cleaned_data['username'])
			user.userprofile.following.add(followee)

			user.userprofile.save()

	if 'unfollow' in response:
		for target in response['unfollow']:
			unfollow_form = UnfollowForm({'username':target})
			if not unfollow_form.is_valid():
				errorlogger.log_error("Error: Invalid unfollow target: "+str(target))
				break

			target_user = User.objects.get(username=unfollow_form.cleaned_data['username'])
			user.userprofile.following.remove(target_user)

			user.userprofile.save()

	if 'log' in response:
		for entry in response['log']:
			errorlogger.log_error(str(entry))