The Seven Year Itch.

MENU

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

November 19, 2017 • Read: 348 • Python

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

# -*- 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='数据库名称')

实现代码

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
QR Code for this page
Tipping QR Code