-
Notifications
You must be signed in to change notification settings - Fork 0
/
modelx.py
59 lines (48 loc) · 1.42 KB
/
modelx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from google.appengine.ext import ndb
import md5
import util
class BaseX(object):
@classmethod
def retrieve_by_key_safe(cls, key_urlsafe):
try:
return ndb.Key(urlsafe=key_urlsafe).get()
except:
return None
@classmethod
def retrieve_by_id(cls, id):
try:
return cls.get_by_id(int(id))
except ValueError:
return None
@classmethod
def retrieve_one_by(cls, name, value):
cls_db_list = cls.query(getattr(cls, name) == value).fetch(1)
if cls_db_list:
return cls_db_list[0]
return None
created_ago = ndb.ComputedProperty(
lambda self: util.format_datetime_ago(self.created) \
if self.created else None
)
modified_ago = ndb.ComputedProperty(
lambda self: util.format_datetime_ago(self.modified) \
if self.modified else None
)
created_utc = ndb.ComputedProperty(
lambda self: util.format_datetime_utc(self.created) \
if self.created else None
)
modified_utc = ndb.ComputedProperty(
lambda self: util.format_datetime_utc(self.modified) \
if self.modified else None
)
class ConfigX(object):
@classmethod
def get_master_db(cls):
return cls.get_or_insert('master')
class UserX(object):
avatar_url = ndb.ComputedProperty(
lambda self: 'http://www.gravatar.com/avatar/%s?d=identicon&r=x' % (
md5.new(self.email or self.name.encode('utf-8')).hexdigest().lower()
)
)