Permalink
Browse files

outsource tag conversion to osgende

This adds a dependency on osgende for the website part as well.
  • Loading branch information...
1 parent 2522a4b commit ec16aebf0f313d2e899a2b81fd0c1ce0c3a0cd8e @lonvia lonvia committed May 6, 2012
@@ -20,6 +20,8 @@
from django.db import connection, transaction
import routemap.util.tagformat as tagformat
+from osgende.tags import TagStore
+
class RouteTableModel(models.Model):
"""Generalized model for table with route information.
"""
@@ -36,58 +38,10 @@ def tags(self):
cursor = connection.cursor()
cursor.execute("SELECT tags FROM relations WHERE id = %s", (self.id,))
ret = cursor.fetchone()
- self._tags = ret[0] if ret is not None else None
+ self._tags = TagStore(ret[0] if ret is not None else {})
return self._tags
- def get_formatted_tags(self, locales):
- """Return a transformed taglist that localizes tags
- according to the given language preferences and
- standardize some other tags.
-
- Current list of standardized tags:
- website, wikipedia
- """
- ret = self.make_localized_tags(locales)
- tags = self.tags()
-
- # special case wikipedia, standard is :en
- if 'wikipedia' not in ret and 'wikipedia:en' in tags:
- ret['wikipedia'] = tags['wikipedia:en']
-
- # make a correct website link
- if 'url' in ret:
- ret['website'] = ret['url']
-
- if 'website' in ret:
- if ret['website'].find(':') < 0:
- ret['website'] = 'http://%s' % ( ret['website'])
-
- return ret
-
- def make_localized_tags(self, locales):
- """Returns the list of tags localized according to the
- language preferences given in locales.
-
- locales must be a hash where the keys are language
- codes and the values are weights.
- """
- ret = {}
- tagweights = {}
- for k,v in self.tags().iteritems():
- idx = k.find(':')
- if idx > 0 and k[idx+1:] in locales:
- outkey = k[:idx]
- w = locales[k[idx+1:]]
- if outkey not in ret or w > tagweights[outkey]:
- ret[outkey] = v
- tagweights[outkey] = w
- else:
- if k not in ret:
- ret[k] = v
- tagweights[k] = -1000.0
- return ret
-
def localize_name(self, locstrings):
for locstring in locstrings:
if locstring in self.intnames:
@@ -101,12 +55,6 @@ def localize_name(self, locstrings):
if 'note' in t:
self.origname = t['note']
- def distance_km(self):
- tags = self.tags()
- dist = tags.get('distance', tags.get('length'))
- return tagformat.convert_to_km(dist)
-
-
def subroutes(self, locales=[]):
"""Returns parent routes of the relation as
a list of triples (id, name, intnames)
@@ -1,45 +0,0 @@
-# This file is part of the Waymarked Trails Map Project
-# Copyright (C) 2011-2012 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.
-
-"""
-Helper functions to format the OSM tag values.
-"""
-
-import re
-
-unit_re = re.compile("\s*(\d+)([.,](\d+))?\s*([a-zA-Z]*)")
-
-def convert_to_km(value):
- """Take an OSM value and make the best guess at a
- conversion into kilometers.
- """
- if value is not None:
- m = unit_re.match(value)
- if m is not None:
- if m.group(3) is None:
- mag = float(m.group(1))
- else:
- mag = float('%s.%s' % (m.group(1), m.group(3)))
- unit = m.group(4).lower()
- if unit == '' or unit == 'km':
- return mag
- elif unit == 'm':
- return mag/1000
- elif unit == 'mi':
- return mag*1.6093
-
- return None
@@ -109,12 +109,12 @@ def info(request, route_id=None, manager=None):
except:
return direct_to_template(request, 'routes/info_error.html', {'id' : route_id})
- loctags = rel.get_formatted_tags(langdict)
+ loctags = rel.tags().get_localized_tagstore(langdict)
# Translators: The length of a route is presented with two values, this is the
# length that has been mapped so far and is actually visible on the map.
infobox = [(_("Displayed on map"), _("%d km") % rel.length)]
- dist = rel.distance_km()
+ dist = loctags.get_as_length(('distance', 'length'), unit='km')
if dist:
# Translators: The length of a route is presented with two values, this is the
# length given in the information about the route.
@@ -125,8 +125,6 @@ def info(request, route_id=None, manager=None):
# Translators: This is someone responsible for maintaining the route. Normally
# an organisation. Read more: http://wiki.openstreetmap.org/wiki/Key:operator
infobox.append((_("Operator"), loctags['operator']))
- if 'website' in loctags:
- infobox.append((_("Links"), '<a href="%s">%s</a>' % (loctags['website'], _('WWW'))))
return direct_to_template(request, 'routes/info.html',
{'route': rel,
@@ -33,6 +33,25 @@ <h1><div class="subtitle"><img src="{{ MEDIA_URL }}/{{ symbolpath }}/{{route.sym
{% endfor %}
</div>
+{% with loctags.get_url as wwwurl %}
+{% with loctags.get_wikipedia_url as wikiurl %}
+{% if wwwurl or wikiurl %}
+<div class="dllist"><div class='dlline'>
+<span class='dlitem'>{% trans "Links:" %}</span>
+{% if wwwurl %}
+<span class='dllink'>
+ <a style="font-size:12pt" href="{{wwwurl|safe}}">{% trans "WWW" %}</a>
+</span>
+{% endif %}
+{% if wikiurl %}
+<span class='dllink'>
+ <a style="font-size:12pt" href="{{wikiurl|safe}}">{% trans "Wikipedia" %}</a>
+</span>
+{% endif %}
+</div></div>
+{% endif %}{% endwith %}{% endwith %}
+
+
{% if subroutes %}<h2>{% trans 'Sections' %}</h2>
<ul>
@@ -236,6 +236,10 @@ a:hover {
padding-right: 1ex
}
+.dllink {
+ padding-right: 1ex
+}
+
#routeselector {
position: absolute;
background-color: #ecece9;

0 comments on commit ec16aeb

Please sign in to comment.