Exemple #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.')
Exemple #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='新增失败')
Exemple #3
0
def create_superuser():
    admin = Admin()
    admin.manager = 'admin'
    admin.password = generate_password_hash('123')
    db.session.add(admin)
    db.session.commit()
    return redirect(url_for('admin.login'))
Exemple #4
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)
Exemple #5
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)
Exemple #6
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)
Exemple #7
0
def account():
    form = ChangeAccount()
    if not current_user.is_root:
        flash('您没有超级用户权限', 'danger')
        return redirect('/index.html')
    if form.validate_on_submit():
        if form.action.data == 'add':
            if Admin.query.filter_by(id=form.admin_id.data).first():
                flash('添加失败,管理员已存在', 'warning')
            elif not form.password.data:
                flash('请设置密码', 'warning')
            else:
                admin = Admin(form.admin_id.data, form.password.data,
                              form.is_root.data)
                db.session.add(admin)
                db.session.commit()
                flash('添加成功', 'success')
        elif form.action.data == 'update':
            admin = Admin.query.filter_by(id=form.admin_id.data).first()
            if admin:
                if form.password.data:
                    admin.password = form.password.data
                if form.is_root.data:
                    admin.is_root = form.is_root.data
                db.session.commit()
                flash('修改成功', 'success')
            else:
                flash('管理员不存在', 'warning')
        elif form.action.data == 'delete':
            admin = Admin.query.filter_by(id=form.admin_id.data).first()
            if admin:
                db.session.delete(admin)
                db.session.commit()
                flash('删除成功', 'success')
            else:
                flash('管理员不存在', 'warning')
    user_id = current_user.get_id()
    auth_list = Admin.query.all()
    return render_template("account.html",
                           username=user_id,
                           auth_list=auth_list,
                           form=form)
Exemple #8
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)
Exemple #9
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)
Exemple #10
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)
Exemple #11
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))))
Exemple #12
0
from app.models import Source, Admin, Settings
from config import Config

db.create_all()

# Add term sources in the db
existing_sources = Source.query.all()
if len(existing_sources) < len(Config.USER_SOURCES + Config.EVENT_SOURCES):
    for source in Config.EVENT_SOURCES:
        s = Source(type=source)
        db.session.add(s)

    for source in Config.USER_SOURCES:
        s = Source(type=source)
        db.session.add(s)

# add administrator in the db
existing_admins = Admin.query.all()
if len(existing_admins) < 1:
    a = Admin()
    a.password = Config.ADMIN_PASSWORD
    db.session.add(a)

# add settings in the db
existing_settings = Settings.query.all()
if len(existing_settings) < 1:
    s = Settings(penalized_correlations_on=True)
    db.session.add(s)

db.session.commit()