Commits

Luke Plant committed 77ee5ce

Added export camp bookings functionality

  • Participants
  • Parent commits c6da8a5
  • Branches bookings

Comments (0)

Files changed (5)

File cciw/bookings/tests.py

 from django.core.urlresolvers import reverse
 from django.test import TestCase
 from django.utils import simplejson
+import xlrd
 
 from cciw.bookings.management.commands.expire_bookings import Command as ExpireBookingsCommand
 from cciw.bookings.models import BookingAccount, Price, Booking, Payment, ChequePayment, RefundPayment, book_basket_now
 from cciw.bookings.models import PRICE_FULL, PRICE_2ND_CHILD, PRICE_3RD_CHILD, PRICE_CUSTOM, PRICE_SOUTH_WALES_TRANSPORT, PRICE_DEPOSIT, BOOKING_APPROVED, BOOKING_INFO_COMPLETE, BOOKING_BOOKED, BOOKING_CANCELLED, BOOKING_CANCELLED_FULL_REFUND
+from cciw.bookings.utils import camp_bookings_to_xls
 from cciw.cciwmain.common import get_thisyear
 from cciw.cciwmain.models import Camp
 from cciw.cciwmain.tests.mailhelpers import read_email_url
 
         acc = self.get_account()
         self.assertEqual(acc.get_balance(), place.amount_due)
+
+
+class TestExportPlaces(CreatePlaceMixin, TestCase):
+
+    fixtures = ['basic.json', 'officers_users.json']
+
+    def test_get(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)
+

File cciw/bookings/utils.py

+import xlwt
+
+from cciw.utils.xl import add_sheet_with_header_row, workbook_to_string
+
+def camp_bookings_to_xls(camp):
+    wkbk = xlwt.Workbook(encoding='utf8')
+    columns = [('First name', lambda b: b.first_name),
+               ('Last name', lambda b: b.last_name),
+               ('Sex', lambda b: b.get_sex_display()),
+               ('Date of birth', lambda b: b.date_of_birth),
+               ('Age on camp', lambda b: b.age_on_camp().years),
+               ('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')])
+    return workbook_to_string(wkbk)
+
+

File cciw/officers/urls.py

     (r'^leaders/references/(?P<year>\d{4})/(?P<number>\d+)/$', 'manage_references'),
     (r'^leaders/officer-list/(?P<year>\d{4})/(?P<number>\d+)/$', 'officer_list'),
     (r'^leaders/export-officer-data/(?P<year>\d{4})/(?P<number>\d+)/$', 'export_officer_data'),
+    (r'^leaders/export-camper-data/(?P<year>\d{4})/(?P<number>\d+)/$', 'export_camper_data'),
     (r'^leaders/remove-officer/(?P<year>\d{4})/(?P<number>\d+)/$', 'remove_officer'),
     (r'^leaders/add-officers/(?P<year>\d{4})/(?P<number>\d+)/$', 'add_officers'),
     (r'^leaders/update-officer/$', 'update_officer'),

File cciw/officers/views.py

 from django.views.generic.base import TemplateView
 
 from cciw.auth import is_camp_admin, is_wiki_user, is_cciw_secretary, is_camp_officer, is_booking_secretary
+from cciw.bookings.utils import camp_bookings_to_xls
 from cciw.cciwmain import common
 from cciw.cciwmain.decorators import json_response
 from cciw.cciwmain.models import Camp
     return response
 
 
+@staff_member_required
+@camp_admin_required
+def export_camper_data(request, year=None, number=None):
+    camp = _get_camp_or_404(year, number)
+    response = HttpResponse(camp_bookings_to_xls(camp), mimetype="application/vnd.ms-excel")
+    response['Content-Disposition'] = ('attachment; filename=camp-%d-%d-campers.xls'
+                                       % (camp.year, camp.number))
+    return response
+
+
 officer_files = access_folder_securely("officers",
                                        lambda request: request.user.is_authenticated() and is_camp_officer(request.user))
 

File templates/cciw/officers/camp_management_links.html

     <br />&nbsp;&nbsp;&nbsp;<a href="{% url 'cciw.officers.views.manage_applications' year=c.year number=c.number %}">Applications</a>
     <br />&nbsp;&nbsp;&nbsp;<a href="{% url 'cciw.officers.views.manage_references' year=c.year number=c.number %}">References</a>
     <br />&nbsp;&nbsp;&nbsp;<a href="{% url 'cciw.officers.views.stats' year=c.year %}">Stats {{ c.year }}</a>
+    <br />&nbsp;&nbsp;&nbsp;<a href="{% url 'cciw.officers.views.export_camper_data' year=c.year number=c.number %}">Campers</a>
     </li>
     {% endfor %}