Commit 1937c3ec authored by alexAubin's avatar alexAubin

Make sure that we can extend/customize the forms for each specific offer type

parent ee242656
# -*- coding: utf-8 -*-
from django import forms
from django.shortcuts import get_object_or_404
from django.forms.widgets import Select
from coin.offers.models import Offer, OfferSubscriptionRequest
from coin.configuration.models import Configuration
......@@ -10,7 +9,7 @@ class OfferAdminForm(forms.ModelForm):
class Meta:
model = Offer
widgets = {
'configuration_type': Select(choices=(('','---------'),) + Configuration.get_configurations_choices_list())
'configuration_type': Select(choices=(('', '---------'),) + Configuration.get_configurations_choices_list())
}
exclude = ('', )
......@@ -25,18 +24,17 @@ class OfferSubscriptionRequestStep1Form(forms.Form):
class OfferSubscriptionRequestStep2Form(forms.Form):
offer = forms.ChoiceField(label="Offre", required=True)
offer = forms.ModelChoiceField(queryset=None, label="Offre", required=True)
member_comments = forms.CharField(label="Commentaires pour l'équipe de bénévole", max_length=1000, widget=forms.Textarea(attrs={'style': 'height: 10em;'}), required=False)
agree_tos = forms.BooleanField(label= "Vous acceptez les conditions d'abonnement et d'utilisation de cette offre", required=True)
agree_tos = forms.BooleanField(label="Vous acceptez les conditions d'abonnement et d'utilisation de cette offre", required=True)
subscriptionrequest_class = OfferSubscriptionRequest
def set_offer_choices(self, offer_type):
self.fields['offer'].choices = tuple((offer.reference, str(offer)) for offer in OfferSubscriptionRequest.requestable_offers(offer_type=offer_type))
self.fields['offer'].queryset = OfferSubscriptionRequest.requestable_offers(offer_type=offer_type)
def create_offersubscriptionrequest(self, request):
offer = get_object_or_404(Offer, reference=self.cleaned_data["offer"], requestable=True)
subscriptionrequest = OfferSubscriptionRequest.objects.create(offer=offer,
member=request.user,
member_comments=self.cleaned_data["member_comments"])
subscriptionrequest.save()
return self.subscriptionrequest_class.objects.create(offer=self.cleaned_data["offer"],
member=request.user,
member_comments=self.cleaned_data["member_comments"])
......@@ -246,7 +246,7 @@ class OfferSubscriptionRequest(Request):
related_name='subscriptionrequest',
verbose_name='abonnement créé')
configuration_type = None
offer_type = None
def accept(self):
......@@ -257,6 +257,9 @@ class OfferSubscriptionRequest(Request):
super(OfferSubscriptionRequest, self).accept()
@property
def configuration_type(self):
return Offer._get_configuration_type(self.offer_type)
@staticmethod
def requestable_offers(offer_type=None):
......
......@@ -70,14 +70,21 @@ def offersubscriptionrequest_step1(request):
def offersubscriptionrequest_step2(request, offer_type):
if offer_type not in OfferSubscriptionRequest.requestable_offertypes():
return HttpResponseServerError("Ce type d'offre n'est pas correct.")
# Try to get the form class specific to this offer type.
# If we do not find such a class, fallback to the regular "OfferSubscriptionRequestStep2Form"
FormsPerOfferType = {x.offer_type:x for x in OfferSubscriptionRequestStep2Form.__subclasses__()}
FormForThisOfferType = FormsPerOfferType.get(offer_type, OfferSubscriptionRequestStep2Form)
if request.method == 'POST':
form = OfferSubscriptionRequestStep2Form(request.POST)
form = FormForThisOfferType(request.POST)
form.set_offer_choices(offer_type)
if form.is_valid():
form.create_offersubscriptionrequest(request)
subscriptionrequest = form.create_offersubscriptionrequest(request)
subscriptionrequest.save()
return HttpResponseRedirect(reverse('members:subscriptionrequest_step3'))
else:
form = OfferSubscriptionRequestStep2Form()
form = FormForThisOfferType()
form.set_offer_choices(offer_type)
return render(request, 'members/offersubscriptionrequest/new_step2.html', {'form': form, 'offer_type': offer_type})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment