Python——個人部落格專案開發

擎天小豬♥發表於2019-01-26
'''
    個人部落格專案開發
'''

import os, sys, time, shelve


def show_login():
    '''顯示登入選單'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t個人部落格專案登入介面\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t\t1. 使用者登入\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t\t2. 使用者註冊\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t\t3. 退出系統\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("請輸入選項:")

    return login_menu_dict.get(choice)() if choice in login_menu_dict else show_login()


def login():
    '''登入函式'''
    username = input("請輸入賬號(R/r返回): ")
    if username.upper() == 'R':
        return show_login()
    password = input("請輸入密碼: ")
    if username in users and users[username]['password'] == password:
        global online_user
        online_user = username
        return show_index()
    else:
        print("賬號或密碼有誤,請重新輸入!")
        time.sleep(1)
        return login()


def show_regist():
    '''顯示註冊選單'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t個人部落格專案註冊介面\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *請按提示資訊,錄入個人資料,完成註冊* ~ * ~ *")
    print("-------------------------------------------------")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    time.sleep(1)
    return regist()


def regist():
    '''註冊函式'''
    username = input("請輸入註冊賬號(R/r返回): ")
    if username.upper() == 'R':
        return show_login()
    if username in users:
        print("使用者賬號已存在,請使用其他賬號註冊")
        time.sleep(1)
        return regist()
    password = input("請輸入密碼: ")
    confirm = input("請確認密碼: ")
    if password != confirm:
        print("兩次輸入密碼不一致,請重新註冊")
        time.sleep(1)
        return regist()
    email = input("請輸入(qq)郵箱: ")
    if not email.endswith("@qq.com"):
        print("郵箱格式有誤,請重新註冊")
        time.sleep(1)
        return regist()

    user = {'username' : username,'password' : password,'email' : email,'nickname' : 'None'}
    users[username] = user
    save_data()
    return show_login()

def show_index():
    '''展示主頁選單'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t個人部落格專案主介面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 個人資訊維護   \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 文章資訊維護   \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 登出登入    \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 退出系統    \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("請輸入選項:")

    return index_menu_dict.get(choice)() if choice in index_menu_dict else show_index()


def userinfo_perfect():
    '''使用者資訊維護'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 使用者資訊維護介面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 查詢個人資訊 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 修改登入密碼 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 完善個人資訊 \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 返回上一級\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("請輸入選項:")

    return userinfo_menu_dict.get(choice)() if choice in userinfo_menu_dict else userinfo_perfect()


def userinfo_look():
    '''檢視個人資訊'''
    global online_user
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  個人資訊介面  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("賬號: ",users.get(online_user).get('username'))
    print("郵箱: ", users.get(online_user).get('email'))
    print("暱稱: ", users.get(online_user).get('nickname'))
    for k,v in users.get(online_user).items():
        if k == 'usersex':
            print('性別: ',v)
        if k == 'userage':
            print('年齡: ',v)
        if k == 'userphone':
            print('手機: ',v)
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    input("任意鍵繼續...")
    return userinfo_perfect()


def loginpass_modify():
    '''修改登入密碼'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  修改登入密碼  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    oldpass = input("請輸入原密碼(R/r鍵返回): ")
    if oldpass.upper() == 'R':
        return userinfo_perfect()
    if oldpass != users[online_user]['password']:
        print("密碼有誤")
        time.sleep(1)
        return loginpass_modify()
    newpass = input("請輸入新密碼: ")
    confirm = input("請確認新密碼: ")
    if newpass != confirm:
        print("兩次輸入密碼不一致,請重新輸入")
        time.sleep(1)
        return loginpass_modify()
    users[online_user]["password"] = newpass
    print("密碼修改成功,請重新登入...")
    time.sleep(1)
    return show_login()


def userinfo_update():
    '''完善個人資料'''
    global online_user
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  完善個人資料  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    if not len(users[online_user]['nickname']):
        print("賬號: ", users.get(online_user).get('username'))
        print("郵箱: ", users.get(online_user).get('email'))
        print("暱稱: 待完善..")
        print("性別: 待完善..")
        print("年齡: 待完善..")
        print("手機: 待完善..")
    else:
        print("賬號: ", users.get(online_user).get('username'))
        print("郵箱: ", users.get(online_user).get('email'))
        print("暱稱: ", users.get(online_user).get('nickname'))
        for k, v in users.get(online_user).items():
            if k == 'usersex':
                print('性別: ', v)
            if k == 'userage':
                print('年齡: ', v)
            if k == 'userphone':
                print('手機: ', v)
    print("請根據提示資訊完成對應填寫:")
    nickname = input("輸入你的暱稱: ")
    usersex = input("輸入你的性別: ")
    userage = input("輸入你的年齡: ")
    userphone = input("輸入你的手機號: ")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    is_save = input("是否儲存(y/n)?")
    if is_save != 'y':
        input("資訊未發生變化,任意鍵退出...")
        return userinfo_perfect()

    user = {'username': online_user, 'password': users.get(online_user).get('password'), 'email': users.get(online_user).get('email'), 'nickname': nickname, 'usersex': usersex, 'userage': userage, 'userphone': userphone}
    users[online_user] = user
    save_data()
    input("資訊修改成功,任意鍵繼續...")
    return userinfo_perfect()


def articalinfo_perfect():
    '''文章資訊維護'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 文章資訊維護介面 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t1. 發表文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t2. 刪除文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t3. 修改文章\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t4. 查詢個人文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t5. 查詢所有文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t6. 發表評論\t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t7. 回收站  \t\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t8. 返回上一級\t\t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")

    choice = input("請輸入選項:")

    return articalinfo_menu_dict.get(choice)() if choice in articalinfo_menu_dict else articalinfo_perfect()


