# HG changeset patch # User Brian Neal # Date 1413236923 18000 # Node ID 09ed84a7394c901e24e0e61b4f3cad19f419f222 # Parent b8c401cf99ca95b8f803019a82b6cbb52a412a4b For issue #76, store pre-rendered HTML for each band map entry. diff -r b8c401cf99ca -r 09ed84a7394c bandmap/admin.py --- 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. diff -r b8c401cf99ca -r 09ed84a7394c bandmap/management/commands/update_bands.py --- /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() diff -r b8c401cf99ca -r 09ed84a7394c bandmap/models.py --- 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) diff -r b8c401cf99ca -r 09ed84a7394c bandmap/views.py --- 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')