maui 开发音乐播放APP 优化 用fastapi_amis_admin创建后台及接口(4)
2023-12-14 22:37:37
相关的models
from datetime import datetime
from typing import List, Optional
import sqlmodel
from fastapi_amis_admin.amis.components import ColumnImage, InputImage, InputRichText
from fastapi_amis_admin.models.enums import IntegerChoices
from fastapi_amis_admin.models.fields import Field
from fastapi_user_auth.auth.models import PkMixin, User
from sqlalchemy import Column, String, select
from sqlalchemy.orm import Session
class ArticleStatus(IntegerChoices):
unpublished = 0, "未发布"
published = 1, "已发布"
inspection = 2, "审核中"
disabled = 3, "已禁用"
class Category(PkMixin, table=True):
__tablename__ = "category"
name: str = Field(title="歌曲分类名", sa_column=Column(String(100), unique=True, index=True, nullable=False))
description: str = Field(default="", title="描述", amis_form_item="textarea")
status: bool = Field(False, title="状态")
articles: List["Article"] = sqlmodel.Relationship(back_populates="category")
class ArticleTagLink(sqlmodel.SQLModel, table=True):
__tablename__ = "music_tags"
tag_id: Optional[int] = Field(default=None, foreign_key="tag.id", primary_key=True)
music_id: Optional[int] = Field(default=None, foreign_key="article.id", primary_key=True)
class Tag(PkMixin, table=True):
__tablename__ = "tag"
name: str = Field(..., title="标签名", sa_column=Column(String(255), unique=True, index=True, nullable=False))
articles: List["Article"] = sqlmodel.Relationship(back_populates="tags", link_model=ArticleTagLink)
class Article(PkMixin, table=True):
__tablename__ = "article"
title: str = Field(title="歌曲名", max_length=200)
img: str = Field(
None,
title="图片",
max_length=300,
amis_form_item=InputImage(maxLength=1, maxSize=2 * 1024 * 1024, receiver="post:/admin/file/upload"),
amis_table_column=ColumnImage(width=100, height=60, enlargeAble=True),
)
url: str = Field(default="", title="歌曲URL", max_length=200)
status: ArticleStatus = Field(ArticleStatus.unpublished, title="status")
content: str = Field(..., title="说明", amis_form_item=InputRichText())
create_time: Optional[datetime] = Field(default_factory=datetime.utcnow, title="增加时间")
source: str = Field(default="", title="来源", max_length=200)
category_id: Optional[int] = Field(default=None, foreign_key="category.id", title="分类")
category: Optional[Category] = sqlmodel.Relationship(back_populates="articles")
tags: List[Tag] = sqlmodel.Relationship(back_populates="articles", link_model=ArticleTagLink)
user_id: int = Field(default=None, foreign_key="auth_user.id", title="用户")
user: User = sqlmodel.Relationship()
@staticmethod
def check_update_permission(session: Session, user: User, item_id: List[str]):
# 管理员可以修改全部文章, 并且可以批量修改.
if user.has_requires(session, roles=["admin"]):
return True
# 非管理员,只能修改自己的文章,并且不可批量修改.
if len(item_id) > 1:
return False
stmt = select(1).where(Article.id == item_id[0], Article.user_id == user.id)
return bool(session.scalar(stmt))
对上面代码进行优化。看下一章
文章来源:https://blog.csdn.net/hzether/article/details/135004015
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!