~speakman/ppvalbok/trunk

« back to all changes in this revision

Viewing changes to jsonhelper/views.py

  • Committer: Daniel Nyström
  • Date: 2009-05-01 09:49:38 UTC
  • Revision ID: daniel@nystrom.st-20090501094938-yr0dv88n85hh314w
Moved all json views from swevote to new app jsonhelper.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
from django.conf import settings
 
4
from django.http import Http404, HttpResponse
 
5
from django.shortcuts import get_object_or_404
 
6
from django.utils import simplejson
 
7
from swevote.models import ZipCode, EarlyVotingStation, VotingStation
 
8
import itertools
 
9
 
 
10
def markers_by_zip(request, zipcode):
 
11
    markers = []
 
12
    zipcode = ZipCode.objects.get(zipcode=zipcode)
 
13
    earlystations = EarlyVotingStation.objects.filter(
 
14
        municipality=zipcode.municipality)
 
15
    stations = VotingStation.objects.filter(
 
16
        municipality=zipcode.municipality)
 
17
    allstations = itertools.chain(earlystations,stations)
 
18
    for station in allstations:
 
19
        if int(station.lat):
 
20
            marker = {
 
21
                "point": {"lat": str(station.lat),
 
22
                          "lon": str(station.lon)},
 
23
                "info": station.name,
 
24
                "code": station.code,
 
25
                "address": (station.address1,
 
26
                            station.address2,
 
27
                            station.address3,
 
28
                            station.city),
 
29
                "early": True if isinstance(station, EarlyVotingStation) else False,
 
30
                }
 
31
            markers.append(marker)
 
32
    return HttpResponse(simplejson.dumps({"markers": markers},
 
33
                                         ensure_ascii=False,
 
34
                                         indent=4),
 
35
                        mimetype='application/javascript')
 
36
 
 
37
def json_marker_response(name, lat, lon):
 
38
    return HttpResponse(simplejson.dumps(
 
39
            {"markers": [{"point": {"lat": str(lat),
 
40
                                    "lon": str(lon)},
 
41
                          "info": name},]
 
42
             }, ensure_ascii=False),
 
43
                        mimetype='application/javascript')
 
44
 
 
45
def markers_by_station_id(request, stationid):
 
46
    station = get_object_or_404(VotingStation, pk=stationid)
 
47
    return json_marker_response(station.name, station.lat, station.lon)
 
48
 
 
49
def markers_by_early_station_id(request, stationid):
 
50
    station = get_object_or_404(EarlyVotingStation, pk=stationid)
 
51
    return json_marker_response(station.name, station.lat, station.lon)