Update existing dynamic forms for the new schema format.

Add timeout field to the HTTP POST action.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-08-26 22:01:47 -04:00
parent 7e7ba85a78
commit 267896ce2b
4 changed files with 67 additions and 54 deletions

View File

@@ -9,15 +9,17 @@ from django.template import Template, Context
from django.utils.translation import ugettext_lazy as _
from .classes import WorkflowAction
from .exceptions import WorkflowStateActionError
__all__ = ('HTTPPostAction',)
logger = logging.getLogger(__name__)
DEFAULT_TIMEOUT = 4 # 4 seconds
class HTTPPostAction(WorkflowAction):
fields = (
{
'name': 'url', 'label': _('URL'),
fields = {
'url': {
'label': _('URL'),
'class': 'django.forms.CharField', 'kwargs': {
'help_text': _(
'Can be an IP address, a domain or a template. Templates '
@@ -29,8 +31,14 @@ class HTTPPostAction(WorkflowAction):
),
'required': True
},
}, {
'name': 'payload', 'label': _('Payload'),
}, 'timeout': {
'label': _('Timeout'),
'class': 'django.forms.IntegerField', 'default': DEFAULT_TIMEOUT,
'help_text': _('Time in seconds to wait for a response.'),
'required': True
}, 'payload': {
'label': _('Payload'),
'class': 'django.forms.CharField', 'kwargs': {
'help_text': _(
'A JSON document to include in the request. Can also be '
@@ -44,7 +52,8 @@ class HTTPPostAction(WorkflowAction):
}
},
)
}
field_order = ('url', 'timeout', 'payload')
label = _('Perform a POST request')
widgets = {
'payload': {
@@ -63,10 +72,9 @@ class HTTPPostAction(WorkflowAction):
context=Context(context)
)
except Exception as exception:
context['action'].error_logs.create(
result='URL template error: {}'.format(exception)
raise WorkflowStateActionError(
_('URL template error: {}'.format(exception))
)
return
logger.debug('URL template result: %s', url)
@@ -75,21 +83,19 @@ class HTTPPostAction(WorkflowAction):
context=Context(context)
)
except Exception as exception:
context['action'].error_logs.create(
result='Payload template error: {}'.format(exception)
raise WorkflowStateActionError(
_('Payload template error: {}'.format(exception))
)
return
logger.debug('payload template result: %s', result)
try:
payload = json.loads(result, strict=False)
except Exception as exception:
context['action'].error_logs.create(
result='Payload JSON error: {}'.format(exception)
raise WorkflowStateActionError(
_('Payload JSON error: {}'.format(exception))
)
return
logger.debug('payload json result: %s', payload)
requests.post(url=url, data=payload)
requests.post(url=url, data=payload, timeout=self.form_data['timeout'])

View File

@@ -1,5 +1,7 @@
from __future__ import unicode_literals
from collections import OrderedDict
from django.utils.translation import ugettext_lazy as _
from .classes import MailerBackend
@@ -9,32 +11,29 @@ __all__ = ('DjangoSMTP', 'DjangoFileBased')
class DjangoSMTP(MailerBackend):
class_path = 'django.core.mail.backends.smtp.EmailBackend'
fields = (
{
'name': 'host', 'label': _('Host'),
fields = {
'host': {
'label': _('Host'),
'class': 'django.forms.CharField', 'default': 'localhost',
'help_text': _('The host to use for sending email.'),
'kwargs': {
'max_length': 48
}, 'required': False
},
{
'name': 'port', 'label': _('Port'),
}, 'port': {
'label': _('Port'),
'class': 'django.forms.IntegerField', 'default': 25,
'help_text': _('Port to use for the SMTP server.'),
'required': False
},
{
'name': 'use_tls', 'label': _('Use TLS'),
}, 'use_tls': {
'label': _('Use TLS'),
'class': 'django.forms.BooleanField', 'default': False,
'help_text': _(
'Whether to use a TLS (secure) connection when talking to '
'the SMTP server. This is used for explicit TLS connections, '
'generally on port 587.'
), 'required': False
},
{
'name': 'use_ssl', 'label': _('Use SSL'),
}, 'use_ssl': {
'label': _('Use SSL'),
'class': 'django.forms.BooleanField', 'default': False,
'help_text': _(
'Whether to use an implicit TLS (secure) connection when '
@@ -45,9 +44,8 @@ class DjangoSMTP(MailerBackend):
'that "Use TLS" and "Use SSL" are mutually exclusive, '
'so only set one of those settings to True.'
), 'required': False
},
{
'name': 'user', 'label': _('Username'),
}, 'user': {
'label': _('Username'),
'class': 'django.forms.CharField', 'default': '',
'help_text': _(
'Username to use for the SMTP server. If empty, '
@@ -55,9 +53,8 @@ class DjangoSMTP(MailerBackend):
), 'kwargs': {
'max_length': 48
}, 'required': False
},
{
'name': 'password', 'label': _('Password'),
}, 'password': {
'label': _('Password'),
'class': 'django.forms.CharField', 'default': '',
'help_text': _(
'Password to use for the SMTP server. This setting is used '
@@ -68,7 +65,8 @@ class DjangoSMTP(MailerBackend):
'max_length': 48
}, 'required': False
},
)
}
field_order = ('host', 'port', 'use_tls', 'use_ssl', 'user', 'password')
widgets = {
'password': {
'class': 'django.forms.widgets.PasswordInput',
@@ -82,12 +80,12 @@ class DjangoSMTP(MailerBackend):
class DjangoFileBased(MailerBackend):
class_path = 'django.core.mail.backends.filebased.EmailBackend'
fields = (
{
'name': 'file_path', 'label': _('File path'),
fields = {
'file_path': {
'label': _('File path'),
'class': 'django.forms.CharField', 'kwargs': {
'max_length': 48
}
},
)
}
label = _('Django file based backend')

View File

@@ -162,10 +162,15 @@ class UserMailingCreateView(SingleObjectDynamicFormCreateView):
}
def get_form_schema(self):
return {
'fields': self.get_backend().fields,
'widgets': getattr(self.get_backend(), 'widgets', {})
backend = self.get_backend()
result = {
'fields': backend.fields,
'widgets': getattr(backend, 'widgets', {})
}
if hasattr(backend, 'field_order'):
result['field_order'] = backend.field_order
return result
def get_instance_extra_data(self):
return {'backend_path': self.kwargs['class_path']}
@@ -193,10 +198,15 @@ class UserMailingEditView(SingleObjectDynamicFormEditView):
}
def get_form_schema(self):
return {
'fields': self.get_object().get_backend().fields,
'widgets': getattr(self.get_object().get_backend(), 'widgets', {})
backend = self.get_object().get_backend()
result = {
'fields': backend.fields,
'widgets': getattr(backend, 'widgets', {})
}
if hasattr(backend, 'field_order'):
result['field_order'] = backend.field_order
return result
class UserMailerLogEntryListView(SingleObjectListView):

View File

@@ -14,15 +14,14 @@ logger = logging.getLogger(__name__)
class AttachTagAction(WorkflowAction):
fields = (
{
'name': 'tags', 'label': _('Tags'),
fields = {
'tags': {'label': _('Tags'),
'class': 'django.forms.ModelMultipleChoiceField', 'kwargs': {
'help_text': _('Tags to attach to the document'),
'queryset': Tag.objects.none(), 'required': False
}
},
)
}
label = _('Attach tag')
widgets = {
'tags': {
@@ -41,7 +40,7 @@ class AttachTagAction(WorkflowAction):
permission_tag_attach, user, queryset=Tag.objects.all()
)
self.fields[0]['kwargs']['queryset'] = queryset
self.fields['tags']['kwargs']['queryset'] = queryset
self.widgets['tags']['kwargs']['queryset'] = queryset
return {
@@ -60,15 +59,15 @@ class AttachTagAction(WorkflowAction):
class RemoveTagAction(AttachTagAction):
fields = (
{
'name': 'tags', 'label': _('Tags'),
fields = {
'tags': {
'label': _('Tags'),
'class': 'django.forms.ModelMultipleChoiceField', 'kwargs': {
'help_text': _('Tags to remove from the document'),
'queryset': Tag.objects.none(), 'required': False
}
},
)
}
label = _('Remove tag')
def execute(self, context):