Skip to main content
 首页 » 编程设计

python之谷歌应用引擎 Python : Know if an entity has been 'seen' by a user

2024年11月01日9落叶无声

如果您使用消息传递应用程序(Whatsapp、BBM、Facebook 的 Messenger),那么您应该熟悉您的消息被收件人“看到”的指示。

我想为我的实体构建此功能。

例如,考虑这个实体(ndb.Model):

class Entity(ndb.Model): 
    title = ndb.StringProperty() 
    seen = ndb.BooleanProperty(default = False) 
 
class RenderEntity(BaseHandler): 
    #renders entity on a template 
    def get(self, entity_id): 
        entity = Entity.get_by_id('Entity', entity_id) 
        self.render('entity_template.html', entity = entity) 

这是entity_template.html

<body> 
    {{entity.title}} 
    {{entity.seen}} 
</body> 

我该如何做到:如果特定用户看到这个实体,那么 seen 属性将被设置为 True?

请您参考如下方法:

简单update the Entity在你看来:

class RenderEntity(BaseHandler): 
    #renders entity on a template 
    def get(self, entity_id): 
        entity = Entity.get_by_id('Entity', entity_id) 
 
        # TODO: determine user_id and reciever_user_id 
        if user_id == receiver_user_id: 
            entity.seen = True 
            entity.put() 
 
        self.render('entity_template.html', entity = entity) 

您没有具体说明如何确定 user_id 也没有说明您如何知道收件人,但是您可以在获得它们后使用简单的 if 测试。