【问题标题】:Google App Engine Python: Know if an entity has been 'seen' by a userGoogle App Engine Python:了解用户是否“看到”实体
【发布时间】:2014-03-22 17:34:37
【问题描述】:

如果您使用消息应用程序(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?

【问题讨论】:

  • 什么代码向用户显示实体?当您将消息呈现给目标用户时,将seen 设置为True 是琐碎的。真的没什么。
  • @MartijnPieters,请查看更新后的帖子。该实体实际上可供任何人访问,但我想要一个特定用户(即匹配特定“user_id”)已经看到它的指示。
  • 那么如何确定用户id?

标签: python google-app-engine app-engine-ndb


【解决方案1】:

在你看来只需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 测试,一旦您有了它们。

【讨论】:

  • 啊,是的,这里的挑战是设置一个从当前正在查看实体的用户那里“接收用户 ID”的代码,这基本上就是我要问的。我们可以假设
  • @haopei:然后在你的问题中明确说明;您的应用程序中是否有任何类型的用户身份验证?
  • 是的,登录用户的 user_id 存储为散列 cookie :) 我认为这只是回答了问题。因此,我应该从 cookie 中检索登录的 user_id 并将其与目标用户匹配。假设这是解决方案,如果 1000 人看到该实体并执行 1000 次匹配,是否效率低下?
  • @haopei:你已经加载了实体,cookie已经收到。比较字符串不会影响你的表现。
  • 感谢您的帮助,@Martijn Pieters。
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 2014-08-05
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2023-03-19
相关资源
最近更新 更多