pombreda / appengine-ndb-experiment

Automatically exported from code.google.com/p/appengine-ndb-experiment
Other
0 stars 0 forks source link

Add support for IM property #235

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
It seems it should go right after TimeProperty:
http://code.google.com/p/appengine-ndb-experiment/source/browse/ndb/model.py?r=3
0c578bfe60c63d858aefaeb8e48a63e64ee872e#2023

from google.appengine.api import datastore_types
from google.appengine.datastore import entity_pb

class IMProperty(Property):

  def _validate(self, value):
    if not isinstance(value, datastore_types.IM):
      raise TypeError('expected an IM, got %r' % (value,))

  def _db_set_value(self, v, p, value):
    v.set_stringvalue(str(value))
    p.set_meaning(entity_pb.Property.GD_IM)

  def _db_get_value(self, v, p):
    if not v.has_stringvalue():
      return None
    meaning = p.meaning()
    if meaning != entity_pb.Property.GD_IM:
      raise ValueError('Value stored not an IM.')
    return datastore_types.IM(v.stringvalue())

This code is so simple because the constructor for IM is written in such a way 
that

    IM(str(im_value)) == im_value

and

    str_value == str(IM(str_value)).

Original issue reported on code.google.com by dhermes@google.com on 11 Jan 2013 at 1:49

Attachments:

GoogleCodeExporter commented 9 years ago
This is code complete. Any reasons not to add?

Original comment by dhermes@google.com on 16 Jan 2013 at 2:03

GoogleCodeExporter commented 9 years ago
IM was left out on purpose (all high level string types were left out), I 
recommend you instead switch to using a db.StringProperty() in your db 
implementation.

Original comment by arful...@google.com on 6 Feb 2013 at 10:52