2012-07-12 19:17:42 +00:00
|
|
|
from django import forms
|
|
|
|
from django.forms.models import inlineformset_factory, BaseInlineFormSet
|
|
|
|
|
|
|
|
from django.contrib.admin.widgets import AdminFileWidget
|
2015-06-26 03:46:09 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-07-12 19:17:42 +00:00
|
|
|
|
|
|
|
from symposion.sponsorship.models import Sponsor, SponsorBenefit
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorApplicationForm(forms.ModelForm):
|
2017-04-24 12:50:48 +00:00
|
|
|
|
|
|
|
required_css_class = 'label-required'
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user = kwargs.pop("user")
|
|
|
|
kwargs.update({
|
|
|
|
"initial": {
|
|
|
|
"contact_name": self.user.get_full_name,
|
|
|
|
"contact_email": self.user.email,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
super(SponsorApplicationForm, self).__init__(*args, **kwargs)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
class Meta:
|
|
|
|
model = Sponsor
|
2012-11-06 18:13:51 +00:00
|
|
|
fields = [
|
|
|
|
"name",
|
|
|
|
"external_url",
|
|
|
|
"contact_name",
|
|
|
|
"contact_email",
|
|
|
|
"level"
|
|
|
|
]
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
def save(self, commit=True):
|
|
|
|
obj = super(SponsorApplicationForm, self).save(commit=False)
|
|
|
|
obj.applicant = self.user
|
|
|
|
if commit:
|
|
|
|
obj.save()
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorDetailsForm(forms.ModelForm):
|
2017-04-24 12:50:48 +00:00
|
|
|
|
|
|
|
required_css_class = 'label-required'
|
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
class Meta:
|
|
|
|
model = Sponsor
|
|
|
|
fields = [
|
|
|
|
"name",
|
|
|
|
"external_url",
|
|
|
|
"contact_name",
|
|
|
|
"contact_email"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class SponsorBenefitsInlineFormSet(BaseInlineFormSet):
|
2017-04-24 12:50:48 +00:00
|
|
|
|
|
|
|
required_css_class = 'label-required'
|
|
|
|
|
2015-07-11 02:22:15 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['queryset'] = kwargs.get('queryset', self.model._default_manager).exclude(benefit__type="option")
|
|
|
|
super(SponsorBenefitsInlineFormSet, self).__init__(*args, **kwargs)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
def _construct_form(self, i, **kwargs):
|
|
|
|
form = super(SponsorBenefitsInlineFormSet, self)._construct_form(i, **kwargs)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
# only include the relevant data fields for this benefit type
|
|
|
|
fields = form.instance.data_fields()
|
|
|
|
form.fields = dict((k, v) for (k, v) in form.fields.items() if k in fields + ["id"])
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
for field in fields:
|
|
|
|
# don't need a label, the form template will label it with the benefit name
|
|
|
|
form.fields[field].label = ""
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
# provide word limit as help_text
|
|
|
|
if form.instance.benefit.type == "text" and form.instance.max_words:
|
2015-06-26 03:46:09 +00:00
|
|
|
form.fields[field].help_text = _("maximum %s words") % form.instance.max_words
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
# use admin file widget that shows currently uploaded file
|
|
|
|
if field == "upload":
|
|
|
|
form.fields[field].widget = AdminFileWidget()
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-12 19:17:42 +00:00
|
|
|
return form
|
|
|
|
|
|
|
|
|
|
|
|
SponsorBenefitsFormSet = inlineformset_factory(
|
|
|
|
Sponsor, SponsorBenefit,
|
|
|
|
formset=SponsorBenefitsInlineFormSet,
|
|
|
|
can_delete=False, extra=0,
|
|
|
|
fields=["text", "upload"]
|
|
|
|
)
|