Day4作业:蛋疼CRM系统

Posted ccorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day4作业:蛋疼CRM系统相关的知识,希望对你有一定的参考价值。

先上流程图,还得27寸4K显示器,画图各种爽:

ReadMe:

运行程序前的提示:
    1.抱歉,你得装prettytable模块......
    2.还得抱歉,如果shell中运行,最好把字体调得小点,表格比较长......蛋疼

为什么写两个版本:
    武sir这期的作业不是很难,但有一个需求的逻辑个人感觉不是很合理:普通用户可以选择管理员操作的选项。
    当然银角的目的是为了我们把装饰器运用到实际程序中。
    一般页面展示中,管理员操作选项对于普通用户会做两种处理:
    1、将选项灰色显示或者不能点击
    2、普通用户登录后不显示管理员操作选项,如果管理员登录后则显示
    出于以上原因,我写了两个版本。
    所以写代码注释时,我只写了第一个版本的,即crm.py

两个版本区别:
    第一个版本crm.py 基本是按武sir的需求写的,略有不同的是,我把登陆和注册作为入口
    第二个版本crm_v2.py 是在第一个版本基础上稍作修改,只是判断登录用户的是否为管理员,如果管理员登录则比普通用户多显示管理操作选项

文件说明:
    crm.py:第一个版本,管理员以及普通用户操作均显示在主菜单
    crm_v2.py:第二个版本,主菜单根据登录用户的角色判断是否显示管理员操作的选项
    user_info:用户信息,以管道符隔开:账号|密码|邮箱|电话|用户角色

测试账号:
    可以自己注册,也可以使用现成的。
    管理员:admin/admin     alex/3714
    普通用户:chengchen/123  cc/123

操作流程:
    登录/注册---->用户操作主菜单(普通用户操作【查看账户信息,修改帐户信息】,管理员操作【搜索,遍历用户,提升权限,重置密码】)

功能:
    1.程序入口为登录、注册页面,用户可选择对应操作
    2.登录时添加了随机验证码的功能
    3.登录后显示操作主菜单,普通用户可查看本帐户信息,也可修改帐户信息
    4.管理员操作权限做了限制,如果普通用户选择会提示用户权限不足
    5.管理员可对所有用户进行搜索、遍历所有用户、提升权限、重置所有用户密码、增加删除用户等操作
    6.管理员提升用户权限时,会对账号角色进行判断,如果已为管理员,会提示
    7.用户做修改操作时,避免了多个用户账号前几个字母相同时出现的用户越权现象:比如管理员为chengchen,普通用户为chengc
    8.显示用户信息时,对密码加密显示,并且查找时,对密码字段不进行查找
View Code

 

user_info 文件:

alex|3714|alex@gmail.com|13956093410|1
admin|admin|admin@admin.org|18888888888|1
chengchen|110|chengchen@163.com|18601206666|0
chengc|456|chengc@oldboy.com|18601203885|1
cc|123|niubi@niubi.com|13333333333|0
123|123|123@163.com|18601208888|0
ccccc|123|123@ccccc.com|18409573465|0
ccc|ccc|ccc@oldboy.org|18739549566|0

Code version1:

#!/usr/bin/env python
# -*-coding=utf-8-*-
# Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/
# GitHub:https://github.com/ccorzorz
import prettytable, time, random, os

USER_STATE = False
LOGIN_STATE=False
USER_NAME = None

