0

I'm making a spam filter library for google app engine, which uses app engine hooks to modify some data right before it is put() into the datastore. To do so, I created a model that subclasses the PolyModel class, which classes wishing to be checkable should subclass.

But for some data in the class, I'd like to do some stuff when the value is changed, for which I'd have to overwrite __setitem__. But I believe the sdk uses this to determine changes in the properties of models, apply filters etc, and so changing overwriting __setitem__ might break this. What's the best way around this?

Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76
bigblind
  • 12,539
  • 14
  • 68
  • 123

2 Answers2

2

There is no danger in overriding methods, if you don't forget to call the parent method with super. But in your case __setitem__ is wrong place, because then everytime when instance is created it will be cleaned from spam, even if it is created from already cleaned values.

What is wrong if you do that explicitly?

filter_out_spam(obj)
obj.put()
Ski
  • 14,197
  • 3
  • 54
  • 64
  • the explicit model would work indeed, but I'm trying to make it automatic. See it as a centralization. If the user wants to add this to their app, they only need to edit one piece of code, instead of prepending this to all put() calls. – bigblind Dec 20 '11 at 17:41
2

Don't modify __setitem__ - write a custom property class that does what you want, instead.

Bear in mind that you can't just override put on your model if you want to do things before the record is put, because entities can also be stored with db.put. If you can, you should avoid the need for this by moving the relevant code into property classes.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
  • I'm not overwriting put, I'm using a hook which is called by the sdk every time a request to the dtabase is made. I check wether the action is 'put', and then do my stuff to the model. – bigblind Dec 20 '11 at 17:39
  • @FrederikCreemers Fair enough. My advice about not overriding the model's `__setitem__` still applies. – Nick Johnson Dec 21 '11 at 00:27