前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >自动创建权限表数据和超级管理员用户(第一次优化)

自动创建权限表数据和超级管理员用户(第一次优化)

作者头像
简单、
发布2018-07-17 17:32:31
5180
发布2018-07-17 17:32:31
举报

在公司python项目中,项目马上要上线,需求是超级管理员和权限表用户没权限访问或修改这两个功能。就是说项目初始化创建的时候,权限和超级管理员就自己在项目中了。 优化后代码

代码语言:javascript
复制
# -*- encoding: utf-8 -*-

import pymysql
import os
from datetime import datetime


def CreateDB(host, user, password, db):
    # 安装pymysql
    cmd = 'pip install -i https://pypi.douban.com/simple pymysql'
    os.system(cmd)
    # 对要操作的数据库进行连接
    conn = pymysql.connect(host=host, user=user, passwd=password, db=db, charset='utf8mb4')
    cur = conn.cursor()

    tb_role = cur.execute("SELECT * FROM tb_role")
    DATETIME = datetime.utcnow()
    # 判断表中是否存在数据
    if tb_role == 0:
        string = [
            (DATETIME, DATETIME, 1, 1, 0, 1, '用户列表', '/api/user/', 'GET'),
            (DATETIME, DATETIME, 1, 1, 0, 2, '添加用户', '/api/user/', 'POST'),
            (DATETIME, DATETIME, 1, 1, 0, 3, '编辑用户', '/api/user/<id>', 'PATCH'),
            (DATETIME, DATETIME, 1, 1, 0, 4, '用户组列表', '/api/userGroup/', 'GET'),
            (DATETIME, DATETIME, 1, 1, 0, 5, '添加用户组', '/api/userGroup/', 'POST'),
            (DATETIME, DATETIME, 1, 1, 0, 6, '编辑用户组', '/api/userGroup/<id>', 'PATCH'),
            (DATETIME, DATETIME, 1, 1, 0, 7, '权限列表', '/api/role/', 'GET'),
            (DATETIME, DATETIME, 1, 1, 0, 8, '添加权限', '/api/role/', 'POST'),
            (DATETIME, DATETIME, 1, 1, 0, 9, '编辑权限', '/api/role/<id>', 'PATCH'),
            (DATETIME, DATETIME, 1, 1, 0, 10, '设备列表', '/api/device/', 'GET'),
            (DATETIME, DATETIME, 1, 1, 0, 11, '添加设备', '/api/device/', 'POST'),
            (DATETIME, DATETIME, 1, 1, 0, 12, '编辑设备', '/api/device/<id>', 'PATCH'),
            (DATETIME, DATETIME, 1, 1, 0, 13, '设备组列表', '/api/deviceGroup/', 'GET'),
            (DATETIME, DATETIME, 1, 1, 0, 14, '添加设备组', '/api/deviceGroup/', 'POST'),
            (DATETIME, DATETIME, 1, 1, 0, 15, '编辑设备组', '/api/deviceGroup/<id>', 'PATCH')
        ]
        # 将string里面的数据转成一个list
        sets = []
        for insert in string:
            sets.append(insert)
        # 执行插入语句
        cur.executemany("INSERT INTO `tb_role` VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s)", sets)
        print 'db insert success!'

    else:
        print "数据已经存在!"

    try:

        super_user = cur.execute("SELECT * FROM auth_user WHERE username='fsaclmain' AND is_superuser='1'")
        # 判断超管是否存在
        if super_user == 0:
            cur.execute(
                "INSERT INTO `auth_user` VALUES (1,'pbkdf2_sha256$36000$qecR6HaHy1Hy$Zw1XE2aXS6pyEBbj2Qi2vwfRc6hjtdrmXb8Ai8rbotc=', NULL, 1, 'fsacladminl', '', '', '1@qq.com', 1, 1, '2017-09-28 06:12:58.417308')")
            print "superuser create success!"

    except Exception:
        print "超管已存在!"
    # commit 数据
    conn.commit()
    # 关闭数据库连接
    cur.close()
