From 0f19d5c78f5bad5fb01e7a5e31be5ba9eaff28ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Fri, 10 Oct 2014 19:19:25 +0200 Subject: [PATCH] map field (work in progress) --- wcs/fields.py | 16 ++++++++++++++++ wcs/qommon/form.py | 27 +++++++++++++++++++++++++++ wcs/qommon/http_response.py | 4 ++++ wcs/qommon/static/css/qommon.css | 4 ++++ wcs/root.py | 4 ++++ 5 files changed, 55 insertions(+) diff --git a/wcs/fields.py b/wcs/fields.py index c346203..aa9fbf6 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -1422,6 +1422,22 @@ class RankedItemsField(WidgetField): register_field_class(RankedItemsField) +class MapField(WidgetField): + key = 'map' + description = N_('Map') + + widget_class = MapWidget + + def get_view_value(self, value): + widget = self.widget_class('x', value, readonly=True) + return widget.render_content() + + def get_rst_view_value(self, value, indent=''): + return indent + value + +register_field_class(MapField) + + class PasswordField(WidgetField): key = 'password' description = N_('Password') diff --git a/wcs/qommon/form.py b/wcs/qommon/form.py index d55fb4b..d3554ea 100644 --- a/wcs/qommon/form.py +++ b/wcs/qommon/form.py @@ -1747,3 +1747,30 @@ class PasswordEntryWidget(CompositeWidget): self.value = hashlib.sha1(pwd1).hexdigest() else: self.value = None + + +class MapWidget(CompositeWidget): + def __init__(self, name, value=None, **kwargs): + CompositeWidget.__init__(self, name, value, **kwargs) + self.add(HiddenWidget, 'latlng') + self.readonly = kwargs.pop('readonly', False) + + def render_content(self): + get_response().add_javascript(['qommon.map.js']) + r = TemplateIO(html=True) + for widget in self.get_widgets(): + r += widget.render() + attrs = { + 'class': 'qommon-map', + 'id': 'map-%s' % self.name, + } + if self.value: + attrs['data-init-lat'], attrs['data-init-lng'] = self.value.split(';') + if self.readonly: + attrs['data-readonly'] = 'true' + r += htmltext('
' % ' '.join(['%s="%s"' % x for x in attrs.items()])) + return r.getvalue() + + def _parse(self, request): + CompositeWidget._parse(self, request) + self.value = self.get('latlng') diff --git a/wcs/qommon/http_response.py b/wcs/qommon/http_response.py index 6d8e2ab..a13bcb9 100644 --- a/wcs/qommon/http_response.py +++ b/wcs/qommon/http_response.py @@ -61,6 +61,10 @@ class HTTPResponse(quixote.http_response.HTTPResponse): self.javascript_scripts = [] for script_name in script_names: if not script_name in self.javascript_scripts: + if script_name == 'qommon.map.js': + self.add_javascript(['jquery.js']) + self.add_javascript(['../../leaflet/leaflet.js']) + self.add_css_include('../../leaflet/leaflet.css') self.javascript_scripts.append(str(script_name)) if script_name == 'afterjob.js': self.add_javascript_code('var QOMMON_ROOT_URL = "%s";\n' % \ diff --git a/wcs/qommon/static/css/qommon.css b/wcs/qommon/static/css/qommon.css index a753050..c6373e0 100644 --- a/wcs/qommon/static/css/qommon.css +++ b/wcs/qommon/static/css/qommon.css @@ -389,3 +389,7 @@ ul.select2-results { outline: 0; border: 1px solid #aaa; } + +div.qommon-map { + height: 280px; +} diff --git a/wcs/root.py b/wcs/root.py index dcb8d94..ce1c1a5 100644 --- a/wcs/root.py +++ b/wcs/root.py @@ -359,6 +359,10 @@ class RootDirectory(Directory): dirname = os.path.join(get_publisher().data_dir, 'qommon') return StaticDirectory(dirname, follow_symlinks = True) + # maps to locations provided by libjs-openlayers + if component == 'leaflet': + return StaticDirectory('/usr/share/javascript/leaflet') + # is this a category ? try: category = Category.get_by_urlname(component) -- 2.1.1