import os
import json
import sys
from datetime import datetime
1.账号注册,登陆验证
2.写文章,包含【标题,作者,时间,内容】
3.查询文章,列出所有文章及时间
4.可查看其中一篇文章
5.删除文章
6.修改文章
7.退出系统
1.账号注册,登陆验证
>>>>>细分小功能
判断路径
def isPath(path):
return os.path.exists(path)
输如输出流
def StreamDumpLoad(path,rOw,obj=None):
if isPath(path):
tempObj=None
with open(path,rOw) as f:
if rOw==”w”:
json.dump(obj, f)
elif rOw==”r”:
tempObj= json.load(f)
return tempObj
创建默认的文章表 ,【Article/文章表.json】
def C_D_Article(userName,path=”Article”):
if not isPath(path):
os.mkdir(path)
if isPath(path):
tempPath = os.path.join(path, userName + “.json”)
# print(tempPath)
tempObj = {“Article”: [{“title”: “DefaultArticle”,”author”: userName,
“time”: datetime.strftime(datetime.now(), “%Y-%m-%d %H:%M:%S”),
“content”: “DefaultContent”,}]}
with open(tempPath,”w”) as f_w:
json.dump(tempObj,f_w)
# print(“写入成功”)
1>>>>>默认的用户配置表 {“user”:[{“name”:”admin”,”pwd”:”123456”}]}
def C_D_User(path=”user.json”):
# 文件不存在创建
defaultUser={“user”:[{“name”:”admin”,”pwd”:”123456”}]}
if not isPath(path):
with open(path,”w”) as f_w:
json.dump(defaultUser,f_w)
C_D_Article(“admin”)
2>>>>>读取用户表 返回 用户字典
def ReadAllUser(path=”user.json”):
return StreamDumpLoad(path,”r”)
3>>>>>添加用户
def AddUser(name,pwd,path=”user.json”):
#本次的用户
tempUser={“name”:name,”pwd”:pwd}
#读取所有的原来用户字典
tempAllUser=ReadAllUser(path)
#读取用户列表
userLIst=tempAllUser[“user”]
#添加本次用户
userLIst.append(tempUser)
#写入配置表
StreamDumpLoad(path,”w”,tempAllUser)
4>>>>>账号的注册 ,不重复才能注册
def Register(path=”user.json”):
if isPath(path):
userName=input(“请输入用户名?”)
userPwd=input(“请输入密码?”)
if userName!=”” and userPwd!=”“:
#读取所有用户列表
userDict=ReadAllUser(path)
#假定该用户不在
isFind=False
for i in userDict[“user”]:
if i[“name”]==userName:
isFind=True
print(“用户已存在,注册失败”)
break
if isFind==False:
userDict[“user”].append({“name”:userName,”pwd”:userPwd})
StreamDumpLoad(path,”w”,userDict)
print(“注册成功”)
C_D_Article(userName)
else:
print(“账号密码不能为空,注册失败”)
5>>>>>登录功能
def Login(path=”user.json”):
if isPath(path):
userName = input(“请输入用户名?”)
userPwd = input(“请输入密码?”)
if userName != “” and userPwd != “”:
# 读取所有用户列表
userDict = ReadAllUser(path)
# 假定该用户不在
isFind = False
for i in userDict[“user”]:
if i[“name”] == userName and i[“pwd”]==userPwd:
print(“登陆成功”)
global globalUser
globalUser=userName
isFind=True
break
if isFind==False:
print(“登录失败”)
return isFind
2.写文章,包含【标题,作者,时间,内容】
>>>>>细分小功能
1>>>>>>读取所有文章
def ReadAllArticle(userName,path=”Article”):
tempPath=os.path.join(path,userName+”.json”)
return StreamDumpLoad(tempPath,”r”)
2>>>>>>添加文章
def AddArticle(userName,path=”Article”):
title=input(“请输入文章标题?\n>>>>>:”)
content=input(“请输入文章内容?\n>>”)
#当前文章的结构
tempArticle={“title”: title,”author”: userName,”time”: datetime.strftime(datetime.now(),”%Y-%m-%d %H:%M:%S”),
“content”: content,}
#读取所有文章
allArticle=ReadAllArticle(userName)
# 添加当前的文章
allArticle[“Article”].append(tempArticle)
#临时路径
tempPath=os.path.join(path,userName+”.json”)
#写入文章
StreamDumpLoad(tempPath,”w”,allArticle)
print(“文章发表成功”)
3.查询文章,列出所有文章及时间
def SearchAllArticle(userName,path=”Article”):
allDict=ReadAllArticle(userName)
print(“以下为所有文章”)
for i in allDict[“Article”]:
if len(i[“title”])>5:
print(“>>>>”,”标题:”, i[“title”][0:5]+”…”, “\t时间:”, i[“time”])
else:
print(“>>>>”,”标题:”, i[“title”], “\t时间:”, i[“time”])
4.可查看其中一篇文章
def FindOneArticle(userName,path=”Article”):
tempTitle=input(“请输入 要【查找】的文章标题?>>>”)
allArticle=ReadAllArticle(userName,path)
for i in allArticle["Article"]:
if i["title"].count(tempTitle):
print("查询到该文章")
print(">>>>","标题:",i["title"])
print(">>>>","作者:", i["author"])
print(">>>>","时间:", i["time"])
print(">>>>","内容:", i["content"])
5.删除文章
def DeleteArticle(userName,path=”Article”):
tempTitle = input(“请输入 要【删除】 的文章标题?>>>”)
allArticle = ReadAllArticle(userName, path)
for i in range(len(allArticle["Article"])):
if allArticle["Article"][i]["title"].count(tempTitle):
del allArticle["Article"][i]
print("删除成功")
break
# 临时路径
tempPath = os.path.join(path, userName + ".json")
StreamDumpLoad(tempPath,"w",allArticle)
6.修改文章
def EditArticle(userName,path=”Article”):
tempTitle = input(“请输入 要【修改】 的文章标题?>>>”)
#获得所有文章
allArticle = ReadAllArticle(userName, path)
for i in range(len(allArticle["Article"])):
if allArticle["Article"][i]["title"].count(tempTitle):
tempContent = input("请输入 要【修改】 的文章【内容】?>>>")
allArticle["Article"][i]["content"]=tempContent
print("修改成功")
break
# 临时路径
tempPath = os.path.join(path, userName + ".json")
StreamDumpLoad(tempPath, "w", allArticle)
7.退出系统
*****************************************
globalUser=””
def Main():
global globalUser
print(“欢迎来到 XXX博客系统”)
path=”user.json”
C_D_User(path) # 默认用户配置表
while True:
step1=int(input("1:注册\t2:登录\t3:退出"))
if step1==1:
Register(path)
elif step1==2:
if Login(path):
while True:
step2 = int(input("1:写文章\t2:查看所有文章\t3:查看一篇\t4:修改文章\t5:删除文章\t6:退出登录"))
if step2 == 1:
AddArticle(globalUser)
elif step2 == 2:
SearchAllArticle(globalUser)
elif step2 == 3:
FindOneArticle(globalUser)
elif step2 == 4:
EditArticle(globalUser)
elif step2 == 5:
DeleteArticle(globalUser)
else:
globalUser = ""
break
else:
sys.exit()
Main()