python项目练习——25、在线菜谱分享网站

本文描述了一个用户管理系统,包括用户注册、登录、资料管理,以及一个功能丰富的菜谱管理模块,涵盖搜索、详情、分享、评论、评分、收藏、点赞、分类和标签等功能,还有用户互动模块如私信和关注,以及管理员后台管理和系统设置,强调了安全性和身份验证措施。
摘要由CSDN通过智能技术生成
  1. 用户管理模块

    • 用户注册:允许用户创建账户。
    • 用户登录:已注册用户可以登录到他们的账户。
    • 用户资料管理:允许用户查看和编辑其个人资料。
  2. 菜谱管理模块

    • 菜谱列表:展示所有用户分享的菜谱。
    • 菜谱搜索:允许用户根据关键字、菜系、食材等条件搜索菜谱。
    • 菜谱详情:提供每个菜谱的详细信息、制作步骤和图片。
    • 菜谱分享:允许用户分享自己的菜谱并添加制作步骤和图片。
  3. 评论和评分模块

    • 评论提交:允许用户在尝试过菜谱后提交评论和评分。
    • 评论展示:在菜谱详情页面显示其他用户的评论和评分。
  4. 收藏和点赞模块

    • 菜谱收藏:允许用户收藏自己喜欢的菜谱。
    • 菜谱点赞:用户可以给菜谱点赞以表达喜爱之情。
  5. 分类和标签模块

    • 菜谱分类:按照菜系、食材、难度等对菜谱进行分类。
    • 菜谱标签:为菜谱添加标签以便用户更快地找到感兴趣的菜谱。
  6. 用户互动模块

    • 私信功能:允许用户之间发送私信进行交流和沟通。
    • 关注功能:用户可以关注其他用户以及其他用户的菜谱。
  7. 管理员后台管理模块

    • 菜谱管理:管理员可以审核和管理用户分享的菜谱。
    • 用户管理:管理员可以管理用户账户和权限。
  8. 系统设置模块

    • 网站设置:包括网站名称、logo、联系信息等。
    • 邮件通知设置:配置系统向用户发送的邮件通知。
  9. 安全和身份验证

    • 用户身份验证:使用用户名和密码进行身份验证。
    • SSL 加密:使用 SSL 加密保护用户的个人信息。

1.用户管理模块

class User:
    def __init__(self, username, password, email):
        self.username = username  # 用户名
        self.password = password  # 密码
        self.email = email  # 邮箱

class UserManager:
    def __init__(self):
        self.users = []  # 用户列表

    def register_user(self, username, password, email):
        """
        用户注册函数
        """
        new_user = User(username, password, email)  # 创建新用户对象
        self.users.append(new_user)  # 将新用户添加到用户列表中

    def login_user(self, username, password):
        """
        用户登录函数
        """
        for user in self.users:
            if user.username == username and user.password == password:
                return True  # 登录成功
        return False  # 登录失败

    def edit_user_profile(self, username, new_password=None, new_email=None):
        """
        编辑用户资料函数
        """
        for user in self.users:
            if user.username == username:
                if new_password:
                    user.password = new_password  # 更新密码
                if new_email:
                    user.email = new_email  # 更新邮箱
 

2.菜谱管理模块

class Recipe:
    def __init__(self, name, cuisine, ingredients, instructions):
        self.name = name  # 菜谱名称
        self.cuisine = cuisine  # 菜系
        self.ingredients = ingredients  # 食材列表
        self.instructions = instructions  # 制作步骤

class RecipeManager:
    def __init__(self):
        self.recipes = []  # 菜谱列表

    def add_recipe(self, name, cuisine, ingredients, instructions):
        """
        添加菜谱函数
        """
        new_recipe = Recipe(name, cuisine, ingredients, instructions)  # 创建新菜谱对象
        self.recipes.append(new_recipe)  # 将新菜谱添加到菜谱列表中

    def search_recipe(self, keyword):
        """
        搜索菜谱函数
        """
        results = []
        for recipe in self.recipes:
            if keyword.lower() in recipe.name.lower():
                results.append(recipe)  # 将匹配的菜谱添加到结果列表中
        return results

    def get_recipe_details(self, recipe_name):
        """
        获取菜谱详情函数
        """
        for recipe in self.recipes:
            if recipe.name == recipe_name:
                return recipe  # 返回匹配的菜谱对象

    def share_recipe(self, name, cuisine, ingredients, instructions):
        """
        分享菜谱函数
        """
        self.add_recipe(name, cuisine, ingredients, instructions)  # 调用添加菜谱函数来分享菜谱
 

