Package: directory-forms-api-client
directory-forms-api-clientDirectory forms API client for headless forms.RequirementsInstallation$ pip install directory_forms_api_clientClient EnrollmentThe api client must be created in directory-forms-api's admin panel to accessDIRECTORY_FORMS_API_API_KEYandDIRECTORY_FORMS_API_SENER_ID.UsageThe api client must be passed into each form instance, so first set the the client settings:SettingNotesDIRECTORY_FORMS_API_BASE_URLDIRECTORY_FORMS_API_API_KEYUnique to client. Retrieved during the on-boarding process.DIRECTORY_FORMS_API_SENDER_IDUnique to client. Retrieved during the on-boarding process.DIRECTORY_FORMS_DEFAULT_TIMEOUTThe followingdirectory client core settingsalso apply to directory cms client:SettingNotesDIRECTORY_CLIENT_CORE_CACHE_EXPIRE_SECONDSDuration to store the retrieved content in the cache.DIRECTORY_CLIENT_CORE_CACHE_LOG_THROTTLING_SECONDSDuration to throttle log events for a given url for.Once that is done the forms can be used.Submit to Zendeskfromdirectory_forms_api_clientimportformsclassZendeskForm(forms.ZendeskAPIForm):# note that the base form provides `requester_email` email fieldtitle=fields.CharField()form=ZendeskForm(data={'name':'Example Person','email':'three@example.com'})assertform.is_valid()form.save(email_address=form.cleaned_data['email'],full_name='Example Person',subject='Bo in the house',service_name='Foo Bar',)Different subdomainThe default behaviour of Forms API is to use the default configured zendesk subdomain when creating the tickets. To use a different subdomain, providesubdomain:form.save(email_address=form.cleaned_data['email'],full_name='Example Person',subject='Some ticket subject',subdomain='some-other-subdomain',)Note that Forms API must first be configured to expect the provided subdomain.Send as emailSend to a user-submitted email addressfromdirectory_forms_api_clientimportformsclassEmailForm(forms.ZendeskAPIForm):title=fields.CharField()email=fields.EmailField()@propertydeftext_body(self):''' Override text_body to text templte of email body.'''text=[]forkey,valueinself.cleaned_data.items():text.append(key)text.append(str(value))text.append('\n')return' '.join(text)@propertydefhtml_body(self):''' Override html_body to return html template of email body.'''cleaned_html=[]forkey,valueinself.cleaned_data.items():cleaned_html.append('<p>')cleaned_html.append(key)cleaned_html.append(':')cleaned_html.append(str(value))cleaned_html.append('</p>')return''.join(cleaned_html)form=TestForm(data={'title':'Example','email':'a@foo.com'})assertform.is_valid()form.save(recipients=[form.cleaned_data['email']],subject='Some email subject',reply_to=['reply@example.com'],service_name='Foo Bar',form_url=self.request.get_full_path())You can also useEmailActionMixinorEmailActiondirectly for more complex requirements. For e.g. When creating form when inheriting from another form or collecting data from multi-step forms (where any one form doesn't have the complete data).
Seeherefor an example.Send to a pre-defined email addressfromdirectory_forms_api_clientimportformsclassEmailForm(forms.EmailAPIForm):title=fields.CharField()@propertydeftext_body(self):''' Override text_body to text templte of email body.'''text='title: '+str(self.cleaned_data['title'])returntext@propertydefhtml_body(self):''' Override html_body to return html template of email body.'''cleaned_html='<p>title: '+str(self.cleaned_data['title'])+'</p>'returncleaned_htmlform=TestForm(data={'title':'Example'})assertform.is_valid()form.save(submission_recipients=[settings.FEEDBACK_EMAIL_RECIPIENTS])Send via gov.uk Notifyfromdirectory_forms_api_clientimportformsclassGovNotifyForm(forms.GovNotifyAPIForm):title=fields.CharField()email=fields.EmailField()form=TestForm(data={'title':'Example','email':'a@foo.com'})assertform.is_valid()form.save(template_id='some-template-id-from-gov-notify',email_address=form.cleaned_data['email'],)Spam checkingSpam checking can be activated by including aspam_contolobject in the submission:fromdirectory_forms_api_clientimporthelpersspam_control=helpers.SpamControl(# data that is worth spam checkingcontents=[form.cleaned_data['comment']],)form.save(template_id=data['template_id'],email_address=data['email_address'],form_url='/the/form/',spam_control=spam_control,)This will result in the content being checked for spam before the submission is sent.SendersReporting on the submissions of specific senders can be facilitated by including asenderobject in the submission.fromdirectory_forms_api_clientimporthelperssender=helpers.Sender(email_address=[form.cleaned_data['email_address']],country_code=forms.cleaned_data['country'])form.save(template_id=data['template_id'],email_address=data['email_address'],form_url='/the/form/',sender=sender,)This allows for country-specific data retention policies to be respected, and to improve spam control functionality.Development$ git clone https://github.com/uktrade/directory-forms-api-client
$ cd directory-forms-api-client
# [create your virtual environment and activate it]
$ pip install -e . # install the requirements defined in setup.pyTesting$ make testPublish to PyPIThe package should be published to PyPI on merge to master. If you need to do it locally then get the credentials from rattic and add the environment variables to your host machine:SettingDIRECTORY_PYPI_USERNAMEDIRECTORY_PYPI_PASSWORDThen run the following command:$ make publish