Permalink
Browse files

add pagination to Ward view and MyReports

  • Loading branch information...
1 parent ead3e67 commit bff4b0e31acfdb5fdeff14640eea5818ea07bf3a @visiblegovernment committed Jul 14, 2011
View
@@ -428,7 +428,7 @@ def notify_on_update(self):
[self.report.first_update().email], fail_silently=False)
- def save(self):
+ def save(self,notify=True):
# does this update require confirmation?
if not self.is_confirmed:
self.get_confirmation()
@@ -449,7 +449,8 @@ def save(self):
self.report.is_confirmed = True
super(ReportUpdate,self).save()
self.report.save()
- self.notify()
+ if notify:
+ self.notify()
def confirm(self):
self.is_confirmed = True
View
@@ -12,6 +12,7 @@
from social_auth.models import UserSocialAuth
from django.conf import settings
from django.contrib.auth import login, REDIRECT_FIELD_NAME
+from django.core.paginator import Paginator, InvalidPage, EmptyPage
LOGO_OFFSETS = { 'facebook': 0,
'twitter': -128,
@@ -42,11 +43,24 @@ def home( request ):
email = request.user.email
subscriberQ = Q(reportsubscriber__email=email,reportsubscriber__is_confirmed=True)
updaterQ = Q(reportupdate__email=email,reportupdate__is_confirmed=True)
- allreports = Report.objects.filter(subscriberQ | updaterQ).order_by('is_fixed','created_at').extra(select=SortedDict([('is_reporter','select case when bool_or(report_updates.first_update) then true else false end from report_updates where report_updates.email=%s and report_updates.is_confirmed=true and report_updates.report_id=reports.id'),
+ allreports = Report.objects.filter(subscriberQ | updaterQ).order_by('-created_at').extra(select=SortedDict([('is_reporter','select case when bool_or(report_updates.first_update) then true else false end from report_updates where report_updates.email=%s and report_updates.is_confirmed=true and report_updates.report_id=reports.id'),
('is_updater','select case when count(report_updates.id) > 0 then true else false end from report_updates where report_updates.report_id=reports.id and report_updates.first_update=false and report_updates.email=%s and report_updates.is_confirmed=true'), ('days_open','case when reports.is_fixed then date(reports.fixed_at) - date(reports.created_at) else CURRENT_DATE - date(reports.created_at) end')]), select_params=( email, email )).distinct()
+ try:
+ page_no = int(request.GET.get('page', '1'))
+ except ValueError:
+ page_no = 1
+
+ paginator = Paginator(allreports, 100)
+
+ try:
+ page = paginator.page(page_no)
+ except (EmptyPage, InvalidPage):
+ page = paginator.page(paginator.num_pages)
+
return render_to_response("account/home.html",
{"profile": request.user.get_profile(),
- 'allreports':allreports },
+ 'reports':page.object_list,
+ 'page':page },
context_instance=RequestContext(request))
@login_required
View
@@ -3,38 +3,19 @@
from django.template import Context, RequestContext
from django.db import connection
from django.utils.translation import ugettext_lazy, ugettext as _
-import datetime
+from django.core.paginator import Paginator, InvalidPage, EmptyPage
-def show_by_number( request, city_id, ward_no ):
- city= get_object_or_404(City, id=city_id)
- wards = Ward.objects.filter( city=city, number=ward_no)
- google = WardMap(wards[0],[])
- return render_to_response("wards/show.html",
- {"ward": wards[0],
- "google": google,
- "reports": [] },
- context_instance=RequestContext(request))
+import datetime
def show( request, ward_id ):
ward = get_object_or_404(Ward, id=ward_id)
- # calculate date range for which to return reports
- if request.GET.has_key('years_ago'):
- years_ago = int(request.GET['years_ago'])
- else:
- years_ago = 0
- yearoffset = datetime.timedelta(days = 365 * years_ago )
-
- date_range_end = datetime.datetime.now() - yearoffset
- date_range_start = date_range_end - datetime.timedelta(days =365)
-
- # do we want to show older reports?
- if Report.objects.filter(ward=ward,created_at__lte=date_range_start).count() > 1:
- older_reports_link = ward.get_absolute_url() + "?years_ago=%i" %( years_ago + 1)
- else:
- older_reports_link = None
+ try:
+ page_no = int(request.GET.get('page', '1'))
+ except ValueError:
+ page_no = 1
- reports = Report.objects.filter( created_at__gte = date_range_start, created_at__lte=date_range_end, ward = ward, is_confirmed = True ).extra( select = { 'status' : """
+ all_reports = Report.objects.filter( ward = ward, is_confirmed = True ).extra( select = { 'status' : """
CASE
WHEN age( clock_timestamp(), created_at ) < interval '1 month' AND is_fixed = false THEN 'New Problems'
WHEN age( clock_timestamp(), created_at ) > interval '1 month' AND is_fixed = false THEN 'Older Unresolved Problems'
@@ -49,17 +30,25 @@ def show( request, ward_id ):
WHEN age( clock_timestamp(), fixed_at ) < interval '1 month' AND is_fixed = true THEN 2
WHEN age( clock_timestamp(), fixed_at ) > interval '1 month' AND is_fixed = true THEN 3
ELSE 4
- END """ }, order_by = ['status_int'] )
-
- google = WardMap(ward,reports)
+ END """ } ).order_by('-created_at')
+ paginator = Paginator(all_reports, 100)
+ try:
+ page = paginator.page(page_no)
+ except (EmptyPage, InvalidPage):
+ page = paginator.page(paginator.num_pages)
+
+ reports = sorted(page.object_list,key=lambda o: o.status_int )
+ google = WardMap(ward,reports)
+
return render_to_response("wards/show.html",
{"ward": ward,
"google": google,
- "reports": reports,
- "date_range_start": date_range_start,
- "date_range_end": date_range_end,
- "older_reports_link": older_reports_link
- # "status_display" : [ _('New Problems'), _('Older Unresolved Problems'), _('Recently Fixed'), _('Older Fixed Problems') ]
- },
- context_instance=RequestContext(request))
+ "page":page,
+ "reports": reports },
+ context_instance=RequestContext(request))
+
+def show_by_number( request, city_id, ward_no ):
+ city= get_object_or_404(City, id=city_id)
+ ward = get_object_or_404( city=city, number=ward_no)
+ show(request,ward.id)
View
@@ -12,6 +12,8 @@ body {
margin: 0px auto;
}
+#content_middle {padding-top:2em;padding-bottom:2em;margin-top:2em;color:#b4b4b4;}
+
#city_top_problems table td:first-child { padding-left: 5px; }
#city_all_problems { }
@@ -537,7 +539,7 @@ input.big_button:hover {background: url("/media/images/search-hover.png") no-rep
#contact_us_form p { margin-bottom: .5em; }
#posters { text-align: center; }
#posters p {width: 450px; margin: 20px auto;}
-.myreports { width:100%; }
+.myreports { width:100%; padding: 2em 0em; }
.myreports td { padding: 2px; }
.myreports td.section {color:#FFFFFF;
font-size:80%;
View
@@ -136,7 +136,6 @@ border:1px solid silver;
border-left:5px solid silver;
margin-bottom:0.8em;
}
-#content_middle {padding-top:2em;margin-top:2em;color:#b4b4b4;}
#content_middle label {text-transform:uppercase;}
#instructable {color:#b4b4b4;}
#report-body p { margin-bottom: .5em;}
@@ -3,6 +3,7 @@
{% block css %}
<style type="text/css" media="screen">
div#profile-menu { text-align: right; padding-right: 20px; padding-bottom: 1.5em; }
+ .open { color: red; };
</style>
{% endblock %}
{% block title %}My Reports{% endblock %}
@@ -14,46 +15,68 @@
</ul>
</div>
<h2>My Reports</h2>
+<div class="pagination">
+ <span class="step-links">
+ {% if page.has_previous %}
+ <a href="?page={{ page.previous_page_number }}">{% trans "Previous" %}</a> |
+ {% endif %}
+ {% if page.has_other_pages %}
+ <span class="current">
+ {{page.start_index}}-{{page.end_index}} {% trans "of" %} {{page.paginator.count}} {% trans "Reports." %}
+ </span>
+ {% endif %}
+ {% if page.has_next %}
+ | <a href="?page={{ page.next_page_number }}">{% trans "Next" %}</a>
+ {% endif %}
+ </span>
+</div>
+
<table class='myreports'>
<tr>
- <th>Report</th>
- <th>Status</th>
- <th>Date Filed</th>
- <th>Days Open</th>
- <th>My Role</th>
- </tr>
- {% for report in allreports %}
- {% ifchanged report.is_fixed %}
- <tr>
- <td colspan="5" class='section'>
- {% if report.is_fixed %}Fixed{% else %}Open{% endif %} Reports
- </td>
+ <th>{% trans "Report" %}</th>
+ <th>{% trans "Date Filed" %}</th>
+ <th>{% trans "Status" %}</th>
+ <th>{% trans "Days Open" %}</th>
+ <th>{% trans "My Role" %}</th>
</tr>
- {% endifchanged %}
+ {% for report in reports %}
<tr class={% cycle 'row-even','row-odd' %}>
<td>
<a href='{{report.get_absolute_url}}'>{{report.title}}</a>
</td>
<td>
- {% if report.is_fixed %}Fixed{% else %}Open{% endif %}
+ {{report.created_at|date:"Y-m-d"}}
</td>
-
- <td>
- {{report.sent_at|date:"Y-m-d"}}
+ <td {%if not report.is_fixed %}class='open'{%endif%} >
+ {% if report.is_fixed %}{% trans "Fixed" %}{% else %}{% trans "Open" %}{% endif %}
</td>
- <td>
+ <td {%if not report.is_fixed %}class='open'{% endif %}>
{{report.days_open}}
</td>
<td>
- {% if report.is_reporter %}Reporter
+ {% if report.is_reporter %}{% trans "Reporter" %}
{% else %}
- {% if report.is_updater %}Updater
- {% else %}Subscriber{% endif %}
+ {% if report.is_updater %}{% trans "Updater" %}
+ {% else %}{% trans "Subscriber" %}{% endif %}
{% endif %}
</td>
</tr>
- {% endfor %}
-
+ {% endfor %}
</table>
+<div class="pagination">
+ <span class="step-links">
+ {% if page.has_previous %}
+ <a href="?page={{ page.previous_page_number }}">{% trans "Previous" %}</a> |
+ {% endif %}
+ {% if page.has_other_pages %}
+ <span class="current">
+ {{page.start_index}}-{{page.end_index}} {% trans "of" %} {{page.paginator.count}} {% trans "Reports." %}
+ </span>
+ {% endif %}
+ {% if page.has_next %}
+ | <a href="?page={{ page.next_page_number }}">{% trans "Next" %}</a>
+ {% endif %}
+ </span>
+</div>
{% endblock %}
View
@@ -4,14 +4,28 @@
{% block content %}
<div id="fullwidth">
<h3>{% trans "Reports for" %} {{ward.name}}, {{ward.city.name}} (Ward {{ward.number}})</h3>
- <p>{% trans "Reports filed between" %} {{date_range_start|date:"N jS, Y"}} {% trans "and" %} {{date_range_end|date:"N jS, Y"}}.
{% if older_reports_link %} (<a href="{{older_reports_link}}" %}>{% trans "See older reports" %}</a>){% endif %}</p>
<div id='leftcol'>
<ol>
{% if not reports %}
<p>{% trans "No problems have been reported." %}</p>
{% endif %}
+ <div class="pagination">
+ <span class="step-links">
+ {% if page.has_previous %}
+ <a href="?page={{ page.previous_page_number }}">{% trans "Previous" %}</a> |
+ {% endif %}
+ {% if page.has_other_pages %}
+ <span class="current">
+ {{page.start_index}}-{{page.end_index}} {% trans "of" %} {{page.paginator.count}} {% trans "Reports." %}
+ </span>
+ {% endif %}
+ {% if page.has_next %}
+ | <a href="?page={{ page.next_page_number }}">{% trans "Next" %}</a>
+ {% endif %}
+ </span>
+ </div>
{% for report in reports %}
{% ifchanged report.status %}
<p/><h2>{% trans report.status %}</h2>

0 comments on commit bff4b0e

Please sign in to comment.