From b9ab4653313c756b8317efba64e9d9719d90530d Mon Sep 17 00:00:00 2001 From: Christopher Groskopf Date: Mon, 21 Sep 2009 21:49:32 -0700 Subject: [PATCH] Added index view (slowly migrating to realistic use) --- api/views.py | 5 ++-- citizen/templates/index.html | 56 ++++++++++++++++++++++++++++++++++++ citizen/views.py | 47 ++++++++++++++++++++++++++++++ urls.py | 2 ++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 citizen/templates/index.html diff --git a/api/views.py b/api/views.py index 159805c..ca180e0 100644 --- a/api/views.py +++ b/api/views.py @@ -25,12 +25,13 @@ def get_query_options(request): options = {} try: - options['endkey'] = request.GET['start'] + # String query parameters must be quoted for CouchDB + options['endkey'] = '"%s"' % request.GET['start'] except KeyError: pass try: - options['startkey'] = request.GET['end'] + options['startkey'] = '"%s"' % request.GET['end'] except KeyError: pass diff --git a/citizen/templates/index.html b/citizen/templates/index.html new file mode 100644 index 0000000..77fea9e --- /dev/null +++ b/citizen/templates/index.html @@ -0,0 +1,56 @@ + + + + Voter's Daily + + +

Voter's Daily

+ Total executive events: {{ executive_events|length }} + + + {% for event in executive_events %} + + + + + + + + + {% endfor %} + +
{{ event.datetime }}{{ event.title }}{{ event.description }}{{ event.end_datetime }}{{ event.entity }}{{ event.source_url }}
+ + Total judicial events: {{ judicial_events|length }} + + + {% for event in judicial_events %} + + + + + + + + + {% endfor %} + +
{{ event.datetime }}{{ event.title }}{{ event.description }}{{ event.end_datetime }}{{ event.entity }}{{ event.source_url }}
+ + Total legislative events: {{ legislative_events|length }} + + + {% for event in legislative_events %} + + + + + + + + + {% endfor %} + +
{{ event.datetime }}{{ event.title }}{{ event.description }}{{ event.end_datetime }}{{ event.entity }}{{ event.source_url }}
+ + \ No newline at end of file diff --git a/citizen/views.py b/citizen/views.py index 1593200..81fcaaf 100644 --- a/citizen/views.py +++ b/citizen/views.py @@ -1,3 +1,4 @@ +import datetime import json import urllib import urllib2 @@ -88,4 +89,50 @@ def entity(request, entity): data = json.loads(api_result.read()) return render_to_response('entity.html', { 'documents': data['rows'] }) + +def index(request): + """ + Renders the public-facing front page. + """ + if request.method != 'GET': + raise Http404 + + utc_now = datetime.datetime.utcnow() + utc_today = datetime.date(utc_now.year, utc_now.month, utc_now.day) + utc_tomorrow = utc_today + datetime.timedelta(days=1) + start = utc_today.isoformat() + end = utc_tomorrow.isoformat() + + #TEMP + start = '2009-09-01' + end = '2009-09-15' + + options = {} + options['start'] = start + options['end'] = end + options['format'] = 'json' + query_string = urllib.urlencode(options) + + api_url = settings.API_ROOT + reverse('api_events_all') + api_url = '%s?%s' % (api_url, query_string) + + api_result = urllib2.urlopen(api_url) + data = json.loads(api_result.read()) + + executive_events = [] + legislative_events = [] + judicial_events = [] + + for key, value in data['results'].items(): + if value['branch'] == 'Executive': + executive_events.append(value) + elif value['branch'] == 'Legislative': + legislative_events.append(value) + elif value['branch'] == 'Judicial': + judicial_events.append(value) + + return render_to_response('index.html', { + 'executive_events': executive_events, + 'legislative_events': legislative_events, + 'judicial_events': judicial_events }) \ No newline at end of file diff --git a/urls.py b/urls.py index 9ac8227..a0d342c 100644 --- a/urls.py +++ b/urls.py @@ -8,4 +8,6 @@ url(r'^events/all', 'votersdaily_web.citizen.views.all', name='events_all'), url(r'^events/branch/(?P.*)', 'votersdaily_web.citizen.views.branch', name='events_branch'), url(r'^events/entity/(?P.*)', 'votersdaily_web.citizen.views.entity', name='events_entity'), + + url(r'', 'votersdaily_web.citizen.views.index', name='index') )