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 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')
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)
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')