Skip to content

Commit

Permalink
tabrole表加数据
Browse files Browse the repository at this point in the history
  • Loading branch information
lihy0 committed Mar 17, 2023
1 parent 113e6c1 commit 6bddf5f
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 2 deletions.
Binary file modified database/role_perm.xlsx
Binary file not shown.
33 changes: 33 additions & 0 deletions lhy_test_user-role-permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from faker import Faker
from torcms.handlers.user_handler import MUser
from torcms.handlers.permission_handler import MPermission


class Create_Data(object):
def __init__(self):
# 选择中文
fake = Faker('zh_CN')
# 生成数据改变循环体来控制数据量rang(?)
self.data_total = [
[fake.name(), fake.job(), fake.company(), fake.phone_number(), fake.company_email(), fake.address(),
fake.date_time(tzinfo=None)] for x in range(100)]
print(self.data_total)

def deal_postgresql(self):
# 写入mysql

# SQL 插入语句
for val in self.data_total:
post_data = {
'user_name': val[0],
'user_email': val[4],
'user_pass': val[0],
}

MUser.create_user(post_data)


if __name__ == '__main__':
data = Create_Data()

data.deal_postgresql()
87 changes: 87 additions & 0 deletions script_gen_role_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# -*- coding: utf-8
'''
Genereting catetory.
'''
import os

import yaml
from openpyxl.reader.excel import load_workbook

from torcms.model.category_model import MCategory

from torcms.model.permission_model import MPermission
from torcms.model.role_model import MRole

XLSX_FILE = './database/role_perm.xlsx'


def gen_xlsx_category():
'''
Genereting role,permission from xlsx file.
'''


all_cate_arr = []

for sheet_ranges in load_workbook(filename=XLSX_FILE):
kind_sig = str(sheet_ranges['A1'].value).strip()


for row_num in range(3, 10000):

# 父类
a_cell_val = sheet_ranges['A{0}'.format(row_num)].value
b_cell_val = sheet_ranges['B{0}'.format(row_num)].value
c_cell_val = sheet_ranges['C{0}'.format(row_num)].value
d_cell_val = sheet_ranges['D{0}'.format(row_num)].value

if a_cell_val and a_cell_val != '':
cell_arr = a_cell_val.strip()
uid = kind_sig + cell_arr.split(":")[0]
name = cell_arr.split(":")[1]
pid = '0000'
auid=uid
elif b_cell_val and b_cell_val != '':
cell_arr = b_cell_val
uid = kind_sig + cell_arr.split(":")[0]
pid = auid
name = name = cell_arr.split(":")[1]
buid=uid
elif c_cell_val and c_cell_val != '':
cell_arr = c_cell_val
uid = kind_sig + cell_arr.split(":")[0]
pid = buid
name = name = cell_arr.split(":")[1]
cuid=uid
elif d_cell_val and d_cell_val != '':
cell_arr = d_cell_val
uid = kind_sig + cell_arr.split(":")[0]
pid = cuid
name = name = cell_arr.split(":")[1]

else:
continue

post_data = {
'name': name,
'uid': uid,
'pid': pid
}

all_cate_arr.append(post_data)
MRole.add_or_update(uid, post_data)

return all_cate_arr





def run_gen_category(*args):
'''
to run
'''
gen_xlsx_category()

if __name__ == '__main__':
gen_xlsx_category()
32 changes: 31 additions & 1 deletion torcms/model/role_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
'''
from torcms.model.abc_model import MHelper
from torcms.model.core_tab import TabRole
from torcms.core import tools


class MRole():
'''
For friends links.
'''

@staticmethod
def get_counts():
'''
Expand Down Expand Up @@ -44,4 +46,32 @@ def update(uid, post_data):
'''
Updat the link.
'''
return False
raw_rec = TabRole.get(TabRole.uid == uid)
entry = TabRole.update(
name=post_data.get('name', raw_rec.name),
pid=post_data.get('pid', raw_rec.pid),
status=post_data.get('status', 0),
time_create=tools.timestamp(),
time_update=tools.timestamp(),
).where(TabRole.uid == uid)
entry.execute()

@staticmethod
def add_or_update(uid, post_data):
'''
Add or update the data by the given ID of post.
'''
catinfo = MRole.get_by_uid(uid)
if catinfo:
MRole.update(uid, post_data)
else:
TabRole.create(
uid=uid,
name=post_data['name'],
pid=post_data['pid'],
status=post_data[0],
time_create=tools.timestamp(),
time_update=tools.timestamp(),

)
return uid
88 changes: 88 additions & 0 deletions torcms/script/script_gen_role_permission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8
'''
Genereting catetory.
'''
import os

import yaml
from openpyxl.reader.excel import load_workbook

from torcms.model.category_model import MCategory

from torcms.model.permission_model import MPermission
from torcms.model.role_model import MRole

XLSX_FILE = './database/role_perm.xlsx'


def gen_xlsx_category():
'''
Genereting catetory from xlsx file.
'''

# 在分类中排序
order_index = 1

all_cate_arr = []

for sheet_ranges in load_workbook(filename=XLSX_FILE):
kind_sig = sheet_ranges.get_sheet_names()
print(kind_sig)

# for row_num in range(3, 10000):
#
# # 父类
# a_cell_val = sheet_ranges['A{0}'.format(row_num)].value
# b_cell_val = sheet_ranges['B{0}'.format(row_num)].value
# c_cell_val = sheet_ranges['C{0}'.format(row_num)].value
#
# if a_cell_val or b_cell_val or c_cell_val:
# pass
# else:
# break
#
# if a_cell_val and a_cell_val != '':
# cell_arr = a_cell_val.strip()
# p_uid = cell_arr[1:] # 所有以 t 开头
# t_slug = sheet_ranges['C{0}'.format(row_num)].value.strip()
# t_title = sheet_ranges['D{0}'.format(row_num)].value.strip()
# u_uid = p_uid + (4 - len(p_uid)) * '0'
# pp_uid = '0000'
# elif b_cell_val and b_cell_val != '':
# cell_arr = b_cell_val
# c_iud = cell_arr[1:]
# t_slug = sheet_ranges['C{0}'.format(row_num)].value.strip()
# t_title = sheet_ranges['D{0}'.format(row_num)].value.strip()
# if len(c_iud) == 4:
# u_uid = c_iud
# else:
# u_uid = '{0}{1}'.format(p_uid, c_iud)
# pp_uid = p_uid + (4 - len(p_uid)) * '0'
# else:
# continue
#
# post_data = {
# 'name': t_title,
# 'slug': t_slug,
# 'order': order_index,
# 'uid': u_uid,
# 'pid': pp_uid,
# 'kind': kind_sig,
# }
# all_cate_arr.append(post_data)
# MCategory.add_or_update(u_uid, post_data)
# order_index += 1
# return all_cate_arr





def run_gen_role_permission(*args):
'''
to run
'''
gen_xlsx_category()

if __name__ == '__main__':
run_gen_role_permission()
3 changes: 2 additions & 1 deletion torcms/script/script_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .script_gen_category import run_gen_category
from .script_init_tabels import run_init_tables
from .script_zero import run_zero

from .script_gen_role_permission import run_gen_role_permission

def run_init(*args):
'''
Expand All @@ -30,6 +30,7 @@ def run_init(*args):
sys.exit()

run_init_tables()
run_gen_role_permission()
run_gen_category()
run_create_admin()
run_whoosh()

0 comments on commit 6bddf5f

Please sign in to comment.