问题描述
我有以下课程/型号:
class Recipe(db.Model):
...
user_id = db.Column(db.ForeignKey(("users.id")),nullable=False,index=True)
author = db.relationship("User",uselist=False,back_populates="diets")
...
# Permissions
def can_view(self,user = None) -> bool:
if user is None:
user = current_user
return self.author == user
我可以使用recipe.can_view()
或recipe.can_view(some_user)
,但我希望能够呼叫recipe.can_view
而不是recipe.can_view()
,但我不知道有什么好的解决方案那个。
谢谢
解决方法
看起来我无法做到这一点(至少没有弄乱)。
因此,我接受了@zvone在评论中提出的解决方案:
我会选择
can_view(user)
和can_current_user_view
所以,我的代码是:
def can_view(self,user) -> bool:
return self.is_author(user) or user.is_admin or self.is_public
@property
def can_current_user_view(self) -> bool:
return self.can_view(user=current_user)