Commits

Mikhail Korobov committed e619357 Merge

Merge pull request #3 from gipi/settings

Use configurable defaults when no address is passed to the template tag

Comments (0)

Files changed (5)

 
     EASY_MAPS_GOOGLE_KEY = "your-google-maps-api-key"
 
+If you need a place where center the map when no address is inserted yet add the
+latitudine and longitude to the EASY_MAPS_CENTER variable in your settings.py
+like the following::
+
+    EASY_MAPS_CENTER = (-41.3, 32)
+
 Usage
 =====
 

easy_maps/templates/easy_maps/map.html

 {% block html %}
     <!-- HTML map container -->
     <div id="map-canvas-{{ map.pk }}"
-         {% if width and map.latitude and not map.geocode_error %}
-            style="width: {{ width }}px; height: {{ height }}px;"
-         {% endif %}
+         style="width: {{ width }}px; height: {{ height }}px;"
          class="easy-map-googlemap">
          {% block noscript %}
          <noscript>
 {% endblock %}
 
 {% endwith %}
-{% endwith %}
+{% endwith %}

easy_maps/templatetags/easy_maps_tags.py

 from django import template
 from django.template.loader import render_to_string
 from easy_maps.models import Address
+from django.conf import settings
+
 register = template.Library()
 
 @register.tag
             address = self.address.resolve(context)
             template_name = self.template_name.resolve(context)
 
-            map, _ = Address.objects.get_or_create(address=address or '')
+            map = None
+            if address == '':
+                map = Address(latitude=settings.EASY_MAPS_CENTER[0], longitude=settings.EASY_MAPS_CENTER[1])
+            else:
+                map, _ = Address.objects.get_or_create(address=address)
+
             context.update({
                 'map': map,
                 'width': self.width,

easy_maps/tests.py

+from django.test import TestCase
+from django.template import Template, Context
+import mock
+from django.test.utils import override_settings
+
+from .models import Address
+
+
+class AddressTests(TestCase):
+    fake_default_center  = (1, 2)
+    def test_empty_dont_save_on_db(self):
+        """If we pass an empty address we don't save nothing in the database"""
+        n_addresses_before = len(Address.objects.all())
+
+        simple_template_string = """{% load easy_maps_tags %}
+        {% easy_map "" 500 500 10 %}
+        """
+        t = Template(simple_template_string)
+        t.render(Context({}))
+
+
+        n_addresses_after = len(Address.objects.all())
+
+        self.assertEqual(n_addresses_after, n_addresses_before)
+
+    @override_settings(EASY_MAPS_CENTER=fake_default_center)
+    def test_empty_address_use_defaults(self):
+        """When an empty address is passed uses the EASY_MAPS_CENTER setting"""
+        a = ""
+        simple_template_string = """{%% load easy_maps_tags %%}
+        {%% easy_map "%s" 500 500 10 %%}
+        """ % a
+        self.address = None
+        # below we patch the render_to_string in order to retrieve the map
+        # context variable and check its coordinate
+        def get_map_context_instance(*args, **kwargs):
+            self.address = (kwargs['context_instance'].dicts[1])['map']
+            return ''
+
+        t = Template(simple_template_string)
+        with mock.patch('easy_maps.templatetags.easy_maps_tags.render_to_string', get_map_context_instance):
+            t.render(Context({}))
+
+        self.assertEqual(self.address.latitude, AddressTests.fake_default_center[0])
+        self.assertEqual(self.address.longitude, AddressTests.fake_default_center[1])
+
+    @override_settings(EASY_MAPS_CENTER=fake_default_center)
+    def test_normal_address(self):
+        """If we pass an address don't use the defaults"""
+        n_addresses_before = len(Address.objects.all())
+
+        a = "Ekaterinburg, Mira 33"
+        simple_template_string = """{%% load easy_maps_tags %%}
+        {%% easy_map "%s" 500 500 10 %%}
+        """ % a
+        t = Template(simple_template_string)
+        t.render(Context({}))
+
+        address = Address.objects.get(address=a)
+        self.assertNotEqual(address.latitude, AddressTests.fake_default_center[0])
+        self.assertNotEqual(address.longitude, AddressTests.fake_default_center[1])
+
+        n_addresses_after = len(Address.objects.all())
+
+        self.assertEqual(n_addresses_after, n_addresses_before + 1)
+

easy_maps_tests/settings.py

 #    'devserver',
     'south',
 )
+
+EASY_MAPS_CENTER = (45.0677201, 7.6825531)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.