Skip to content

Commit

Permalink
Refactoring maps to prepare for additional map functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ltucker committed Jun 30, 2011
1 parent c583159 commit 8a05a15
Show file tree
Hide file tree
Showing 30 changed files with 1,609 additions and 64 deletions.

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions ebpub/ebpub/db/models.py
Expand Up @@ -125,6 +125,10 @@ class Schema(models.Model):

# number of records to show on place_overview
number_in_overview = models.SmallIntegerField(default=5)

map_icon_url = models.TextField(blank=True, null=True)
map_color = models.CharField(max_length=255, blank=True, null=True, help_text="CSS Color used on maps to display this type of news. eg #FF0000")


objects = SchemaManager()
public_objects = SchemaPublicManager()
Expand Down
16 changes: 16 additions & 0 deletions ebpub/ebpub/db/templates/admin/db/schema/change_form.html
@@ -0,0 +1,16 @@
{% extends "admin/change_form.html" %}

{% block after_field_sets %}
{% if original.map_icon_url %}
<div>
Current Icon:
<div><img src="{{ original.map_icon_url }}" alt="current icon preview" /></div>
</div>
{% endif %}

{% if original.map_color %}
<div>
Current Color: <div style="width: 50px; height: 50px; background-color: {{ original.map_color }};">&nbsp;</div>
</div>
{% endif %}
{% endblock %}
95 changes: 70 additions & 25 deletions ebpub/ebpub/db/views.py
Expand Up @@ -20,6 +20,7 @@
from django.conf import settings
from django.contrib.gis.shortcuts import render_to_kml
from django.core.cache import cache
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.http import Http404
from django.http import HttpResponse
Expand Down Expand Up @@ -47,6 +48,7 @@
from ebpub import geocoder
from ebpub.geocoder.parser.parsing import normalize, ParsingError
from ebpub.metros.allmetros import get_metro
from ebpub.openblockapi.views import _items_json as _api_items_json
from ebpub.preferences.models import HiddenSchema
from ebpub.streets.models import Street, City, Block, Intersection
from ebpub.streets.utils import full_geocode
Expand Down Expand Up @@ -272,30 +274,7 @@ def newsitems_geojson(request):
# We can't do it in the qs because we want to first slice the qs
# by date, and we can't call order_by() after a slice.
newsitem_list = sorted(newsitem_qs, key=lambda ni: ni.schema.id)
popup_list = _map_popups(newsitem_list)

features = {'type': 'FeatureCollection', 'features': []}
for newsitem, popup_info in zip(newsitem_list, popup_list):
if newsitem.location is None:
# Can happen, see NewsItem docstring.
# TODO: We should probably allow for newsitems that have a
# location_object too?
continue

features['features'].append(
{'type': 'Feature',
'geometry': {'type': 'Point',
'coordinates': [newsitem.location.centroid.x,
newsitem.location.centroid.y],
},
'properties': {
'title': newsitem.title,
'id': popup_info[0],
'popup_html': popup_info[1],
'schema': popup_info[2],
}
})
output = simplejson.dumps(features, indent=1)
output = _api_items_json(newsitem_list)
cache.set(cache_key, output, cache_seconds)

response = HttpResponse(output, mimetype="application/javascript")
Expand Down Expand Up @@ -351,6 +330,7 @@ def homepage(request):
'default_zoom': settings.DEFAULT_MAP_ZOOM,
'bodyclass': 'homepage',
'breadcrumbs': breadcrumbs.home({}),
'map_configuration': _preconfigured_map({})
})

def search(request, schema_slug=''):
Expand Down Expand Up @@ -497,6 +477,7 @@ def newsitem_detail(request, schema_slug, newsitem_id):
'bodyid': schema_slug,
}
context['breadcrumbs'] = breadcrumbs.newsitem_detail(context)
context['map_configuration'] = _preconfigured_map(context)
return eb_render(request, templates_to_try, context)

def schema_list(request):
Expand Down Expand Up @@ -806,6 +787,7 @@ def schema_filter(request, slug, args_from_url):
'start_date': start_date,
'end_date': end_date,
})
context['map_configuration'] = _preconfigured_map(context);

return eb_render(request, 'db/filter.html', context)

Expand Down Expand Up @@ -944,12 +926,75 @@ def place_detail_timeline(request, *args, **kwargs):


context['filtered_schema_list'] = s_list

context['map_configuration'] = _preconfigured_map(context);
response = eb_render(request, 'db/place_detail.html', context)
for k, v in context['cookies_to_set'].items():
response.set_cookie(k, v)
return response

def _preconfigured_map(context):
"""
helper to rig up a map configuration for
templates based on the menagerie of
values provided in the template context.
returns a json string with the layer configuration
for the map that should be displayed on the page.
"""

config = {
'locations': [],
'layers': []
};

# load layer boundary if a Location is specified
location = context.get('location')
if location is not None:
loc_json_url = reverse('location_detail_json',
kwargs={'loctype': location.location_type.slug,
'slug': location.slug})
loc_boundary = {
'url': loc_json_url,
'params': {},
'title': "%s Boundary" % location.pretty_name,
'visible': True
}
config['locations'].append(loc_boundary);

###########################
# configure newsitem layer
#
# TODO filtering? via api?
###########################

# single news item ?
layer_params = {}
item = context.get('newsitem')
if item is not None:
layer_params['newsitem'] = item.id

# restricted to place, start or end date ?
for key in ['pid', 'start_date', 'end_date']:
val = context.get(key)
if val is not None:
layer_params[key] = val

# restricted by schema ?
schema = context.get('schema_slug')
if schema is not None:
layer_params['schema'] = schema_slug

items_layer = {
'url': reverse('ajax-newsitems-geojson'),
'params': layer_params,
'title': "News" ,
'visible': True
}
config['layers'].append(items_layer)

return simplejson.dumps(config);



def place_detail_overview(request, *args, **kwargs):
context, response = _place_detail_normalize_url(request, *args, **kwargs)
Expand Down

0 comments on commit 8a05a15

Please sign in to comment.