def check_user_type(func):
    def inner(*args, **kwargs):
        if USER_STATE:
            r = func(*args, **kwargs)
            return r
        else:
            print(\'\\033[31;1m用户%s权限不够,管理员才可使用此功能\\033[0m\' % USER_NAME)
    return inner

def check_user_state(USER_NAME):
    with open(\'user_info\', \'r\') as f:
        for line in f:
            line = line.strip()
            if line.startswith(USER_NAME) and USER_NAME == line.split(\'|\')[0]:
                user_states = line.split(\'|\')[4]
                if user_states == \'1\':
                    global USER_STATE
                    USER_STATE = True
                else:
                    pass
    return USER_STATE

def user_name_list():
    name_list = []
    with open(\'user_info\', \'r\') as f:
        for line in f:
            name_list.append(line.strip().split(\'|\')[0])
        return name_list

def user_pwd_list():
    pwd_list = []
    with open(\'user_info\', \'r\') as f:
        for line in f:
            pwd_list.append(line.strip().split(\'|\')[1])
        return pwd_list


def veri_code():
    li = []
    for i in range(6):
        r = random.randrange(0, 5)
        if r == 1 or r == 4:
            num = random.randrange(0, 9)
            li.append(str(num))
        else:
            temp = random.randrange(65, 91)
            char = chr(temp)
            li.append(char)
    r_code = \'\'.join(li)
    print(\'\\033[31;1m%s\\033[0m\' % r_code)
    return r_code

def find_user_line_list(USER_NAME):
    with open(\'user_info\',\'r\') as f:
       for line in f:
            if line.strip().startswith(USER_NAME) and USER_NAME == line.strip().split(\'|\')[0]:
                user_name=USER_NAME
                pwd=line.strip().split(\'|\')[1]
                mail=line.strip().split(\'|\')[2]
                tel=line.strip().split(\'|\')[3]
                user_t=line.strip().split(\'|\')[4]
                line_list=[user_name,pwd,mail,tel,user_t]
            else:
                pass
    return line_list

def change_pwd(USER_NAME):
    new_pwd=input(\'请输入新的密码:\')
    with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
       for line in f:
            if line.strip().startswith(USER_NAME) and USER_NAME == line.strip().split(\'|\')[0]:
                line_list=find_user_line_list(USER_NAME)
                line_list[1]=new_pwd
                new_line=\'|\'.join(line_list)
                new_f.write(\'%s\\n\'%new_line)
            else:
                new_f.write(line)
    os.rename(\'user_info\',\'user_info.bak\')
    os.rename(\'user_info_new\',\'user_info\')
    os.remove(\'user_info.bak\')
    return True

def change_mail(USER_NAME):
    new_mail=input(\'请输入新邮箱:\')
    with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
       for line in f:
            if line.strip().startswith(USER_NAME) and USER_NAME == line.strip().split(\'|\')[0]:
                line_list=find_user_line_list(USER_NAME)
                line_list[2]=new_mail
                new_line=\'|\'.join(line_list)
                new_f.write(\'%s\\n\'%new_line)
            else:
                new_f.write(line)
    os.rename(\'user_info\',\'user_info.bak\')
    os.rename(\'user_info_new\',\'user_info\')
    os.remove(\'user_info.bak\')
    return True

def change_tel(USER_NAME):
    new_tel=input(\'请输入新的电话号码:\')
    with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
       for line in f:
            if line.strip().startswith(USER_NAME) and USER_NAME == line.strip().split(\'|\')[0]:
                line_list=find_user_line_list(USER_NAME)
                line_list[3]=new_tel
                new_line=\'|\'.join(line_list)
                new_f.write(\'%s\\n\'%new_line)
            else:
                new_f.write(line)
    os.rename(\'user_info\',\'user_info.bak\')
    os.rename(\'user_info_new\',\'user_info\')
    os.remove(\'user_info.bak\')
    return True

def modify_user_info(USER_NAME):
    row=prettytable.PrettyTable()
    row.field_names=[\'修改密码\',\'修改邮箱\',\'修改联系电话\']
    row.add_row([1,2,3])
    print(row)
    while True:
        inp=input(\'请选择功能菜单,\\033[32;1m返回主菜单请输入b或者back\\033[0m:\')
        if inp == \'1\':
            res=change_pwd(USER_NAME)
            if res:
                print(\'密码修改成功\')
                break
        elif inp == \'2\':
            res=change_mail(USER_NAME)
            if res:
                print(\'邮箱修改成功\')
                break
        elif inp == \'3\':
            res=change_tel(USER_NAME)
            if res:
                print(\'电话修改成功\')
                break
        elif inp == \'back\' or inp == \'b\':
            break
        else:
            print(\'输入有误,请重新输入!\')
    return True

def no_pwd_file():
    with open(\'user_info\', \'r\') as f, open(\'no_pwd\', \'w\') as new_f:
        for line in f:
            line = line.strip().lower().split(\'|\')
            del line[1]
            line = \'|\'.join(line)
            new_f.write(\'%s\\n\' % line)

@check_user_type
def show_all_user():
    no_pwd_file()
    row = prettytable.PrettyTable()
    row.field_names = [\'用户名\', \'密码\', \'邮箱\', \'电话\', \'账户类型\']
    with open(\'no_pwd\',\'r\') as f:
        for line in f:
            line = line.strip()
            if line.split(\'|\')[3]==\'1\':
                user_type=\'管理员\'
            elif line.split(\'|\')[3]==\'0\':
                user_type=\'普通用户\'
            row.add_row([line.split(\'|\')[0],\'***\',line.split(\'|\')[1],
                         line.split(\'|\')[2],user_type])
    print(row)
    os.remove(\'no_pwd\')
    return True

def show_user_info(USER_NAME):
    user_info_list = []
    with open(\'user_info\', \'r\') as f:
        for line in f:
            line = line.strip()
            if line.startswith(USER_NAME) and USER_NAME == line.split(\'|\')[0]:
                user_info_list.extend(line.split(\'|\'))
        if len(user_info_list) == 0:
            print(\'无此用户或者没有相关权限\')
        else:
            user_type = None
            if user_info_list[4] == \'1\':
                user_type = \'管理员\'
            elif user_info_list[4] == \'0\':
                user_type = \'普通用户\'
            row = prettytable.PrettyTable()
            row.field_names = [\'用户名\', \'密码\', \'邮箱\', \'电话\', \'账户类型\']
            row.add_row([user_info_list[0], \'****\', user_info_list[2],
                         user_info_list[3], user_type])
            print(row)

def regis():
    name_list = user_name_list()
    i = 0
    while i < 3:
        name = input(\'请输入用户名:\')
        if name in name_list:
            print(\'用户名%s已被注册\' % name)
            i += 1
            return False,name
        else:
            pwd = input(\'请输入密码:\')
            mail = input(\'请输入邮箱:\')
            tel = input(\'请输入电话:\')
            info = [name, pwd, mail, tel, \'0\']
            new_line = \'|\'.join(info)
            with open(\'user_info\', \'a\') as f:
                f.write(\'%s\\n\' % new_line)
            break
    return True,name

def login():
    name_list = user_name_list()
    pwd_list = user_pwd_list()
    exit_flag = 0
    i_a = 0
    i_b = 0
    i_c = 0
    while i_a < 3 and exit_flag == 0:
        USER_NAME= input(\'请输入用户名:\')
        if USER_NAME in name_list:
            while i_b < 3 and exit_flag == 0:
                pwd = input(\'请输入%s的密码:\' % USER_NAME)
                if pwd == pwd_list[name_list.index(USER_NAME)]:
                    while i_c < 3 and exit_flag == 0:
                        r_code = veri_code()
                        c_cod = input(\'请输入红色字体显示的校验码:\')
                        if c_cod.lower() == r_code.lower():
                            global LOGIN_STATE
                            LOGIN_STATE = True
                            print("登陆成功")
                            exit_flag = 1
                        elif i_c == 2:
                            exit_flag = 1
                            print(\'验证次数超过三次,登陆退出...\')
                        else:
                            i_c += 1
                            \'校验码不正确,请重新验证\'
                elif i_b == 2:
                    exit_flag = 1
                    print(\'尝试次数过多,退出登陆系统.\')
                else:
                    i_b += 1
                    print(\'密码不正确,请重新输入..\')
        else:
            i_a += 1
            print(\'无此账户,请确认用户名\')
    return LOGIN_STATE, USER_NAME

@check_user_type
def search(keywords):
    no_pwd_file()
    res = []
    search_res_list = []
    user_type = None
    with open(\'no_pwd\', \'r\') as f:
        for line in f:
            line = line.strip()
            if keywords in line:
                res.append(line)
    if len(res) == 0:
        print(\'通过关键字查询,无结果.\')
    else:
        for line in res:
            if line.split(\'|\')[3] == \'0\':
                user_type = \'普通用户\'
            elif line.split(\'|\')[3] == \'1\':
                user_type = \'管理员\'
            li = [line.split(\'|\')[0], line.split(\'|\')[1],
                  line.split(\'|\')[2], user_type, ]
            search_res_list.append(li)
        row = prettytable.PrettyTable()
        row.field_names = [\'用户名\', \'邮箱\', \'电话\', \'用户类型\']
        for line in search_res_list:
            row.add_row([line[0], line[1], line[2], line[3], ])
        print(row)
    os.remove(\'no_pwd\')

@check_user_type
def update_user(account):
    with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
        for line in f:
            if line.strip().startswith(account) and account == line.strip().split(\'|\')[0]:
                new_line_list=line.strip().split(\'|\')
                if new_line_list[4] == \'1\':
                    return False
                else:
                    new_line_list[4]=\'1\'
                    new_line=\'|\'.join(new_line_list)
                    new_f.write(\'%s\\n\'%new_line)
            else:
                new_f.write(line)
    os.rename(\'user_info\',\'user_info.bak\')
    os.rename(\'user_info_new\',\'user_info\')
    os.remove(\'user_info.bak\')
    return True

@check_user_type
def reset_pwd(account,new_pwd):
    with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
        for line in f:
            if line.strip().startswith(account) and account == line.strip().split(\'|\')[0]:
                new_line_list=line.strip().split(\'|\')
                new_line_list[1]=new_pwd
                new_line=\'|\'.join(new_line_list)
                new_f.write(\'%s\\n\'%new_line)
            else:
                new_f.write(line)
    os.rename(\'user_info\',\'user_info.bak\')
    os.rename(\'user_info_new\',\'user_info\')
    os.remove(\'user_info.bak\')
    return True

@check_user_type
def dele_user():
    show_all_user()
    name=input(\'输入您要删除的用户\')
    name_list=user_name_list()
    if name in name_list:
        with open(\'user_info\',\'r\') as f,open(\'user_info_new\',\'w\') as new_f:
            for line in f:
                if line.strip().startswith(name) and name == line.strip().split(\'|\')[0]:
                     pass
                else:
                    new_f.write(line)
        os.rename(\'user_info\',\'user_info.bak\')
        os.rename(\'user_info_new\',实践作业3:白盒测试----简单介绍被测系统DAY4

day4作业小代码练习

团队项目Scrum冲刺-day4

Beta冲刺-day4

白盒测试实践(小组作业)day4

day4 作业,插入图片以及音乐