4👍
✅
Here is what I came up with..
I couldn’t seem to get a modelForm to work nicely so I kept the two separate and merged them at done
. It isn’t perfect yet but it’s getting close..
class BulkHomeForm1(forms.Form):
lot_number = forms.CharField(max_length=16, required=True)
street_line1 = forms.CharField(max_length=100, required=True)
floorplan = forms.ModelChoiceField(queryset=Floorplan.objects.filter(is_active=True), required=False)
start_date = forms.DateField(required=False)
temp_storage_location = tempfile.mkdtemp(dir=settings.MEDIA_ROOT, prefix="bulk_homes_")
os.chmod(temp_storage_location, 02775) # TODO FIX ME
temp_storage = FileSystemStorage(location=temp_storage_location)
class BulkHomeWizard(SessionWizardView):
file_storage = temp_storage
def get_form(self, step=None, data=None, files=None):
form = super(BulkHomeWizard, self).get_form(step=step, data=data, files=files)
if self.steps.current == 'start' and form.prefix != "step2":
# Limit the subdivisions down to the specifics
sub_qs = Subdivision.objects.filter(is_active=True)
if self.request.user.company_type == "rater":
sub_qs = sub_qs.filter(rater_orgs=self.request.user.company.id)
elif self.request.user.company_type == "eep":
sub_qs = sub_qs.filter(eep_orgs=self.request.user.company.id)
form.fields['subdivision'].queryset = sub_qs
return form
def get_context_data(self, form, **kwargs):
context = super(BulkHomeWizard, self).get_context_data(form, **kwargs)
self.template_name = 'axis/bulk_%s.html' % self.steps.current
return context
def get_form_initial(self, step):
"""This is used to seed the model set with information from the previous step"""
if step == 'step2':
log.info("Into Step 2")
data = self.get_cleaned_data_for_step('start')['homes']
return data
return self.initial_dict.get(step, {})
def done(self, form_list, **kwargs):
cleaned_data = [form.cleaned_data for form in form_list]
subdivision = cleaned_data[0].get('subdivision')
city = subdivision.city
state = subdivision.state
zipcode = cleaned_data[0].get('zipcode')
for form in cleaned_data[1]:
data = Home.objects.get_or_create(lot_number = form.get('lot_number'),
floorplan = form.get('floorplan', None),
street_line1 = form.get('street_line1', None),
subdivision = subdivision,
city = subdivision.city, state=subdivision.state,
zipcode=zipcode,
start_date = form.get('start_date', None),)
obj, created = data
obj.clean()
obj.save()
if created:
log.info("Create new Home")
return HttpResponseRedirect(reverse("subdivision_view", kwargs={'subdivision_id': subdivision.id}))
Source:stackexchange.com