3.评论和评分模块

class Review:
    def __init__(self, user, rating, comment):
        self.user = user  # 评论用户
        self.rating = rating  # 评分
        self.comment = comment  # 评论内容

class ReviewManager:
    def __init__(self):
        self.reviews = []  # 评论列表

    def add_review(self, user, rating, comment):
        """
        添加评论函数
        """
        new_review = Review(user, rating, comment)  # 创建新评论对象
        self.reviews.append(new_review)  # 将新评论添加到评论列表中

    def get_reviews_for_recipe(self, recipe_name):
        """
        获取菜谱评论函数
        """
        reviews_for_recipe = []
        for review in self.reviews:
            if review.recipe == recipe_name:
                reviews_for_recipe.append(review)  # 将与菜谱匹配的评论添加到结果列表中
        return reviews_for_recipe
 

4.收藏和点赞模块

class User:
    def __init__(self, username):
        self.username = username  # 用户名
        self.favorite_recipes = []  # 收藏的菜谱列表
        self.liked_recipes = []  # 点赞的菜谱列表

    def add_favorite_recipe(self, recipe):
        """
        添加收藏菜谱函数
        """
        if recipe not in self.favorite_recipes:
            self.favorite_recipes.append(recipe)  # 将菜谱添加到收藏列表中

    def like_recipe(self, recipe):
        """
        点赞菜谱函数
        """
        if recipe not in self.liked_recipes:
            self.liked_recipes.append(recipe)  # 将菜谱添加到点赞列表中

class Recipe:
    def __init__(self, name):
        self.name = name  # 菜谱名称

# 创建用户对象
user1 = User("Alice")
user2 = User("Bob")

# 创建菜谱对象
recipe1 = Recipe("Spaghetti Carbonara")
recipe2 = Recipe("Chicken Alfredo")

# 用户收藏菜谱和点赞菜谱
user1.add_favorite_recipe(recipe1)
user1.like_recipe(recipe1)
user2.add_favorite_recipe(recipe2)
user2.like_recipe(recipe2)
 

5.分类和标签模块

class Recipe:
    def __init__(self, name, cuisine, ingredients, difficulty):
        self.name = name  # 菜谱名称
        self.cuisine = cuisine  # 菜系
        self.ingredients = ingredients  # 食材列表
        self.difficulty = difficulty  # 难度级别

class RecipeManager:
    def __init__(self):
        self.recipes = []  # 菜谱列表

    def add_recipe(self, recipe):
        """
        添加菜谱函数
        """
        self.recipes.append(recipe)  # 将菜谱添加到菜谱列表中

    def search_by_cuisine(self, cuisine):
        """
        按菜系搜索菜谱函数
        """
        matching_recipes = []
        for recipe in self.recipes:
            if recipe.cuisine == cuisine:
                matching_recipes.append(recipe)  # 将与指定菜系匹配的菜谱添加到结果列表中
        return matching_recipes

    def search_by_ingredient(self, ingredient):
        """
        按食材搜索菜谱函数
        """
        matching_recipes = []
        for recipe in self.recipes:
            if ingredient in recipe.ingredients:
                matching_recipes.append(recipe)  # 将包含指定食材的菜谱添加到结果列表中
        return matching_recipes

# 创建菜谱管理器对象
manager = RecipeManager()

# 添加菜谱
recipe1 = Recipe("Spaghetti Carbonara", "Italian", ["spaghetti", "bacon", "eggs", "cheese"], "Intermediate")
recipe2 = Recipe("Chicken Tikka Masala", "Indian", ["chicken", "tomatoes", "spices", "cream"], "Advanced")
manager.add_recipe(recipe1)
manager.add_recipe(recipe2)

# 按菜系搜索菜谱
italian_recipes = manager.search_by_cuisine("Italian")
print("Italian Recipes:", [recipe.name for recipe in italian_recipes])

# 按食材搜索菜谱
ingredient_recipes = manager.search_by_ingredient("chicken")
print("Recipes with Chicken:", [recipe.name for recipe in ingredient_recipes])
 

6.用户互动模块

class User:
    def __init__(self, username):
        self.username = username  # 用户名
        self.following = []  # 关注的用户列表
        self.messages = []  # 收到的私信列表

    def send_message(self, recipient, message):
        """
        发送私信函数
        """
        recipient.messages.append((self.username, message))  # 将发送的私信添加到接收者的私信列表中

    def follow_user(self, other_user):
        """
        关注用户函数
        """
        self.following.append(other_user)  # 将要关注的用户添加到关注列表中


