Ejemplo n.º 1
0
    def admin(username, password):
        """设置管理员用户名与密码"""

        # 处理 MySQL 错误
        try:
            admin = Admin.query.first()
        except Exception as e:
            if '1146' in str(e.orig):
                click.echo('数据表不存在,请执行 `flask initdb` 创建数据表')
            else:
                print(e)
                click.echo('请检查错误信息')
            return

        with db.auto_commit():
            if admin:
                click.echo('更新管理员账户信息...')
                admin.username = username
                admin.password = password
            else:
                click.echo('创建管理员账户中...')
                admin = Admin()
                admin.username = username
                admin.password = password
                admin.blog_title = '临时博客名'
                admin.blog_subtitle = '临时博客副标题'
                admin.blog_about = '临时博客关于'
                admin.nickname = '临时昵称'
            db.session.add(admin)
            click.echo('Done.')
Ejemplo n.º 2
0
 def post(self):
     '''
     添加用户
     '''
     args_register = parse_register.parse_args()
     password = args_register.get('password')
     username = args_register.get('username').lower()
     name = args_register.get('name')
     email = args_register.get('email')
     phone = args_register.get('phone')
     has_admin = Admin.query.filter_by(username = username,is_del='0').first()
     if has_admin:
         abort(RET.Forbidden,msg='管理员已存在')     
     admin = Admin()
     admin.username = username
     admin.password = password
     admin.name = name
     admin.email = email
     admin.phone = phone
     admin.last_editor = g.admin.username
     if admin.add():
         data = {
             'status':RET.Created,
             'msg':'新增管理员成功',
             'data':admin
         }
         return marshal(data,sing_user_fields)
     abort(RET.BadRequest,msg='新增失败')
Ejemplo n.º 3
0
 def fake_admin(cls):
     """
     生成 admin 表虚拟数据
     :return: None
     """
     with db.auto_commit():
         admin = Admin()
         admin.username = '******'
         admin.password = '******'
         admin.nickname = '临时管理员昵称'
         admin.blog_title = '临时博客名'
         admin.blog_subtitle = '临时博客副标题'
         admin.blog_about = FakeData.FAKER.text(1000)
         db.session.add(admin)
Ejemplo n.º 4
0
 def fake_admin(cls):
     """
     生成 admin 表虚拟数据
     """
     with db.auto_commit():
         admin = Admin()
         admin.username = '******'
         admin.password = '******'
         admin.nickname = '临时管理员昵称'
         admin.blog_title = '临时Blog名'
         admin.blog_subtitle = '临时Blog副标题'
         admin.blog_about = FakeData.FAKER.text(1000)
         admin.email = '*****@*****.**'
         db.session.add(admin)
Ejemplo n.º 5
0
 def fake_admin(cls):
     """
     生成 admin 表虚拟数据
     :return: None
     """
     with db.auto_commit():
         admin = Admin()
         admin.username = '******'
         admin.password = '******'
         admin.nickname = '临时管理员昵称'
         admin.blog_title = '临时博客名'
         admin.blog_subtitle = '临时博客副标题'
         admin.blog_about = FakeData.FAKER.text(1000)
         # 请务必执行完 `flask fake` 之后,执行 `flask admin` 重设管理员账户,并填入你的真实邮箱
         # 否则你将无法收到新评论邮件
         admin.email = '*****@*****.**'
         db.session.add(admin)
Ejemplo n.º 6
0
def add_user():
    try:
        res = request.get_json()
        username = res.get('username')

        user_obj = Admin.query.filter_by(username=username).first()
        if user_obj:
            return jsonify(errno=-1, errmsg='用户名已存在')

        new_user = Admin()
        new_user.username = username

        db.session.add(new_user)
        db.session.commit()
        return jsonify(errno=0, errmsg='ok')
    except Exception as e:
        logging.error(e)
        return jsonify(errno=-1, errmsg='网络异常')
