-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
60 lines (54 loc) · 2.87 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, cgi, json, pystache, cherrypy
from appdir import APPDIR
os.chdir(APPDIR) # For some reason, it matters at webfaction.
DEBUG = True
TEMPLATE_FOLDER = 'templates'
DEFAULTS = {
'headline': u'הודלף המאגר הביומטרי, נחשפו פרטיהם האינטימיים של רבבות אזרחים',
'subtitle': u'בין הנחשפים, ישראל ישראלי מיפו, שהתוודה על חיבתו לברבורי בר.',
'imgfilename': 'default.gif'
}
ARTICLE_DATA_FILE = '{0}/static/data/main.json'.format(APPDIR)
stache = pystache.Renderer(
search_dirs=TEMPLATE_FOLDER,file_encoding='utf-8',string_encoding='utf-8',file_extension='html')
class EditorApp(object):
@cherrypy.expose()
def index(self, headline=DEFAULTS['headline'], subtitle=DEFAULTS['subtitle'], imgsrc=None, image=None):
conf = cherrypy.request.app.config['editor']
urlbase = conf['urlbase'] or cherrypy.request.base
scriptname = urlbase+cherrypy.request.script_name
if not imgsrc:
imgsrc = '{0}/data/images/{1}'.format(scriptname,DEFAULTS['imgfilename'])
if cherrypy.request.method == 'POST':
if image and image.filename:
imgfilepath = '{0}/static/data/images/{1}'.format(APPDIR,image.filename)
file(imgfilepath,'w').write(image.file.read())
imgsrc = '{0}/data/images/{1}'.format(scriptname,image.filename)
params = {'headline': headline, 'subtitle': subtitle, 'imgsrc': imgsrc}
json.dump(params,open(ARTICLE_DATA_FILE,'w'),indent=4)
return stache.render(stache.load_template('form'), params,
scriptname=scriptname, imgsrc=imgsrc,
title=u'הכתבה עודכנה')
else:
return stache.render(stache.load_template('form'),
scriptname=scriptname, imgsrc=imgsrc,
headline=headline, subtitle=subtitle,
title=u'עורך ראשי')
@cherrypy.expose()
def live(self):
if cherrypy.request.method == 'OPTIONS':
cherrypy.response.headers.update({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'X-Requested-With, Content-Type',
'Access-Control-Allow-Methods': 'GET, OPTIONS',
'Access-Control-Allow-Headers': 'X-Requested-With'})
return '' # Is this the "proper" thing to return?
return stache.render(stache.load_template('live'), json.load(file(ARTICLE_DATA_FILE)))
if __name__ == '__main__':
cherrypy.config.update('{0}/cherrypy.config'.format(APPDIR))
app = EditorApp()
cherrypy.tree.mount(app,'/',config='{0}/cherrypy.config'.format(APPDIR))
cherrypy.engine.start()
cherrypy.engine.block()