1
1
from itertools import chain
2
+ from django_countries .fields import CountryField
2
3
from django import forms
3
4
from django .utils .text import slugify
4
5
from django .utils .translation import ugettext_lazy as _
5
6
from django .utils .functional import cached_property
7
+ from django .conf import settings
6
8
7
9
from sponsors .models import (
8
10
SponsorshipBenefit ,
@@ -164,11 +166,25 @@ class SponsorshipApplicationForm(forms.Form):
164
166
max_length = 32 ,
165
167
required = False ,
166
168
)
167
- mailing_address = forms .CharField (
168
- label = "Sponsor Mailing/Billing Address" ,
169
+ mailing_address_line_1 = forms .CharField (
170
+ label = "Mailing Address line 1 " ,
169
171
widget = forms .TextInput ,
170
172
required = False ,
171
173
)
174
+ mailing_address_line_2 = forms .CharField (
175
+ label = "Mailing Address line 2" ,
176
+ widget = forms .TextInput ,
177
+ required = False ,
178
+ )
179
+
180
+ city = forms .CharField (max_length = 64 , required = False )
181
+ state = forms .CharField (
182
+ label = "State/Province/Region" , max_length = 64 , required = False
183
+ )
184
+ postal_code = forms .CharField (
185
+ label = "Zip/Postal Code" , max_length = 64 , required = False
186
+ )
187
+ country = CountryField ().formfield (required = False )
172
188
173
189
def __init__ (self , * args , ** kwargs ):
174
190
self .user = kwargs .pop ("user" , None )
@@ -203,6 +219,20 @@ def clean(self):
203
219
msg = "You have to mark at least one contact as the primary one."
204
220
raise forms .ValidationError (msg )
205
221
222
+ def clean_sponsor (self ):
223
+ sponsor = self .cleaned_data .get ("sponsor" )
224
+ if not sponsor :
225
+ return
226
+
227
+ if Sponsorship .objects .in_progress ().filter (sponsor = sponsor ).exists ():
228
+ msg = f"The sponsor { sponsor .name } already have open Sponsorship applications. "
229
+ msg += f"Get in contact with { settings .SPONSORSHIP_NOTIFICATION_FROM_EMAIL } to discuss."
230
+ raise forms .ValidationError (msg )
231
+
232
+ return sponsor
233
+
234
+ # Required fields are being manually validated because if the form
235
+ # data has a Sponsor they shouldn't be required
206
236
def clean_name (self ):
207
237
name = self .cleaned_data .get ("name" , "" )
208
238
sponsor = self .data .get ("sponsor" )
@@ -224,12 +254,33 @@ def clean_primary_phone(self):
224
254
raise forms .ValidationError ("This field is required." )
225
255
return primary_phone .strip ()
226
256
227
- def clean_mailing_address (self ):
228
- mailing_address = self .cleaned_data .get ("mailing_address" , "" )
257
+ def clean_mailing_address_line_1 (self ):
258
+ mailing_address_line_1 = self .cleaned_data .get ("mailing_address_line_1" , "" )
259
+ sponsor = self .data .get ("sponsor" )
260
+ if not sponsor and not mailing_address_line_1 :
261
+ raise forms .ValidationError ("This field is required." )
262
+ return mailing_address_line_1 .strip ()
263
+
264
+ def clean_city (self ):
265
+ city = self .cleaned_data .get ("city" , "" )
266
+ sponsor = self .data .get ("sponsor" )
267
+ if not sponsor and not city :
268
+ raise forms .ValidationError ("This field is required." )
269
+ return city .strip ()
270
+
271
+ def clean_postal_code (self ):
272
+ postal_code = self .cleaned_data .get ("postal_code" , "" )
273
+ sponsor = self .data .get ("sponsor" )
274
+ if not sponsor and not postal_code :
275
+ raise forms .ValidationError ("This field is required." )
276
+ return postal_code .strip ()
277
+
278
+ def clean_country (self ):
279
+ country = self .cleaned_data .get ("country" , "" )
229
280
sponsor = self .data .get ("sponsor" )
230
- if not sponsor and not mailing_address :
281
+ if not sponsor and not country :
231
282
raise forms .ValidationError ("This field is required." )
232
- return mailing_address .strip ()
283
+ return country .strip ()
233
284
234
285
def save (self ):
235
286
selected_sponsor = self .cleaned_data .get ("sponsor" )
@@ -240,7 +291,12 @@ def save(self):
240
291
name = self .cleaned_data ["name" ],
241
292
web_logo = self .cleaned_data ["web_logo" ],
242
293
primary_phone = self .cleaned_data ["primary_phone" ],
243
- mailing_address = self .cleaned_data ["mailing_address" ],
294
+ mailing_address_line_1 = self .cleaned_data ["mailing_address_line_1" ],
295
+ mailing_address_line_2 = self .cleaned_data .get ("mailing_address_line_2" , "" ),
296
+ city = self .cleaned_data ["city" ],
297
+ state = self .cleaned_data .get ("state" , "" ),
298
+ postal_code = self .cleaned_data ["postal_code" ],
299
+ country = self .cleaned_data ["country" ],
244
300
description = self .cleaned_data .get ("description" , "" ),
245
301
landing_page_url = self .cleaned_data .get ("landing_page_url" , "" ),
246
302
print_logo = self .cleaned_data .get ("print_logo" ),
0 commit comments