From: philipp Date: Sun, 6 Feb 2011 22:52:03 +0000 (+0000) Subject: Major update: Use wrpylib. X-Git-Url: https://git.toastfreeware.priv.at/philipp/winterrodeln/wradmin.git/commitdiff_plain/5324d810733a97057bde002cd1baf8d1b632bdd0 Major update: Use wrpylib. git-svn-id: http://www.winterrodeln.org/svn/servermediawiki/trunk/wradmin@758 7aebc617-e5e2-0310-91dc-80fb5f6d2477 --- diff --git a/wradmin/setup.py b/wradmin/setup.py index e6e37f2..86083ae 100644 --- a/wradmin/setup.py +++ b/wradmin/setup.py @@ -7,7 +7,7 @@ except ImportError: setup( name='wradmin', - version='1.3.3', + version='1.4.0', description='Administrative Interface for Winterrodeln', author='Philipp Spitzer', author_email='philipp.spitzer@winterrodeln.org', @@ -20,7 +20,8 @@ setup( "AuthKit>=0.4.3,<=0.4.99", "lxml>=2.2", # "matplotlib>=0.9", - "Babel>=0.9" + "Babel>=0.9", + "wrpylib>=0.0.1" ], setup_requires=["PasteScript>=1.6.3"], packages=find_packages(exclude=['ez_setup']), diff --git a/wradmin/wradmin/controllers/bericht.py b/wradmin/wradmin/controllers/bericht.py index a4464fa..fcf81f6 100644 --- a/wradmin/wradmin/controllers/bericht.py +++ b/wradmin/wradmin/controllers/bericht.py @@ -12,7 +12,7 @@ import wradmin.model as model import sqlalchemy as sa import datetime import formencode -import wradmin.model.validators +import wrpylib.wrvalidators log = logging.getLogger(__name__) @@ -33,7 +33,7 @@ class ChangeDateInvalidForm(formencode.Schema): allow_extra_fields = True filter_extra_fields = True date_invalid = formencode.validators.OneOf(['no_change', 'tomorrow', 'one_week', 'two_weeks', 'one_week_more', 'end_of_saison', 'now', 'userdefined'], not_empty=True) - date_userdefined = wradmin.model.validators.DateTimeNoSec + date_userdefined = wrpylib.wrvalidators.DateTimeNoSec chained_validators = [RequireDateIfUserDefined()] diff --git a/wradmin/wradmin/controllers/coordtool.py b/wradmin/wradmin/controllers/coordtool.py index b99e4d3..e8180ee 100644 --- a/wradmin/wradmin/controllers/coordtool.py +++ b/wradmin/wradmin/controllers/coordtool.py @@ -8,7 +8,7 @@ from pylons.controllers.util import abort, redirect import formencode from wradmin.lib.base import BaseController, render -import wradmin.model.validators +import wrpylib.wrvalidators log = logging.getLogger(__name__) @@ -33,7 +33,7 @@ class CoordtoolController(BaseController): c.result = None return redirect(url(controller='coordtool', action='index')) - geo = wradmin.model.validators.MultiGeo() + geo = wrpylib.wrvalidators.MultiGeo() try: c.result = geo.to_python(input) except formencode.Invalid, e: session['flash'] = unicode(e) @@ -46,9 +46,9 @@ class CoordtoolController(BaseController): if no_elevation: c.result = [(longitude, latitude, None) for (longitude, latitude, elevation) in c.result] - c.geo_winterrodeln = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_WINTERRODELN) - c.geo_gmapplugin = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GMAPPLUGIN) - c.geo_gpx = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GPX) - c.geo_geocaching = wradmin.model.validators.MultiGeo(output_format = geo.FORMAT_GEOCACHING) + c.geo_winterrodeln = wrpylib.wrvalidators.MultiGeo(output_format = geo.FORMAT_WINTERRODELN) + c.geo_gmapplugin = wrpylib.wrvalidators.MultiGeo(output_format = geo.FORMAT_GMAPPLUGIN) + c.geo_gpx = wrpylib.wrvalidators.MultiGeo(output_format = geo.FORMAT_GPX) + c.geo_geocaching = wrpylib.wrvalidators.MultiGeo(output_format = geo.FORMAT_GEOCACHING) return render('coordtool.html') diff --git a/wradmin/wradmin/controllers/gasthaus.py b/wradmin/wradmin/controllers/gasthaus.py index 6f95d7b..fcbea1d 100644 --- a/wradmin/wradmin/controllers/gasthaus.py +++ b/wradmin/wradmin/controllers/gasthaus.py @@ -11,7 +11,8 @@ import wradmin.model as model import sqlalchemy as sa import formencode import re -from wradmin.lib.mediawiki import wikipage_to_wrinncache, unicode_e + +import wrpylib.wrmwmarkup log = logging.getLogger(__name__) @@ -33,44 +34,6 @@ class GasthausController(BaseController): return render('gasthaus_view.html') - # def _wikipage_to_wrinncache(self, inn_wiki): - # "Converts an inn wiki page to an inn wrinncache database record." - # inn = model.WrInnCache() - # inn.page_id = inn_wiki.page_id - # inn.page_title = to_title(inn_wiki.page_title) - # - # # Match Gasthausbox - # wikitext = inn_wiki.old_text - # regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL) - # match = regexp.search(wikitext) - # if not match: - # raise Exception(u"No 'Gasthausbox' found") - # box = match.group(1) - # - # # Process Gashausbox - # for property in box.split('|'): - # property = property.strip() - # if property == u'Gasthausbox': continue - # key_value = property.split('=') - # if len(key_value) != 2: - # raise Exception(u"Property '%s' has unexpected format" % key_value) - # key = key_value[0].strip() - # value = key_value[1].strip() - # if key == u'Gasthausnummer': pass - # elif key == u'E-Mail': inn.email = conv(to_email, value, u'E-Mail') - # elif key == u'Homepage': inn.homepage = conv(to_url, value, u'Homepage') - # elif key == u'Höhe': inn.height = conv(to_unsigned, value, u'Höhe') - # elif key == u'Bild': inn.image = value - # elif key == u'Position': (inn.position_latitude, inn.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E' - # elif key == u'Telefon (Festnetz)': inn.phone = conv(to_phone, value, u'Telefon (Festnetz)') - # elif key == u'Telefon (Mobil)': inn.mobile_phone = conv(to_phone, value, u'Telefon (Mobil)') - # elif key == u'Rauchfrei': (inn.nonsmoker_area, inn.smoker_area) = conv(to_tristate, value, u'Rauchfrei') - # elif key == u'Aufnahmedatum': inn.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15' - # else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None) - # inn.under_construction = None - # return inn - - def update(self): "Updates the wrinncache table from the wiki" from wradmin.model import page_table as page, wrinncache_table as wrinncache, categorylinks_table as categorylinks, revision_table as revision, text_table as text @@ -85,13 +48,15 @@ class GasthausController(BaseController): c.execute(wrinncache.delete()) # Refill wrinncache table - error_msg = '' - for inn in inn_pages: + error_msg = u'' + for inn_page in inn_pages: try: - inn = wikipage_to_wrinncache(inn.page_id, inn.page_title, inn.old_text) - inn.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==inn.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above + start, end, inn = wrpylib.wrmwmarkup.gasthausbox_to_inn(inn_page.old_text, model.WrInnCache()) + inn.page_id = inn_page.page_id + inn.page_title = inn_page.page_title + inn.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==inn_page.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above model.meta.Session.add(inn) - except (RuntimeError, formencode.Invalid) as e: error_msg = u"Fehler bei Gasthaus '%s': " % inn.page_title + unicode_e(e) + except (RuntimeError, formencode.Invalid) as e: error_msg = u"Fehler bei Gasthaus '%s': " % inn_page.page_title + unicode(e) model.meta.Session.commit() # Redirect to result page diff --git a/wradmin/wradmin/controllers/rodelbahn.py b/wradmin/wradmin/controllers/rodelbahn.py index 46dbba4..8f571a0 100644 --- a/wradmin/wradmin/controllers/rodelbahn.py +++ b/wradmin/wradmin/controllers/rodelbahn.py @@ -11,7 +11,8 @@ import wradmin.model as model import sqlalchemy as sa import formencode import re -from wradmin.lib.mediawiki import wikipage_to_wrsleddingcache, unicode_e + +import wrpylib.wrmwmarkup from weberror import collector # Prevent a bug of weberror # See https://bitbucket.org/bbangert/weberror/issue/3/nameerror-global-name-fallback_encoding-is-not collector.FALLBACK_ENCODING = 'utf-8' # --"-- @@ -26,15 +27,15 @@ class RodelbahnController(BaseController): def list(self): "Lists all sledding routes" - q = model.meta.Session.query(model.WrSleddingCache) - q = q.order_by(model.WrSleddingCache.page_title) + q = model.meta.Session.query(model.WrSledrunCache) + q = q.order_by(model.WrSledrunCache.page_title) c.paginator = paginate.Page(q, page=int(request.params.get('page', 1)), items_per_page = 25) return render('rodelbahn_list.html') def view(self, id): "Displays a sledding route" - q = model.meta.Session.query(model.WrSleddingCache) + q = model.meta.Session.query(model.WrSledrunCache) c.sledding = q.get(id) if c.sledding is None: abort(404) q = model.meta.Session.query(model.WrReport) @@ -43,62 +44,9 @@ class RodelbahnController(BaseController): return render('rodelbahn_view.html') - # def _wikipage_to_wrsleddingcache(self, sledding_wiki): - # "Converts a sledding route wiki page to a sledding route wrsleddingcache database record." - # # TODO: Use mediawiki.wikipage_to_wrsleddingcache - # - # - # sl = model.WrSleddingCache() - # sl.page_id = sledding_wiki.page_id - # sl.page_title = to_title(sledding_wiki.page_title) - # - # # Match Rodelbahnbox - # wikitext = sledding_wiki.old_text - # regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL) - # match = regexp.search(wikitext) - # if not match: - # raise Exception(u"No 'Rodelbahnbox' found") - # box = match.group(1) - # - # # Process Rodelbahnbox - # for property in box.split('|'): - # property = property.strip() - # if property == u'Rodelbahnbox': continue - # key_value = property.split('=') - # if len(key_value) != 2: - # raise Exception(u"Property '%s' has unexpected format" % key_value) - # key = key_value[0].strip() - # value = key_value[1].strip() - # if key == u'Rodelbahnnummer': pass - # elif key == u'Länge': sl.length = conv(to_unsigned, value, u'Länge') - # elif key == u'Gehzeit': sl.walktime = conv(to_unsigned, value, u'Gehzeit') - # elif key == u'Höhe oben': sl.height_top = conv(to_unsigned, value, u'Höhe oben') - # elif key == u'Höhe unten': sl.height_bottom = conv(to_unsigned, value, u'Höhe unten') - # elif key == u'Aufstieg getrennt': sl.walkup_separate = conv(to_bool, value, u'Aufstieg getrennt') - # elif key == u'Lift': sl.lift = conv(to_bool, value, u'Lift') - # elif key == u'Beleuchtung': sl.night_light = conv(to_bool, value, u'Beleuchtung') - # elif key == u'Rodelverleih': sl.sledge_rental = conv(to_bool, value, u'Rodelverleih') - # elif key == u'Öffentliche Anreise': sl.public_transport = conv(to_bool, value, u'Öffentliche Anreise') - # elif key == u'Bild': sl.image = value - # elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(to_geo, value, u'Position') # '47.583333 N 15.75 E' - # elif key == u'Auskunft': sl.information = conv(to_phone_info, value, u'Auskunft') - # elif key == u'In Übersichtskarte': sl.show_in_overview = conv(to_bool, value, u'In Übersichtskarte') - # elif key == u'Aufnahmedatum': sl.creation_date = conv(to_date, value, u'Aufnahmedatum') # '2006-03-15' - # elif key == u'Lawinengefahr': - # if not value in [u'kaum', u'selten', u'gelegentlich', u'häufig']: raise formencode.Invalid(u"No valid value for 'Lawinengefahr': '%s'" % value, value, None) - # else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None) - # sl.under_construction = None - # - # # Match Forumlink (e.g. {{Forumlink|68}}) - # match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext) - # if match: sl.forum_id = match.group(1) - # - # return sl - - def update(self): - "Updates the wrsleddingcache table from the wiki" - from wradmin.model import page_table as page, wrsleddingcache_table as wrsleddingcache, categorylinks_table as categorylinks, revision_table as revision, text_table as text + "Updates the wrsledruncache table from the wiki" + from wradmin.model import page_table as page, wrsleddingcache_table1_3 as wrsledruncache, categorylinks_table as categorylinks, revision_table as revision, text_table as text from sqlalchemy.sql import select c = model.meta.Session.connection() @@ -110,17 +58,19 @@ class RodelbahnController(BaseController): # Original SQL: # sql = u"select page_id, rev_id, old_id, page_title, old_text, 'In_Arbeit' in (select cl_to from categorylinks where cl_from=page_id) as under_construction from page, revision, text, categorylinks where page_latest=rev_id and old_id=rev_text_id and cl_from=page_id and cl_to='Rodelbahn' order by page_title" - # Delete all existing entries in wrsleddingcache - if i == 1: c.execute(wrsleddingcache.delete()) + # Delete all existing entries in wrsledruncache + if i == 1: c.execute(wrsledruncache.delete()) - # Refill wrsleddingcache table + # Refill wrsledruncache table error_msg = u'' for sl in sledding_pages: try: - sl = wikipage_to_wrsleddingcache(sl.page_id, sl.page_title, sl.old_text) - sl.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==sl.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above - if i == 1: model.meta.Session.add(sl) - except (RuntimeError, formencode.Invalid) as e: error_msg = u"Fehler bei Rodelbahn '%s': " % sl.page_title + unicode_e(e) + start, end, sledrun = wrpylib.wrmwmarkup.rodelbahnbox_to_sledrun(sl.old_text, model.WrSledrunCache()) + sledrun.page_id = sl.page_id + sledrun.page_title = sl.page_title + sledrun.under_construction = c.execute(select([categorylinks], (categorylinks.c.cl_from==sl.page_id) & (categorylinks.c.cl_to == u'In_Arbeit')).alias('x').count()).fetchone()[0] > 0 # It would be better to do this in the query above + if i == 1: model.meta.Session.add(sledrun) + except (RuntimeError, formencode.Invalid) as e: error_msg = u"Fehler bei Rodelbahn '%s': " % sl.page_title + unicode(e) if i == 1: model.meta.Session.commit() # Redirect to result page diff --git a/wradmin/wradmin/lib/helpers.py b/wradmin/wradmin/lib/helpers.py index a5c4413..c2f5e92 100644 --- a/wradmin/wradmin/lib/helpers.py +++ b/wradmin/wradmin/lib/helpers.py @@ -7,7 +7,7 @@ available to Controllers. This module is available to templates as 'h'. #from webhelpers.html.tags import checkbox, password from pylons import url -import wradmin.model.validators +import wrpylib.wrvalidators from webhelpers.html.tags import file, form, end_form, submit def wiki(page_title=None): @@ -29,20 +29,20 @@ def google_maps(latitude, longitude): def bool(value): "Takes a bool value and creates a German representation" - return wradmin.model.validators.GermanBoolNone().from_python(value) + return wrpylib.wrvalidators.GermanBoolNone().from_python(value) def tristate_tuple(yes, no): "Takes a German representation of a tristate value" - return wradmin.model.validators.GermanTristateTuple().from_python((yes, no)) + return wrpylib.wrvalidators.GermanTristateTuple().from_python((yes, no)) def tristate_float(value): "Takes a German representation of a tristate value" - return wradmin.model.validators.GermanTristateFloat().from_python(value) + return wrpylib.wrvalidators.GermanTristateFloat().from_python(value) def public_transport(value): if isinstance(value, long): value = int(value) - return wradmin.model.validators.GermanPublicTransport().from_python(value) + return wrpylib.wrvalidators.GermanPublicTransport().from_python(value) diff --git a/wradmin/wradmin/lib/mediawiki.py b/wradmin/wradmin/lib/mediawiki.py index 30c8ba9..57e4390 100644 --- a/wradmin/wradmin/lib/mediawiki.py +++ b/wradmin/wradmin/lib/mediawiki.py @@ -15,406 +15,6 @@ import wradmin.model as model import wradmin.model.validators -# Converter functions -# ------------------- - -def to_title(value): - """Line 2237 of includes/Title.php says: $this->mTextform = str_replace( '_', ' ', $dbkey ); - No not check for None because a missing title is an error""" - return value.replace(u'_', u' ') - - -# deprecated -def to_phone_info(value): - return model.validators.PhoneInfo(messages={'phoneInfo': u"Bitte verwenden Sie ein Format wie '0512/123456 (Schnee Alm)'."}).to_python(value) - - -def conv(fnct, value, fieldname): - "Like one of the to_xxx functions (e.g. to_bool), but adds the field name to the error message" - try: return fnct(value) - except formencode.Invalid, e: raise formencode.Invalid(u"Conversion error in field '%s': %s" % (fieldname, unicode_e(e)), e.value, e.state) - - -def unicode_e(exception): - """Does "unicode(exception)" as it should be. This is a workaround for bug http://bugs.python.org/issue2517 - that is not fixed in python 2.5.2. - Details of bug: "unicode(Exception(u'\xe4'))" raises an UnicodeEncodeError exception.""" - #if exception.message: return unicode(exception.message) # this bug is already fixed. TODO: Cleanup calls to unicode_e - return unicode(exception) - - -def wikipage_to_wrsleddingcache1_2(page_id, page_title, page_text): - """Converts a wiki page about a sledding route to a wradmin.model.WrSleddingCache1_2 class - that can be inserted to the wradmin.model.wrsleddingcache1_2_table. - It needs the wiki page id, the wiki page title and the page text ("old_text") as they come from the database.""" - sl = model.WrSleddingCache1_2() - sl.page_id = page_id - sl.page_title = to_title(page_title) - - # Match Rodelbahnbox - wikitext = page_text - regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL) - match = regexp.search(wikitext) - if not match: - raise Exception(u"No 'Rodelbahnbox' found") - box = match.group(1) - - # Process Rodelbahnbox - for property in box.split('|'): - property = property.strip() - if property == u'Rodelbahnbox': continue - key_value = property.split('=') - if len(key_value) != 2: - raise Exception(u"Property '%s' has unexpected format" % key_value) - key = key_value[0].strip() - value = key_value[1].strip() - if key == u'Rodelbahnnummer': pass - elif key == u'Länge': sl.length = conv(model.validators.Unsigned().to_python, value, u'Länge') - elif key == u'Gehzeit': sl.walktime = conv(model.validators.Unsigned().to_python, value, u'Gehzeit') - elif key == u'Höhe oben': sl.height_top = conv(model.validators.Unsigned().to_python, value, u'Höhe oben') - elif key == u'Höhe unten': sl.height_bottom = conv(model.validators.Unsigned().to_python, value, u'Höhe unten') - elif key == u'Aufstieg getrennt': sl.walkup_separate = conv(model.validators.GermanBoolNone().to_python, value, u'Aufstieg getrennt') - elif key == u'Lift': sl.lift = conv(model.validators.GermanBoolNone().to_python, value, u'Lift') - elif key == u'Beleuchtung': sl.night_light = conv(model.validators.GermanBoolNone().to_python, value, u'Beleuchtung') - elif key == u'Rodelverleih': sl.sledge_rental = conv(model.validators.GermanBoolNone().to_python, value, u'Rodelverleih') - elif key == u'Öffentliche Anreise': sl.public_transport = conv(model.validators.GermanBoolNone().to_python, value, u'Öffentliche Anreise') - elif key == u'Bild': sl.image = conv(model.validators.UnicodeNone().to_python, value, key) - elif key == u'Position': (sl.position_latitude, sl.position_longitude) = conv(model.validators.GeoNone().to_python, value, u'Position') # '47.583333 N 15.75 E' - elif key == u'Auskunft': sl.information = conv(model.validators.AustrianPhoneNumberCommentLoop().to_python, value, u'Auskunft') - elif key == u'In Übersichtskarte': sl.show_in_overview = conv(model.validators.GermanBoolNone().to_python, value, u'In Übersichtskarte') - elif key == u'Aufnahmedatum': sl.creation_date = conv(model.validators.DateNone().to_python, value, u'Aufnahmedatum') # '2006-03-15' - elif key == u'Lawinengefahr': - # sl.avalanches is not part of the 1.2 sleddingcache table. We store it in the WrSleddingCache1_2 anyway. - sl.avalanches = conv(model.validators.GermanAvalanches().to_python, value, key) - else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None) - sl.under_construction = None - - # Match Forumlink (e.g. {{Forumlink|68}}) - match = re.search(u"\{\{Forumlink\|(\d+)\}\}", wikitext) - if match: sl.forum_id = match.group(1) - - return sl - - -def wikipage_to_wrsleddingcache(page_id, page_title, page_text): - """Converts a sled-route wiki page (wradmin.model.page_table) - to a sledding route wrsleddingcache database record (wradmin.model.wrsleddingcache_table). - Raises a RuntimeError if the format is not OK - sledding_wiki is a column of tabe "page". - Returns the WrSleddingCache class""" - sl = model.WrSleddingCache() - sl.page_id = page_id - sl.page_title = to_title(page_title) - errors = [] # List of errors with localized messages - - # Match Rodelbahnbox - wikitext = page_text - regexp = re.compile(u"\{\{(Rodelbahnbox[^\}]*)\}\}", re.DOTALL) - match = regexp.search(wikitext) - if not match: - raise RuntimeError(u"Rodelbahnbox nicht gefunden") - box = match.group(1) - - # Process Rodelbahnbox - for property in box.split('|'): - property = property.strip() - if property == u'Rodelbahnbox': continue - equalsign_pos = property.find('=') - if equalsign_pos < 0: - raise RuntimeError(u"Die Eigenschaft '%s' hat ein unerwartetes Format." % property) - key = property[:equalsign_pos].strip() - value = property[equalsign_pos+1:].strip() - if key in [u'Rodelbahnnummer', u'Lift']: - errors.append("Eigenschaft '%s' wird nicht mehr unterstuetzt, siehe %s." % (key, 'http://www.winterrodeln.org/wiki/Vorlage:Rodelbahnbox')) - elif key == u'Position': sl.position_latitude, sl.position_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E' - elif key == u'Position oben': sl.top_latitude, sl.top_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E' - elif key == u'Höhe oben': sl.top_elevation = conv(model.validators.UnsignedNone().to_python, value, key) # '2000' - elif key == u'Position unten': sl.bottom_latitude, sl.bottom_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E' - elif key == u'Höhe unten': sl.bottom_elevation = conv(model.validators.UnsignedNone().to_python, value, key) # '1200' - elif key == u'Länge': sl.length = conv(model.validators.UnsignedNone().to_python, value, key) # 3500 - elif key == u'Schwierigkeit': sl.difficulty = conv(model.validators.GermanDifficulty().to_python, value, key) # 'mittel' - elif key == u'Lawinen': sl.avalanches = conv(model.validators.GermanAvalanches().to_python, value, key) # 'kaum' - elif key == u'Betreiber': sl.operator = conv(model.validators.UnicodeNone().to_python, value, key) # 'Max Mustermann' - elif key == u'Öffentliche Anreise': sl.public_transport = conv(model.validators.GermanPublicTransport().to_python, value, key) # 'Mittelmäßig' - elif key == u'Gehzeit': sl.walkup_time = conv(model.validators.UnsignedNone().to_python, value, key) # 90 - elif key == u'Aufstieg getrennt': sl.walkup_separate, sl.walkup_separate_comment = conv(model.validators.GermanTristateFloatComment().to_python, value, key) # 'Ja' - elif key == u'Aufstiegshilfe': sl.lift, sl.lift_details = conv(model.validators.GermanLift().to_python, value, key) # 'Gondel (unterer Teil)' - elif key == u'Beleuchtungsanlage': sl.night_light, sl.night_light_comment = conv(model.validators.GermanTristateFloatComment().to_python, value, key) - elif key == u'Beleuchtungstage': sl.night_light_days, sl.night_light_days_comment = conv(model.validators.UnsignedCommentNone(7).to_python, value, key) # '3 (Montag, Mittwoch, Freitag)' - elif key == u'Rodelverleih': sl.sled_rental, sl.sled_rental_comment = conv(model.validators.SledRental().to_python, value, key) # 'Talstation Serlesbahnan' - elif key == u'Gütesiegel': sl.cachet = conv(model.validators.GermanCachet().to_python, value, key) # 'Tiroler Naturrodelbahn-Gütesiegel 2009 mittel' - elif key == u'Webauskunft': sl.information_web = conv(model.validators.UrlNeinNone().to_python, value, key) # 'http://www.nösslachhütte.at/page9.php' - elif key == u'Telefonauskunft': sl.information_phone = conv(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=False).to_python, value, key) # '+43-664-5487520 (Mitterer Alm)' - elif key == u'Bild': sl.image = conv(model.validators.UnicodeNone().to_python, value, key) - elif key == u'In Übersichtskarte': sl.show_in_overview = conv(model.validators.GermanBoolNone().to_python, value, key) - elif key == u'Forumid': sl.forum_id = conv(model.validators.UnsignedNeinNone().to_python, value, key) - else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Rodelbahnbox: '%s' (mit Wert '%s')" % (key, value), value, None) - sl.under_construction = None - return sl - - -def wrSleddingCache1_2_to_WrSleddingCache(wrSleddingCache1_2): - """Converts the old WrSleddingCache format (1.2) WrSleddingCache1_2 - to the new format (1.3) WrSleddingCache.""" - wrSleddingCache = model.WrSleddingCache() # Create an object in the new format - wrSleddingCache.page_id = wrSleddingCache1_2.page_id - wrSleddingCache.page_title = wrSleddingCache1_2.page_id - wrSleddingCache.position_latitude = wrSleddingCache1_2.position_latitude - wrSleddingCache.position_longitude = wrSleddingCache1_2.position_longitude - wrSleddingCache.top_latitude = None - wrSleddingCache.top_longitude = None - wrSleddingCache.top_elevation = wrSleddingCache1_2.height_top - wrSleddingCache.bottom_latitude = None - wrSleddingCache.bottom_longitude = None - wrSleddingCache.bottom_elevation = wrSleddingCache1_2.height_bottom - wrSleddingCache.length = wrSleddingCache1_2.length - wrSleddingCache.difficulty = None - if 'avalanches' in dir(wrSleddingCache1_2): wrSleddingCache.avalanches = wrSleddingCache1_2.avalanches - else: wrSleddingCache.avalanches = None - wrSleddingCache.operator = None - if wrSleddingCache1_2.public_transport is None: wrSleddingCache.public_transport = None - else: wrSleddingCache.public_transport = 6 if wrSleddingCache1_2.public_transport else 5 - wrSleddingCache.walkup_time = wrSleddingCache1_2.walktime - if wrSleddingCache1_2.walkup_separate is None: wrSleddingCache.walkup_separate = None - wrSleddingCache.walkup_separate = 1.0 if wrSleddingCache1_2.walkup_separate else 0.0 - wrSleddingCache.walkup_separate_comment = None - wrSleddingCache.lift = wrSleddingCache1_2.lift - if wrSleddingCache1_2.lift is None: wrSleddingCache.lift_details = None - elif wrSleddingCache1_2.lift: wrSleddingCache.lift_details = "Sonstige" - else: wrSleddingCache.lift_details = None - if wrSleddingCache1_2.night_light is None: wrSleddingCache.night_light = None - else: wrSleddingCache.night_light = 1.0 if wrSleddingCache1_2.night_light else 0.0 - wrSleddingCache.night_light_comment = None - wrSleddingCache.night_light_days = None - wrSleddingCache.night_light_days_comment = None - wrSleddingCache.sled_rental = wrSleddingCache1_2.sledge_rental - if wrSleddingCache.sled_rental: wrSleddingCache.sled_rental_comment = u'Ja' - else: wrSleddingCache.sled_rental_comment = None - wrSleddingCache.cachet = None - wrSleddingCache.information_web = None - if wrSleddingCache1_2.information is None: wrSleddingCache.information_phone = None - else: - m = re.match('^([-\d/\+]{5,}) \((.+)\)', wrSleddingCache1_2.information) - if m is None: raise formencode.Invalid('PhoneInfo is invalid', value, None) - (phone, info) = m.groups() - # check phone - phone = wradmin.model.validators.AustrianPhoneNumber().to_python(phone) - # convert phone - c = formencode.national.InternationalPhoneNumber(default_cc=lambda: 43) - phone = c.to_python(phone) - wrSleddingCache.information_phone = '%s (%s)' % (phone, info) - wrSleddingCache.image = wrSleddingCache1_2.image - wrSleddingCache.show_in_overview = wrSleddingCache1_2.show_in_overview - wrSleddingCache.forum_id = wrSleddingCache1_2.forum_id - wrSleddingCache.under_construction = wrSleddingCache1_2.under_construction - return wrSleddingCache - - -def wrSleddingCache_to_Rodelbahnbox(wrSleddingCache): - """Converts the WrSleddingCache class to the {{Rodelbahnbox}} representation.""" - keys = [] - values = [] - keys.append(u'Position') - values.append(model.validators.GeoNone().from_python((wrSleddingCache.position_latitude, wrSleddingCache.position_longitude))) - keys.append(u'Position oben') - values.append(model.validators.GeoNone().from_python((wrSleddingCache.top_latitude, wrSleddingCache.top_longitude))) - keys.append(u'Höhe oben') - values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.top_elevation)) - keys.append(u'Position unten') - values.append(model.validators.GeoNone().from_python((wrSleddingCache.bottom_latitude, wrSleddingCache.bottom_longitude))) - keys.append(u'Höhe unten') - values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.bottom_elevation)) - keys.append(u'Länge') - values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.length)) - keys.append(u'Schwierigkeit') - values.append(model.validators.GermanDifficulty().from_python(wrSleddingCache.difficulty)) - keys.append(u'Lawinen') - values.append(model.validators.GermanAvalanches().from_python(wrSleddingCache.avalanches)) - keys.append(u'Betreiber') - values.append(model.validators.UnicodeNone().from_python(wrSleddingCache.operator)) - keys.append(u'Öffentliche Anreise') - values.append(model.validators.GermanPublicTransport().from_python(wrSleddingCache.public_transport)) - keys.append(u'Gehzeit') - values.append(model.validators.UnsignedNone().from_python(wrSleddingCache.walkup_time)) - keys.append(u'Aufstieg getrennt') - values.append(model.validators.GermanTristateFloatComment().from_python((wrSleddingCache.walkup_separate, wrSleddingCache.walkup_separate_comment))) - keys.append(u'Aufstiegshilfe') - values.append(model.validators.GermanLift().from_python((wrSleddingCache.lift, wrSleddingCache.lift_details))) - keys.append(u'Beleuchtungsanlage') - values.append(model.validators.GermanTristateFloatComment().from_python((wrSleddingCache.night_light, wrSleddingCache.night_light_comment))) - keys.append(u'Beleuchtungstage') - values.append(model.validators.UnsignedCommentNone(max=7).from_python((wrSleddingCache.night_light_days, wrSleddingCache.night_light_days_comment))) - keys.append(u'Rodelverleih') - values.append(model.validators.SledRental().from_python((wrSleddingCache.sled_rental, wrSleddingCache.sled_rental_comment))) - keys.append(u'Gütesiegel') - values.append(model.validators.GermanCachet().from_python(wrSleddingCache.cachet)) - keys.append(u'Webauskunft') - values.append(model.validators.UrlNeinNone().from_python(wrSleddingCache.information_web)) - keys.append(u'Telefonauskunft') - values.append(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=False).from_python(wrSleddingCache.information_phone)) - keys.append(u'Bild') - values.append(model.validators.UnicodeNone().from_python(wrSleddingCache.image)) - keys.append(u'In Übersichtskarte') - values.append(model.validators.GermanBoolNone().from_python(wrSleddingCache.show_in_overview)) - keys.append(u'Forumid') - values.append(model.validators.UnsignedNeinNone().from_python(wrSleddingCache.forum_id)) - result = [u'{{Rodelbahnbox'] - for i in xrange(len(keys)): result.append(u'| %-20s = %s' % (keys[i], values[i])) - result.append('}}') - return '\n'.join(result) - - -def wikipage_to_wrinncache1_2(page_id, page_title, page_text): - """Converts a wiki page about an inn to an wradmin.model.WrInnCache1_2 class - that can be inserted to the wradmin.model.wrinncache1_2_table. - It uses only text operations and does not query or update the database. - It needs the wiki page id, the wiki page title and the page text ("old_text") as they come from the database.""" - inn = model.WrInnCache1_2() - inn.page_id = page_id - inn.page_title = to_title(page_title) - - # Match Gasthausbox - wikitext = page_text - regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL) - match = regexp.search(wikitext) - if not match: - raise Exception(u"No 'Gasthausbox' found") - box = match.group(1) - - # Process Gashausbox - for property in box.split('|'): - property = property.strip() - if property == u'Gasthausbox': continue - key_value = property.split('=') - if len(key_value) != 2: - raise Exception(u"Property '%s' has unexpected format" % key_value) - key = key_value[0].strip() - value = key_value[1].strip() - if key == u'Gasthausnummer': pass - elif key == u'E-Mail': inn.email = conv(formencode.validators.Email().to_python, value, u'E-Mail') - elif key == u'Homepage': inn.homepage = conv(model.validators.UrlNeinNone().to_python, value, u'Homepage') - elif key == u'Höhe': inn.height = conv(model.validators.Unsigned().to_python, value, u'Höhe') - elif key == u'Bild': inn.image = conv(model.validators.UnicodeNone().to_python, value, key) - elif key == u'Position': inn.position_latitude, inn.position_longitude = conv(model.validators.GeoNone().to_python, value, u'Position') # '47.583333 N 15.75 E' - elif key == u'Telefon (Festnetz)': inn.phone = conv(model.validators.AustrianPhoneNumberNone().to_python, value, u'Telefon (Festnetz)') - elif key == u'Telefon (Mobil)': inn.mobile_phone = conv(model.validators.AustrianPhoneNumberNone().to_python, value, u'Telefon (Mobil)') - elif key == u'Rauchfrei': inn.nonsmoker_area, inn.smoker_area = conv(model.validators.GermanTristateTuple().to_python, value, u'Rauchfrei') - elif key == u'Aufnahmedatum': inn.creation_date = conv(model.validators.DateNone().to_python, value, u'Aufnahmedatum') # '2006-03-15' - else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None) - inn.under_construction = None - return inn - - -def wikipage_to_wrinncache(page_id, page_title, page_text): - """Converts a inn wiki page (wradmin.model.page_table) to a wrinncache database record - (wradmin.model.wrinncache_table).""" - inn = model.WrInnCache() - inn.page_id = page_id - inn.page_title = to_title(page_title) - - # Match Gasthausbox - wikitext = page_text - regexp = re.compile(u"\{\{(Gasthausbox[^\}]*)\}\}", re.DOTALL) - match = regexp.search(wikitext) - if not match: - raise Exception(u"No 'Gasthausbox' found") - box = match.group(1) - - # Process Gashausbox - for property in box.split('|'): - property = property.strip() - if property == u'Gasthausbox': continue - key_value = property.split('=') - if len(key_value) != 2: - raise Exception(u"Property '%s' has unexpected format" % key_value) - key = key_value[0].strip() - value = key_value[1].strip() - if key == u'Position': inn.position_latitude, inn.position_longitude = conv(model.validators.GeoNone().to_python, value, key) # '47.583333 N 15.75 E' - elif key == u'Höhe': inn.position_elevation = conv(model.validators.UnsignedNone().to_python, value, key) - elif key == u'Betreiber': inn.operator = conv(model.validators.UnicodeNone().to_python, value, key) - elif key == u'Sitzplätze': inn.seats = conv(model.validators.UnsignedNone().to_python, value, key) - elif key == u'Übernachtung': inn.overnight, inn.overnight_comment = conv(model.validators.BoolUnicodeTupleValidator().to_python, value, key) - elif key == u'Rauchfrei': inn.nonsmoker_area, inn.smoker_area = conv(model.validators.GermanTristateTuple().to_python, value, key) - elif key == u'Rodelverleih': inn.sled_rental, inn.sled_rental_comment = conv(model.validators.BoolUnicodeTupleValidator().to_python, value, key) - elif key == u'Handyempfang': inn.mobile_provider = conv(model.validators.ValueCommentListNeinLoopNone().to_python, value, key) - elif key == u'Homepage': inn.homepage = conv(model.validators.UrlNeinNone().to_python, value, key) - elif key == u'E-Mail': inn.email_list = conv(model.validators.EmailCommentListNeinLoopNone().to_python, value, key) - elif key == u'Telefon': inn.phone_list = conv(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=True).to_python, value, key) - elif key == u'Bild': inn.image = conv(model.validators.UnicodeNone().to_python, value, key) - elif key == u'Rodelbahnen': inn.sledding_list = conv(model.validators.WikiPageListLoopNone().to_python, value, key) - else: raise formencode.Invalid(u"Unbekannte Eigenschaft der Gasthausbox: '%s' (mit Wert '%s')" % (key, value), value, None) - inn.under_construction = None - return inn - - -def wrInnCache1_2_to_WrInnCache(wrInnCache1_2): - """Converts the old WrInnCache format to the new format.""" - wrInnCache = model.WrInnCache() # Create an object in the new format - wrInnCache.page_id = wrInnCache1_2.page_id - wrInnCache.page_title = wrInnCache1_2.page_id - wrInnCache.position_latitude = wrInnCache1_2.position_latitude - wrInnCache.position_longitude = wrInnCache1_2.position_longitude - wrInnCache.position_elevation = wrInnCache1_2.height - wrInnCache.operator = None - wrInnCache.seats = None - wrInnCache.overnight = None - wrInnCache.overnight_comment = None - wrInnCache.smoker_area = wrInnCache1_2.smoker_area - wrInnCache.nonsmoker_area = wrInnCache1_2.nonsmoker_area - wrInnCache.sled_rental = None - wrInnCache.sled_rental_comment = None - wrInnCache.mobile_provider = None - wrInnCache.homepage = wrInnCache1_2.homepage - wrInnCache.email_list = wrInnCache1_2.email - phone_list = [] - c = formencode.national.InternationalPhoneNumber(default_cc=lambda: 43) - if not wrInnCache1_2.phone is None: phone_list.append(c.to_python(wrInnCache1_2.phone)) - if not wrInnCache1_2.mobile_phone is None: phone_list.append(c.to_python(wrInnCache1_2.mobile_phone)) - if len(phone_list) >= 1: wrInnCache.phone_list = "; ".join(phone_list) - else: phone_list = None - wrInnCache.image = wrInnCache1_2.image - wrInnCache.sledding_list = None - wrInnCache.under_construction = wrInnCache1_2.under_construction - return wrInnCache - - -def wrInnCache_to_Gasthausbox(wrInnCache): - """Converts the WrInnCache class to the {{Gasthausbox}} representation.""" - keys = [] - values = [] - keys.append(u'Position') - values.append(model.validators.GeoNone().from_python((wrInnCache.position_latitude, wrInnCache.position_longitude))) - keys.append(u'Höhe') - values.append(model.validators.UnsignedNone().from_python(wrInnCache.position_elevation)) - keys.append(u'Betreiber') - values.append(model.validators.UnicodeNone().from_python(wrInnCache.operator)) - keys.append(u'Sitzplätze') - values.append(model.validators.UnsignedNone().from_python(wrInnCache.seats)) - keys.append(u'Übernachtung') - values.append(model.validators.BoolUnicodeTupleValidator().from_python((wrInnCache.overnight, wrInnCache.overnight_comment))) - keys.append(u'Rauchfrei') - values.append(model.validators.GermanTristateTuple().from_python((wrInnCache.nonsmoker_area, wrInnCache.smoker_area))) - keys.append(u'Rodelverleih') - values.append(model.validators.BoolUnicodeTupleValidator().from_python((wrInnCache.sled_rental, wrInnCache.sled_rental_comment))) - keys.append(u'Handyempfang') - values.append(model.validators.ValueCommentListNeinLoopNone().from_python(wrInnCache.mobile_provider)) - keys.append(u'Homepage') - values.append(model.validators.UrlNeinNone().from_python(wrInnCache.homepage)) - keys.append(u'E-Mail') - values.append(model.validators.EmailCommentListNeinLoopNone().from_python(wrInnCache.email_list)) - keys.append(u'Telefon') - values.append(model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=True).from_python(wrInnCache.phone_list)) - keys.append(u'Bild') - values.append(model.validators.UnicodeNone().from_python(wrInnCache.image)) - keys.append(u'Rodelbahnen') - values.append(model.validators.WikiPageListLoopNone().from_python(wrInnCache.sledding_list)) - result = [u'{{Gasthausbox'] - for i in xrange(len(keys)): result.append(u'| %-17s = %s' % (keys[i], values[i])) - result.append('}}\n') - return '\n'.join(result) - - - # User management # --------------- diff --git a/wradmin/wradmin/model/__init__.py b/wradmin/wradmin/model/__init__.py index 4ae923f..2cf9f5f 100644 --- a/wradmin/wradmin/model/__init__.py +++ b/wradmin/wradmin/model/__init__.py @@ -13,6 +13,7 @@ def init_model(engine): # Current table definition # - version 1.2 # - version 1.3 (no changes) +# - version 1.4 (no changes) wrreport_table = sa.Table("wrreport", meta.metadata, sa.Column("id", types.Integer, primary_key=True), sa.Column("page_id", types.Integer, schema.ForeignKey('wrsleddingcache.page_id')), @@ -59,9 +60,49 @@ wrsleddingcache_table1_2 = sa.Table("wrsleddingcache1_2", meta.metadata, ) -# Current table definition +# Old table definition # - version 1.3 (changes made from version 1.2) -wrsleddingcache_table = sa.Table("wrsleddingcache", meta.metadata, +# wrsleddingcache_table1_3 = sa.Table("wrsleddingcache1_3", meta.metadata, +wrsleddingcache_table1_3 = sa.Table("wrsleddingcache", meta.metadata, + sa.Column("page_id", types.Integer, primary_key=True), + sa.Column("page_title", types.Unicode(255)), + sa.Column("position_latitude", types.Float), + sa.Column("position_longitude", types.Float), + sa.Column("top_latitude", types.Float), + sa.Column("top_longitude", types.Float), + sa.Column("top_elevation", types.Integer), + sa.Column("bottom_latitude", types.Float), + sa.Column("bottom_longitude", types.Float), + sa.Column("bottom_elevation", types.Integer), + sa.Column("length", types.Integer), + sa.Column("difficulty", types.Integer), + sa.Column("avalanches", types.Integer), + sa.Column("operator", types.Unicode(255)), + sa.Column("public_transport", types.Integer), + sa.Column("walkup_time", types.Integer), + sa.Column("walkup_separate", types.Float), + sa.Column("walkup_separate_comment", types.Unicode(255)), + sa.Column("lift", types.Boolean), + sa.Column("lift_details", types.Unicode(255)), + sa.Column("night_light", types.Float), + sa.Column("night_light_comment", types.Unicode(255)), + sa.Column("night_light_days", types.Integer), + sa.Column("night_light_days_comment", types.Unicode(255)), + sa.Column("sled_rental", types.Boolean), + sa.Column("sled_rental_comment", types.Unicode(255)), + sa.Column("cachet", types.Unicode(255)), + sa.Column("information_web", types.Unicode(255)), + sa.Column("information_phone", types.Unicode(255)), + sa.Column("image", types.Unicode(255)), + sa.Column("show_in_overview", types.Boolean), + sa.Column("forum_id", types.Integer), + sa.Column("under_construction", types.Boolean), + ) + + +# Current table definition +# - version 1.4 (renamed table and added column walkup_possible) +wrsledruncache_table = sa.Table("wrsledruncache", meta.metadata, sa.Column("page_id", types.Integer, primary_key=True), sa.Column("page_title", types.Unicode(255)), sa.Column("position_latitude", types.Float), @@ -77,6 +118,7 @@ wrsleddingcache_table = sa.Table("wrsleddingcache", meta.metadata, sa.Column("avalanches", types.Integer), sa.Column("operator", types.Unicode(255)), sa.Column("public_transport", types.Integer), + sa.Column("walkup_possible", types.Boolean), sa.Column("walkup_time", types.Integer), sa.Column("walkup_separate", types.Float), sa.Column("walkup_separate_comment", types.Unicode(255)), @@ -119,6 +161,7 @@ wrinncache_table1_2 = sa.Table("wrinncache1_2", meta.metadata, # Current table definition # - version 1.3 (changes made from version 1.2) +# - version 1.4 (no changes) wrinncache_table = sa.Table("wrinncache", meta.metadata, sa.Column("page_id", types.Integer, primary_key=True), sa.Column("page_title", types.Unicode(255)), @@ -196,7 +239,7 @@ class WrSleddingCache1_2(object): pass -class WrSleddingCache(object): +class WrSledrunCache(object): pass @@ -216,7 +259,7 @@ class Page(object): orm.mapper(WrReport, wrreport_table) # We could add a relation but we don't need it yet: -# orm.mapper(WrSleddingCache, wrsleddingcache_table, properties = {'reports': orm.relation(WrReport, backref='sledding')}) -orm.mapper(WrSleddingCache, wrsleddingcache_table) +# orm.mapper(WrSledrunCache, wrsledruncache_table, properties = {'reports': orm.relation(WrReport, backref='sledding')}) +orm.mapper(WrSledrunCache, wrsleddingcache_table1_3) # TODO: Change to wrsledruncache_table orm.mapper(WrInnCache, wrinncache_table) diff --git a/wradmin/wradmin/model/validators.py b/wradmin/wradmin/model/validators.py index 72c4ac5..0561495 100644 --- a/wradmin/wradmin/model/validators.py +++ b/wradmin/wradmin/model/validators.py @@ -1,9 +1,12 @@ +#!/usr/bin/python2.6 # -*- coding: iso-8859-15 -*- """This file contains "validators" that convert between string and python (database) representation of properties used in the "Rodelbahnbox" and "Gasthausbox". The "to_python" method has to get a unicode argument. You can run tests with >>> nosetests --with-pylons=test.ini + +TODO: Replace by wrpylib.wrvalidators """ import formencode import formencode.national diff --git a/wradmin/wradmin/tests/test_models.py b/wradmin/wradmin/tests/test_models.py index 35fbbce..9de7cfc 100644 --- a/wradmin/wradmin/tests/test_models.py +++ b/wradmin/wradmin/tests/test_models.py @@ -1,146 +1,4 @@ # -*- coding: iso-8859-15 -*- -import wradmin.model.validators -import formencode +# No tests yet. -def test_NoneValidator(): - v = wradmin.model.validators.NoneValidator(wradmin.model.validators.Unicode()) - assert v.to_python(u'') == None - assert v.from_python(None) == u'' - - -def test_UnsignedNone(): - v = wradmin.model.validators.UnsignedNone() - assert v.to_python(u'42') == 42 - assert v.to_python(u'') == None - assert v.from_python(42) == u'42' - assert v.from_python(None) == u'' - - -def test_GeoNone(): - coord = u'47.076207 N 11.453553 E' - v = wradmin.model.validators.GeoNone() - (lat, lon) = v.to_python(coord) - assert lat == 47.076207 - assert lon == 11.453553 - assert v.to_python(u'') == (None, None) - - assert v.from_python((lat, lon)) == coord - assert v.from_python((None, None)) == u'' - - -def test_GermanTristateTuple(): - v = wradmin.model.validators.GermanTristateTuple() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Ja') == (True, False) - assert v.to_python(u'Nein') == (False, True) - assert v.to_python(u'Teilweise') == (True, True) - assert v.from_python((None, None)) == u'' - assert v.from_python((False, True)) == u'Nein' - assert v.from_python((True, False)) == u'Ja' - assert v.from_python((True, True)) == u'Teilweise' - - -def tes_GermanTristateFloat(): - v = wradmin.model.validators.GermanTristateFloat() - assert v.to_python(u'') == None - assert v.to_python(u'Ja') == 1.0 - assert v.to_python(u'Nein') == 0.0 - assert v.to_python(u'Teilweise') == 0.5 - assert v.from_python(None) == u'' - assert v.from_python(0.0) == u'Nein' - assert v.from_python(1.0) == u'Ja' - assert v.from_python(0.5) == u'Teilweise' - - -def test_ValueCommentList(): - v = wradmin.model.validators.ValueCommentList() - assert v.to_python(u'abc') == [(u'abc', None)] - assert v.to_python(u'abc def') == [(u'abc def', None)] - assert v.to_python(u'value (comment)') == [(u'value', u'comment')] - assert v.to_python(u'value (comment)') == [(u'value', u'comment')] - assert v.to_python(u'value1 (comment); value2') == [(u'value1', u'comment'), (u'value2', None)] - assert v.to_python(u'value1 (comment1); value2; value3 (comment3)') == [(u'value1', u'comment1'), (u'value2', None), ('value3', 'comment3')] - assert v.to_python(u'value1 (comment1); value2 (test (not easy))') == [(u'value1', u'comment1'), (u'value2', u'test (not easy)')] - - -def test_UrlNeinNone(): - v = wradmin.model.validators.UrlNeinNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'http://www.höttingeralm.at') == u'http://www.höttingeralm.at' - - -def test_ValueCommentListNeinLoopNone(): - v = wradmin.model.validators.ValueCommentListNeinLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'T-Mobile (gut); A1') == u'T-Mobile (gut); A1' - - -def test_PhoneCommentListNeinLoopNone(): - v = wradmin.model.validators.PhoneCommentListNeinLoopNone(comments_are_optional=True) - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456') == u'+43-699-1234567 (nicht nach 20:00 Uhr); +43-512-123456' - - -def test_EmailCommentListNeinLoopNone(): - v = wradmin.model.validators.EmailCommentListNeinLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'Nein') == u'Nein' - assert v.to_python(u'first@example.com') == u'first@example.com' - assert v.to_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' - assert v.from_python(None) == u'' - assert v.from_python(u'Nein') == u'Nein' - assert v.from_python(u'first@example.com') == u'first@example.com' - assert v.from_python(u'first@example.com (Nur Winter); second@example.com') == u'first@example.com (Nur Winter); second@example.com' - - -def test_GermanLift(): - v = wradmin.model.validators.GermanLift() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'Sessellift (4 Euro)') == (True, u'Sessellift (4 Euro)') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'Sessellift (4 Euro)')) == u'Sessellift (4 Euro)' - - -def test_WikiPageListLoopNone(): - v = wradmin.model.validators.WikiPageListLoopNone() - assert v.to_python(u'') == None - assert v.to_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' - assert v.from_python(None) == u'' - assert v.from_python(u'[[Birgitzer Alm]]; [[Kemater Alm]]') == u'[[Birgitzer Alm]]; [[Kemater Alm]]' - - -def test_BoolUnicodeTupleValidator(): - v = wradmin.model.validators.BoolUnicodeTupleValidator() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'any text') == (True, u'any text') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'any text')) == u'any text' - - -def test_SledRental(): - v = wradmin.model.validators.SledRental() - assert v.to_python(u'') == (None, None) - assert v.to_python(u'Nein') == (False, None) - assert v.to_python(u'Ja') == (True, u'Ja') - assert v.to_python(u'Talstation (nur mit Ticket); Schneealm') == (True, u'Talstation (nur mit Ticket); Schneealm') - assert v.from_python((None, None)) == u'' - assert v.from_python((False, None)) == u'Nein' - assert v.from_python((True, u'Talstation (nur mit Ticket); Schneealm')) == u'Talstation (nur mit Ticket); Schneealm' - assert v.from_python((True, u'Ja')) == u'Ja'