-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
73 lines (47 loc) · 1.54 KB
/
models.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
60
61
62
63
64
65
66
67
68
69
70
71
from google.appengine.ext import db
import tweepy
class User ( db.Model ):
id = db.StringProperty()
screen_name = db.StringProperty()
@classmethod
def get_or_insert_for ( cls, **kwargs ):
try: user = tweepy.api.get_user(**kwargs)
except tweepy.TweepError: return None
return cls.get_or_insert(user.screen_name,
id = user.id_str, screen_name = user.screen_name
)
@classmethod
def for_user_id ( cls, user_id ):
return (
cls.all().filter('user_id =', user_id).get() or
cls.get_or_insert_for(id = user_id)
)
@classmethod
def for_screen_name ( cls, screen_name ):
return (
cls.all().filter('screen_name =', screen_name).get() or
cls.get_or_insert_for(screen_name = screen_name)
)
def __str__ ( self ): return unicode(self)
def __unicode__ ( self ):
return unicode(self.screen_name)
class Tweet ( db.Model ):
status_id = db.StringProperty()
from_user = db.ReferenceProperty(User,
collection_name = 'tweets_from'
)
to_user = db.ReferenceProperty(User,
collection_name = 'tweets_to'
)
created_at = db.DateTimeProperty()
text = db.StringProperty(
multiline = True
)
@classmethod
def all ( cls, *args, **kwargs ):
return super(Tweet, cls).all(
*args, **kwargs
).order('-created_at')
def __str__ (self ): return self.__unicode__()
def __unicode__ ( self ):
return unicode(self.text)