-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
节点数据模型设计及演进 #2
Comments
节点关注数据模型设计节点的关注和用户的关注一样也需要支持关注和屏蔽的操作。所以这里也设计一个多对多的关联。 也先定义一个关注类型枚举: class UserNodeStarState(IntEnum):
FOLLOWING = 1 # 关注
BLOCKING = 4 # 屏蔽
@property
def label(self):
return _uer_node_star_state_to_text[self]
_uer_node_star_state_to_text = {
UserNodeStarState.FOLLOWING: "关注",
UserNodeStarState.BLOCKING: "屏蔽"
} class UserNodeStar(BaseModel):
user = models.ForeignKey(WepostUser, on_delete=models.CASCADE, verbose_name="用户")
node = models.ForeignKey(Node, on_delete=models.CASCADE, verbose_name="节点")
order = models.IntegerField("排序", default=0, help_text="用于排序关注显示顺序")
state = models.PositiveSmallIntegerField("状态", choices=UserNodeStarState.choices(),
default=UserNodeStarState.FOLLOWING)
class Meta:
verbose_name = "关注节点"
verbose_name_plural = verbose_name
unique_together = ("user", "node") |
节点服务层设计每一个模型应该对应一个服务层,一个企业开发中比如 JAVA 中是有比如 Dao 层,Service层。 NodeManager 的功能分析NodeManager 提供的主要功能如下:
体现在代码上类似如下: class UserNodeStarManager(models.Manager):
def update_star_state(self, user: WepostUser, node: Node, star_state: UserNodeStarState):
"""更新关注状态"""
obj, created = self.update_or_create(user=user, node=node, state=star_state)
return obj, created
def star_node(self, user: WepostUser, node: Node):
"""关注节点"""
return self.update_star_state(user, node, UserNodeStarState.FOLLOWING)
def unstar_node(self, user: WepostUser, node: Node):
"""取消关注节点"""
return self.filter(user=user, node=node).delete()
def block_node(self, user: WepostUser, node: Node):
"""屏蔽节点"""
return self.update_star_state(user, node, UserNodeStarState.BLOCKING)
def unblock_node(self, user: WepostUser, node: Node):
"""取消屏蔽节点"""
return self.filter(user=user, node=node).delete() 在代码模块的组织结构上需要注意两个问题:
# Add all attributes to the class.
for obj_name, obj in attrs.items():
new_class.add_to_class(obj_name, obj) def add_to_class(cls, name, value):
if _has_contribute_to_class(value):
value.contribute_to_class(cls, name)
else:
setattr(cls, name, value) BaseManager 类实现 def contribute_to_class(self, model, name):
self.name = self.name or name
self.model = model
setattr(model, name, ManagerDescriptor(self))
model._meta.add_manager(self)
|
最终的服务模块设计上面对服务模块进行了一些思考。其实从上面的服务的方法的签名来看,其实这些可以算作是用户的服务。 我最终选择的一种方式是。创建一个 class UserNodeService:
"""用户与节点相关服务类"""
def __init__(self, user: WepostUser):
self.user = user
def _update_star_state(self, node: Node, star_state: UserNodeStarState):
"""更新关注状态"""
obj, created = UserNodeStar.objects.update_or_create(user=self.user, node=node, state=star_state)
return obj, created
def _delete_node(self, node: Node):
"""移除跟某一节点关系"""
return UserNodeStar.objects.filter(user=self.user, node=node).delete()
def star_node(self, node: Node):
"""关注节点"""
return self._update_star_state(node, UserNodeStarState.FOLLOWING)
def unstar_node(self, node: Node):
"""取消关注节点"""
return self._delete_node(node)
def block_node(self, node: Node):
"""屏蔽节点"""
return self._update_star_state(node, UserNodeStarState.BLOCKING)
def unblock_node(self, node: Node):
"""取消屏蔽节点"""
return self._delete_node(node) |
首先我们需要创建的数据模型就是节点数据模型。
这种有层次的数据模型我选择基于
django-mptt
来创建。比如数据库创建好之后,我想创建一些初始的数据,后面换数据库重新开始时,也不用再手动填充初始的节点数据 。
./manage_dev.py makemigrations wepost_posts --empty --name init_nodes
此时在
wepost_posts
下面会创建一个名为0002_init_nodes.py
的迁移脚本骨架如下:init_nodes
这个 Python 函数 。部分代码如下:写好之后,再继续执行迁移脚本
The text was updated successfully, but these errors were encountered: