Luke Plant avatar Luke Plant committed d989baa

Added 'birthdays' sheet to exported camper data

Comments (0)

Files changed (2)

cciw/bookings/tests.py

 # -*- coding: utf-8 -*-
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, date
 from decimal import Decimal
 import re
 
         self.camp = Camp.objects.create(year=get_thisyear(), number=1,
                                         minimum_age=self.camp_minimum_age,
                                         maximum_age=self.camp_maximum_age,
-                                        start_date=datetime.now() + timedelta(20),
-                                        end_date=datetime.now() + timedelta(27),
+                                        start_date=date.today() + timedelta(20),
+                                        end_date=date.today() + timedelta(27),
                                         site_id=1)
 
 
 
     fixtures = ['basic.json', 'officers_users.json']
 
-    def test_get(self):
+    def test_summary(self):
         self.create_place()
         acc = self.get_account()
         acc.bookings.update(state=BOOKING_BOOKED)
 
         workbook = camp_bookings_to_xls(self.camp)
-        self.assertTrue(workbook is not None)
         wkbk = xlrd.open_workbook(file_contents=workbook)
         wksh_all = wkbk.sheet_by_index(0)
 
         self.assertEqual(wksh_all.cell(0, 0).value, u"First name")
         self.assertEqual(wksh_all.cell(1, 0).value, acc.bookings.all()[0].first_name)
 
+    def test_birthdays(self):
+        camp = self.camp
+        bday = self.camp.start_date + timedelta(1)
+        dob = bday.replace(bday.year - 12)
+        self.create_place({'date_of_birth': dob.isoformat()})
+
+        acc = self.get_account()
+        acc.bookings.update(state=BOOKING_BOOKED)
+
+        workbook = camp_bookings_to_xls(self.camp)
+        wkbk = xlrd.open_workbook(file_contents=workbook)
+        wksh_bdays = wkbk.sheet_by_index(1)
+
+        self.assertEqual(wksh_bdays.cell(0, 0).value, u"First name")
+        self.assertEqual(wksh_bdays.cell(1, 0).value, acc.bookings.all()[0].first_name)
+
+        self.assertEqual(wksh_bdays.cell(0, 2).value, u"Birthday")
+        self.assertEqual(wksh_bdays.cell(1, 2).value, bday.strftime("%A %d %B"))
+
+        self.assertEqual(wksh_bdays.cell(0, 3).value, u"Age")
+        self.assertEqual(wksh_bdays.cell(1, 3).value, "12")

cciw/bookings/utils.py

+from dateutil.relativedelta import relativedelta
 import xlwt
 
 from cciw.utils.xl import add_sheet_with_header_row, workbook_to_string
 
 def camp_bookings_to_xls(camp):
+    bookings = list(camp.bookings.confirmed().order_by('first_name', 'last_name'))
+
     wkbk = xlwt.Workbook(encoding='utf8')
     columns = [('First name', lambda b: b.first_name),
                ('Last name', lambda b: b.last_name),
                ('Address', lambda b: b.address),
                ]
 
+
     wksh_campers = add_sheet_with_header_row(wkbk,
                                              "Summary",
                                              [n for n, f in columns],
                                              [[f(b) for n, f in columns]
-                                              for b in camp.bookings.confirmed().order_by('first_name', 'last_name')])
+                                              for b in bookings])
+
+    def get_birthday(b):
+        start = camp.start_date
+        born = b.date_of_birth
+        try:
+            return born.replace(year=start.year)
+        except ValueError:
+            # raised when birth date is February 29 and the current year is not a leap year
+            return born.replace(year=start.year, day=born.day - 1)
+
+    bday_columns = [('First name', lambda b: b.first_name),
+                    ('Last name', lambda b: b.last_name),
+                    ('Birthday', lambda b: get_birthday(b).strftime("%A %d %B")),
+                    ('Age', lambda b: unicode(relativedelta(get_birthday(b), b.date_of_birth).years)),
+                    ('Date of birth', lambda b: b.date_of_birth)
+                    ]
+
+
+    wksh_bdays = add_sheet_with_header_row(wkbk,
+                                           "Birthdays",
+                                           [n for n, f in bday_columns],
+                                           [[f(b) for n, f in bday_columns]
+                                            for b in bookings if
+                                            camp.start_date <= get_birthday(b) <= camp.end_date])
+
     return workbook_to_string(wkbk)
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.