-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
frontend.py
executable file
·142 lines (121 loc) · 5.75 KB
/
frontend.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
# This file is part of waymarkedtrails.org
# Copyright (C) 2015-2020 Sarah Hoffmann
#
# This is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import sys
from os.path import join as os_join
from os import environ as os_environ
from json import dumps
import cherrypy
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
import config.defaults
import api.tools
cherrypy.tools.db = api.tools.SATool()
from api.routes import RoutesApi
from frontend.compatibility import CompatibilityLinks
from frontend.help import Helppages
_ = lambda x: x
http_error = {
None : _('Something unexpected happend.'),
'404' : _("It looks like the page you are looking for doesn't exist. If you think that you have found a broken link, don't hesitate to contact us.")
}
@cherrypy.tools.I18nTool()
class Trails(object):
def __init__(self, mapdb, maptype, langs, debug=False):
self.api = RoutesApi(mapdb, maptype)
self.help = Helppages()
compobj = CompatibilityLinks()
for l in langs:
setattr(self, l[0], compobj)
if not debug:
cherrypy.config.update({'error_page.default': Trails.error_page})
@cherrypy.expose
def index(self, **params):
gconf = cherrypy.request.app.config.get('Global')
lconf = cherrypy.request.app.config.get('Site')
_ = cherrypy.request.i18n.gettext
js_params = { 'MEDIA_URL': gconf['MEDIA_URL'],
'API_URL' : gconf['API_URL'],
'TILE_URL' : lconf['tile_url'],
'HILLSHADING_URL' : gconf['HILLSHADING_URL'],
'GUIDEPOST_URL' : gconf['GUIDEPOST_URL'],
'GROUPS' : dict([(k, _(v)) for k,v in lconf['groups'].items()]),
'GROUPS_DEFAULT' : lconf['groups_default'],
'BASEMAPS' : gconf['BASEMAPS']}
if hasattr(self.api, 'tiles'):
js_params['VTILE_URL'] = gconf['API_URL'] + '/tiles/';
else:
js_params['VTILE_URL'] = False
return cherrypy.request.templates.get_template('index.html').render(
g=gconf, l=lconf, jsparam = dumps(js_params))
@staticmethod
def error_page(status, message, traceback, version):
gconf = cherrypy.request.app.config.get('Global')
lconf = cherrypy.request.app.config.get('Site')
_ = cherrypy.request.i18n.gettext
bug = _("Bugs may be reported via the [github project page](https://github.com/lonvia/waymarked-trails-site/issues). Please make sure to describe what you did to produce this error and include the original message below.")
return cherrypy.request.templates.get_template('error.html').render(
g=gconf, l=lconf, bugreport=bug,
message=_(http_error.get(status[0:3], http_error[None])),
srcmsg= status + ': ' + message)
class _MapDBOption:
no_engine = True
def setup_site(confname, script_name='', debug=False):
globalconf = {}
for var in dir(sys.modules['config.defaults']):
if var.isupper():
globalconf[var] = getattr(sys.modules['config.defaults'], var)
site_cfg = {}
try:
__import__('config.sites.' + confname)
site_cfg = getattr(sys.modules['config.sites.' + confname], 'SITE', {})
except ImportError:
print("Missing config for site '%s'. Skipping." % site)
raise
os_environ['ROUTEMAPDB_CONF_MODULE'] = 'maps.%s' % confname
from db import conf as db_config
mapdb_pkg = 'db.%s_maptype' % db_config.get('MAPTYPE')
mapdb_class = __import__(mapdb_pkg, globals(), locals(), ['DB'], 0).DB
mapdb = mapdb_class(_MapDBOption())
app = cherrypy.tree.mount(Trails(mapdb, db_config.get('MAPTYPE'),
globalconf['LANGUAGES'], debug=debug),
script_name + '/',
{
'/favicon.ico':
{
'tools.staticfile.on': True,
'tools.staticfile.filename':
'%s/img/map/map_%s.ico' %
(globalconf['MEDIA_ROOT'], confname)
}
})
app.config['DB'] = { 'map' : mapdb }
app.config['Global'] = globalconf
app.config['Global']['BASENAME'] = confname
app.config['Global']['MAPTYPE'] = db_config.get('MAPTYPE')
app.config['Site'] = site_cfg
# now disable trailing slash
cherrypy.config.update({'tools.trailing_slash.on': False })
def setup_application():
dba = URL('postgresql', username=config.defaults.DB_USER,
database=config.defaults.DB_NAME,
password=config.defaults.DB_PASSWORD)
cherrypy.thread_data.conn = create_engine(dba, echo=False).connect()
setup_site(os_environ['WMT_CONFIG'], debug='WMT_DEBUG' in os_environ)
#cherrypy.config.update({'log.wsgi' : True, 'log.screen' : False})
cherrypy.config.update({'engine.autoreload.on': False})
return cherrypy.tree
application = setup_application()