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