cciw-website / cciw / bookings /

# -*- coding: utf-8 -*-
from django import forms
from django.utils.html import escape
from django.utils.safestring import mark_safe

from cciw.bookings.models import BookingAccount, Booking, Price
from cciw.cciwmain.forms import CciwFormMixin
from cciw.cciwmain.common import get_thisyear
from cciw.cciwmain.models import Camp

class EmailForm(CciwFormMixin, forms.Form):
    email = forms.EmailField()

class AccountDetailsForm(CciwFormMixin, forms.ModelForm):
    class Meta:
        model = BookingAccount
        fields = [

# Need to override these to fix various details for use by user
AccountDetailsForm.base_fields['name'].required = True
AccountDetailsForm.base_fields['address'].required = True
AccountDetailsForm.base_fields['post_code'].required = True

class FixPriceMixin(object):
    Changes the 'price_type' field to include prices from the current year.
    def fix_price_choices(self):
        price_choices = self.fields['price_type'].choices
        prices = dict((p.price_type, p.price) for p in Price.objects.filter(year=get_thisyear()))

        for i, (price_type, label) in enumerate(price_choices):
            if price_type in prices:
                price_choices[i] = (price_choices[i][0], price_choices[i][1] + " - £%s" % prices[price_type])
        self.fields['price_type'].choices = price_choices

class AddPlaceForm(FixPriceMixin, CciwFormMixin, forms.ModelForm):

    camp = forms.ChoiceField(choices=[],

    def __init__(self, *args, **kwargs):
        super(AddPlaceForm, self).__init__(*args, **kwargs)
        def render_camp(c):
            return (escape("Camp %d, %s, %s" % (c.number, c.leaders_formatted,
                                                c.start_date.strftime("%e %b %Y"))) +
                    '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '<span class="placeAvailability">' +
                    escape("(Places left: %d total, max %d for boys, max %d for girls)" %
                           c.get_places_left()) + '</span>')
        self.fields['camp'].choices = [(, mark_safe(render_camp(c)))
                                       for c in Camp.objects.filter(year=get_thisyear())]

    class Meta:
        model = Booking
        fields = [

    def clean_camp(self):
        camp_id = self.cleaned_data['camp']
        return Camp.objects.get(id=int(camp_id))

AddPlaceForm.base_fields['agreement'].required = True
AddPlaceForm.base_fields['date_of_birth'].widget.attrs['placeholder'] = 'YYYY-MM-DD'
AddPlaceForm.base_fields['date_of_birth'].help_text = u'(YYYY-MM-DD)'
AddPlaceForm.base_fields['last_tetanus_injection'].widget.attrs['placeholder'] = 'YYYY-MM-DD'
AddPlaceForm.base_fields['last_tetanus_injection'].help_text = u'(YYYY-MM-DD)'
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
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.