Skip to content

Instantly share code, notes, and snippets.

@grudelsud
Created December 4, 2013 17:59
Show Gist options
  • Save grudelsud/7792339 to your computer and use it in GitHub Desktop.
Save grudelsud/7792339 to your computer and use it in GitHub Desktop.
class BaseModel(ndb.Model):
@classmethod
def populate(cls, key):
self = key.get()
sources = getattr(self.__class__, 'sources', False)
source_data = {}
if not sources:
return False
for source_name in sorted(sources.keys(), key=lambda k: 0 if k == 'gplus' else 9):
source_id_property = 'source_id_' + source_name
entity_source_id = getattr(self, source_id_property, False)
if not entity_source_id:
continue
try:
data = source.load(self.__class__.__name__, entity_source_id, self)
source_data[source_name] = data
except:
logging.warn('Error loading source')
setattr(self, 'source_failed_' + source_name, True)
self.update_data(source_data)
return self
def put(self, *args, **kwargs):
# execute all the logic needed to populate the model, then:
if do_populate:
deferred.defer(self.__class__.populate, self.key, _queue='populate-entity')
if self.__class__.__name__ != 'Connection':
deferred.defer(Connection.update_connections, self.key)
class Artist(BaseModel):
"""an artist"""
sources = {
'gplus': {
'properties': {
'content': ['/aboutMe'],
'blob_key_image': ['/image/url', '/cover/coverPhoto/url'],
'website': ['/url', '/urls/value'],
'source_id_gplus': ['/id'],
'source_rating_gplus': ['/circledByCount'],
},
},
'freebase': {
'properties': {
'content': ['/common/topic/description', '/common/topic/article'],
'address': ['/people/person/place_of_birth'],
},
},
'discogs': {
'properties': {
'content': ['/profile'],
'website': ['/urls'],
'blob_key_image': ['/images'],
'aliases': ['/aliases'],
'source_id_discogs': ['/id'],
},
},
'soundcloud': {
'properties': {
'source_rating_soundcloud': ['/followers_count'],
},
},
'lastfm': {
'properties': {
'content': ['/bio/content'],
'website': ['/url', '/website'],
'blob_key_image': ['/image[size=extralarge]/#text'],
'active_from': ['/bio/yearformed'],
'source_id_lastfm': ['/mbid'],
'source_rating_lastfm': ['/stats/listeners'],
},
},
}
name = ndb.StringProperty()
content = ContentProperty(ndb.TextProperty)
address = ContentProperty(ndb.TextProperty)
blob_key_image = ContentProperty(ndb.BlobKeyProperty)
active_from = ContentProperty(ndb.DateProperty)
website = ContentProperty(ndb.StringProperty, repeated=True)
aliases = ContentProperty(ndb.StringProperty, repeated=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment