def categories(category_id=None): page = request.args.get('page', 1, type=int) pagination = Category.query.filter_by().paginate( page, per_page=current_app.config['FOLLOWERS_PER_PAGE'], error_out=False) categories_list = pagination.items if category_id: category = Category.query.filter_by(id=category_id).first() form = CategoryForm() if request.method == 'POST' and form.validate(): category.name = form.name.data category.save() flash("已修改") return redirect( url_for('admin_manager.categories', page=page, form=form)) form.name.data = category.name form.parent.choices = [ (category.parent_id, Category.query.filter_by(id=category.parent_id).first().name) ] else: form = CategoryForm() if request.method == 'POST' and form.validate(): new_category = Category( form.name.data, Category.query.filter_by(id=form.parent.data).first()) new_category.save() flash("已添加") return redirect( url_for('admin_manager.categories', page=page, form=form)) return render_template('admin_manager/categories.html', form=form, pagination=pagination, categories=categories_list)
def add_category(name): """Add New Category""" c = Category() c.name = name c.slug = c.set_slug(name) c.save() print('New category added.')
def add_role(name): """ Add New Post Category """ try: c = Category() c.name = name c.set_slug(name) c.description = 'Default Description' c.save() except Exception as exp: print(exp) else: print(c.__repr__()) del c
def __post(): post = Post(title=self.text.title(), summary=text.text(3), body=text.text(6), author=user) category = Category(name=text.word()) category.save() post.category = category tag = Tag(name=text.word()) tag.save() post.tags.append(tag) post.save() return post
def test_06_category_posts_count(self): Category.add_none() none = Category.query.get(1) category = Category(name='tester', parent_category=Category.query.get(1)) category.save() self.assertTrue(none.posts_count() == 0) post = Post(body='# Post Big title', category=category) post.save() self.assertTrue(none.posts_count() == 1) self.assertTrue(category.posts_count() == 1) query = Post.query self.assertTrue(none.posts_count(query) == 1) self.assertTrue(category.posts_count(query) == 1)
def test_04_category_repr_save_and_delete(self): self.assertTrue(Category.query.count() == 0) Category.add_none() category = Category(name='tester', parent_category=Category.query.get(1)) category.save() self.assertTrue( '<Category tester Parent None Son []>' == category.__repr__()) self.assertTrue(Category.query.count() == 2) post = Post(body='# Post Big title', category=category) post.save() self.assertTrue(category.posts.count() == 1) category.delete() self.assertTrue(Category.query.count() == 1) self.assertTrue(post.category_id == 1)
def add_category(): form = CategoryForm() if form.validate_on_submit(): content = request.get_json() if content is not None: category = Category() category.name = content['name'] category.description = content['description'] category.set_slug(content['name']) category.save() category_id = str(category.id) return make_response(jsonify({'Success': category_id})) else: return make_response(jsonify({'Err': 'Request Content None'})) else: return make_response(jsonify({'Err': form.errors}))
def test_02_category_check(self): self.login() title = self.text.title() summary = self.text.text(3) body = self.text.text(6) category = self.text.word() data = dict(title=title, summary=summary, body=body, category=category) with self.app.test_request_context("/api/post", method="post", data=json.dumps(data), content_type="application/json"): with self.assertRaises(BadRequest): parser_post_post.parse_args() category = Category(name=category) category.save() args = parser_post_post.parse_args() self.assertEqual(category, args.get("category"))
def test_user_page(self): response = self.client.get(url_for('main.user', username='******')) self.assertIn('NOT FOUND', response.data) response = self.client.get(url_for('main.user', username='******')) self.assertIn('Admin', response.data) self.assertIn('无栏目', response.data) self.assertIn('无标签', response.data) self.assertIn('无文章', response.data) response = self.client.get(url_for('main.user', username='******')) self.assertIn('tester', response.data) self.assertIn('无栏目', response.data) self.assertIn('无标签', response.data) self.assertIn('无文章', response.data) category_other = Category(name='other', parent_category=Category.query.first()) category_other.save() category_test = Category(name='test', parent_category=Category.query.first()) category_test.save() tag_one = Tag(content='tag_one') tag_one.save() tag_two = Tag(content='tag_two') tag_two.save() article_test_one = Post(title='article_title', summary='article_summary', body='#article_body', category=category_test, author=User.query.first()) article_test_one.save() article_test_two = Post(title='article', summary='article', body='#article', category=category_other, author=User.query.first()) article_test_two.save() article_test_one.tag(tag_one) article_test_one.tag(tag_two) response = self.client.get('/user/Admin') self.assertNotIn('无栏目', response.data) self.assertNotIn('无标签', response.data) self.assertNotIn('无文章', response.data) self.assertIn('article_title', response.data) self.assertIn('article_summary', response.data) self.assertIn('tag_one', response.data) self.assertIn('other', response.data) self.assertIn('test', response.data) self.assertNotIn('article_body', response.data) # get key response = self.client.get('/user/Admin?key=article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('article_summary', response.data) self.assertIn('tag_one', response.data) self.assertIn('other', response.data) self.assertIn('test', response.data) response = self.client.get('/user/Admin?key_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # get category response = self.client.get('/user/Admin?category=other', follow_redirects=True) self.assertNotIn('article_title', response.data) self.assertIn('article', response.data) self.assertNotIn('tag_one', response.data) self.assertIn('无标签', response.data) response = self.client.get('/user/Admin?category=test', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) response = self.client.get('/user/Admin?category_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # get tag response = self.client.get('/user/Admin?tag=tag_one', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) response = self.client.get('/user/Admin?tag=tag_two', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) self.client.get('/user/Admin?tag=tag_one', follow_redirects=True) response = self.client.get('/user/Admin?tag=tag_two', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) response = self.client.get('/user/Admin?tag_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # get something with prev_url response = self.client.get('/user/Admin?tag=tag_one&prev_url=/article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertNotIn('无标签', response.data) response = self.client.get( '/user/Admin?category=test&prev_url=/article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data)
def test_neighbourhood(self): response = self.client.get(url_for('main.neighbourhood')) self.assertIn('无栏目', response.data) self.assertIn('无标签', response.data) self.assertIn('无文章', response.data) category_other = Category(name='other', parent_category=Category.query.first()) category_other.save() category_test = Category(name='test', parent_category=Category.query.first()) category_test.save() tag_one = Tag(content='tag_one') tag_one.save() tag_two = Tag(content='tag_two') tag_two.save() article_test = Post(title='article_title', summary='article_summary', body='#article_body', category=category_test, author=User.query.first()) article_test.save() article_test.tag(tag_one) article_test.tag(tag_two) article_test.save() response = self.client.get(url_for('main.neighbourhood')) self.assertNotIn('无栏目', response.data) self.assertNotIn('无标签', response.data) self.assertNotIn('无文章', response.data) self.assertIn('article_title', response.data) self.assertIn('article_summary', response.data) self.assertIn('tag_one', response.data) self.assertIn('other', response.data) self.assertIn('test', response.data) self.assertNotIn('article_body', response.data) # get key response = self.client.get('/?key=article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('article_summary', response.data) self.assertIn('tag_one', response.data) self.assertIn('other', response.data) self.assertIn('test', response.data) response = self.client.get('/?key=ABC', follow_redirects=True) self.assertNotIn('article_title', response.data) self.assertNotIn('article_summary', response.data) self.assertNotIn('tag_one', response.data) self.assertIn('other', response.data) self.assertIn('test', response.data) self.assertIn('无标签', response.data) self.assertIn('无文章', response.data) response = self.client.get('/?key_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # get category response = self.client.get('/?category=other', follow_redirects=True) self.assertNotIn('article_title', response.data) self.assertNotIn('tag_one', response.data) self.assertIn('无标签', response.data) self.assertIn('无文章', response.data) response = self.client.get('/?category=test', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) response = self.client.get('/?category_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # show followed self.login(username='******', password='******') response = self.client.get('/?show_followed=1', follow_redirects=True) self.assertIn('article_title', response.data) self.logout() # get tag self.client.get('/') response = self.client.get('/?tag=tag_one', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) response = self.client.get('/?tag=tag_two', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) self.client.get('/?tag=tag_one', follow_redirects=True) response = self.client.get('/?tag=tag_two', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertIn('tag_two', response.data) response = self.client.get('/?tag_disable=1', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) # get something with prev_url response = self.client.get('/?tag=tag_one&prev_url=/article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data) self.assertNotIn('无标签', response.data) response = self.client.get('/?category=test&prev_url=/article', follow_redirects=True) self.assertIn('article_title', response.data) self.assertIn('tag_one', response.data)
def test_post(self): # login self.login(username='******', password='******') # new post with worry tags response = self.client.get(url_for('post.new_article')) self.assertIn('博文', response.data) response = self.client.post( url_for('post.new_article'), data={ 'title': 'title', 'summary': 'summary', 'editor-markdown-doc': 'test', 'category': 1, # worry tags 'tags': 'TEST,test again' }, follow_redirects=True) self.assertIn('请使用英文逗号‘,’来分隔标签', response.data) # new post self.new_post() post = Post.query.first() self.assertEqual('title', post.title) self.assertEqual('summary', post.summary) self.assertEqual('test', post.body) self.assertEqual('None', post.category.name) self.assertIn('TEST', [tag.content for tag in post.tags]) self.assertIn('test again', [tag.content for tag in post.tags]) # show post response = self.client.get(url_for('post.article', post_id=post.id)) self.assertIn('title', response.data) # edit post new_category = Category(name='test', parent_category=Category.query.first()) new_category.save() response = self.client.get( url_for('post.edit_article', post_id=post.id)) self.assertIn('title', response.data) self.assertIn('summary', response.data) self.assertIn('test', response.data) self.assertIn('test again', response.data) self.assertIn('None', response.data) response = self.client.post(url_for('post.edit_article', post_id=post.id), data={ 'title': 'title again', 'summary': 'summary again', 'editor-markdown-doc': 'test again', 'category': 2, 'tags': 'TEST,TEST_TEST' }, follow_redirects=True) self.assertIn('该文章已修改', response.data) self.assertTrue('title again' == post.title) self.assertTrue('summary again' == post.summary) self.assertTrue('test again' == post.body) self.assertTrue('test' == post.category.name) self.assertFalse('test again' in [tag.content for tag in post.tags]) self.assertTrue('TEST' in [tag.content for tag in post.tags]) self.assertTrue('TEST_TEST' in [tag.content for tag in post.tags]) response = self.client.post(url_for('post.edit_article', post_id=post.id), data={ 'title': 'title again', 'summary': 'summary again', 'editor-markdown-doc': 'test again', 'category': 2, 'tags': 'TEST,test again' }, follow_redirects=True) self.assertTrue('该文章已修改' in response.data) self.assertFalse('TEST_TEST' in [tag.content for tag in post.tags]) self.assertTrue('TEST' in [tag.content for tag in post.tags]) self.assertTrue('test again' in [tag.content for tag in post.tags]) response = self.client.post(url_for('post.edit_article', post_id=post.id), data={ 'title': 'title again', 'summary': 'summary again', 'editor-markdown-doc': 'test again', 'category': 2, 'tags': '' }, follow_redirects=True) self.assertTrue('该文章已修改' in response.data) self.assertTrue(len(post.tags) == 0) # worry user self.logout() self.login(username='******', password='******') response = self.client.get( url_for('post.edit_article', post_id=post.id)) self.assertTrue('Forbidden' in response.data) response = self.client.get(url_for('post.delete_post', post_id=post.id), follow_redirects=True) self.assertTrue('Forbidden' in response.data) response = self.client.get(url_for('auth.logout'), follow_redirects=True) self.assertTrue('已注销' in response.data) self.login(username='******', password='******') # delete post response = self.client.get(url_for('post.delete_post', post_id=post.id), follow_redirects=True) self.assertTrue(Post.query.count() == 0) self.assertIn('已删除', response.data)