Example #1
0
def fake_categories(count=10):
    category=Category(name='Default')
    db.session.add(category)
    for i in range(count):
        category=Category(name=fake.word())
        db.session.add(category)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()
Example #2
0
def fake_categories():
    category = Category(name="默认分类")
    db.session.add(category)
    category1 = Category(name="Flask")
    category2 = Category(name="Leetcode")
    category3 = Category(name="Python")
    category4 = Category(name="Basketball")
    db.session.add(category1)
    db.session.add(category2)
    db.session.add(category3)
    db.session.add(category4)
    db.session.commit()
Example #3
0
 def setUp(self):
     self.now = datetime.datetime.utcnow().replace(tzinfo=utc)
     self.timedelta = datetime.timedelta(15)
     author = User.objects.get(pk=1)
     category = Category()
     category.name = "Test Category"
     for count in range(1, 11):
         post = Post(title="Post %d Title" % count,
                     text="foo",
                     author=author)
         if count < 6:
             # publish the first five posts
             pubdate = self.now - self.timedelta * count
             post.published_date = pubdate
         post.save()
Example #4
0
    def init(username, password):
        """Building Blog, just for you."""
        click.echo("Initializing the database...")
        db.create_all()

        admin = Admin.query.first()
        if admin:
            click.echo("The administrator already exists, updating... ")
            admin.username = username
            admin.set_password(password)
        else:
            click.echo("Creating the temporary administrator account...")
            admin = Admin(
                username=username,
                blog_title="Blog",
                blog_sub_title="No, I'm the real thing.",
                name="Admin",
                about="Anything about you.",
            )
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if not category:
            click.echo("Creating the default category... ")
            category = Category(name="Default")
            db.session.add(category)

        db.session.commit()
        click.echo("Done.")
