Question

My entities can currently be viewed using the path /12345 where 12345 represents its entity ID.

I want to create a handler which deletes any entity using /12345/delete.

The handler would look something like this:

class DeleteHandler(BaseHandler):
    def get(self, entity_id):
       # cannot retrieve entity using just entity_id
       # We cannot use KIND.get_by_id(entity_id), since 'entity kind' is unknown

How can we get the entity kind using the url, so that we may use get_by_id() to retrieve the entity, and then delete it?

Am I approaching this correctly? Or is there a better way?

Was it helpful?

Solution

Entity ids are not unique across kinds, so I don't really know how you are expecting this to work.

The websafe encoded version of the full key, however, does give enough information to uniquely identify the entity. You can then use that to instantiate a key, and you can call delete on that key.

OTHER TIPS

It doesn't feel right.

I would go simply with kind/12345/delete

To answer your question, one approach could be with whitelisting.

Set somewhere the kinds (classes) you want this operation to delete their objects (entities).

Suppose you have these Kinds:

class Foo(ndb.Expando):
  pass


class Boo(ndb.Expando):
  pass


class Bar(ndb.Expando):
  pass

Now since you have the id:

kinds_to_delete = ['Foo', 'Boo', 'Bar']
for kind_to_delete in kinds_to_delete:
  ndb.Key(kind_to_delete, entity_id).delete()

Or as a dirty oneliner:

[ndb.Key(kind, entity_id).delete() for kind in ['Foo', 'Boo', 'Bar']]

/<key>/delete 

Maybe it's easier if you passed around the base64 representation of the key

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top