class MultipleChoiceForm(PrefixIdMixin, forms.Form): multiple_choice = fields.MultipleChoiceField( label='Q1: Multiple choice checkboxes', help_text='This is some help text.', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=( ('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ), )
class CategoryForm(forms.Form): error_css_class = 'input-field-container has-error' CATEGORY_CHOICES = ( 'Securing upfront funding', 'Offering competitive but secure payment terms', 'Guidance on export finance and insurance', ) categories = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=((choice, choice) for choice in CATEGORY_CHOICES))
class CompanyDetailsForm(forms.Form): EXPORT_CHOICES = ( 'I have customers outside the UK', 'I supply UK companies that sell overseas', 'I don\'t currently export or supply businesses that export', ) INDUSTRY_CHOICES = [('', '')] + [(value.replace('_', ' ').title(), label) for (value, label) in choices.INDUSTRIES ] + [('Other', 'Other')] error_css_class = 'input-field-container has-error' trading_name = fields.CharField(label='Trading name') company_number = fields.CharField(label='Companies House number', required=False) address_line_one = fields.CharField(label='Building and street') address_line_two = fields.CharField(label='', required=False) address_town_city = fields.CharField(label='Town or city') address_county = fields.CharField(label='County') address_post_code = fields.CharField(label='Postcode') industry = fields.ChoiceField(initial='thing', choices=INDUSTRY_CHOICES) industry_other = fields.CharField( label='Type in your industry', widget=TextInput(attrs={'class': 'js-field-other'}), required=False, ) export_status = fields.MultipleChoiceField( label='Do you currently export?', help_text='Select all that apply', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=((choice, choice) for choice in EXPORT_CHOICES), ) def clean(self): cleaned_data = super().clean() return { **cleaned_data, 'not_companies_house': not cleaned_data.get('company_number') }
class DemoFormErrors(PrefixIdMixin, forms.Form): text_field1 = fields.CharField(label='Simple text field', help_text='Some help text', required=True) checkbox1 = fields.BooleanField( label='Label text', required=True, help_text=('Some help text.'), widget=widgets.CheckboxWithInlineLabel(attrs={'id': 'checkbox-one'})) multiple_choice = fields.MultipleChoiceField( label='Multiple choice checkboxes', required=True, help_text='Some help text.', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=( ('red', 'Red'), ('green', 'Green'), ('blue', 'Blue'), ), ) radio = fields.ChoiceField(label='Radio select', required=True, label_suffix='', help_text='Some help text.', widget=widgets.RadioSelect( use_nice_ids=True, attrs={'id': 'radio-one'}), choices=( (True, 'Yes'), (False, 'No'), ))
class CompanySearchForm(forms.Form): MESSAGE_MINIMUM_VIABLE_SEARCH = ( 'Please specify a search term or expertise.') q = fields.CharField( label='Search by product, service or company keyword', max_length=255, required=False, widget=TextInput( attrs={ 'placeholder': 'Search for UK suppliers', 'autofocus': 'autofocus', 'dir': 'auto' }), ) page = IntegerField( required=False, widget=HiddenInput, initial=1, ) expertise_industries = fields.MultipleChoiceField( label='Industry expertise', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-industry-expertise'}, use_nice_ids=True, ), choices=choices.INDUSTRIES, required=False, ) expertise_regions = fields.MultipleChoiceField( label='Regional expertise', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-regional-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_REGION_CHOICES, required=False, ) expertise_countries = fields.MultipleChoiceField( label='International expertise', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-international-expertise'}, use_nice_ids=True, ), choices=choices.COUNTRY_CHOICES, required=False, ) expertise_languages = fields.MultipleChoiceField( label='Language expertise', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-language-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_LANGUAGES, required=False, ) expertise_products_services_financial = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-expertise-products-services-financial'}, use_nice_ids=True, ), choices=choices.EXPERTISE_FINANCIAL, required=False, ) expertise_products_services_management = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-products-services-management-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_MANAGEMENT_CONSULTING, required=False, ) expertise_products_services_human_resources = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-products-services-human-expertise'}, use_nice_ids=True, ), choices=[(value.replace(' ', '-'), label) for value, label in choices.EXPERTISE_HUMAN_RESOURCES], required=False, ) expertise_products_services_legal = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-products-services-legal-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_LEGAL, required=False, ) expertise_products_services_publicity = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-products-services-publicity-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_PUBLICITY, required=False, ) expertise_products_services_business_support = fields.MultipleChoiceField( label='', widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-products-services-further-expertise'}, use_nice_ids=True, ), choices=choices.EXPERTISE_BUSINESS_SUPPORT, required=False, ) def clean_page(self): return self.cleaned_data['page'] or self.fields['page'].initial def clean(self): super().clean() # these field values are all stored in expertise_products_services, but # the form expresses them as separate fields for better user experience product_services_fields = [ 'expertise_products_services_financial', 'expertise_products_services_management', 'expertise_products_services_human_resources', 'expertise_products_services_legal', 'expertise_products_services_publicity', 'expertise_products_services_business_support', ] products_services = [] for field_name in product_services_fields: if field_name in self.cleaned_data: products_services += self.cleaned_data.get(field_name) self.cleaned_data['expertise_products_services_labels'] = ( products_services) def clean_expertise_products_services_human_resources(self): # Hack for AWS WAF 403 caused by spaces in 'on' within the querystring field = 'expertise_products_services_human_resources' return [item.replace('-', ' ') for item in self.cleaned_data[field]]
class HighPotentialOpportunityForm(forms.Form): action_class = GovNotifyAction COMPANY_SIZE_CHOICES = [ ('1 - 10', '1 - 10'), ('11 - 50', '11 - 50'), ('51 - 250', '51 - 250'), ('250+', '250+'), ] def __init__(self, field_attributes, opportunity_choices, *args, **kwargs): for field_name, field in self.base_fields.items(): attributes = field_attributes.get(field_name) if attributes: field.__dict__.update(attributes) self.base_fields['opportunities'].choices = opportunity_choices return super().__init__(*args, **kwargs) full_name = fields.CharField() role_in_company = fields.CharField() email_address = fields.EmailField() phone_number = fields.CharField() company_name = fields.CharField() website_url = fields.CharField(required=False) country = fields.ChoiceField( choices=[('', 'Please select')] + choices.COUNTRY_CHOICES, widget=Select(attrs={'id': 'js-country-select'}), ) company_size = fields.ChoiceField(choices=COMPANY_SIZE_CHOICES) opportunities = fields.MultipleChoiceField( widget=widgets.CheckboxSelectInlineLabelMultiple( attrs={'id': 'checkbox-multiple'}, use_nice_ids=True, ), choices=[] # set in __init__ ) comment = fields.CharField(widget=Textarea, required=False) terms_agreed = fields.BooleanField(label=mark_safe( 'Tick this box to accept the ' f'<a href="{urls.TERMS_AND_CONDITIONS}" target="_blank">terms and ' 'conditions</a> of the great.gov.uk service.')) captcha = ReCaptchaField( label='', label_suffix='', ) @property def serialized_data(self): formatted_opportunities = [ '• {opportunity[1]}: {opportunity[0]}'.format(opportunity=item) for item in self.base_fields['opportunities'].choices if item[0] in self.cleaned_data['opportunities'] ] return { **self.cleaned_data, 'opportunity_urls': '\n'.join(formatted_opportunities), } def send_agent_email(self, form_url): sender = Sender(email_address=self.cleaned_data['email_address'], country_code=self.cleaned_data['country']) action = self.action_class( template_id=settings.HPO_GOV_NOTIFY_AGENT_TEMPLATE_ID, email_address=settings.HPO_GOV_NOTIFY_AGENT_EMAIL_ADDRESS, form_url=form_url, sender=sender, ) response = action.save(self.serialized_data) response.raise_for_status() def send_user_email(self, form_url): # no need to set `sender` as this is just a confirmation email. action = self.action_class( template_id=settings.HPO_GOV_NOTIFY_USER_TEMPLATE_ID, email_address=self.cleaned_data['email_address'], form_url=form_url, email_reply_to_id=settings.HPO_GOV_NOTIFY_USER_REPLY_TO_ID, ) response = action.save(self.serialized_data) response.raise_for_status() def save(self, form_url): self.send_agent_email(form_url=form_url) self.send_user_email(form_url=form_url)