# 创建用户对象
user1 = User("Alice")
user2 = User("Bob")

# 用户1发送私信给用户2
user1.send_message(user2, "Hello Bob, I like your recipes!")

# 用户1关注用户2
user1.follow_user(user2)

# 打印用户2收到的私信列表和用户1关注的用户列表
print(f"Messages for {user2.username}: {user2.messages}")
print(f"{user1.username} is following: {[user.username for user in user1.following]}")
 

7.管理员后台管理模块

class Admin:
    def __init__(self, username):
        self.username = username  # 管理员用户名

    def manage_recipes(self, recipes):
        """
        管理菜谱函数
        """
        # 管理员可以审核和删除菜谱
        for recipe in recipes:
            if recipe.needs_approval:  # 如果菜谱需要审核
                recipe.approve()  # 审核通过
            if recipe.needs_deletion:  # 如果菜谱需要删除
                recipes.remove(recipe)  # 删除菜谱

    def manage_users(self, users):
        """
        管理用户函数
        """
        # 管理员可以查看和禁用用户
        for user in users:
            if user.needs_disabled:  # 如果用户需要禁用
                user.disable()  # 禁用用户


class Recipe:
    def __init__(self, name, needs_approval=False, needs_deletion=False):
        self.name = name  # 菜谱名称
        self.needs_approval = needs_approval  # 是否需要审核
        self.needs_deletion = needs_deletion  # 是否需要删除

    def approve(self):
        """
        审核通过函数
        """
        self.needs_approval = False  # 将菜谱的审核状态设置为通过

    def delete(self):
        """
        删除菜谱函数
        """
        self.needs_deletion = True  # 将菜谱的删除状态设置为需要删除


class User:
    def __init__(self, username, is_disabled=False):
        self.username = username  # 用户名
        self.is_disabled = is_disabled  # 是否被禁用

    def disable(self):
        """
        禁用用户函数
        """
        self.is_disabled = True  # 将用户状态设置为禁用


# 创建管理员对象
admin = Admin("Admin")

# 创建菜谱对象列表
recipes = [Recipe("Spaghetti", needs_approval=True), Recipe("Salad")]

# 创建用户对象列表
users = [User("Alice"), User("Bob", is_disabled=True)]

# 管理员管理菜谱和用户
admin.manage_recipes(recipes)
admin.manage_users(users)

# 打印最终菜谱和用户列表
print("Final recipes:")
for recipe in recipes:
    print(recipe.name)
print("Final users:")
for user in users:
    print(user.username, "(Disabled)" if user.is_disabled else "")
 

8.系统设置模块

class SystemSettings:
    def __init__(self, site_name, logo, contact_info):
        self.site_name = site_name  # 网站名称
        self.logo = logo  # 网站logo
        self.contact_info = contact_info  # 联系信息

    def update_site_name(self, new_name):
        """
        更新网站名称函数
        """
        self.site_name = new_name  # 更新网站名称

    def update_logo(self, new_logo):
        """
        更新网站logo函数
        """
        self.logo = new_logo  # 更新网站logo

    def update_contact_info(self, new_contact_info):
        """
        更新联系信息函数
        """
        self.contact_info = new_contact_info  # 更新联系信息


# 创建系统设置对象
settings = SystemSettings("MyRecipeSite", "logo.png", "contact@example.com")

# 打印当前系统设置
print("Current site name:", settings.site_name)
print("Current logo:", settings.logo)
print("Current contact info:", settings.contact_info)

# 更新系统设置
settings.update_site_name("Delicious Recipes")
settings.update_logo("new_logo.png")
settings.update_contact_info("new_contact@example.com")

# 打印更新后的系统设置
print("Updated site name:", settings.site_name)
print("Updated logo:", settings.logo)
print("Updated contact info:", settings.contact_info)
 

9.安全和身份验证

class User:
    def __init__(self, username, password):
        self.username = username  # 用户名
        self.password = password  # 密码

    def authenticate(self, entered_password):
        """
        用户身份验证函数
        """
        return self.password == entered_password  # 检查输入的密码是否与用户密码匹配


# 创建用户对象
user1 = User("user123", "password123")

# 用户登录
entered_password = input("Enter your password: ")
if user1.authenticate(entered_password):
    print("Authentication successful!")
else:
    print("Authentication failed. Please try again.")
 

  • 36
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

F——

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值