Ejemplo n.º 7
0
    def test_login_logout(self):
        admin = Admin()
        admin.username = '******'
        admin.password = '******'
        db.session.add(admin)
        db.session.commit()
        user = Admin.query.filter_by(id=1).first()
        self.assertTrue('test' == user.username)
        self.assertTrue(user.verify_password('test'))

        response = self.client.post(url_for('main.login'),
                                    data=dict(
                                        username='******',
                                        password='******',
                                    ),
                                    follow_redirects=True)
        self.assertTrue('退出' in response.data)
        self.assertTrue('文章' in response.data)

        response = self.client.get(url_for('main.logout'),
                                   follow_redirects=True)
        self.assertTrue('登录' in response.data)
        self.assertTrue('文章' in response.data)
Ejemplo n.º 8
0
    def test_about_me(self):
        admin = Admin()
        admin.username = '******'
        admin.password = '******'
        admin.about_me = 'about_me_test'
        db.session.add(admin)
        db.session.commit()
        self.client.post(url_for('main.login'),
                         data=dict(username='******', password='******'),
                         follow_redirects=True)

        response = self.client.get(url_for('main.about_me'),
                                   follow_redirects=True)
        self.assertTrue('about_me_test' in response.data)

        response = self.client.get(url_for('main.edit_about_me'),
                                   follow_redirects=True)
        self.assertTrue('about_me_test' in response.data)

        response = self.client.post(url_for('main.edit_about_me'),
                                    data=dict(about_me='m_about_me_test'),
                                    follow_redirects=True)
        self.assertTrue('m_about_me_test' in response.data)
Ejemplo n.º 9
0
    def test_post(self):
        #添加用户并登录获取权限admin = Admin()
        admin = Admin()
        admin.username = '******'
        admin.password = '******'
        db.session.add(admin)
        db.session.commit()
        self.client.post(url_for('main.login'),
                         data=dict(username='******', password='******'),
                         follow_redirects=True)

        title_test = 'title_test'
        abstract_test = 'abstract_test'
        tag_test = 'code'
        body_test = 'body_test '

        #写博客
        response = self.client.get(url_for('main.write_post'),
                                   follow_redirects=True)
        self.assertTrue(response.status_code == 200)
        self.assertTrue('提交' in response.data)
        response = self.client.post(url_for('main.write_post'),
                                    data=dict(title=title_test,
                                              abstract=abstract_test,
                                              tag=tag_test,
                                              body=body_test),
                                    follow_redirects=True)
        self.assertTrue(response.status_code == 200)
        p = Post.query.filter_by(title=title_test).first()
        self.assertTrue(p.abstract == abstract_test)
        self.assertTrue(p.tag == u'code-编程')
        self.assertTrue(p.body == body_test)

        #修改博客
        response = self.client.get(url_for('main.edit_post', id=p.id),
                                   follow_redirects=True)
        self.assertTrue(title_test in response.data)
        self.assertTrue(abstract_test in response.data)
        self.assertTrue('编程' in response.data)
        self.assertTrue(body_test in response.data)

        m_title_test = 'm_title_test'
        m_abstract_test = 'm_abstract_test'
        m_tag_test = 'net'
        m_body_test = 'm_body_test '
        response = self.client.post(url_for('main.edit_post', id=p.id),
                                    data=dict(title=m_title_test,
                                              abstract=m_abstract_test,
                                              tag=m_tag_test,
                                              body=m_body_test),
                                    follow_redirects=True)
        self.assertTrue(m_title_test in response.data)
        p1 = Post.query.filter_by(id=p.id).first()
        self.assertTrue(p1.title == m_title_test)
        self.assertTrue(p1.abstract == m_abstract_test)
        self.assertTrue(p1.tag == u'net-网络')
        self.assertTrue(p1.body == m_body_test)

        # 查看博客
        # 评论功能
        comment = 'comment_test'
        connection = '*****@*****.**'
        self.client.post(url_for('main.post', id=p.id),
                         data=dict(comment=comment, connect=connection),
                         follow_redirects=True)
        comments = Comment.query.filter_by(post_id=p.id).first()
        self.assertTrue(comments.connection == connection)
        self.assertTrue(comments.body == comment)
        response = self.client.get(url_for('main.post', id=p.id))
        self.assertTrue(response.status_code == 200)
        self.assertTrue(p.title in response.data)
        self.assertTrue(comments.body in response.data)

        #删除评论
        self.client.post(url_for('main.delete_comment', id=comments.id))
        comment_del = Comment.query.filter_by(id=comments.id).first()
        self.assertIsNone(comment_del)

        #删除博客
        self.client.post(url_for('main.delete_post', id=p.id))
        p2 = Post.query.filter_by(id=p.id).first()
        self.assertFalse(p2.is_active)
