class ChoosePartnerForm(forms.Form): partSvc = PartnerService(Config()) partKeys = partSvc.fetchPartnerKeys() # define validator alnumPunc = RegexValidator( r'^[\w\.\-\,\"\' ]+$', 'Only letters, numbers, spaces, quotes, commas and periods are allowed.' ) # define fields prop_partnerKey = forms.ChoiceField(label='Partner', choices=partKeys)
class PropertyForm(forms.Form): config = Config() partSvc = PartnerService(config) partKeys = partSvc.fetchPartnerKeys() # define validator alnumPunc = RegexValidator( r'^[\w\.\-\,\"\' ]+$', 'Only letters, numbers, spaces, quotes, commas and periods are allowed.' ) # define fields prop_partnerKey = forms.ChoiceField(label='Partner', choices=partKeys) prop_propName = forms.CharField(label='Property Name', validators=[alnumPunc])
def updateContact(request): # retrieve form and property services formSvc = FormService() message = '' config = Config() partSvc = PartnerService(config) propSvc = PropertyService(config) # number of validation checks to be performed check = 4 # if this is a POST request process the form data if request.method == 'POST': # hydrate subforms from post data formSvc.hydrateFormsFromPost(request.POST) name_form = formSvc.getForm('name') contact_form = formSvc.getForm('contact') # validate partner key if 'prop_partnerKey' in request.POST: partKey = request.POST['prop_partnerKey'] part = partSvc.fetchByKey(partKey) if part: check -= 1 else: message += '<br>Unable to confirm this partner.' else: message += '<br>You need to choose a partner.' # validate property key if 'prop_propertyKey' in request.POST: propKey = request.POST['prop_propertyKey'] prop = propSvc.fetchByKey(propKey) if prop: check -= 1 else: message += '<br>Unable to confirm this property.' else: message += '<br>You need to choose a property.' # validate form data if name_form.is_valid(): check -= 1 if contact_form.is_valid(): check -= 1 # proceed if all validation checks are successful if check == 0: # extract name and contact information from subforms prop.set('contactName', Name(formSvc.getCleanDataByKey('name'))) prop.set('contactInfo', Contact(formSvc.getCleanDataByKey('contact'))) # use PropertyService to update Property if propSvc.edit(prop): message += '<br>Property contact information updated successfully!' return HttpResponseRedirect('/property/list/' + prop.getKey()) else: message += '<br>Unable to update form data.' else: message += '<br>Unable to validate this form.' # build master form context = { 'message': message, 'choose_part_form': ChoosePartnerForm(), 'name_form_html': render_to_string('property_name.html', {'form': formSvc.getForm('name')}), 'contact_form_html': render_to_string('property_contact.html', {'form': formSvc.getForm('contact')}) } prop_master = render_to_string('property_update_contact.html', context) # embed form in layout page = render_to_string('layout.html', {'contents': prop_master}) return HttpResponse(page)