Permalink
Browse files

Merge branch 'hotfix/scheme_browse' into 2.0

  • Loading branch information...
2 parents 3aec193 + 441f8d9 commit bd1013fb5f8d204ce2cd70f5c161ae3b31d86006 @symroe symroe committed Dec 9, 2010
Showing with 87 additions and 32 deletions.
  1. +14 −0 web/data/management/commands/normalize.py
  2. +23 −1 web/data/models.py
  3. +40 −21 web/data/templates/scheme.html
  4. +1 −0 web/data/urls.py
  5. +9 −10 web/data/views.py
View
14 web/data/management/commands/normalize.py
@@ -128,6 +128,20 @@ def schemes(self):
COMMIT;
""", {'country' : self.country})
+ print "Making recipient scheme year totals"
+ cursor = connection.cursor()
+ cursor.execute("""
+ BEGIN;
+ DELETE FROM data_recipientschemeyear WHERE country=%(country)s;
+ COMMIT;
+ BEGIN;
+ INSERT INTO data_recipientschemeyear (recipient_id, scheme_id, country, year, total)
+ SELECT globalrecipientidx, globalschemeid, '%(country)s', '0', SUM(amounteuro)
+ FROM data_payment
+ GROUP BY globalschemeid, globalrecipientidx
+ COMMIT;
+ """, {'country' : self.country})
+
View
24 web/data/models.py
@@ -156,19 +156,41 @@ def get_absolute_url(self):
class SchemeYear(models.Model):
- globalschemeid = models.CharField(blank=True, max_length=40, db_index=True)
+ globalschemeid = models.ForeignKey(Scheme, db_column='globalschemeid')
nameenglish = models.TextField(blank=True)
countrypayment = models.CharField(blank=True, max_length=2)
year = models.IntegerField(blank=True, null=True)
total = models.FloatField()
objects = SchemeYearManager()
+ class Meta():
+ ordering = ('year',)
+
def get_absolute_url(self):
return reverse('scheme_view', args=[self.countrypayment,
self.globalschemeid,
slugify(self.nameenglish)])
+class RecipientSchemeYear(models.Model):
+ """
+ Denormalized data, containing the sum of the payments to each recipient in a
+ year, including '0' for all years
+ """
+ recipient = models.ForeignKey(Recipient)
+ scheme = models.ForeignKey(Scheme)
+ country = models.CharField(blank=True, max_length=2)
+ year = models.IntegerField(blank=True, null=True)
+ total = models.FloatField()
+
+ def __unicode__(self):
+ return u"%s - %s" (self.recipient, self.year)
+
+ class Meta():
+ ordering = ('-total',)
+
+ def get_absolute_url(self):
+ return reverse('recipient_view', args=[self.country, self.recipient_id, slugify(self.recipient.name)])
class SchemeType(models.Model):
View
61 web/data/templates/scheme.html
@@ -11,30 +11,49 @@
{% endblock %}
{% block content %}
- {% load pagination_tags %}
- {% autopaginate top_recipients 30 %}
- {% cache 10000000 scheme_recipients request.page %}
- <h3>All Recipients</h3>
- <table>
- <thead>
- <th>Name</th>
- <th class="money">Total (for this scheme, all years)</th>
- </thead>
- <tbody>
- {% for recipient in top_recipients %}
- <tr>
- <td><a href="/{{ recipient.countrypayment }}/recipient/{{ recipient.pk }}/{{ recipient.name|slugify }}">{{ recipient.name }}</a></td>
- <td class="money">&euro;{{ recipient.scheme_total|floatformat:0|intcomma }}</td>
- </tr>
- {% endfor %}
- </tbody>
- </table>
- {% endcache %}
- <h4 class="pager">{% paginate %}</h4>
+
+ {% if scheme_years %}
+ <div class="year_selector section">
+ Show subsidies for
+ {% if selected_year == 0 %}
+ <span class="selected">All Years</span>
+ {% else %}
+ <a href="{% url scheme_view country=country.code globalschemeid=scheme.pk name=scheme.nameenglish|slugify %}" class="selected">All Years</a>
+ {% endif %}
+
+ {% for year in scheme_years %}
+ {% if year.year == selected_year %}
+ <strong>{{ year.year }}</strong>
+ {% else %}
+ {% url scheme_view country=country.code globalschemeid=scheme.pk name=scheme.nameenglish|slugify year=year.year as year_url %}
+ <a href="{{ year_url }}">{{ year.year }}</a>
+ {% endif %}
+ {% endfor %}
+ </div>
+ {% endif %}
+
+ {% load pagination_tags %}
+ {% autopaginate top_recipients 30 %}
+ <h3>All Recipients</h3>
+ <table>
+ <thead>
+ <th>Name</th>
+ <th class="money">Total (for this scheme, all years)</th>
+ </thead>
+ <tbody>
+ {% for recipient in top_recipients %}
+ <tr>
+ <td><a href="{{ recipient.get_absolute_url }}">{{ recipient.recipient.name }}</a></td>
+ <td class="money">&euro;{{ recipient.total|floatformat:0|intcomma }}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+ <h4 class="pager">{% paginate %}</h4>
{% endblock %}
{% block right %}
- {% include "breadcrumbs.html" %}
+{% include "breadcrumbs.html" %}
{% endblock %}
View
1 web/data/urls.py
@@ -30,6 +30,7 @@ def country_url(pattern, *args, **kwargs):
# Schemes
country_url(r'scheme/$', 'all_schemes', name='all_schemes'),
+ country_url(r'scheme/(?P<globalschemeid>[^/]+)/(?P<name>(.*))/(?P<year>(\d+))/', 'scheme', name='scheme_view'),
country_url(r'scheme/(?P<globalschemeid>[^/]+)/(?P<name>(.*))/', 'scheme', name='scheme_view'),
View
19 web/data/views.py
@@ -240,31 +240,30 @@ def all_schemes(request, country='EU'):
)
-def scheme(request, country, globalschemeid, name):
+def scheme(request, country, globalschemeid, name, year=0):
"""
Show a single scheme and a list of top recipients to get payments under it
- `country` ISO country, as defined by countryCodes
- ``globalschemeid` globalschemeid from the data_schemes table in the database
"""
+ selected_year = int(year)
+
scheme = models.Scheme.objects.get(globalschemeid=globalschemeid)
# To add one day
- # scheme_years = models.SchemeYear.objects.filter(globalschemeid=globalschemeid)
- top_recipients = models.Recipient.objects.all()
- top_recipients = models.Recipient.objects.filter(
- payment__scheme=globalschemeid)\
- .values('name', 'pk', 'countrypayment')\
- .annotate(scheme_total=Sum('payment__amounteuro'))\
- .order_by('-scheme_total')
-
- top_recipients = CachedCountQuerySetWrapper(top_recipients, key="data.scheme.%s.%s.top_recipients" % (country, globalschemeid))
+ scheme_years = models.SchemeYear.objects.filter(globalschemeid=scheme)
+
+ top_recipients = models.RecipientSchemeYear.objects.filter(scheme=scheme, year=selected_year)
+
+ top_recipients = CachedCountQuerySetWrapper(top_recipients, key="data.scheme.%s.%s.%s.top_recipients" % (country, globalschemeid, year))
return render_to_response(
country_template('scheme.html', country),
{
'scheme' : scheme,
+ 'scheme_years' : scheme_years,
'top_recipients' : top_recipients,
},
context_instance=RequestContext(request)

0 comments on commit bd1013f

Please sign in to comment.