Python-S13作业-day4-之登陆,管理后台
需求:
本节作业,用户管理程序:
普通用户:
登录,注册,修改密码,查看本用户信息
管理员用户:
查看所有普通用户,按照指定关键字搜索用户信息;
登录,注册,修改密码,查看本用户信息;
删除、添加普通用户
修改普通用户密码
提高普通用户权限
注意:
1、用户信息:要存文件里
2、权限验证,要用装饰器
思路:
小感悟:写这个程序让我知道,其实写一个大的程序,
(1)就是把一个个功能(函数)拼凑起来;
(2)然后按照正常的逻辑执行调用就行了;
Readme:
1.这个程序共包含两个文件,主程序文件,db存放账号密码的文件;
2.代码结构:
最上面放的是login()函数;
中间放的是功能;
下面放的是入口管理功能;
主程序:
#!/usr/bin/python env
#_*_coding:utf-8 _*_
import sys
import shutil
import os LOG_USER = {'is_login':False}
#使用字典形式,设置一个标志位,用于判断用户是否登陆成功; def outer(func):
def inner(*args,**kwargs):
if LOG_USER['is_login']: #判断用户是否登陆
r = func()
return r
else:
print("Please login.") #否则提示没有登陆
return inner
#这个装饰器给普通用户使用,
# 这里判断用户已经登陆,才能执行mamger_user()函数; def outer1(func):
def inner(*args,**kwargs):
if LOG_USER['is_login'] and LOG_USER['user_type'] == '': #判断条件
r = func()
return r
else:
print("权限不够") ##普通账号会提示“权限不足”;
return inner
#这个装饰器给管理员账号使用;
#用户登陆,并且是管理员账号,才能执行manger_admin()函数; def login(user,pwd):
with open('db','r',encoding='utf-8') as f:
for line in f:
line = line.strip().split("|")
if line[0] == user and line[1] == pwd:
LOG_USER['is_login'] = True #如果登陆成功,这里为True
LOG_USER['user_type'] = line[-1] #获取用户的UID
LOG_USER['user_info'] = line[:] #获取当前登陆用户所有信息
return True
return False
#登陆功能 def changepwd(user,new_pwd):
with open('db','r',encoding='utf-8') as f1,open('db_new','w',encoding='utf-8') as f2:
for line in f1:
line_list = line.strip().split("|")
if user == line_list[0]:
new_str = line.replace(line_list[1],new_pwd)
f2.write(new_str)
continue
f2.write(line) #上面条件不成立,会先执行这里
shutil.move('db','db_bak') #备份
os.rename('db_new','db') #文件覆盖
#修改密码 def deleteUser(username):
with open('db','r+') as f1,open('db_new','w') as f2:
for line in f1:
line_list = line.strip().split("|")
if line_list[0] == username: #这里如果匹配,对这行文件不操作
pass
else:
f2.write(line) #把条件不匹配的行写到新文件中
shutil.move('db','db_bak')
os.rename('db_new','db')
#删除用户 def register(user,pwd,mail,phone):
with open('db','a',encoding='utf-8') as f:
temp = "\n" + user + "|" + pwd + "|" + mail + "|" + phone + "|" +""
f.write(temp)
res = ''.join(temp)
print("Register Info:",res)
#注册账号 def fetch(content):
with open('db','r') as f:
for line in f:
if content in line:
print(line.strip())
#模糊查询 def auth(username):
with open('db','r',encoding='utf-8') as f1,open('db_new','w',encoding='utf-8') as f2:
for line in f1:
line_list = line.strip().split("|")
if username == line_list[0]:
new_str = line.replace(line_list[-1],"")
f2.write(new_str)
continue
f2.write(line)
shutil.move('db','db_back')
os.rename('db_new','db')
#用户权限升级 @outer1 #先执行outer1装饰器,条件成立再调用manger_admin()函数;
def manger_admin():
num = input("1.查看所有信息 2.模糊查询 3.添加删除用户 4.密码管理 5.权限管理 6.退出")
if num == '':
with open('db','r',encoding='utf-8') as f:
res = f.read()
print(res)
elif num == '':
content = input("请输入要查找的关键字:")
fetch(content)
elif num == '':
r_num = input("1.register 2.DeleteUser 3.退出程序")
if r_num == '':
user = input("input register user:")
pwd = input("input register pwd:")
mail = input("input register pwd:")
phone = input("input register pwd:")
register(user,pwd,mail,phone)
elif r_num == '':
user = input("input Delete user:")
deleteUser(user)
print("\033[32m---------用户 %s 删除成功---------\033[0m" %user)
elif r_num == '':
sys.exit()
elif num == '':
old_user = input("input modify is username:")
new_pwd = input("input new password::")
changepwd(old_user,new_pwd)
print("modify %s user is password sucess,new password is :%s"%(old_user,new_pwd))
elif num == '':
user = input("请输入要升级为管理员权限的用户:")
auth(user)
elif num == '':
sys.exit() @outer #先执行outer函数,outer函数条件成立再执行manger_user()函数;
def manger_user():
r_num = input("1.管理员配置 2.我的配置 3.退出")
if r_num == '':
manger_admin()
elif r_num == '':
rr_num = input("1.查看我的信息 2.修改密码 3.退出")
if rr_num == '':
user = LOG_USER['user_info']
print(user)
elif rr_num == '':
old_user = input("input modify is username:")
new_pwd = input("input new password::")
changepwd(old_user,new_pwd)
elif rr_num == '':
sys.exit()
elif r_num == '':
sys.exit() def main():
print("welcome to user manger system".center(40,'*'))
while True:
num = input("1.登陆 2.注册 3.后台管理 4.退出")
if num == '':
user = input("username:")
pwd = input("password:")
login_status = login(user,pwd)
if login_status == True:
print('login sucess')
else:
print("login fail.")
elif num == '':
user = input("username:")
pwd = input("password:")
mail = input("mail:")
phone = input("phone:")
register(user,pwd,mail,phone)
elif num == '':
manger_user()
elif num == '':
sys.exit()
#入口文件 main()
账号密码文件:
admin|123123|faith_wmh@126.com|13141299640|2
xxx|223223|faith_wmh@226.com|242|2
wmh|123|faith@126.com|1221|2
nbbb|123123|faith@|123123|1
流程图:
写作业过程中,学到的内容:
【re.match:把不是1开头的行,查找出来】
import re
for line in open("dbfile"):
if not re.match("1",line):
print(line[:-1])
【删除内容为,PermissionError的字段】
lines = [l for l in open("file.txt", "r") if l.find("PermissionError") != 0]
fd = open("file.txt", "w")
fd.writelines(lines)
fd.close() 【输入行号,删除指定行】
import fileinput
num = input("input delete num:")
f = fileinput.input("dbfile",inplace=True)
for line in f:
if f.filelineno() == num:
print('')
else:
print(line,end="")