def artical_publish():
    '''發表文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  發表部落格文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    title = input("請輸入文章標題(R/r鍵返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    if title in articals:
        print("標題已存在,請用其他標題")
        time.sleep(1)
        return artical_publish()
    content = ''
    for i in range(0,50):
        one_line = input("請輸入文章內容(R/r結束):")
        if one_line.upper() == 'R':
            break
        else:
            content += one_line
    print(content)
    print("文章發表中...")
    artical = {'title' : title,'content' : content,'author' : online_user,'readed_count' : 0,'statue' : True,'comment' : comments}
    articals[title] = artical
    save_data()
    time.sleep(1)
    input("文章發表成功!任意鍵返回...")
    return articalinfo_perfect()


def artical_delete():
    '''刪除文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  刪除部落格文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")

    title = input("請輸入您要刪除的部落格文章標題(R/r鍵返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            articals[title]['statue'] = False
            input("文章刪除成功,任意鍵返回...")
        else:
            input("未找到您要刪除的文章,任意鍵繼續...")
        return articalinfo_perfect()


def artical_modify():
    '''修改文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  修改部落格文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")

    title = input("請輸入您要修改的部落格文章標題(R/r鍵返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            print(articals.get(title).get('content'))
            content = ''
            for i in range(0, 50):
                one_line = input("請修改文章內容(R/r結束):")
                if one_line.upper() == 'R':
                    break
                else:
                    content += one_line

            print("文章修改中...")
            articals[title]['content'] = content
            time.sleep(1)
            input("文章修改成功,任意鍵返回...")
        else:
            input("未找到您要修改的文章,任意鍵繼續...")
        return articalinfo_perfect()


def artical_find_self():
    '''查詢自己所有文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 檢視個人部落格文章 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    artical_loop(online_user)
    print("---------------------------------------------")
    title = input("請輸入您要檢視的部落格文章標題(R/r鍵返回): ")
    if title.upper() == 'R':
        return articalinfo_perfect()
    else:
        if title in articals and articals.get(title).get('author') == online_user:
            return artical_detail(title)
        else:
            print("您尚未發表此篇文章")
            time.sleep(1)
            return articalinfo_perfect()


def artical_find_all():
    '''查詢所有文章'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 檢視所有部落格文章 \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >標題\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue'):
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("請輸入您要檢視的部落格文章標題(R/r鍵返回): ")
    if title.upper() != 'R':
        if title not in articals:
            print("尚未發表此篇文章")
            time.sleep(1)
        else:
            return artical_detail(title)
    return articalinfo_perfect()


def comment_publish():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  評論部落格文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >標題\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue'):
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("請輸入您要評論的部落格文章標題(R/r鍵返回): ")
    if title.upper() != 'R':
        if title in articals and articals.get(title).get('statue'):
            if articals.get(title).get('author') == online_user:
                print("不能評論自己的文章→→!")
                time.sleep(1)
                return comment_publish()
            else:
                print(articals.get(title).get('content'))
                print("---------------------------------------------")
                comment = input("請輸入評論的內容: ")
                print("評論上傳中...")
                if online_user in articals[title]['comment']:
                    # articals[title]['comment'][online_user].append(comment)
                    if title in articals[title]['comment'][online_user]:
                        articals[title]['comment'][online_user][title].append(comment)
                    else:
                        articals[title]['comment'][online_user][title] = [comment]
                else:
                    # articals[title]['comment'][online_user] = [comment]
                    articals[title]['comment'][online_user] = {title : [comment]}
                    print(articals[title]['comment'][online_user])
                time.sleep(1)
                input("評論發表成功,任意鍵返回...")
                # return articalinfo_perfect()
        else:
            print("此篇文章不存在,不能釋出評論。")
            time.sleep(1)
    return articalinfo_perfect()


def artical_detail(title):
    '''文章詳情'''
    artical = articals.get(title)
    rc = artical.get("readed_count")
    rc += 1
    artical['readed_count'] = rc
    print("文章標題: {}".format(artical.get('title')))
    print("文章作者: {}".format(artical.get('author')))
    print("文章內容: {}".format(artical.get('content')))
    print("閱讀次數: {}".format(artical.get('readed_count')))
    if len(articals.get(title).get('comment')):
        # for c in articals.get(title).get('comment'):
        for u in articals.get(title).get('comment'):
            if title in articals.get(title).get('comment').get(u):
                print(u, articals.get(title).get('comment').get(u).get(title))
    input("任意鍵返回...")
    return articalinfo_perfect()


def artical_loop(online):
    '''遍歷文章'''
    print("* ~ * ~ >標題\t\t作者")
    for k in articals.keys():
        if articals.get(k).get('statue') and articals.get(k).get('author') == online:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))


def recover_menu():
    '''回收站'''
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t    回收站     \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t  1. 恢復刪除的文章\t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t  2. 清空回收站  \t\t* ~ * ~ *")
    print("* ~ * ~ *\t\t  3. 返回上一級  \t\t* ~ * ~ *")

    choice = input("請輸入您的選項:")

    return recover_menu_dict.get(choice)() if choice in recover_menu_dict else recover_menu()


def artical_recover():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t 恢復刪除的文章  \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >標題\t\t作者")
    for k in articals.keys():
        if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    title = input("請輸入您要恢復的部落格文章標題(R/r鍵返回): ")
    if title.upper() != 'R':
        if title in articals and articals.get(title).get('author') == online_user:
            articals[title]['statue'] = True
            input("恢復成功,任意鍵返回...")
        else:
            print("未找到你要恢復的文章")
            time.sleep(1)
    return articalinfo_perfect()


def delete_recover():
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ *\t\t   清空回收站    \t\t* ~ * ~ *")
    print("* ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ * ~ *")
    print("* ~ * ~ >標題\t\t作者")
    for k in articals.keys():
        if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
            print("* ~ * ~ >{}\t\t{}".format(k, articals.get(k).get('author')))
    print("---------------------------------------------")
    is_delete = input("是否清空回收站(y/n): ")
    if is_delete != 'y':
        return recover_menu()
    else:
        for k in articals.keys():
            if not articals.get(k).get('statue') and articals.get(k).get('author') == online_user:
                articals.pop(k)
                input("回收站已清空,任意鍵返回...")
                return recover_menu()


def save_data():
    '''儲存資料到檔案'''

    if not os.path.exists('./data'):
        os.mkdir('./data')
    file = shelve.open('./data/data')
    file['users'] = users
    file['articals'] = articals


def get_data():
    '''從檔案提取資料到程式'''
    global users, articals
    if os.path.isdir('./data') and os.path.isfile('./data/data.dat'):
        file = shelve.open('./data/data')
        users = file['users']
        articals = file['articals']


def exit_system():
    '''退出系統'''
    for i in range(1,4):
        print("系統{}s後退出".format(4 - i))
        time.sleep(1)
    save_data()
    sys.exit(1)


users = dict()

articals = dict()

comments = dict()

online_user = None
#登入選單選項函式對應關係
login_menu_dict = {
    '1' : login,
    '2' : show_regist,
    '3' : exit_system
}
#首頁選單選項函式對應關係
index_menu_dict = {
    '1' : userinfo_perfect,
    '2' : articalinfo_perfect,
    '3' : show_login,
    '4' : exit_system
}
#使用者資訊維護選單選項函式對應關係
userinfo_menu_dict = {
    '1' : userinfo_look,
    '2' : loginpass_modify,
    '3' : userinfo_update,
    '4' : show_index
}
#文章資訊維護選單選項函式對應關係
articalinfo_menu_dict = {
    '1' : artical_publish,
    '2' : artical_delete,
    '3' : artical_modify,
    '4' : artical_find_self,
    '5' : artical_find_all,
    '6' : comment_publish,
    '7' : recover_menu,
    '8' : show_index
}
#回收站選單選項函式對應關係
recover_menu_dict = {
    '1' : artical_recover,
    '2' : delete_recover,
    '3' : articalinfo_perfect,
}


def engine():
    '''初始化資料'''
    get_data()
    '''初始化引擎: 展示登入選單'''
    show_login()


engine()

測試:


     

     

     

   

   

   

   

   

    

  

相關文章