-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.py
473 lines (353 loc) · 10.3 KB
/
star.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import json,os
import bottle
from bottle import Bottle
from bottle import template
from bottle import route, run, request, abort, view
from pymongo import Connection
from bottle import static_file
#call json
from bson import Binary, Code
from bson.json_util import dumps
from bson.json_util import loads
from bson.objectid import ObjectId
#verify in LOCAL or SERVER
# beaker session
from beaker.middleware import SessionMiddleware
import md5,urllib2
import feedparser
import socket
session_opts = {
'session.type': 'file',
'session.cookie_expires': 30000,
'session.data_dir': './data',
'session.auto': True
#'key':'timercount',
#'secret':'victorysecret'
}
host = socket.gethostbyname(socket.gethostname())
app = bottle.default_app()
if host=='127.0.1.1':
WEB_PATH = os.environ.get('SERVER_NAME')
WEB_STATIC =''
webroot = './static'
webpath = template('views/'+'banner')
app.config.setdefault('webpath',webpath)
else:
#SERVER STATIC FILES
WEB_PATH = os.environ.get('SERVER_NAME')
WEB_STATIC = '/home/hanchakra/webapps/starblogstatic/views/'
webroot = 'home/hanchakra/webapps/starblogstatic/static'
webpath = template(WEB_STATIC+'banner')
app.config.setdefault('webpath',webpath)
application = SessionMiddleware(app, session_opts)
app_session = bottle.request.environ.get('beaker.session')
connection = Connection('localhost',27017)
db = connection.mydatabase
#db = connection['stars']
stardb = db.stars
#CREATE STAR
@bottle.route('/star_detail/<name>',method='GET')
@view(WEB_STATIC+'editStar')
def star_detail(name):
setup_request()
star = stardb.find_one({'_id':ObjectId(name)})
allblog = ''
countLoop = 1
if len(star['blog']) > 1:
for b in star['blog']:
if countLoop != len(star['blog']):
allblog += b+';'
else:
allblog += b
countLoop = countLoop+1
print star
else:
print 'one and one only'
allblog = star['blog']
star['blog_list'] = allblog
if star.get('country'):
print star['country']
else:
print 'none country'
webpath = app.config.get('webpath')
return dict(sname=star,webpath=webpath)
@bottle.route('/star_info/<name>', method='GET')
@view(WEB_STATIC+'star_info')
def star_info(name):
id = name
starx = stardb.find_one({'_id':ObjectId(name)})
chosen = starx['blog'][0]
print chosen
d = feedparser.parse('http://'+chosen)
stars = []
i = 0
for e in d.entries:
s = {}
print e.description
s['link'] = e.link
s['title'] = e.title
s['description'] = e.description
stars.append(s)
webpath = app.config.get('webpath')
return dict(stars=stars,starx=starx,webpath=webpath)
@bottle.route('/star', method='POST')
def create_star():
star = request.body.readline()
star = loads(star)
if not star:
abort(400,"no data")
stardb.insert(star)
@bottle.route('/upload/<name>',method='POST')
def upload_photo(name):
data = bottle.request.files.get('file')
fname, ext = os.path.splitext(data.filename)
#print fname
upload_dir = os.path.join(os.getcwd(),'static/upload')
if host == '127.0.1.1':
upload_dir = os.path.join(os.getcwd(),'static/upload')
else:
upload_dir = 'home/hanchakra/webapps/starblogstatic/upload'
# if not os.path.isdir(upload_dir):
# os.mkdir(upload_dir)
print os.path.join(upload_dir+'/'+name+ext)
try:
destination = open(upload_dir+'/'+name+ext, 'w+') #.write(data.file.read())
destination.write(data.file.read())
full_dir = name+ext
stardb.update({'_id':ObjectId(name)},{'$set':{'image':full_dir}},upsert=False)
except Exception as inst:
print 'fail'
pass
bottle.redirect('/star_detail/'+name)
#READ
@bottle.route('/',method = 'GET')
@view(WEB_STATIC+'star_index')
def allstars():
stars = db['stars'].find().sort('published')
#return stars
session = bottle.request.environ.get('beaker.session')
user_roles = 'user_roles' in session
user_id = 'user_id' in session
if user_roles:
print user_roles
if user_id:
print user_id
webpath = app.config.get('webpath')
return dict(stars=stars,webpath=webpath)
#ADMIN BACKEND
@bottle.route('/admin',method = 'GET')
@view(WEB_STATIC+'admin_index')
def allstars():
setup_request()
stars = db['stars'].find().limit(10)
stars_count = db['stars'].count()
pages = stars_count/10
if pages == 0:
pages = 1
else:
if stars_count%10 != 0:
pages += 1
total_pages =''
for i in range(0,pages):
i = i + 1
total_pages += '<li><a href="/admin/'+str(i)+'">'+str(i)+'</a></li>'
#return stars
webpath = app.config.get('webpath')
return dict(stars=stars,pages=pages,total_pages=total_pages,webpath=webpath)
@bottle.route('/admin/<id:int>',method = 'GET')
@view(WEB_STATIC+'admin_index')
def star_pagination(id):
setup_request()
offset = (id-1) * 10
stars = db['stars'].find().skip(offset).limit(10)
stars_count = db['stars'].count()
pages = stars_count/10
if pages == 0:
pages = 1
else:
if stars_count%10 != 0:
pages += 1
total_pages =''
for i in range(0,pages):
i = i + 1
total_pages += '<li><a href="/admin/'+str(i)+'">'+str(i)+'</a></li>'
return dict(stars=stars,pages=pages,total_pages=total_pages)
@bottle.route('/category/<name>',method='GET')
@view(WEB_STATIC+'country_category')
def country_category(name):
stars = stardb.find({'country':name})
webpath = app.config.get('webpath')
return dict(stars=stars,webpath=webpath)
@bottle.route('/starc', method='GET')
@view(WEB_STATIC+'addStar')
def morning_star():
setup_request()
title = "New User"
webpath = app.config.get('webpath')
return dict(name=title,webpath=webpath)
@bottle.route('/star/<name>', method='GET')
@view(WEB_STATIC+'star_template')
def get_star(name):
star = stardb.find_one({'_id':ObjectId(name)})
print name
if not star:
abort(404,'No document with id %s' % id)
webpath = app.config.get('webpath')
return dict(name=star['name'],webpath=webpath)
#UPDATE
@bottle.route('/star', method='PUT')
def add_star():
data = request.body.readline()
_id = request.params.get('_id')
name = request.params.get('name')
surname = request.params.get('surname')
blogs = request.params.get('blog')
fb = request.params.get('fb')
gender = request.params.get('gender')
occupanys = request.params.get('occupation')
country = request.params.get('country')
occupant = []
blog_list = []
blog_split = blogs.split(';')
print blog_split
for blog in blog_split:
blog_list.append(blog)
print blog_list
if not data:
abort(400, 'No data received')
try:
stardb.update({'_id':ObjectId(_id)},{'$set':{'name':name,'surname':surname,
'blog':blog_list,'fb':fb, 'gender':gender, 'occupant':occupant, 'country':country}},upsert=False)
except:
abort(400,str(ve))
@bottle.route('/update_blog', method='PUT')
def update_blog():
setup_request()
url = bottle.request.POST.get('urls')
id = bottle.request.POST.get('id')
d = feedparser.parse('http://'+url)
d.entries[0].title
try:
title = d.feed.title
title = d.entries[0].title_detail.value
except:
title = ''
try:
link = d.feed.link
link = d.entries[0].link
except:
link = ''
try:
published = d.entries[0].published
except:
published = ''
try:
pid = d.entries[0].id
except:
pid = ''
try:
description = d.entries[0].description
except:
description = ''
data = {'title':title, 'link':link , 'published':published , 'description':description}
#data = {"title": d.feed.title,"description":d.feed.description,"link":d.feed.link}
stardb.update({'_id':ObjectId(id)},{'$set':{'lastPost':data}},upsert=False)
return
#DELETE
@bottle.route('/star/<name>', method='DELETE')
def delete_star(name):
setup_request()
id = name
stardb.remove({'_id':ObjectId(id)})
#basic static file setting
@bottle.route('/static/js/<filename:path>')
def send_static2(filename):
#return static_file(filename, root = './static/js/')
return static_file(filename, root= webroot+'/js/')
@bottle.route('/static/css/<filename>')
def send_static(filename):
#return static_file(filename, root = './static/css/')
return static_file(filename, root= webroot+'/css/')
@route('/static/template/<filename:path>')
def file_static(filename):
#return static_file(filename, root='./static/template/')
return static_file(filename, root= webroot+'/template/')
@route('/static/upload/<filename:path>')
def upload_static(filename):
return static_file(filename, root='./static/upload/')
return static_file(filename, root= webroot+'/upload/')
#Authenication
@bottle.route('/user/new',method='GET')
@view(WEB_STATIC+'new_user')
def new_user():
title = "New User"
webpath = app.config.get('webpath')
return dict(name=title,webpath=webpath)
@bottle.route('/user/login',method='GET')
@view(WEB_STATIC+'login')
def login_pages():
title = 'login user'
webpath = app.config.get('webpath')
return dict(name=title,webpath=webpath)
@bottle.route('/user',method='POST')
def register():
username = request.forms.get('username')
password = request.forms.get('password')
if username and password:
print username + password
user = db['user'].find_one({'username':username})
if not user:
hash = md5.new()
hash.update(password)
save_password = hash.hexdigest()
db['user'].insert({'username':username,'password':save_password})
return
else:
print 'User exists!'
return
@bottle.route('/login_auth',method='POST')
def login_auth():
username = request.forms.get('username')
password = request.forms.get('password')
hash = md5.new()
hash.update(password)
password_md5 = hash.hexdigest()
user = db['user'].find_one({'username':username})
print user
if user['password'] == password_md5:
session = bottle.request.environ.get('beaker.session')
session['user_roles']= 'user'
session['user_id'] = user['_id']
session.save()
bottle.redirect('/admin')
else:
bottle.redirect('/user/login')
@bottle.route('/logout',method='GET')
def logout():
session = bottle.request.environ.get('beaker.session')
session.delete()
bottle.redirect('/')
def setup_request():
try:
request.session = bottle.request.environ['beaker.session']
except:
abort(401,"Fails")
try:
name = request.session['user_roles']
except:
bottle.redirect('/user/login')
# MODULES FEED_PARSER
@bottle.route('/parse_xml',method='POST')
def parse_xml():
setup_request()
url = bottle.request.POST.get('urls')
d = feedparser.parse(url)
try:
title = d.feed.title
data = {"title": d.feed.title,"description":d.feed.description,"link":d.feed.link}
return dict(data=data)
except:
return dict(data='')
#run(host='localhost', port=8080)
#bottle.run(app=app,reloader=True)
bottle.run(app=application,reloader=True)