Example #5
0
    def test_delete_category(self):
        category = Category(name='Tech')
        post = Post(title='test', category=category)
        db.session.add(category)
        db.session.add(post)
        db.session.commit()

        response = self.client.get(url_for('admin.delete_category',
                                           category_id=1),
                                   follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertNotIn('Category deleted.', data)
        self.assertIn('405 Method Not Allowed', data)

        response = self.client.post(url_for('admin.delete_category',
                                            category_id=1),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('You can not delete the default category.', data)
        self.assertNotIn('Category deleted.', data)
        self.assertIn('Default', data)

        response = self.client.post(url_for('admin.delete_category',
                                            category_id=2),
                                    follow_redirects=True)
        data = response.get_data(as_text=True)
        self.assertIn('Category deleted.', data)
        self.assertIn('Default', data)
        self.assertNotIn('Tech', data)
Example #6
0
def add_category():
    category_id = request.json.get('id')
    name = request.json.get('name')
    category = Category(id=category_id, name=name)
    db.session.add(category)
    db.session.commit()
    return jsonify(type="add category success")
Example #7
0
    def init(username, password):
        """Building Bluelog, just for you."""

        click.echo('Initializing the database...')
        db.create_all()

        admin = Admin.query.first()
        if admin is not None:
            click.echo('The administrator already exists, updating...')
            admin.username = username
            admin.set_password(password)
        else:
            click.echo('Creating the temporary administrator account...')
            admin = Admin(
                username="******",
                blog_title='ligewudi',
                blog_sub_title="cookie!",
                name='lyh',
                about="a little bit of niubi",
            )
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo('Creating the default category...')
            category = Category(name='Default')
            db.session.add(category)

        db.session.commit()
        click.echo('Done.')
Example #8
0
def new_post():
    form = UploadForm()
    if form.validate_on_submit():
        file = form.post.data
        content = file.read().decode('utf-8')
        yaml_config = yaml.load(content.split("---")[1].replace('\t', ' '))
        title = yaml_config.get('title')
        body = markdown.markdown(''.join(content.split("---")[2:]),
                                 extensions=['markdown.extensions.extra', 'markdown.extensions.codehilite',
                                             'markdown.extensions.tables', 'markdown.extensions.toc'])

        category_name = yaml_config.get('category')[0]
        category_exist = Category.query.filter_by(name=category_name).first()
        if category_exist:
            category = Category.query.get(category_exist.id)
        else:
            category = Category(name=category_name)
            db.session.add(category)
            db.session.commit()
        post = Post(title=title, body=body, category=category)
        db.session.add(post)
        db.session.commit()
        flash('Post created.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Example #9
0
    def init(username, password):
        """Building myblog, just for you."""

        click.echo('Initializing the database...')
        db.create_all()

        admin = Admin.query.first()
        if admin is not None:
            click.echo('The administrator already exists, updating...')
            admin.username = username
            admin.set_password(password)
        else:
            click.echo('Creating the temporary administrator account...')
            admin = Admin(
                username=username,
                blog_title='myblog',
                name='Admin',
            )
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo('Creating the default category...')
            category = Category(name='Default')
            db.session.add(category)

        db.session.commit()
        click.echo('Done.')
Example #10
0
    def init(username, password):
        """Building MyBlog, just for you."""
        click.echo('Initializing the database...')
        db.create_all()

        admin = Admin.query.first()
        if admin:  # 如果数据库中已经有管理员记录就更新用户和密码
            click.echo('The administrator already exsists, updating...')
            admin.username = username
            admin.set_password(password)
        else:
            click.echo('Creating the temporary administrator account...')
            admin = Admin(username=username,
                          blog_title='MyBlog',
                          blog_sub_title="Discover the beauty everywhere.",
                          name='Admin',
                          about='Some things about you')
            admin.set_password(password)
            db.session.add(admin)

            category = Category.query.first()
            if category is None:
                click.echo('Creating the default category...')
                category = Category(name='default')
                db.session.add(category)

            db.session.commit()
            click.echo('Done.')
Example #11
0
    def init(username, password):
        click.echo('initilize the database')
        db.create_all()
        admin = Admin.query.first()
        if admin is not None:
            click.echo("the administrator already exists, updating...")
            admin.username = username
            admin.set_password(password)
        else:
            click.echo("Creating the temporary adminstrator account")
            admin = Admin(username=username,
                          blog_title='Bluelog',
                          blog_sub_title="No, I'm the real thing.",
                          name='Admin',
                          about='Anything about you.')
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo("Creating the default category...")
            category = Category(name='Default')
            db.session.add(category)

        db.session.commit()
        click.echo('None')
Example #12
0
    def init(username, password):
        click.echo('初始化数据库...')
        db.create_all()

        admin = Admin.query.first()
        if admin is not None:  # 如果管理员已经存在就更新用户名和密码
            click.echo('管理员已经存在,正在更新...')
            admin.username = username
            admin.set_password(password)
        else:  # 否则,创建新管理员
            click.echo('创建临时管理员帐户...')
            admin = Admin(username="******",
                          blog_title="维度's Blog",
                          blog_sub_title="快乐生活,快乐编程!",
                          name="Gweid",
                          about="我,Gweid,一个小小的程序员...")
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo('创建默认分类...')
            category = Category(name='默认')
            db.session.add(category)

        db.session.commit()
        click.echo('完成')
Example #13
0
    def init(username, password):
        """Building myblog, just for you."""

        click.echo('初始化数据库中...')
        db.create_all()

        admin = Admin.query.first()
        if admin is not None:
            click.echo('管理员已存在,更新管理员中...')
            admin.username = username
            admin.set_password(password)
        else:
            click.echo('新建一个管理员账号...')
            admin = Admin(
                username=username,
                blog_title='myblog',
            )
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo('正在新建一个默认分类...')
            category = Category(name='默认')
            db.session.add(category)

        db.session.commit()
        click.echo('已完成')
Example #14
0
def category_detail_view(request, category_id):
    cat = Post.objects.exclude(published_date__exact=None)
    try:
        post = Category.get(pk=post_id)
    except Post.DoesNotExist:
        raise Http404
    context = {'post': post}
    return render(request, 'detail.html', context)
Example #15
0
def new_category():
    form = CategoryForm()
    if form.validate_on_submit():
        category = Category(name=form.name.data)
        db.session.add(category)
        db.session.commit()
        return redirect(url_for("blog.index"))
    return render_template("/admin/new_category.html", form=form)
Example #16
0
    def setUp(self) -> None:
        super().setUp()
        self.login()

        category = Category(name="Default")
        post = Post(title="Hello", category=category, body="Blah...")
        comment = Comment(body="A comment", post=post, from_admin=True)
        link = Link(name="GitHub", url="https://github.com/greyli")
        db.session.add_all([category, post, comment, link])
        db.session.commit()
Example #17
0
    def setUp(self):
        super(AdminTestCase, self).setUp()
        self.login()

        category = Category(name='Default')
        post = Post(title='Hello', category=category, body='Blah...')
        comment = Comment(body='A comment', post=post, from_admin=True)
        link = Link(name='GitHub', url='https://github.com/greyli')
        db.session.add_all([category, post, comment, link])
        db.session.commit()
Example #18
0
def new_category():
    form=CategoryForm()
    if form.validate_on_submit():
        name=form.name.data
        category=Category(name=name)
        db.session.add(category)
        db.session.commit()
        flash('Category created','success')
        return redirect(url_for('.manage_category'))
    return render_template('admin/new_category.html',form=form)
Example #19
0
class FrontEndTestCase(TestCase):
    """test views provided in the front-end"""
    fixtures = ['myblog_test_fixture.json', ]
    
    def setUp(self):
        self.now = datetime.datetime.utcnow().replace(tzinfo=utc)
        self.timedelta = datetime.timedelta(15)
        author = User.objects.get(pk=1)
        self.category = Category(name='A Category')
        self.category.save()
        for count in range(1,11):
            post = Post(title="Post %d Title" % count,
                        text="foo",
                        author=author)
            if count < 6:
                # publish the first five posts
                pubdate = self.now - self.timedelta * count
                post.published_date = pubdate
            post.save()
            if bool(count & 1):
                # put odd items in category:
                self.category.posts.add(post)

    def test_list_only_published(self):
        resp = self.client.get('/')
        self.assertTrue("Recent Posts" in resp.content)
        for count in range(1,11):
            title = "Post %d Title" % count
            if count < 6:
                self.assertContains(resp, title, count=1)
            else:
                self.assertNotContains(resp, title)

    def test_details_only_published(self):
        for count in range(1,11):
            title = "Post %d Title" % count
            post = Post.objects.get(title=title)
            resp = self.client.get('/posts/%d/' % post.pk)
            if count < 6:
                self.assertEqual(resp.status_code, 200)
                self.assertContains(resp, title)
            else:
                self.assertEqual(resp.status_code, 404)
Example #20
0
def new_category():
    form = CategoryForm()
    if form.validate_on_submit():
        # 可以对name再做一个防重复验证
        category = Category(name=form.name.data)
        db.session.add(category)
        db.session.commit()
        flash("Category created.", "success")
        return redirect(url_for(".manage_category"))

    return render_template("admin/new_category.html", form=form)
Example #21
0
    def initdb(drop):
        """Initialize the database."""
        if drop:
            click.confirm(
                'This operation will delete the database, do you want to continue',
                abort=True)
            db.drop_all()
            click.echo('Drop tables')

        db.create_all()

        click.echo('Creating the categorys...')
        Math = Category(name="Math")
        Computer = Category(name="CS")
        Physics = Category(name="Physics")
        Life = Category(name="Life")

        db.session.add_all([Math, Computer, Physics, Life])
        db.session.commit()

        click.echo('Initialized databases.')
Example #22
0
 def setUp(self):
     self.now = datetime.datetime.utcnow().replace(tzinfo=utc)
     self.timedelta = datetime.timedelta(15)
     author = User.objects.get(pk=1)
     self.category = Category(name='A Category')
     self.category.save()
     for count in range(1,11):
         post = Post(title="Post %d Title" % count,
                     text="foo",
                     author=author)
         if count < 6:
             # publish the first five posts
             pubdate = self.now - self.timedelta * count
             post.published_date = pubdate
         post.save()
         if bool(count & 1):
             # put odd items in category:
             self.category.posts.add(post)
Example #23
0
    def setUp(self):
        super(BlogTestCase, self).setUp()
        self.login()

        category = Category(name='Default')
        topic = Topic(name="test")
        post = Post(title='Hello Post',
                    category=category,
                    topic=topic,
                    body='Blah...')
        comment = Comment(body='A comment',
                          post=post,
                          from_admin=True,
                          reviewed=True)
        link = Link(name='GitHub', url='https://github.com/syntomic')

        db.session.add_all([category, post, comment, link])
        db.session.commit()
Example #24
0
    def init(username, password):
        """
        注册管理员
        :param username:
        :param password:
        :return:
        """

        click.echo('Initializing the database...')
        db.create_all()

        admin = Admin.query.first()
        if admin is not None:
            click.echo('The administrator already exists, updating...')
            admin.username = username
            admin.set_password(password)
        else:
            click.echo('Creating the temporary administrator account...')
            admin = Admin(
                username=username,
                blog_title='Bluelog',
                blog_sub_title="No, I'm the real thing.",
                name='Admin',
                about='Anything about you.'
            )
            admin.set_password(password)
            db.session.add(admin)

        category = Category.query.first()
        if category is None:
            click.echo('Creating the default category...')
            category = Category(name='Default')
            db.session.add(category)

        db.session.commit()
        click.echo('Done.')
Example #25
0
 def test_string_representation(self):
     expected = "A Category"
     c1 = Category(name=expected)
     actual = str(c1)
     self.assertEqual(expected, actual)
Example #26
0
 def setUp(self):
     for count in range(1, 5):
         category = Category(name="Category %d" % count, description="This is the %d category." % count)
         category.save()
Example #27
0
def fake_default():
    tag = Tag(name='默认')
    category = Category(name='默认')
    db.session.add(tag)
    db.session.add(category)
    db.session.commit()
Example #28
0
 def test_unicode(self):
     expected = "A Category"
     c1 = Category(name=expected)
     actual = unicode(c1)
     self.assertEqual(expected, actual)