예제 #1
0
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)
예제 #2
0
 def setUp(self):
     self.app = create_app('testing')
     self.app_context = self.app.app_context()
     self.app_context.push()
     db.create_all()
     Role.insert_roles()
     User.add_admin()
     User.add_test_user()
     Category.add_none()
     self.client = self.app.test_client()
예제 #3
0
 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
예제 #4
0
def generate_category(count=40, locale="en"):
    text = Text(locale)
    for _ in range(count):
        while True:
            name = text.word()
            if Category.query.filter_by(name=name).first() is None:
                break
        new = Category(name=name)
        if _ % 2:
            parent = Category.query.order_by(sql_expression_rand()).first()
            new.parent_category = parent
        db.session.add(new)
    db.session.commit()
예제 #5
0
    def test_04_post_put(self):
        self.login()
        post = self.new_post()

        title = self.text.title()
        summary = self.text.text(3)
        body = self.text.text(6)
        category = self.text.word()
        Category(name=category).save()
        tag = ",".join("'" + self.text.word() + "'" for _ in range(3))
        data = dict(title=title,
                    summary=summary,
                    body=body,
                    category=category,
                    tag=tag)
        self.assertEqual(len(post.tags), 1)

        response = self.client.put("/api/post/1",
                                   data=json.dumps(data),
                                   content_type="application/json")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(post.title, title)
        self.assertEqual(post.summary, summary)
        self.assertEqual(post.body, body)
        self.assertEqual(post.category.name, category)
        self.assertEqual(len(post.tags), 3)
예제 #6
0
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
예제 #7
0
    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"))
예제 #8
0
 def test_07_category_posts_query(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_query().count() == 0)
     self.assertTrue(category.posts_query().count() == 0)
     post = Post(body='# Post Big title', category=category)
     post.save()
     query = Post.query
     self.assertTrue(none.posts_query().count() == 1)
     self.assertTrue(category.posts_query().count() == 1)
     self.assertTrue(none.posts_query(query).count() == 1)
     self.assertTrue(category.posts_query(query).count() == 1)
예제 #9
0
    def add_category(name):
        """Add New Category"""
        c = Category()
        c.name = name
        c.slug = c.set_slug(name)
        c.save()

        print('New category added.')
예제 #10
0
 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)
예제 #11
0
    def setUpClass(cls):
        cls.base_url = 'http://127.0.0.1:5000'
        try:
            cls.client = webdriver.Firefox()
        except:
            pass

        if cls.client:
            cls.app = create_app('testing')
            cls.app_context = cls.app.app_context()
            cls.app_context.push()

            import logging
            logger = logging.getLogger('werkzeug')
            logger.setLevel("ERROR")

            db.create_all()
            Role.insert_roles()
            User.add_admin()
            User.add_test_user()
            Category.add_none()

            threading.Thread(target=cls.app.run).start()
예제 #12
0
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}))
예제 #13
0
파일: manage.py 프로젝트: zxb88918/weblog
def deploy():
    """Run deployment task"""
    if not os.path.isfile(app.config['SQLALCHEMY_DATABASE_URI'][10:]):
        db.create_all()
        print 'Datebase created.'
        Role.insert_roles()
        User.add_self_follows()
        User.add_admin()
        User.add_test_user()
        print 'add admin :'\
            '\[email protected]'\
            '\npassword=1234'
        Category.add_none()
        print 'Category insert None.'
    else:
        print 'database already exists!'
    admin_id = input('admin_id:')
    while True:
        if User.query.get(admin_id):
            User.add_admin_dialogue(admin_id)
            break
        else:
            admin_id = input('get admin error!\nadmin_id:')
        print 'User config.'
예제 #14
0
 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)
예제 #15
0
 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)
예제 #16
0
 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)
예제 #17
0
 def test_05_category_add_none(self):
     post = Post(body='# Post Big title')
     post.save()
     self.assertTrue(post.category is None)
     Category.add_none()
     self.assertTrue(post.category == Category.query.get(1))