/
server.py
330 lines (284 loc) · 12.3 KB
/
server.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import tornado.ioloop
import tornado.web
import os
import tornado.template
from tornado.options import define, options
from tornado.template import Template, Loader
from pymongo import Connection, errors
from ukrgadget.db import convert_to_json
from ukrgadget.model import ArticleObject
from time import strftime, strptime, gmtime
from bson.objectid import *
define('port', default=80, help='run on the given port', type=int)
class Application(tornado.web.Application):
def __init__(self):
try:
conn = Connection('127.0.0.1', 27017).ukrgadget
conn.authenticate('skl1f', 'ukr')
self.database = conn['Articles']
except errors.AutoReconnect as er:
print('Error database connection:', er)
try:
self.loader = Loader(
"/home/skl1f/code/ukrgadget.com/templates")
except:
pass
handlers = [
# (r'/data.json', LongRequestHandler),
# (r'/page/', PaginateRequestHandler),
# (r'/page/([0-9]+)', PaginateRequestHandler),
(r'/admin/edit/(.*)', EditHandler),
(r'/', MainHandler),
(r'/admin/list/', ListHandler),
(r'/admin/list/([0-9]+)', ListHandler),
(r'/admin/new/', NewHandler),
(r'/admin/delete/(.*)', DeleteHandler),
(r'/admin/post/(.*)', PostHandler),
(r'/admin/twit/(.*)', TwitHandler),
]
settings = dict(
cookie_secret='43oETzKXQAGaYdk6fd8fG1kJFuYh7EQnp2XdTP1o/Vo=',
template_path=os.path.join(os.path.dirname(__file__), 'templates'),
static_path=os.path.join(os.path.dirname(__file__), 'static'),
xsrf_cookies=True,
autoescape=False,
login_url='/admin/new/',
# debug=True,
)
tornado.web.Application.__init__(self, handlers, **settings)
class LongRequestHandler(tornado.web.RequestHandler):
def database_callback(self, *args, **kwargs):
if not args[0]:
self.finish()
else:
self.set_header("Content-Type", "application/json; charset=UTF-8")
self.write(args[0])
self.flush()
self.finish()
@tornado.web.asynchronous
def get(self):
data = self.application.database.find().sort("pub_date", -1)
data = convert_to_json(data)
render = Template("{% autoescape None %}{{ json_data }}").generate(
json_data=data)
tornado.ioloop.IOLoop.instance().add_callback(
self.async_callback(self.database_callback, render))
class PaginateRequestHandler(tornado.web.RequestHandler):
"""docstring for PaginaRequestPage"""
def list_callback(self, *args, **kwargs):
self.set_header("Content-Type", "application/json; charset=UTF-8")
self.write(args[0])
self.flush()
self.finish()
@tornado.web.asynchronous
def get(self, page=1):
page = int(page)
all_items = self.application.database.find().sort('pub_date', -1)
all_items_count = all_items.count()
items = list(range(-15, all_items_count, 15))
list_items = list(range(items[page], items[page + 1]))
list_articles = []
for x in list_items:
list_articles.append(all_items[x])
list_articles = convert_to_json(list_articles)
tornado.ioloop.IOLoop.instance().add_callback(
self.async_callback(self.list_callback, list_articles))
class NewHandler(tornado.web.RequestHandler):
def get(self):
item = ArticleObject().dict()
xsrf = self.xsrf_form_html()
date = strftime("%m/%d/%Y", gmtime())
rfc822_date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
html_template = self.application.loader.load("editor.html")\
.generate(title=item['title'],
slug_name=item['slug_name'],
content=item['content'],
morecontent=item['morecontent'],
pub_date=date,
author=item['author'],
category=item['category'],
posted=item['posted'],
twit=item['twit'],
tags=item['tags'],
absolute_url=item['absolute_url'],
short_url=item['short_url'],
rfc822_date=rfc822_date,
xsrf_input=xsrf,)
self.write(html_template)
def post(self, *args, **kwargs):
art = ArticleObject()
art.title = self.get_argument("title")
art.slug_name = self.get_argument("uri")
art.content = self.get_argument("content")
art.morecontent = self.get_argument("morecontent")
try:
q = strptime(self.get_argument("pub_date"), "%m/%d/%Y")
art.pub_date = strftime("%Y-%m-%d", q)
art.rfc822_date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
except:
q = strptime(self.get_argument("pub_date"), "%Y-%m-%d")
art.pub_date = self.get_argument("pub_date")
art.rfc822_date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
art.author = self.get_argument("name")
art.category = self.get_argument("category")
art.tags = self.get_argument("tags")
art.posted = False
art.twit = False
art.absolute_url = 'http://ukrgadget/' + \
self.get_argument("category") + '/' + self.get_argument("uri")
art.short_url = 'http://ukrgadget.com/go/' + \
str(self.application.database.find().count() + 1)
ident = self.application.database.insert(art.dict())
self.redirect('/admin/edit/' + str(ident), permanent=False, status=302)
class EditHandler(tornado.web.RequestHandler):
def get(self, ident):
item = self.application.database.find_one({"_id": ObjectId(ident)})
if self.application.database.find({
"slug_name": item['slug_name']}).count() == 1:
xsrf = self.xsrf_form_html()
q = strptime(item['pub_date'], "%Y-%m-%d")
date = strftime("%m/%d/%Y", q)
ident = item['_id']
html_template = self.application.loader.load("edit_articles.html")\
.generate(title=item['title'],
slug_name=item['slug_name'],
content=item['content'],
morecontent=item['morecontent'],
pub_date=date,
author=item['author'],
category=item['category'],
posted=item['posted'],
twit=item['twit'],
tags=item['tags'],
absolute_url=item['absolute_url'],
short_url=item['short_url'],
rfc822_date=rfc822_date,
xsrf_input=xsrf,)
self.write(html_template)
else:
xsrf = self.xsrf_form_html()
q = strptime(item['pub_date'], "%Y-%m-%d")
date = strftime("%m/%d/%Y", q)
ident = item['_id']
html_template = self.application.loader.load("edit_articles.html")\
.generate(title=item['title'],
slug_name=item['slug_name'],
content=item['content'],
morecontent=item['morecontent'],
pub_date=date,
author=item['author'],
category=item['category'],
posted=item['posted'],
twit=item['twit'],
tags=item['tags'],
absolute_url=item['absolute_url'],
short_url=item['short_url'],
rfc822_date=rfc822_date,
xsrf_input=xsrf,)
self.write(html_template)
def post(self, *args, **kwargs):
art = ArticleObject()
item = self.application.database.find_one(
{"_id": ObjectId(self.get_argument("ident_form"))})
art.title = self.get_argument("title")
art.slug_name = self.get_argument("uri")
art.content = self.get_argument("content")
art.morecontent = self.get_argument("morecontent")
try:
q = strptime(self.get_argument("pub_date"), "%m/%d/%Y")
art.pub_date = strftime("%Y-%m-%d", q)
art.rfc822_date = strftime("%a, %d %b %Y %H:%M:%S +0000", q)
except:
q = strptime(self.get_argument("pub_date"), "%Y-%m-%d")
art.pub_date = self.get_argument("pub_date")
art.rfc822_date = strftime("%a, %d %b %Y %H:%M:%S +0000", q)
art.author = self.get_argument("name")
art.category = self.get_argument("category")
art.tags = self.get_argument("tags")
art.posted = item['posted']
art.twit = item['twit']
art.absolute_url = 'http://ukrgadget/' + \
self.get_argument("category") + '/' + self.get_argument("uri")
art.short_url = item['short_url']
self.application.database.update(
{"_id": ObjectId(self.get_argument("ident_form"))}, art.dict())
self.redirect(
'/admin/edit/' + self.get_argument("ident_form"),
permanent=False, status=302)
class DeleteHandler(tornado.web.RequestHandler):
def get(self, ident):
self.application.database.remove({"_id": ObjectId(ident)}),
self.redirect('/admin/list/', permanent=False, status=302)
class PostHandler(tornado.web.RequestHandler):
def get(self, ident):
art, item = self.application.database.find_one(
{"_id": ObjectId(ident)}),\
self.application.database.find_one({"_id": ObjectId(ident)})
if self.application.database.find(
{"slug_name": item['slug_name']}).count() == 1:
art['posted'] = True
self.application.database.update(item, art)
self.redirect('/admin/list/', permanent=False, status=302)
else:
self.redirect('/admin/edit/' + ident, permanent=False, status=302)
class TwitHandler(tornado.web.RequestHandler):
def get(self, ident):
art, item = self.application.database.find_one(
{"_id": ObjectId(ident)}),\
self.application.database.find_one({"_id": ObjectId(ident)})
art['twit'] = True
self.application.database.update(item, art)
self.redirect('/admin/list/', permanent=False, status=302)
class ListHandler(tornado.web.RequestHandler):
"""docstring for ListRequestPage"""
def list_callback(self, *args, **kwargs):
html_template = self.application.loader.load("list_articles.html")\
.generate(articles=args[0],
paginator=args[1],
page=args[2],)
self.write(html_template)
self.flush()
self.finish()
@tornado.web.asynchronous
def get(self, page=1):
page = int(page)
all_items = self.application.database.find().sort(
[("pub_date", -1), ("short_url", 1)])
all_items_count = all_items.count()
items = list(range(-30, all_items_count + 30, 30))
list_items = list(range(items[page], items[page + 1]))
if ((all_items_count / 30) % round(all_items_count / 30) == 0.0):
paginator = all_items_count / 30
else:
paginator = int(all_items_count / 30) + 1
paginator = [1 + i for i in range(paginator)]
list_articles = []
for x in list_items:
try:
list_articles.append(all_items[x])
except IndexError:
pass
tornado.ioloop.IOLoop.instance().add_callback(
self.async_callback(self.list_callback,
list_articles, paginator, page))
class MainHandler(tornado.web.RequestHandler):
"""docstring for MainHandler"""
def get(self):
html_template = '''<!DOCTYPE HTML>
<html>
<head>
<title>Mustache Samples</title>
</head>
<body>
Olololo
</body>
</html>'''
self.write(html_template)
def main():
tornado.options.parse_command_line()
app = Application()
app.listen(options.port)
print('Server started on port %s' % options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
main()