def setUp(self): super().setUp() self.session.begin_nested() self.commentor = User(name="Commentor", email="*****@*****.**") self.author = User(name="Author", email="*****@*****.**") self.post = Post(title="Test Post", body="Test Post", user_id=self.author.id) self.answer = Answer(post_id=self.post.id, user_id=self.author.id) self.author.posts.append(self.post) self.author.answers.append(self.answer) self.post.answers.append(self.answer) self.session.add(self.commentor) self.session.add(self.author) self.session.add(self.post) self.session.add(self.answer) self.session.commit() with self.client as c: with c.session_transaction() as sess: set_session_user(self.commentor, current_session=sess) g.user = self.commentor
def create(self, request: Request): category_id = request.input('category_id') content = request.input('content') title = request.input('title') slug = sluglify(title) date = datetime.now() Post.create(content=content, title=title, slug=slug, date=date, category_id=category_id, author_id=request.user().id) return 'Post created !'
def create_post(title, body, categories): if g.user is None: return abort(403) # We don't really need to provide too much data since this is # validated client-side if not posts['min_title'] <= len(title) <= posts['max_title']: return abort(400) if not posts['min_len'] <= len(body) <= posts['max_len']: return abort(400) if not len(categories) < posts['max_tags']: return abort(400) for category in categories: if not Category.query.filter_by(name=category).first(): # Category doesn't exist so error return abort(400) # TODO: insert categories when models support new_post = Post(title=title, body=body) g.user.posts.append(new_post) db.session.add(new_post) db.session.commit() return url_for('get_post', post_id=new_post.id, title=slugify(new_post.title))
def post(postId): """ Handles creating new posts and updating new ones with the date passed in the POST request :param postId: The postId to update. If None, a new post will be created :return: result: dict """ title = request.form['title'] body = request.form['body'] groupId = int(request.form.get("group")) if (postId): selectedPost = Post.query.filter_by(id=postId).first() selectedPost.title = title selectedPost.body = body selectedPost.group_id = None if groupId == -1 else groupId db.session.commit() else: postedBy = User.query.filter_by( id=session.get("logged_in")).first() newPost = Post(title=title, body=body, postedby=postedBy, group_id=None if groupId == -1 else groupId) db.session.add(newPost) db.session.commit() return redirect('/listview')
def store(self, request: Request): post = Post.find(request.param('id')) post.title = request.input('title') post.body = request.input('body') post.save() return 'post updated'
def update(self, request: Request): post = Post.find(request.params.get('id')) post.category_id = request.input('category_id') post.content = request.input('content') post.title = request.input('title') post.slug = sluglify(request.input('title')) post.date = datetime.now() post.save() return 'Post updated !'
def post(): form = PostForm() if current_user.username != current_app.config[ 'ADMIN'] and form.validate_on_submit(): post = Post(body=form.body.data, author_id=current_user.id, title=form.title.data, post_time=datetime.utcnow()) db.session.add(post) db.session.commit() return redirect(request.args.get('next') or url_for('main.index')) return render_template('department/post_add.html', form=form)
def setUp(self): super().setUp() self.session.begin_nested() self.user = User(name='Test User', email='*****@*****.**') self.session.add(self.user) self.post = Post(title='Test Post Please ignore', body='I said please ignore!', user_id=self.user.id) self.user.posts.append(self.post) self.session.add(self.post)
def setUp(self): super().setUp() user1 = User(username='******', email='*****@*****.**', password='******') user2 = User(username='******', email='*****@*****.**', password='******') self.db.session.add(user1) self.db.session.add(user2) self.db.session.commit() post = Post(title="title", body="body", poster_id=user1.id) self.db.session.add(post) self.db.session.commit() self.user1Id = user1.id self.user2Id = user2.id self.postId = post.id
def test_showPost(self): post = Post(title="My New post", body="Body of new post", poster_id=self.userId) self.db.session.add(post) self.db.session.commit() postId = post.id with self.app as c: with c.session_transaction() as session: session["logged_in"] = self.userId response = c.get("/post/{}".format(postId)) self.assertEqual(response.status_code, 200) # route responds without errors self.assertTrue(b'My New post' in response.data) # Title is there self.assertTrue(b'Body of new post' in response.data) # Body is there self.assertTrue(self.username.encode() in response.data) # Username is there
def create_post(title, body, tags): if g.user is None: return abort(403) # We don't really need to provide too much data since this is # validated client-side if not (posts['min_title'] <= len(title) <= posts['max_title']): return abort(400) if not (posts['min_len'] <= len(body) <= posts['max_len']): return abort(400) new_post = Post(title=title, body=body) g.user.posts.append(new_post) db.session.add(new_post) db.session.commit() return redirect(url_for('get_post', post_id=new_post.id))
def setUp(self): super().setUp() self.session.begin_nested() self.user = User(name="Test User", email="*****@*****.**") self.post = Post(title="Test Post", body="Test Post", user_id=self.user.id) self.user.posts.append(self.post) self.session.add(self.user) self.session.add(self.post) self.session.commit() with self.client as c: with c.session_transaction() as sess: set_session_user(self.user, current_session=sess) g.user = self.user
def articleList(request, pid, page=1): article_list = Post.get(pid=pid) article_list.comments = Comment.objects.filter( pid=pid).order_by('-commentid') article_list.comments_count = article_list.comments.count() article_list.creator_lists = Copyright.objects.filter(pid=pid) for creator in article_list.creator_lists: creator.composer = Composer.get(cid=creator.cid) # paginator = Paginator(article_list,10) # posts = paginator.page(page) for comment in article_list.comments: if comment.reply != 0: comment.replys = Comment.objects.filter(commentid=comment.reply) context = { "article_list": article_list, } return render(request, 'production.html', context=context)
def user(username): user = User.query.filter_by(user_name=username).first_or_404() form = PostForm() # on post submitted if form.validate_on_submit(): post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) db.session.commit() flash(alerts['post_success']) return redirect(url_for('main.user', username=user.user_name)) posts = Post.query.filter_by(user_id=user.id).order_by( Post.timestamp.desc()).all() return render_template('profile.html', user=user, form=form, posts=posts, profile_page=True)
def setUp(self): super().setUp() self.session.begin_nested() light_theme = Theme.query.filter_by(name='light').first().id self.user = User(name='Test User', email='*****@*****.**', theme=light_theme) self.session.add(self.user) self.user2 = User(name='Test User 2', email='*****@*****.**', theme=light_theme) self.session.add(self.user2) self.test_post = Post(title='Testing Votes API', body='Testing Votes API') self.user2.posts.append(self.test_post) self.session.add(self.test_post) self.answer = Answer(post_id=self.test_post.id) self.user2.answers.append(self.answer) self.test_post.answers.append(self.answer) self.session.add(self.answer) self.session.commit() with self.client as c: with c.session_transaction() as sess: set_session_user(self.user, current_session=sess) g.user = self.user self.session.begin_nested() with self.app.test_request_context(): vote_controller.do_post_vote(self.test_post.id, 1) self.session.begin_nested() with self.app.test_request_context(): vote_controller.do_answer_vote(self.answer.id, -1)
def post_blog(): form = PostForm() if current_user.can(Permission.WRITE_ARTICLES) and \ form.validate_on_submit(): post = Post(body=form.body.data, author=current_user._get_current_object()) db.session.add(post) db.session.commit() return redirect(url_for('.post_blog')) show_followed = False if current_user.is_authenticated: show_followed = bool(request.cookies.get('show_followed', '')) if show_followed: query = current_user.followed_posts else: query = Post.query page = request.args.get('page', 1, type=int) pagination = query.order_by(Post.timestamp.desc()).paginate( page, per_page=current_app.config['FLASKY_POSTS_PER_PAGE'], error_out=False ) posts = pagination.items return render_template('main/index.html', form=form, posts=posts, pagination=pagination)
def delete(self, request: Request): post = Post.find(request.param('id')) post.delete() return 'post deleted'
def show(self, view: View): posts = Post.all() return view.render("posts/list", {"posts": posts})
def update(self, view: View, request: Request): post = Post.find(request.param('id')) return view.render('update', {'post': post})
def show(self, view: View): posts = Post.all() return view.render('posts', {'posts': posts})
def detail(self, view: View, request: Request): post = Post.where('slug', request.params.get('slug')).first() return view.render('posts/detail', {'post': post})
def edit(self, view: View, request: Request): post = Post.find(request.params.get('id')) return view.render("posts/edit", { "categories": Category.get().all(), "post": post })