# HG changeset patch # User Brian Neal # Date 1348706006 18000 # Node ID 00c14431e911541a0bb0e87fbbb44b9542f04134 # Parent 8c901a03b2e3b017d834d093bfc8e5f2f2cfe0cc Created a donations side block for BB issue 21. diff -r 8c901a03b2e3 -r 00c14431e911 donations/fixtures/donations_test.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/donations/fixtures/donations_test.json Wed Sep 26 19:33:26 2012 -0500 @@ -0,0 +1,62 @@ +[ + { + "pk": 150, + "model": "donations.donation", + "fields": { + "payment_date": "2012-09-20T02:54:40", + "first_name": "Matthew", + "last_name": "Jones", + "txn_id": "31JFAKETXN_ID0118", + "is_anonymous": false, + "memo": "", + "mc_fee": "1.08", + "payer_id": "HLCCFAKEZIDTS", + "payer_email": "fake@example.com", + "user": null, + "mc_gross": "20.00", + "payer_status": "verified", + "txn_type": "web_accept", + "test_ipn": false + } + }, + { + "pk": 151, + "model": "donations.donation", + "fields": { + "payment_date": "2012-09-19T23:02:20", + "first_name": "Daniel", + "last_name": "Smith", + "txn_id": "17UFAKETXNID16417", + "is_anonymous": false, + "memo": "", + "mc_fee": "0.45", + "payer_id": "S4UFAKEPAYERY", + "payer_email": "fake2@example.com", + "user": null, + "mc_gross": "5.00", + "payer_status": "verified", + "txn_type": "web_accept", + "test_ipn": false + } + }, + { + "pk": 149, + "model": "donations.donation", + "fields": { + "payment_date": "2012-08-19T19:00:21", + "first_name": "Daryl", + "last_name": "A", + "txn_id": "75PFAKETXNID77721", + "is_anonymous": false, + "memo": "", + "mc_fee": "1.03", + "payer_id": "QUFAKEPAYERID", + "payer_email": "fake3@example.com", + "user": null, + "mc_gross": "25.00", + "payer_status": "unverified", + "txn_type": "web_accept", + "test_ipn": false + } + } +] diff -r 8c901a03b2e3 -r 00c14431e911 donations/models.py --- a/donations/models.py Sat Sep 22 11:27:16 2012 -0500 +++ b/donations/models.py Wed Sep 26 19:33:26 2012 -0500 @@ -7,9 +7,12 @@ from django.db import models from django.contrib.auth.models import User from django.conf import settings +from django.db.models import Sum class DonationManager(models.Manager): + """Manager for the Donations model.""" + def monthly_stats(self, year=None, month=None): """ Returns a tuple of items for the given month in the given @@ -43,6 +46,36 @@ return gross, net, donations + def monthly_goal_pct(self, year=None, month=None, limit=True): + """Returns progress towards the given monthly goal as an integer + percent. + + If year is None, the current year is used. + If month is None, the current month is used. + If limit is True, the return value is limited to 100. + + """ + today = datetime.datetime.today() + if year is None: + year = today.year + if month is None: + month = today.month + + r = self.filter(payment_date__year=year, payment_date__month=month).aggregate( + Sum('mc_gross'), Sum('mc_fee')) + + gross, fee = r['mc_gross__sum'], r['mc_fee__sum'] + + if gross is not None and fee is not None: + pct = int((gross - fee) / settings.DONATIONS_GOAL * 100) + else: + pct = 0 + + if limit: + pct = min(pct, 100) + + return pct + class Donation(models.Model): """Model to represent a donation to the website.""" diff -r 8c901a03b2e3 -r 00c14431e911 donations/templatetags/donations_tags.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/donations/templatetags/donations_tags.py Wed Sep 26 19:33:26 2012 -0500 @@ -0,0 +1,17 @@ +""" +Template tags for the donations application. + +""" +from django import template + +from donations.models import Donation + + +register = template.Library() + + +@register.inclusion_tag('donations/monthly_goal_tag.html') +def monthly_goal(): + return { + 'pct': Donation.objects.monthly_goal_pct() + } diff -r 8c901a03b2e3 -r 00c14431e911 donations/tests.py --- a/donations/tests.py Sat Sep 22 11:27:16 2012 -0500 +++ b/donations/tests.py Wed Sep 26 19:33:26 2012 -0500 @@ -8,6 +8,7 @@ from django.contrib.auth.models import User from django.test import TestCase from django.core.urlresolvers import reverse +from django.conf import settings from donations.models import Donation import bio.badges @@ -27,7 +28,7 @@ mc_gross=100.00&protection_eligibility=Ineligible&payer_id=FAKEPAYERID02&tax=0.00&payment_date=05:40:33 Jan 16, 2011 PST&payment_status=Completed&charset=windows-1252&first_name=John&option_selection1=No&mc_fee=3.20¬ify_version=3.0&custom=test_user&payer_status=unverified&business=brian@surfguitar101.com&quantity=1&verify_sign=AIkKNFAKE-DATA-NOT-REALpqCSxA-E7Tm4rMGlUpNy6ym0.exBzfiyI&payer_email=test_user@example.com&option_name1=List your name?&txn_id=TESTTXNIDK548343A&payment_type=instant&last_name=Doe&receiver_email=brian@surfguitar101.com&payment_fee=3.20&receiver_id=FAKERECEIVERU&txn_type=web_accept&item_name=Donation for www.surfguitar101.com&mc_currency=USD&item_number=501&residence_country=US&handling_amount=0.00&transaction_subject=test_user&payment_gross=100.00&shipping=0.00""" -class DonationsTest(TestCase): +class DonationsIpnTestCase(TestCase): fixtures = ['badges'] def test_ipn_post_1(self): @@ -108,3 +109,39 @@ # user should not have got a badge for donating p = user.get_profile() self.assertEqual(p.badges.count(), 0) + + +class DonationsManagerTestCase(TestCase): + fixtures = ['donations_test'] + + def test_monthly_goal_pct(self): + + self.assertEqual(settings.DONATIONS_GOAL, Decimal('100.00')) + pct = Donation.objects.monthly_goal_pct(year=2012, month=9) + self.assertEqual(pct, 23) + + def test_monthly_goal_pct2(self): + + # Ensure we get 0 when there are no records + + Donation.objects.all().delete() + pct = Donation.objects.monthly_goal_pct(year=2012, month=9) + self.assertEqual(pct, 0) + + def test_monthly_stats(self): + + gross, net, donations = Donation.objects.monthly_stats(year=2012, month=9) + self.assertEqual(gross, Decimal('25.00')) + self.assertEqual(net, Decimal('23.47')) + self.assertEqual(len(donations), 2) + + def test_monthly_stats2(self): + + # Ensure we get 0 when there are no records + + Donation.objects.all().delete() + + gross, net, donations = Donation.objects.monthly_stats(year=2012, month=9) + self.assertEqual(gross, Decimal('0.00')) + self.assertEqual(net, Decimal('0.00')) + self.assertEqual(len(donations), 0) diff -r 8c901a03b2e3 -r 00c14431e911 sg101/templates/base.html --- a/sg101/templates/base.html Sat Sep 22 11:27:16 2012 -0500 +++ b/sg101/templates/base.html Wed Sep 26 19:33:26 2012 -0500 @@ -11,6 +11,7 @@ {% load potd_tags %} {% load script_tags %} {% load poll_tags %} +{% load donations_tags %} {% load core_tags %} {% load cache %} SurfGuitar101.com | {% block title %}{% endblock %} @@ -86,6 +87,9 @@ {% cache 3600 poll_block %} {% latest_poll_block %} {% endcache %} + {% cache 600 donations_block %} + {% monthly_goal %} + {% endcache %} {% cache 3600 birthday_block %} {% birthday_block %} {% endcache %} diff -r 8c901a03b2e3 -r 00c14431e911 sg101/templates/donations/monthly_goal_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sg101/templates/donations/monthly_goal_tag.html Wed Sep 26 19:33:26 2012 -0500 @@ -0,0 +1,12 @@ +{% extends 'side_block.html' %} +{% load url from future %} +{% block block_title %}Donations{% endblock %} +{% block block_content %} +

Help us meet our monthly goal:

+
+
  +
+
+

{{ pct }}%

+

Donate Now

+{% endblock %}