maposmatic-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Maposmatic-dev] [PATCH 2/3] Allow the use of MapOSMatic without a GIS-c


From: Maxime Petazzoni
Subject: [Maposmatic-dev] [PATCH 2/3] Allow the use of MapOSMatic without a GIS-capable database.
Date: Wed, 9 Sep 2009 13:04:49 +0200

Removes the dependency on a GIS-capable database backend when you just
want to run MapOSMatic and not do any map rendering.
---
 www/maposmatic/views.py   |    6 ++++++
 www/maposmatic/widgets.py |   27 ++++++++++++++++++---------
 www/settings.py           |    4 ++++
 3 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/www/maposmatic/views.py b/www/maposmatic/views.py
index 9666a13..9ed74aa 100644
--- a/www/maposmatic/views.py
+++ b/www/maposmatic/views.py
@@ -41,6 +41,12 @@ from www.maposmatic.widgets import AreaField, PointField
 # OpenStreetMap database. We don't go through the Django ORM but
 # directly to the database for simplicity reasons.
 def city_exists(city):
+
+    # If no GIS database is available, always return True to allow
+    # normal form processing as if the city existed.
+    if not www.settings.has_gis_database():
+        return True
+
     try:
         conn = psycopg2.connect("dbname='%s' user='%s' host='%s' 
password='%s'" %
                                 (www.settings.GIS_DATABASE_NAME,
diff --git a/www/maposmatic/widgets.py b/www/maposmatic/widgets.py
index 522f934..ef123b1 100644
--- a/www/maposmatic/widgets.py
+++ b/www/maposmatic/widgets.py
@@ -23,7 +23,6 @@ Extra widgets and fields
 
 from django import forms
 from django.utils.safestring import mark_safe
-from django.contrib.gis.db import models
 
 from www import settings
 
@@ -31,14 +30,24 @@ URL_OSM_CSS = 
["http://www.openlayers.org/api/theme/default/style.css";]
 URL_OSM_JS = ["http://www.openlayers.org/api/OpenLayers.js";,
           "http://www.openstreetmap.org/openlayers/OpenStreetMap.js";]
 
-class PointField(models.PointField):
-    '''
-    Set the widget for the form field
-    '''
-    def clean(self, value):
-        if len(value) != 2 and self.required:
-            raise ValidationError(_("Invalid point"))
-        return value
+class PointField(forms.FloatField):
+    """A non-GIS dependant point field."""
+    pass
+
+if settings.has_gis_database():
+    from django.contrib.gis.db import models
+
+    class GISPointField(models.PointField):
+        '''
+        Set the widget for the form field
+        '''
+        def clean(self, value):
+            if len(value) != 2 and self.required:
+                raise ValidationError(_("Invalid point"))
+            return value
+
+    # Override the default PointField with our GIS-dependant PointField
+    PointField = GISPointField
 
 class AreaWidget(forms.TextInput):
     """
diff --git a/www/settings.py b/www/settings.py
index e871133..bdbd30a 100644
--- a/www/settings.py
+++ b/www/settings.py
@@ -120,3 +120,7 @@ LOG.addHandler(_fh)
 LOG.info("log restarted.")
 
 ITEMS_PER_PAGE = 25
+
+def has_gis_database():
+    """Returns True if a GIS database is defined, False otherwise."""
+    return GIS_DATABASE_NAME and GIS_DATABASE_NAME != ''
-- 
1.6.3.3.210.g29cb3





reply via email to

[Prev in Thread] Current Thread [Next in Thread]