forked from ponyorm/pycon.ru-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.py
56 lines (47 loc) · 1.79 KB
/
entities.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
from datetime import datetime
from pony.orm import *
db = Database("sqlite", "data.sqlite", create_db=True)
class User(db.Entity):
username = Required(unicode, unique=True)
password = Required(unicode)
dt = Required(datetime, default=datetime.now)
following = Set("Following", reverse="follower")
followers = Set("Following", reverse="followee")
photos = Set("Photo")
likes = Set("Like")
comments = Set("Comment", reverse="user")
mentioned = Set("Comment", reverse="mentioned")
class Photo(db.Entity):
filename = Required(unicode)
photo_url = Required(unicode)
dt = Required(datetime, default=datetime.now)
tags = Set("Tag")
user = Required(User)
liked = Set("Like")
comments = Set("Comment")
# current_username is passed in order to find out if the current user liked the photo
def to_json(self, current_username):
return {'id': self.id, 'photo_url': self.photo_url,
'username': self.user.username, 'likes_count': len(self.liked),
'liked': current_username in self.liked.user.username}
class Tag(db.Entity):
name = PrimaryKey(unicode)
photos = Set(Photo)
class Comment(db.Entity):
photo = Required(Photo)
user = Required(User, reverse="comments")
dt = Required(datetime, default=datetime.now)
text = Required(unicode)
mentioned = Set(User, reverse="mentioned")
class Like(db.Entity):
user = Required(User)
photo = Required(Photo)
dt = Required(datetime, default=datetime.now)
PrimaryKey(user, photo)
class Following(db.Entity):
follower = Required(User, reverse="following")
followee = Required(User, reverse="followers")
dt = Required(datetime, default=datetime.now)
PrimaryKey(follower, followee)
sql_debug(True)
db.generate_mapping(create_tables=True)