目录窗体的PythonAPI客户端。

directory-forms-api-client的Python项目详细描述


目录窗体API客户端

code-climate-imagecircle-ci-imagecodecov-imagepypi-imagesemver-image

directory forms无头表单的api客户端。


要求

安装

$ pip install directory_forms_api_client

客户注册

必须在目录窗体API的管理面板中创建API客户端,才能访问DIRECTORY_FORMS_API_API_KEYDIRECTORY_FORMS_API_SENER_ID

Enrol-api-client

用法

必须将api客户端传递到每个表单实例中,因此首先设置客户端设置:

SettingNotes
^{}
^{}Unique to client. Retrieved during the on-boarding process.
^{}Unique to client. Retrieved during the on-boarding process.
^{}

以下directory client core settings也适用于目录cms客户端:

SettingNotes
^{}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())

您还可以直接使用EmailActionMixinEmailAction来处理更复杂的需求。例如,当从另一个表单继承或从多步骤表单收集数据时创建表单(其中任何一个表单没有完整的数据)。 请参见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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
tomcat Java条带错误   java OPENTSDB fsck修复程序不更正重复点   java JavaFX在控制器内切换自身的可见性   java maven surefire插件未并行执行运行程序   读取导致Freemarker模板引擎中TemplateException的Java对象   无法使Java库与我的Android应用程序一起工作   安卓 java。lang.IllegalStateException游标   使用Java检索XML文件中的XSL URL和名称   java如何从文本文件集合中提取特定值   电子邮件java mail gmail   java为什么finalize()只被垃圾收集器调用一次?   java方法findViewById(int)对于Json类型是未定义的。蛇形   java在安卓中尝试从brother打印机打印位图时遇到以下异常   java在颤振中支持Kotlin的优势   java从后面编写文本   java制作列表。第一个列表可以有相同的数字,第二个将是价格,尝试制作第三个列表,如果它们是相同的数字,它将添加价格