# HG changeset patch # User Madhusudan.C.S # Date 1289813416 -19800 # Node ID 9d90fbae12161dbdcddf1d0289efdd90c99787d9 # Parent 0fc144c69e84c4043c1eeee5b957efcb466f34c7 Add a form for Payment and respective save and clean methods. diff -r 0fc144c69e84 -r 9d90fbae1216 project/scipycon/registration/forms.py --- a/project/scipycon/registration/forms.py Mon Nov 15 14:59:46 2010 +0530 +++ b/project/scipycon/registration/forms.py Mon Nov 15 15:00:16 2010 +0530 @@ -4,6 +4,7 @@ from project.scipycon.registration.models import SIZE_CHOICES from project.scipycon.registration.models import OCCUPATION_CHOICES from project.scipycon.registration.models import Accommodation +from project.scipycon.registration.models import Payment from project.scipycon.registration.models import Wifi @@ -127,6 +128,49 @@ fields = ('accommodation_required', 'sex', 'accommodation_days') +class PaymentForm(forms.ModelForm): + """SciPyCon Payment form + """ + + def save(self, user, scope): + try: + payment = Payment.objects.get(user=user, scope=scope) + except ObjectDoesNotExist: + payment = Payment(user=user, scope=scope) + + paid = self.cleaned_data['paid'] + type = self.cleaned_data['type'] + details = self.cleaned_data['details'] + + payment.paid = paid + payment.type = type + payment.details = details + + payment.save() + + return payment + + def clean(self): + """Makes sure that payment form is correct, i.e. type and details + are filled in when the required fees is paid. + """ + + paid = self.cleaned_data['paid'] + type = self.cleaned_data['type'] + details = self.cleaned_data['details'] + + if paid and (not type or not details): + raise forms.ValidationError( + u"If you have already paid the fee it is mandatory to " + "fill in the type and mandatory fields.") + + return super(PaymentForm, self).clean() + + class Meta: + model = Payment + fields = ('paid', 'type', 'type') + + PC = ( ('all', 'all'), ('paid', 'paid'),