Ejemplo n.º 10
0
    def test_pic_upload(self):
        #登录获取权限以供后续测试
        admin = Admin()
        admin.username = '******'
        admin.password = '******'
        db.session.add(admin)
        db.session.commit()
        self.client.post(url_for('main.login'),
                         data=dict(username='******', password='******'),
                         follow_redirects=True)

        title_test = 'title_test'
        abstract_test = 'abstract_test'
        tag_test = 'code'
        body_test = 'body_test '

        # 插入一个文章以供后续测试
        response = self.client.post(url_for('main.write_post'),
                                    data=dict(title=title_test,
                                              abstract=abstract_test,
                                              tag=tag_test,
                                              body=body_test),
                                    follow_redirects=True)
        self.assertTrue(response.status_code == 200)
        p = Post.query.filter_by(title=title_test).first()
        self.assertTrue(p.abstract == abstract_test)
        self.assertTrue(p.tag == u'code-编程')
        self.assertTrue(p.body == body_test)

        #上传主题图片
        file = open('test.txt', 'w+')
        file.write('111111')
        response = self.client.post(url_for('main.uploaded_file', id=p.id),
                                    data=dict(file=file),
                                    follow_redirects=True)
        file.close()
        self.assertTrue(
            os.path.join(current_app.config['PIC_FOLDER'], 'test.txt') in
            response.data)
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'], 'test.txt')))
        self.assertTrue(p.head_pic == os.path.join(
            current_app.config['PIC_FOLDER'], 'test.txt'))
        os.remove('test.txt')

        #修改主题图片
        file = open('test1.txt', 'w+')
        file.write('222222')
        response = self.client.post(url_for('main.uploaded_file', id=p.id),
                                    data=dict(file=file),
                                    follow_redirects=True)
        file.close()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            os.path.join(current_app.config['PIC_FOLDER'], 'test1.txt') in
            response.data)
        self.assertFalse(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'], 'test.txt')))
        self.assertTrue(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'],
                             'test1.txt')))
        self.assertTrue(p.head_pic == os.path.join(
            current_app.config['PIC_FOLDER'], 'test1.txt'))
        os.remove('test1.txt')

        #测试上传文章内容图片
        file = open('test.txt', 'w+')
        file.write('111111')
        response = self.client.post(url_for('main.post_pic', id=p.id),
                                    data=dict(file=file),
                                    follow_redirects=True)
        file.close()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            os.path.join(current_app.config['PIC_FOLDER'], '0' +
                         str(p.id), 'test.txt') in response.data)
        self.assertTrue(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'],
                             '0' + str(p.id), 'test.txt')))
        self.assertTrue(
            p.body_pic == os.path.join(current_app.config['PIC_FOLDER'], '0' +
                                       str(p.id), 'test.txt'))
        os.remove('test.txt')

        file = open('test1.txt', 'w+')
        file.write('2222222')
        response = self.client.post(url_for('main.post_pic', id=p.id),
                                    data=dict(file=file),
                                    follow_redirects=True)
        file.close()
        self.assertTrue(response.status_code == 200)
        self.assertTrue(
            os.path.join(current_app.config['PIC_FOLDER'], '0' +
                         str(p.id), 'test1.txt') in response.data)
        self.assertTrue(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'],
                             '0' + str(p.id), 'test1.txt')))
        self.assertTrue(
            p.body_pic == os.path.join(current_app.config['PIC_FOLDER'], '0' +
                                       str(p.id), 'test.txt') + '|' +
            os.path.join(current_app.config['PIC_FOLDER'], '0' +
                         str(p.id), 'test1.txt'))
        os.remove('test1.txt')

        #删除文章后对相关图片的删除
        response = self.client.get(url_for('main.delete_post_fully', id=p.id),
                                   follow_redirects=True)
        self.assertTrue(response.status_code == 200)
        self.assertFalse(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'],
                             os.path.basename(p.head_pic))))
        self.assertFalse(
            os.path.exists(
                os.path.join(current_app.config['BASE_DIR'],
                             current_app.config['UPLOAD_FOLDER'],
                             '0' + str(p.id))))