def test_permission_user(): ps = [ 'LOGIN', 'VIEW_POST', 'PUBLISH_POST', 'PUBLISH_COMMENT', 'LOGIN_CMS', 'MANAGE_POST', 'MANAGE_COMMENTE', 'BOARDER', 'BANNER', 'FRONTUSER', 'CMSUSER', 'ADMINER' ] msg = {} for p in ps: permissions = Permission.ALL_PERMISSION - getattr(Permission, p) role = Role(name='FrontUser', permissions=permissions) u = User() u.telephone = fake.phone_number() u.username = '******' + p u.email = fake.ascii_free_email() u.password = '******' u.confirm = True u.realname = 'NOT-' + p with open('mxavatar.json', 'r') as f: avatar_list = json.load(f) u.avatar = choice(avatar_list) u.signature = fake.paragraph(nb_sentences=3, variable_nb_sentences=True) u.gender = GenderEnum(randint(1, 4)) u.join_time = fake.date_this_year(before_today=True, after_today=False) u.role = role db.session.add(u) msg[u.username] = u.telephone try: db.session.commit() except Exception: db.session.rollback() print('测试用户生成成功!') print(msg)
def test_group_user(): roles = ['FrontUser', 'Operator', 'Administrator', 'Super'] msg = {} for r in roles: role = Role(group=r) u = User() u.telephone = fake.phone_number() u.username = r + '-test' u.email = fake.ascii_free_email() u.password = '******' u.confirm = True u.realname = fake.name() with open('mxavatar.json', 'r') as f: avatar_list = json.load(f) u.avatar = choice(avatar_list) u.signature = fake.paragraph(nb_sentences=3, variable_nb_sentences=True) u.gender = GenderEnum(randint(1, 3)) u.join_time = fake.date_this_year(before_today=True, after_today=False) u.role = role db.session.add(u) msg[r] = u.telephone try: db.session.commit() except Exception as e: print(e) db.session.rollback() print('测试用户生成失败!') return print('测试用户生成成功!') print(msg)
def front_user(count=10): fake = Faker(locale='zh_CN') n = 0 for i in range(count): u = User() u.telephone = fake.phone_number() u.username = fake.user_name() u.email = fake.ascii_free_email() u.password = '******' u.confirm = choice([True, False]) u.realname = fake.name() with open('mxavatar.json', 'r') as f: avatar_list = json.load(f) u.avatar = choice(avatar_list) u.signature = fake.paragraph(nb_sentences=3, variable_nb_sentences=True) u.gender = GenderEnum(randint(1, 4)) u.join_time = fake.date_this_year(before_today=True, after_today=False) role = Role() u.role = role db.session.add(u) try: db.session.commit() n += 1 except Exception: db.session.rollback() print('Front用户添加成功,共同添加%d个用户' % n)