forked from phith0n/Minos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.py
155 lines (145 loc) · 3.94 KB
/
ajax.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
#coding=utf-8
__author__ = 'phithon'
import tornado.web, time
from controller.base import BaseHandler
from tornado import gen
from bson.objectid import ObjectId
from util.function import time_span, intval
class AjaxHandler(BaseHandler):
def get(self, *args, **kwargs):
self.json("fail", "no action!")
def post(self, *args, **kwargs):
action = "_%s_action" % args[0]
if hasattr(self, action):
getattr(self, action)()
else:
self.json("fail", "no action!")
@tornado.web.asynchronous
@gen.coroutine
def _like_action(self):
id = self.get_body_argument("post")
post = yield self.db.article.find_one({
"_id": ObjectId(id)
})
if not post:
self.json("fail", "post id error")
self.__check_already(post)
post["like"].append(self.current_user["username"])
yield self.db.article.find_and_modify({
"_id": ObjectId(id)
}, {
"$set": {"like": post["like"]}
})
self.json("success", len(post["like"]))
@tornado.web.asynchronous
@gen.coroutine
def _unlike_action(self):
id = self.get_body_argument("post")
post = yield self.db.article.find_one({
"_id": ObjectId(id)
})
if not post:
self.json("fail", "post id error")
self.__check_already(post)
post["unlike"].append(self.current_user["username"])
yield self.db.article.find_and_modify({
"_id": ObjectId(id)
}, {
"$set": {"unlike": post["unlike"]}
})
self.json("success", len(post["unlike"]))
@tornado.web.asynchronous
@gen.coroutine
def _bookmark_action(self):
id = self.get_body_argument("post")
post = yield self.db.article.find_one({
"_id": ObjectId(id)
})
if not post:
self.json("fail", "article not found")
user = yield self.db.member.find_one({
"username": self.current_user["username"]
})
for row in user["bookmark"]:
if id == row["id"]:
self.json("fail", "already bookmark")
bookmark = {
"id": str(post["_id"]),
"title": post["title"],
"user": post["user"],
"sort": post["sort"],
"time": time.time()
}
ret = yield self.db.member.update({
"username": self.current_user["username"]
}, {
"$push": {"bookmark": bookmark}
})
self.json("success", "done")
@tornado.web.asynchronous
@gen.coroutine
def _thanks_action(self):
id = self.get_body_argument("id")
if not id:
self.json("fail", "post not exists!")
post = yield self.db.article.find_one({
"_id": ObjectId(id),
"thanks": {"$nin": [self.current_user["username"]]}
})
if not post:
self.json("fail", "already thanks")
if post["user"] == self.current_user["username"]:
self.json("fail", "cannot thanks to yourself")
old = yield self.db.member.find_and_modify({
"username": self.current_user["username"],
"money": {"$gt": 0}
}, {
"$inc": {"money": -1}
})
if not old:
self.json("fail", "money not enough")
yield self.db.member.find_and_modify({
"username": post["user"]
}, {
"$inc": {"money": 1}
})
yield self.db.article.find_and_modify({
"_id": ObjectId(id)
}, {
"$push": {"thanks": self.current_user["username"]}
})
yield self.message(fromuser = None, touser = post["user"],
content = u"你的文章《%s》被%s感谢了" % (post["title"], self.current_user["username"]),
jump="/post/%s" % id)
self.clear_cookie("flush_info")
self.json("success", "thanks success")
@tornado.web.asynchronous
@gen.coroutine
def _newmsg_action(self):
cursor = self.db.message.find({
"$and": [
{"read": False},
{"$or": [
{"to": self.current_user["username"]},
{"from": self.current_user["username"]}
]},
]
})
count = yield cursor.count()
self.json("success", count)
def __check_already(self, post):
'''
检查like、unlike是否重复
:param post:
:return:
'''
if (self.current_user["username"] in post["unlike"]):
self.json("fail", "already unliked")
if (self.current_user["username"] in post["like"]):
self.json("fail", "already liked")
def json(self, status, info):
self.write({
"status": status,
"info": info
})
raise tornado.web.Finish()