-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdomain.py
170 lines (136 loc) · 4.84 KB
/
domain.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import pytz
from coopy.decorators import readonly
from datetime import datetime, timedelta
SLICE = timedelta(minutes=30)
DATE_FORMAT = '%Y-%m-%d %H:%M:%S %f %Z'
def make_url_item(title, link, owner=None):
item = {
'title': title,
'url': link
}
return make_item(title, item, owner=owner)
def make_text_item(title, text, owner=None):
item = {
'text': text,
'title': title
}
return make_item(title, item, owner=owner)
def make_item(title, item, owner=None):
instance = {}
instance['id'] = None
instance['title'] = title
instance['item'] = item
instance['votes'] = 0
utc_date = pytz.utc.localize(datetime.now())
instance['posted'] = utc_date.strftime(DATE_FORMAT)
instance['owner'] = owner
instance['comments'] = []
return instance
class Tree(object):
def __init__(self, name=None):
self.name = name
self.children = {}
self.items = None
def add(self, full_path, obj=None):
path = full_path.split('/')
name = path[0]
if not obj:
instance = List(full_path)
else:
instance = obj
if len(path) == 1:
tree = Tree(name)
self.children[name] = tree
tree.items = instance
return instance
else:
if not name in self.children:
self.children[name] = Tree(name)
tree = self.children[name]
tree.add(full_path.replace("{}/".format(name), ""), obj=instance)
return instance
return instance
def get(self, full_path):
path = full_path.split('/')
name = path[0]
if len(path) == 1:
return self.children[name]
else:
tree = self.children[name]
return tree.get(full_path.replace("{}/".format(name), ""))
@readonly
def has(self, full_path):
path = full_path.split('/')
name = path[0]
if len(path) == 1:
return name in self.children
else:
if name in self.children:
tree = self.children[name]
return tree.has(full_path.replace("{}/".format(name), ""))
else:
return False
def add_item(self, full_path, item):
channel = self.get(full_path).items
channel.add(item)
return item
def remove_item(self, full_path, item):
channel = self.get(full_path).items
channel.remove(item)
return item
@readonly
def find_item(self, full_path, item):
channel = self.get(full_path).items
return channel.find(item)
def add_vote(self, full_path, item, vote):
channel = self.get(full_path).items
return channel.vote(item, vote)
def add_comment(self, full_path, item, user, content):
channel = self.get(full_path).items
return channel.add_comment(item, user, content)
def del_comment(self, full_path, item, comment_id):
channel = self.get(full_path).items
return channel.del_comment(item, user, content)
@readonly
def get_items(self, full_path):
channel = self.get(full_path).items
return channel.get_items()
def __eq__(self, other):
return self.name == other.name
class List(object):
def __init__(self, name):
self.name = name
self.items = []
self.index = 1
def add(self, item):
item['id'] = self.index
self.items.append(item)
self.index += 1
def remove(self, item_id):
self.items = list(filter(lambda x: not x['id'] == item_id, self.items))
def find(self, item_id):
found = list(filter(lambda x: x['id'] == item_id, self.items))[0]
return found
def vote(self, item_id, vote):
found = list(filter(lambda x: x['id'] == item_id, self.items))
if found:
found[0]['votes'] += vote
return found[0]
raise Exception("Unknow/Removed item")
def add_comment(self, item_id, user_id, content):
found = list(filter(lambda x: x['id'] == item_id, self.items))[0]
comment = dict(id=len(found['comments']) + 1,user=user_id, content=content)
found['comments'].append(comment)
return comment
def del_comment(self, item_id, comment_id):
found = list(filter(lambda x: x['id'] == item_id, self.items))[0]
comment = list(filter(lambda x: x['id'] == comment_id, found['comments']))[0]
found['comments'].remove(comment)
return comment
def get_items(self):
return sorted(self.items, key=lambda item: item['votes'], reverse=True)[:10]
def get_user_items(self, user_id):
return sorted(filter(lambda x: x['owner'] == user_id, self.items),
key=lambda item: datetime.strptime(item['posted'], DATE_FORMAT))
def __len__(self):
return len(self.items)