目录窗体的PythonAPI客户端。
directory-forms-api-client的Python项目详细描述
目录窗体API客户端
directory forms无头表单的api客户端。
要求
安装
$ pip install directory_forms_api_client
客户注册
必须在目录窗体API的管理面板中创建API客户端,才能访问DIRECTORY_FORMS_API_API_KEY
和DIRECTORY_FORMS_API_SENER_ID
。
用法
必须将api客户端传递到每个表单实例中,因此首先设置客户端设置:
Setting | Notes |
---|---|
^{ | |
^{ | Unique to client. Retrieved during the on-boarding process. |
^{ | Unique to client. Retrieved during the on-boarding process. |
^{ |
以下directory client core settings也适用于目录cms客户端:
Setting | Notes |
---|---|
^{ | Duration to store the retrieved content in the cache. |
^{ | Duration to throttle log events for a given url for. |
完成后,可以使用表单。
提交给Zendesk
fromdirectory_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',)
不同子域
forms api的默认行为是在创建票据时使用默认配置的zendesk子域。要使用不同的子域,请提供subdomain
:
form.save(email_address=form.cleaned_data['email'],full_name='Example Person',subject='Some ticket subject',subdomain='some-other-subdomain',)
注意,必须首先将forms api配置为期望提供的子域。
以电子邮件形式发送
发送到用户提交的电子邮件地址
fromdirectory_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())
您还可以直接使用EmailActionMixin
或EmailAction
来处理更复杂的需求。例如,当从另一个表单继承或从多步骤表单收集数据时创建表单(其中任何一个表单没有完整的数据)。
请参见here以获取示例。
发送到预定义的电子邮件地址
fromdirectory_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])
通过gov.uk发送通知
fromdirectory_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_contol
对象来激活垃圾邮件检查:
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,)
这将导致在发送提交之前检查内容是否存在垃圾邮件。
发件人
通过在提交中包含sender
对象,可以方便地报告特定发件人的提交。
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,)
这允许遵守特定国家的数据保留策略,并改进垃圾邮件控制功能。
开发
$ 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.py
测试
$ make test
发布到pypi
包应该在合并到主服务器时发布到pypi。如果您需要在本地执行此操作,请从rattic获取凭据并将环境变量添加到主机:
Setting |
---|
^{ |
^{ |
然后运行以下命令:
$ make publish