changeset 843:09ed84a7394c

For issue #76, store pre-rendered HTML for each band map entry.
author Brian Neal <bgneal@gmail.com>
date Mon, 13 Oct 2014 16:48:43 -0500
parents b8c401cf99ca
children 6900400f971b
files bandmap/admin.py bandmap/management/__init__.py bandmap/management/commands/__init__.py bandmap/management/commands/update_bands.py bandmap/models.py bandmap/views.py
diffstat 4 files changed, 26 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/bandmap/admin.py	Sun Oct 12 14:59:43 2014 -0500
+++ b/bandmap/admin.py	Mon Oct 13 16:48:43 2014 -0500
@@ -22,6 +22,7 @@
     search_fields = ['name', 'location', 'note']
     raw_id_fields = ['user']
     actions = ['update_location', 'approve_bands']
+    readonly_fields = ['html']
 
     def approve_bands(self, request, qs):
         """This admin action awards a map pin to the user who added the band.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bandmap/management/commands/update_bands.py	Mon Oct 13 16:48:43 2014 -0500
@@ -0,0 +1,19 @@
+"""
+This command calls save() on all bandmap entries.
+
+This causes the html field to be rebuilt. This is the text displayed inside the
+"balloon" on the map when a band's marker is clicked.
+
+"""
+from django.core.management.base import NoArgsCommand
+
+from bandmap.models import BandEntry
+
+
+class Command(NoArgsCommand):
+    help = "This command rebuilds the html field on all bandmap entries."
+
+    def handle_noargs(self, **kwargs):
+
+        for band in BandEntry.objects.all().iterator():
+            band.save()
--- a/bandmap/models.py	Sun Oct 12 14:59:43 2014 -0500
+++ b/bandmap/models.py	Mon Oct 13 16:48:43 2014 -0500
@@ -5,6 +5,7 @@
 
 from django.db import models
 from django.contrib.auth.models import User
+from django.template.loader import render_to_string
 
 
 class BandEntryManager(models.Manager):
@@ -25,6 +26,7 @@
     note = models.CharField(max_length=255, blank=True)
     is_active = models.BooleanField(default=True, db_index=True)
     is_approved = models.BooleanField(default=False, db_index=True)
+    html = models.TextField(blank=True)
 
     objects = BandEntryManager()
 
@@ -38,4 +40,6 @@
     def save(self, *args, **kwargs):
         if not self.pk and not self.date_submitted:
             self.date_submitted = datetime.datetime.now()
+        html = render_to_string('bandmap/balloon.html', {'band': self})
+        self.html = html.strip().replace('\n', '')
         super(BandEntry, self).save(*args, **kwargs)
--- a/bandmap/views.py	Sun Oct 12 14:59:43 2014 -0500
+++ b/bandmap/views.py	Mon Oct 13 16:48:43 2014 -0500
@@ -7,7 +7,6 @@
 from django.shortcuts import redirect, render
 from django.contrib import messages as django_messages
 from django.http import HttpResponse
-from django.template.loader import render_to_string
 from django.core.cache import cache
 from django.conf import settings
 
@@ -67,18 +66,16 @@
 
     bands = []
     for band in qs.iterator():
-        note = render_to_string('bandmap/balloon.html', {'band': band})
-        note = note.strip().replace('\n', '')
         bands.append({
             'name': band.name,
             'lat': band.lat,
             'lon': band.lon,
             'is_active': band.is_active,
-            'note': note,
+            'note': band.html,
         })
     results = json.dumps(bands)
 
     # Store in cache
-    cache.set(cache_key, results, 300)
+    cache.set(cache_key, results, 600)
 
     return HttpResponse(results, content_type='application/json')