If you use messaging apps (Whatsapp, BBM, Facebook's Messenger), then you should be familiar with the indication of your messages being 'seen' by your recipient.

I would like to build this feature to my entities.

For example, consider this Entity(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)

Here is the entity_template.html

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

How do I make it so that: if a particular user sees this entity, then the seen property will be set to True?

有帮助吗?

解决方案

Simply update the Entity in your view:

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)

You did not specify how you determine the user_id nor how you know it the recipient, but you can use a simple if test once you do have them.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top