if __name__ == '__main__':

    CreateDB(host='数据库地址', user='用户', password='密码', db='数据库名称')

实现代码

代码语言:javascript
复制
import pymysql
import os

def useDB(host, user, password, db):
    cmd = 'pip install pymysql'
    os.system(cmd)
    conn = pymysql.connect(host=host, user=user, passwd=password, db=db, charset='utf8mb4')
    cur = conn.cursor()
    tb_role = cur.execute("SELECT * FROM tb_role")
    if tb_role == 0:
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:28:39.000000', '2017-11-16 08:28:42.000000', 1, 1, 0, 1, '用户列表', '/api/user/', 'GET')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:29:32.000000', '2017-11-16 08:29:35.000000', 1, 1, 0, 2, '添加用户', '/api/user/', 'POST')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:30:14.000000', '2017-11-16 08:30:17.000000', 1, 1, 0, 3, '编辑用户', '/api/user/<id>', 'PATCH')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:30:56.000000', '2017-11-16 08:30:59.000000', 1, 1, 0, 4, '用户组列表', '/api/userGroup/', 'GET')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:33:52.000000', '2017-11-16 08:33:55.000000', 1, 1, 0, 5, '添加用户组', '/api/userGroup/', 'POST')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:34:25.000000', '2017-11-16 08:34:27.000000', 1, 1, 0, 6, '编辑用户组', '/api/userGroup/<id>', 'PATCH')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:35:38.000000', '2017-11-16 08:35:40.000000', 1, 1, 0, 7, '权限列表', '/api/role/', 'GET')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:36:08.000000', '2017-11-16 08:36:11.000000', 1, 1, 0, 8, '添加权限', '/api/role/', 'POST')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:36:40.000000', '2017-11-16 08:36:45.000000', 1, 1, 0, 9, '编辑权限', '/api/role/<id>', 'PATCH')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:37:58.000000', '2017-11-16 08:38:02.000000', 1, 1, 0, 10, '设备列表', '/api/device/', 'GET')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:38:37.000000', '2017-11-16 08:38:40.000000', 1, 1, 0, 11, '添加设备', '/api/device/', 'POST')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:39:06.000000', '2017-11-16 08:39:09.000000', 1, 1, 0, 12, '编辑设备', '/api/device/<id>', 'PATCH')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:40:03.000000', '2017-11-16 08:40:05.000000', 1, 1, 0, 13, '设备组列表', '/api/deviceGroup/', 'GET')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:40:35.000000', '2017-11-16 08:40:37.000000', 1, 1, 0, 14, '添加设备组', '/api/deviceGroup/', 'POST')")
        cur.execute(
            "INSERT INTO `tb_role` VALUES ('2017-11-16 08:41:03.000000', '2017-11-16 08:41:05.000000', 1, 1, 0, 15, '编辑设备组', '/api/deviceGroup/<id>', 'PATCH')")
    else:
        print "数据已经存在"

    try:

        super_user = cur.execute("SELECT * FROM auth_user WHERE username='fsaclmain' AND is_superuser='1'")
        if super_user == 0:
            cur.execute(
                "INSERT INTO `auth_user` VALUES (1,'pbkdf2_sha256$36000$qecR6HaHy1Hy$Zw1XE2aXS6pyEBbj2Qi2vwfRc6hjtdrmXb8Ai8rbotc=', NULL, 1, 'fsacladminl', '', '', '1@qq.com', 1, 1, '2017-09-28 06:12:58.417308')")
            print "superuser create success!"
    except Exception, e:
        print e

    conn.commit()

    print 'db insert success!'

    cur.close()


if __name__ == '__main__':
    useDB(host='数据库地址', user='用户', password='密码', db='数据库名称')

后面再慢慢优化!

Tags: None

Archives QR Code

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档