mirror of
https://github.com/Hopiu/django-fobi.git
synced 2026-05-10 22:03:09 +00:00
mail pep8
This commit is contained in:
parent
77bb7cde19
commit
bd17bdb2bd
9 changed files with 113 additions and 99 deletions
|
|
@ -10,6 +10,8 @@ try:
|
|||
from django.apps import AppConfig
|
||||
|
||||
class Config(AppConfig):
|
||||
"""Config."""
|
||||
|
||||
name = 'fobi.contrib.plugins.form_handlers.mail'
|
||||
label = 'fobi_contrib_plugins_form_handlers_mail'
|
||||
|
||||
|
|
|
|||
|
|
@ -1,22 +1,25 @@
|
|||
from django.conf import settings
|
||||
|
||||
from . import defaults
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.conf'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('get_setting',)
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from . import defaults
|
||||
|
||||
def get_setting(setting, override=None):
|
||||
"""
|
||||
"""Get setting.
|
||||
|
||||
Get a setting from ``fobi.contrib.plugins.form_handlers.mail`` conf
|
||||
module, falling back to the default.
|
||||
|
||||
If override is not None, it will be used instead of the setting.
|
||||
|
||||
:param setting: String with setting name
|
||||
:param override: Value to use when no setting is available. Defaults to None.
|
||||
:param override: Value to use when no setting is available. Defaults to
|
||||
None.
|
||||
:return: Setting value.
|
||||
"""
|
||||
if override is not None:
|
||||
|
|
|
|||
|
|
@ -6,5 +6,4 @@ __all__ = (
|
|||
'MULTI_EMAIL_FIELD_VALUE_SPLITTER',
|
||||
)
|
||||
|
||||
MULTI_EMAIL_FIELD_VALUE_SPLITTER = ',' # But can be '\n'
|
||||
|
||||
MULTI_EMAIL_FIELD_VALUE_SPLITTER = ',' # But can be '\n'
|
||||
|
|
|
|||
|
|
@ -1,33 +1,36 @@
|
|||
from django import forms
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.validators import validate_email
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .widgets import MultiEmailWidget
|
||||
from .settings import MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.widgets'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MultiEmailField',)
|
||||
|
||||
from django import forms
|
||||
from django.core.validators import validate_email
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from .widgets import MultiEmailWidget
|
||||
from .settings import MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
|
||||
class MultiEmailField(forms.Field):
|
||||
"""MultiEmailField."""
|
||||
|
||||
message = _('Enter valid email addresses.')
|
||||
code = 'invalid'
|
||||
widget = MultiEmailWidget
|
||||
|
||||
def to_python(self, value):
|
||||
"Normalize data to a list of strings."
|
||||
"""Normalize data to a list of strings."""
|
||||
# Return None if no input was given.
|
||||
if not value:
|
||||
return []
|
||||
return [v.strip()
|
||||
return [v.strip()
|
||||
for v in value.split(MULTI_EMAIL_FIELD_VALUE_SPLITTER)
|
||||
if v != ""]
|
||||
|
||||
def validate(self, value):
|
||||
"Check if value consists only of valid emails."
|
||||
"""Check if value consists only of valid emails."""
|
||||
|
||||
# Use the parent's handling of required fields, etc.
|
||||
super(MultiEmailField, self).validate(value)
|
||||
|
|
|
|||
|
|
@ -1,19 +1,13 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.fobi_form_handlers'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2015 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MailHandlerPlugin',)
|
||||
|
||||
from mimetypes import guess_type
|
||||
import os
|
||||
|
||||
from six import string_types
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.template.loader import render_to_string
|
||||
from django.conf import settings
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from fobi.base import (
|
||||
FormHandlerPlugin, form_handler_plugin_registry, get_processed_form_data
|
||||
|
|
@ -25,18 +19,27 @@ from .forms import MailForm
|
|||
from .helpers import send_mail
|
||||
from .settings import MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
|
||||
class MailHandlerPlugin(FormHandlerPlugin):
|
||||
"""
|
||||
Mail handler plugin. Sends emails to the person specified.
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.fobi_form_handlers'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2015 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MailHandlerPlugin',)
|
||||
|
||||
Should be executed before ``db_store`` and ``http_repost`` plugins.
|
||||
|
||||
class MailHandlerPlugin(FormHandlerPlugin):
|
||||
"""Mail handler plugin.
|
||||
|
||||
Sends emails to the person specified. Should be executed before
|
||||
``db_store`` and ``http_repost`` plugins.
|
||||
"""
|
||||
|
||||
uid = UID
|
||||
name = _("Mail")
|
||||
form = MailForm
|
||||
|
||||
def run(self, form_entry, request, form, form_element_entries=None):
|
||||
"""
|
||||
"""Run.
|
||||
|
||||
:param fobi.models.FormEntry form_entry: Instance of
|
||||
``fobi.models.FormEntry``.
|
||||
:param django.http.HttpRequest request:
|
||||
|
|
@ -45,15 +48,15 @@ class MailHandlerPlugin(FormHandlerPlugin):
|
|||
``fobi.models.FormElementEntry`` objects.
|
||||
"""
|
||||
base_url = 'http{secure}://{host}'.format(
|
||||
secure = ('s' if request.is_secure() else ''),
|
||||
host = request.get_host()
|
||||
)
|
||||
secure=('s' if request.is_secure() else ''),
|
||||
host=request.get_host()
|
||||
)
|
||||
|
||||
# Clean up the values, leave our content fields and empty values.
|
||||
field_name_to_label_map, cleaned_data = get_processed_form_data(
|
||||
form,
|
||||
form_element_entries
|
||||
)
|
||||
)
|
||||
|
||||
rendered_data = []
|
||||
for key, value in cleaned_data.items():
|
||||
|
|
@ -61,11 +64,11 @@ class MailHandlerPlugin(FormHandlerPlugin):
|
|||
and value.startswith(settings.MEDIA_URL):
|
||||
cleaned_data[key] = '{base_url}{value}'.format(
|
||||
base_url=base_url, value=value
|
||||
)
|
||||
)
|
||||
label = field_name_to_label_map.get(key, key)
|
||||
rendered_data.append('{0}: {1}\n'.format(
|
||||
safe_text(label), safe_text(cleaned_data[key]))
|
||||
)
|
||||
)
|
||||
|
||||
files = self._prepare_files(request, form)
|
||||
|
||||
|
|
@ -74,36 +77,36 @@ class MailHandlerPlugin(FormHandlerPlugin):
|
|||
to_email = self.data.to_email
|
||||
else:
|
||||
# Assume that it's string
|
||||
to_email = self.data.to_email.split(MULTI_EMAIL_FIELD_VALUE_SPLITTER)
|
||||
to_email = self.data.to_email.split(
|
||||
MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
)
|
||||
|
||||
send_mail(
|
||||
safe_text(self.data.subject),
|
||||
"{0}\n\n{1}".format(safe_text(self.data.body),
|
||||
''.join(rendered_data)),
|
||||
"{0}\n\n{1}".format(
|
||||
safe_text(self.data.body),
|
||||
''.join(rendered_data)
|
||||
),
|
||||
self.data.from_email,
|
||||
to_email,
|
||||
fail_silently = False,
|
||||
attachments = files.values()
|
||||
)
|
||||
fail_silently=False,
|
||||
attachments=files.values()
|
||||
)
|
||||
|
||||
def _prepare_files(self, request, form):
|
||||
"""
|
||||
Prepares the files for being attached to the mail message.
|
||||
"""
|
||||
"""Prepares the files for being attached to the mail message."""
|
||||
files = {}
|
||||
|
||||
def process_path(file_path, imf):
|
||||
"""
|
||||
Processes the file path and the file.
|
||||
"""
|
||||
"""Processes the file path and the file."""
|
||||
if file_path:
|
||||
#if file_path.startswith(settings.MEDIA_URL):
|
||||
# file_path = file_path[1:]
|
||||
#file_path = settings.PROJECT_DIR('../{0}'.format(file_path))
|
||||
# if file_path.startswith(settings.MEDIA_URL):
|
||||
# file_path = file_path[1:]
|
||||
# file_path = settings.PROJECT_DIR('../{0}'.format(file_path))
|
||||
file_path = file_path.replace(
|
||||
settings.MEDIA_URL,
|
||||
os.path.join(settings.MEDIA_ROOT, '')
|
||||
)
|
||||
)
|
||||
mime_type = guess_type(imf.name)
|
||||
files[field_name] = (
|
||||
imf.name,
|
||||
|
|
@ -115,15 +118,14 @@ class MailHandlerPlugin(FormHandlerPlugin):
|
|||
try:
|
||||
file_path = form.cleaned_data.get(field_name, '')
|
||||
process_path(file_path, imf)
|
||||
except Exception as e:
|
||||
except Exception as err:
|
||||
file_path = extract_file_path(imf.name)
|
||||
process_path(file_path, imf)
|
||||
|
||||
return files
|
||||
|
||||
def plugin_data_repr(self):
|
||||
"""
|
||||
Human readable representation of plugin data.
|
||||
"""Human readable representation of plugin data.
|
||||
|
||||
:return string:
|
||||
"""
|
||||
|
|
@ -131,8 +133,8 @@ class MailHandlerPlugin(FormHandlerPlugin):
|
|||
# Handling more than one email address
|
||||
if isinstance(self.data.to_email, (list, tuple)):
|
||||
to_email = '{0} '.format(MULTI_EMAIL_FIELD_VALUE_SPLITTER).join(
|
||||
self.data.to_email
|
||||
)
|
||||
self.data.to_email
|
||||
)
|
||||
else:
|
||||
# Assume that it's string
|
||||
to_email = self.data.to_email
|
||||
|
|
|
|||
|
|
@ -1,11 +1,5 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.forms'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MailForm',)
|
||||
|
||||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
|
|
@ -14,12 +8,18 @@ from fobi.base import BasePluginForm, get_theme
|
|||
from .fields import MultiEmailField
|
||||
from .widgets import MultiEmailWidget
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.forms'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MailForm',)
|
||||
|
||||
theme = get_theme(request=None, as_instance=True)
|
||||
|
||||
|
||||
class MailForm(forms.Form, BasePluginForm):
|
||||
"""
|
||||
Form for ``BooleanSelectPlugin``.
|
||||
"""
|
||||
"""Form for ``BooleanSelectPlugin``."""
|
||||
|
||||
plugin_data_fields = [
|
||||
("from_name", ""),
|
||||
("from_email", ""),
|
||||
|
|
@ -30,44 +30,44 @@ class MailForm(forms.Form, BasePluginForm):
|
|||
]
|
||||
|
||||
from_name = forms.CharField(
|
||||
label = _("From name"),
|
||||
label=_("From name"),
|
||||
required=True,
|
||||
widget = forms.widgets.TextInput(
|
||||
widget=forms.widgets.TextInput(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
)
|
||||
from_email = forms.EmailField(
|
||||
label = _("From email"),
|
||||
label=_("From email"),
|
||||
required=True,
|
||||
widget = forms.widgets.TextInput(
|
||||
widget=forms.widgets.TextInput(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
)
|
||||
to_name = forms.CharField(
|
||||
label = _("To name"),
|
||||
label=_("To name"),
|
||||
required=True,
|
||||
widget = forms.widgets.TextInput(
|
||||
widget=forms.widgets.TextInput(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
to_email = MultiEmailField(#forms.EmailField(
|
||||
label = _("To email"),
|
||||
)
|
||||
to_email = MultiEmailField( # forms.EmailField(
|
||||
label=_("To email"),
|
||||
required=True,
|
||||
widget = MultiEmailWidget(#forms.widgets.TextInput(
|
||||
widget=MultiEmailWidget( # forms.widgets.TextInput(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
)
|
||||
subject = forms.CharField(
|
||||
label = _("Subject"),
|
||||
label=_("Subject"),
|
||||
required=True,
|
||||
widget = forms.widgets.TextInput(
|
||||
widget=forms.widgets.TextInput(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
)
|
||||
body = forms.CharField(
|
||||
label = _("Body"),
|
||||
label=_("Body"),
|
||||
required=False,
|
||||
widget = forms.widgets.Textarea(
|
||||
widget=forms.widgets.Textarea(
|
||||
attrs={'class': theme.form_element_html_class}
|
||||
)
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,18 +1,20 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from django.core.mail import get_connection
|
||||
from django.core.mail.message import EmailMultiAlternatives
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.helpers'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('send_mail',)
|
||||
|
||||
from django.core.mail import get_connection
|
||||
from django.core.mail.message import EmailMultiAlternatives
|
||||
|
||||
def send_mail(subject, message, from_email, recipient_list,
|
||||
fail_silently=False, auth_user=None, auth_password=None,
|
||||
connection=None, html_message=None, attachments=None):
|
||||
"""
|
||||
"""Send email.
|
||||
|
||||
Easy wrapper for sending a single message to a recipient list. All members
|
||||
of the recipient list will see the other recipients in the 'To' field.
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
from .conf import get_setting
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.settings'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
|
|
@ -6,8 +8,6 @@ __all__ = (
|
|||
'MULTI_EMAIL_FIELD_VALUE_SPLITTER',
|
||||
)
|
||||
|
||||
from .conf import get_setting
|
||||
|
||||
MULTI_EMAIL_FIELD_VALUE_SPLITTER = get_setting(
|
||||
'MULTI_EMAIL_FIELD_VALUE_SPLITTER'
|
||||
)
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,20 +1,22 @@
|
|||
import six
|
||||
|
||||
from django.core import validators
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.forms.widgets import Textarea
|
||||
|
||||
from .settings import MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
|
||||
__title__ = 'fobi.contrib.plugins.form_handlers.mail.widgets'
|
||||
__author__ = 'Artur Barseghyan <artur.barseghyan@gmail.com>'
|
||||
__copyright__ = '2014-2016 Artur Barseghyan'
|
||||
__license__ = 'GPL 2.0/LGPL 2.1'
|
||||
__all__ = ('MultiEmailWidget',)
|
||||
|
||||
import six
|
||||
|
||||
from django.forms.widgets import Textarea
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core import validators
|
||||
|
||||
from .settings import MULTI_EMAIL_FIELD_VALUE_SPLITTER
|
||||
|
||||
MULTI_EMAIL_FIELD_EMPTY_VALUES = validators.EMPTY_VALUES + ('[]',)
|
||||
|
||||
|
||||
class MultiEmailWidget(Textarea):
|
||||
"""Multi email widget."""
|
||||
|
||||
is_hidden = False
|
||||
|
||||
|
|
@ -29,5 +31,6 @@ class MultiEmailWidget(Textarea):
|
|||
raise ValidationError('Invalid format.')
|
||||
|
||||
def render(self, name, value, attrs=None):
|
||||
"""Render."""
|
||||
value = self.prep_value(value)
|
||||
return super(MultiEmailWidget, self).render(name, value, attrs)
|
||||
|
|
|
|||
Loading…
Reference in a new issue