def follower_stream(request): context = dict() context['page_name'] = "Follower Stream" p = get_object_or_404(Profile, profile_user = request.user) if request.method == "POST": # create post new_post = Post() new_post_form = PostForm(request.POST, instance=new_post) new_post.post_date_time = timezone.now() new_post.post_profile = p if not new_post_form.is_valid(): context['post_form'] = new_post_form return render(request, 'socialnetwork/follower_stream.html', context) new_post_form.save() context['message'] = "Post created" # display all posts context['post_form'] = PostForm() all_posts = Post.objects.order_by('post_date_time').reverse() # context['all_posts'] = all_posts.filter(post_profile__in = p.following.all()) return render(request, 'socialnetwork/global_stream.html', context)
def create_post(request): post = Post(user=request.user) postForm = PostForm(request.POST, instance=post) if not postForm.is_valid(): context = {'form': postForm} return render(request, 'socialnetwork/global.html', context) post.post_text = postForm.cleaned_data['post_input_text'] post.post_date_time = timezone.now() post.post_profile = Profile(user=request.user) postForm.save() context = { 'form': PostForm(), 'posts': reversed(Post.objects.all().order_by('post_date_time')) } return render(request, 'socialnetwork/global.html', context)
def create(self, request): json_data = json.loads(request.body) username = json_data['owner']['username'] user = None error_message = '' try: user = User.objects.get(username=username) except Exception as e: error_message = str(e) if (user != None): new_post = Post() new_post.content = json_data['content'] new_post.owner = user new_post.save() return Response({'ok': 'true'}) else: return Response({ 'ok': 'false', 'error': error_message }, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def add_post(request): context = {} if request.method == 'GET': context['form'] = PostForm() context['posts'] = Post.objects.all().order_by('-date') context['comments'] = Comment.objects.all().order_by('comment_date_time') # print(context['comments']) return render(request, 'socialnetwork/global_stream.html', context) new_post = Post() new_post.poster = request.user new_post.poster_name = new_post.poster.username new_post.poster_user_id = new_post.poster.id print("time_now: ", timezone.now()) new_post.date = parse_datetime(timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]) # new_post.date = parse_datetime(timezone.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-4]) print("NEW_POST_DATE:", new_post.date) new_form = PostForm(request.POST, instance=new_post) if not new_form.is_valid(): context = {'form': new_form} else: new_form.save() context['form'] = PostForm() context['posts'] = Post.objects.all().order_by('-date') context['comments'] = Comment.objects.all().order_by('comment_date_time') return render(request, 'socialnetwork/global_stream.html', context)
def post(request): context={} if request.method == 'GET': context['posts'] = Post.objects.all().order_by('-id') context['post_form'] = PostForm() return render(request, 'socialnetwork/stream.html',context) form = PostForm(request.POST) context['form']=form if not form.is_valid(): context['posts'] = Post.objects.all().order_by('-id') return render(request, 'socialnetwork/stream.html',context) new_post = Post(content=form.cleaned_data['content'], user=request.user) new_post.save() on_post(new_post) context['post_form'] = PostForm() context['posts'] = Post.objects.all().order_by('-id') return render(request, 'socialnetwork/stream.html', context)
def create_post(): form = PostForm() if form.validate_on_submit(): post = Post(title=form.title.data, text=form.text.data, user_id=current_user.id) db.session.add(post) db.session.commit() flash("Post Created") return redirect(url_for('core.index')) return render_template('create_post.html', form=form)
def new_post(request): errors = [] if not request.method == 'POST': errors.append('New Post should use POST method') globalpost = Post.objects.all().order_by('-post_time') context = {'errors':errors, 'globalpost': globalpost, 'form': PostForm()} return render(request, 'socialnetwork/globalstream.html', context) post = Post(post_by=request.user, post_time=timezone.now()) create_post = PostForm(request.POST, instance=post) if not create_post.is_valid(): errors.append('Post not valid') globalpost = Post.objects.all().order_by('-post_time') context = {'errors': errors, 'globalpost': globalpost, 'form': PostForm()} return render(request, 'socialnetwork/globalstream.html', context) create_post.save() # message = 'Post created' # Show by reverse-chronological order # globalpost = Post.objects.all().order_by('-post_time') # context = {'message': message, 'globalpost': globalpost, 'form': PostForm()} # return render(request, 'socialnetwork/globalstream.html', context) return redirect('globalstream')
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 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))