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 django.utils.translation import ugettext_lazy as _
from .classes import WorkflowAction from .classes import WorkflowAction
from .exceptions import WorkflowStateActionError
__all__ = ('HTTPPostAction',) __all__ = ('HTTPPostAction',)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
DEFAULT_TIMEOUT = 4 # 4 seconds
class HTTPPostAction(WorkflowAction): class HTTPPostAction(WorkflowAction):
fields = ( fields = {
{ 'url': {
'name': 'url', 'label': _('URL'), 'label': _('URL'),
'class': 'django.forms.CharField', 'kwargs': { 'class': 'django.forms.CharField', 'kwargs': {
'help_text': _( 'help_text': _(
'Can be an IP address, a domain or a template. Templates ' 'Can be an IP address, a domain or a template. Templates '
@@ -29,8 +31,14 @@ class HTTPPostAction(WorkflowAction):
), ),
'required': True 'required': True
}, },
}, { }, 'timeout': {
'name': 'payload', 'label': _('Payload'), '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': { 'class': 'django.forms.CharField', 'kwargs': {
'help_text': _( 'help_text': _(
'A JSON document to include in the request. Can also be ' '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') label = _('Perform a POST request')
widgets = { widgets = {
'payload': { 'payload': {
@@ -63,10 +72,9 @@ class HTTPPostAction(WorkflowAction):
context=Context(context) context=Context(context)
) )
except Exception as exception: except Exception as exception:
context['action'].error_logs.create( raise WorkflowStateActionError(
result='URL template error: {}'.format(exception) _('URL template error: {}'.format(exception))
) )
return
logger.debug('URL template result: %s', url) logger.debug('URL template result: %s', url)
@@ -75,21 +83,19 @@ class HTTPPostAction(WorkflowAction):
context=Context(context) context=Context(context)
) )
except Exception as exception: except Exception as exception:
context['action'].error_logs.create( raise WorkflowStateActionError(
result='Payload template error: {}'.format(exception) _('Payload template error: {}'.format(exception))
) )
return
logger.debug('payload template result: %s', result) logger.debug('payload template result: %s', result)
try: try:
payload = json.loads(result, strict=False) payload = json.loads(result, strict=False)
except Exception as exception: except Exception as exception:
context['action'].error_logs.create( raise WorkflowStateActionError(
result='Payload JSON error: {}'.format(exception) _('Payload JSON error: {}'.format(exception))
) )
return
logger.debug('payload json result: %s', payload) 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 __future__ import unicode_literals
from collections import OrderedDict
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .classes import MailerBackend from .classes import MailerBackend
@@ -9,32 +11,29 @@ __all__ = ('DjangoSMTP', 'DjangoFileBased')
class DjangoSMTP(MailerBackend): class DjangoSMTP(MailerBackend):
class_path = 'django.core.mail.backends.smtp.EmailBackend' class_path = 'django.core.mail.backends.smtp.EmailBackend'
fields = ( fields = {
{ 'host': {
'name': 'host', 'label': _('Host'), 'label': _('Host'),
'class': 'django.forms.CharField', 'default': 'localhost', 'class': 'django.forms.CharField', 'default': 'localhost',
'help_text': _('The host to use for sending email.'), 'help_text': _('The host to use for sending email.'),
'kwargs': { 'kwargs': {
'max_length': 48 'max_length': 48
}, 'required': False }, 'required': False
}, }, 'port': {
{ 'label': _('Port'),
'name': 'port', 'label': _('Port'),
'class': 'django.forms.IntegerField', 'default': 25, 'class': 'django.forms.IntegerField', 'default': 25,
'help_text': _('Port to use for the SMTP server.'), 'help_text': _('Port to use for the SMTP server.'),
'required': False 'required': False
}, }, 'use_tls': {
{ 'label': _('Use TLS'),
'name': 'use_tls', 'label': _('Use TLS'),
'class': 'django.forms.BooleanField', 'default': False, 'class': 'django.forms.BooleanField', 'default': False,
'help_text': _( 'help_text': _(
'Whether to use a TLS (secure) connection when talking to ' 'Whether to use a TLS (secure) connection when talking to '
'the SMTP server. This is used for explicit TLS connections, ' 'the SMTP server. This is used for explicit TLS connections, '
'generally on port 587.' 'generally on port 587.'
), 'required': False ), 'required': False
}, }, 'use_ssl': {
{ 'label': _('Use SSL'),
'name': 'use_ssl', 'label': _('Use SSL'),
'class': 'django.forms.BooleanField', 'default': False, 'class': 'django.forms.BooleanField', 'default': False,
'help_text': _( 'help_text': _(
'Whether to use an implicit TLS (secure) connection when ' '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, ' 'that "Use TLS" and "Use SSL" are mutually exclusive, '
'so only set one of those settings to True.' 'so only set one of those settings to True.'
), 'required': False ), 'required': False
}, }, 'user': {
{ 'label': _('Username'),
'name': 'user', 'label': _('Username'),
'class': 'django.forms.CharField', 'default': '', 'class': 'django.forms.CharField', 'default': '',
'help_text': _( 'help_text': _(
'Username to use for the SMTP server. If empty, ' 'Username to use for the SMTP server. If empty, '
@@ -55,9 +53,8 @@ class DjangoSMTP(MailerBackend):
), 'kwargs': { ), 'kwargs': {
'max_length': 48 'max_length': 48
}, 'required': False }, 'required': False
}, }, 'password': {
{ 'label': _('Password'),
'name': 'password', 'label': _('Password'),
'class': 'django.forms.CharField', 'default': '', 'class': 'django.forms.CharField', 'default': '',
'help_text': _( 'help_text': _(
'Password to use for the SMTP server. This setting is used ' 'Password to use for the SMTP server. This setting is used '
@@ -68,7 +65,8 @@ class DjangoSMTP(MailerBackend):
'max_length': 48 'max_length': 48
}, 'required': False }, 'required': False
}, },
) }
field_order = ('host', 'port', 'use_tls', 'use_ssl', 'user', 'password')
widgets = { widgets = {
'password': { 'password': {
'class': 'django.forms.widgets.PasswordInput', 'class': 'django.forms.widgets.PasswordInput',
@@ -82,12 +80,12 @@ class DjangoSMTP(MailerBackend):
class DjangoFileBased(MailerBackend): class DjangoFileBased(MailerBackend):
class_path = 'django.core.mail.backends.filebased.EmailBackend' class_path = 'django.core.mail.backends.filebased.EmailBackend'
fields = ( fields = {
{ 'file_path': {
'name': 'file_path', 'label': _('File path'), 'label': _('File path'),
'class': 'django.forms.CharField', 'kwargs': { 'class': 'django.forms.CharField', 'kwargs': {
'max_length': 48 'max_length': 48
} }
}, },
) }
label = _('Django file based backend') label = _('Django file based backend')

View File

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

View File

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