Commit 4ba251ca authored by Patrick's avatar Patrick

Add : custom mail server.

parent 41e49235
...@@ -56,8 +56,11 @@ class ConfigurationDataForm(TranslatableModelForm): ...@@ -56,8 +56,11 @@ class ConfigurationDataForm(TranslatableModelForm):
'send_abstract_order_mail_to_customer', 'send_abstract_order_mail_to_customer',
_('The abstract can only be send if the order is also send to producer')) _('The abstract can only be send if the order is also send to producer'))
email_host_password = self.cleaned_data["email_host_password"] email_host_password = self.cleaned_data["email_host_password"]
if email_host_password: email_is_custom = self.cleaned_data["email_is_custom"]
if email_is_custom:
# Send test email # Send test email
if not email_host_password:
email_host_password = self.instance.previous_email_host_password
email_host = self.cleaned_data["email_host"] email_host = self.cleaned_data["email_host"]
email_port = self.cleaned_data["email_port"] email_port = self.cleaned_data["email_port"]
email_use_tls = self.cleaned_data["email_use_tls"] email_use_tls = self.cleaned_data["email_use_tls"]
...@@ -71,8 +74,9 @@ class ConfigurationDataForm(TranslatableModelForm): ...@@ -71,8 +74,9 @@ class ConfigurationDataForm(TranslatableModelForm):
) )
if not email_send: if not email_send:
self.add_error( self.add_error(
'email_host_password', 'email_is_custom',
_('Repanier tried to send a test email without success.')) _('Repanier tried to send a test email without success.'))
self.instance.email_is_custom = False
class Meta: class Meta:
model = Configuration model = Configuration
...@@ -162,18 +166,10 @@ class ConfigurationAdmin(TranslatableAdmin): ...@@ -162,18 +166,10 @@ class ConfigurationAdmin(TranslatableAdmin):
'invoice', 'invoice',
('currency', 'vat_id'), ('currency', 'vat_id'),
'sms_gateway_mail', 'sms_gateway_mail',
'email_is_custom',
('email_host', 'email_port', 'email_use_tls'), ('email_host', 'email_port', 'email_use_tls'),
('email_host_user', 'email_host_password') ('email_host_user', 'email_host_password')
), ),
}), }),
] ]
return fieldsets return fieldsets
# def get_readonly_fields(self, request, configuration=None):
# permanence = Permanence.objects.all().order_by('?')
# is_coordinator = request.user.is_superuser or request.user.is_staff
#
# if is_coordinator or not permanence.exists():
# return []
# else:
# return ['bank_account']
This diff is collapsed.
...@@ -100,6 +100,8 @@ class Configuration(TranslatableModel): ...@@ -100,6 +100,8 @@ class Configuration(TranslatableModel):
default=DECIMAL_ZERO, max_digits=5, decimal_places=2, default=DECIMAL_ZERO, max_digits=5, decimal_places=2,
validators=[MinValueValidator(0)]) validators=[MinValueValidator(0)])
notification_is_public = models.BooleanField(_("the notification is public"), default=False) notification_is_public = models.BooleanField(_("the notification is public"), default=False)
email_is_custom = models.BooleanField(
_("Email is customised"), default=False)
email_host = models.CharField( email_host = models.CharField(
_("email host"), _("email host"),
help_text=_("For @gmail.com, see: https://mail.google.com/mail/u/0/#settings/fwdandpop and activate POP"), help_text=_("For @gmail.com, see: https://mail.google.com/mail/u/0/#settings/fwdandpop and activate POP"),
...@@ -107,6 +109,7 @@ class Configuration(TranslatableModel): ...@@ -107,6 +109,7 @@ class Configuration(TranslatableModel):
email_port = models.IntegerField( email_port = models.IntegerField(
_("email port"), _("email port"),
help_text=_("Usually 587 for @gmail.com, otherwise 25"), help_text=_("Usually 587 for @gmail.com, otherwise 25"),
blank=True, null=True,
default=587) default=587)
email_use_tls = models.BooleanField( email_use_tls = models.BooleanField(
_("email use tls"), _("email use tls"),
...@@ -311,7 +314,7 @@ def configuration_pre_save(sender, **kwargs): ...@@ -311,7 +314,7 @@ def configuration_pre_save(sender, **kwargs):
config = kwargs["instance"] config = kwargs["instance"]
if not config.bank_account: if not config.bank_account:
config.bank_account = None config.bank_account = None
if not config.email_host_password: if config.email_is_custom and not config.email_host_password:
config.email_host_password = config.previous_email_host_password config.email_host_password = config.previous_email_host_password
......
...@@ -102,6 +102,7 @@ def send_test_email(host=None, port=None, host_user=None, host_password=None, us ...@@ -102,6 +102,7 @@ def send_test_email(host=None, port=None, host_user=None, host_password=None, us
) )
return send_email( return send_email(
email, email,
test=True,
host=host, host=host,
port=port, port=port,
host_user=host_user, host_user=host_user,
...@@ -113,13 +114,15 @@ def send_test_email(host=None, port=None, host_user=None, host_password=None, us ...@@ -113,13 +114,15 @@ def send_test_email(host=None, port=None, host_user=None, host_password=None, us
def send_email( def send_email(
email=None, from_name=EMPTY_STRING, track_customer_on_error=False, email=None, from_name=EMPTY_STRING, track_customer_on_error=False,
host=None, port=None, host_user=None, host_password=None, use_tls=None): test=False, host=None, port=None, host_user=None, host_password=None, use_tls=None):
email_send = False email_send = False
from_email, host, host_password, host_user, port, use_tls = send_email_get_connection_param(
test, host, host_password, host_user, port, use_tls)
if settings.DJANGO_SETTINGS_DEMO: if settings.DJANGO_SETTINGS_DEMO:
email.to = [DEMO_EMAIL] email.to = [DEMO_EMAIL]
email.cc = [] email.cc = []
email.bcc = [] email.bcc = []
email_send = send_email_with_error_log(email, from_name, host=host, email_send = send_email_with_error_log(email, from_name, from_email=from_email, host=host,
port=port, port=port,
host_user=host_user, host_user=host_user,
host_password=host_password, host_password=host_password,
...@@ -132,7 +135,7 @@ def send_email( ...@@ -132,7 +135,7 @@ def send_email(
# Send the mail only if there is at least one tester # Send the mail only if there is at least one tester
email.cc = [] email.cc = []
email.bcc = [] email.bcc = []
email_send = send_email_with_error_log(email, from_name, host=host, email_send = send_email_with_error_log(email, from_name, from_email=from_email, host=host,
port=port, port=port,
host_user=host_user, host_user=host_user,
host_password=host_password, host_password=host_password,
...@@ -158,7 +161,12 @@ def send_email( ...@@ -158,7 +161,12 @@ def send_email(
customer = None customer = None
for email_to in send_email_to: for email_to in send_email_to:
email.to = [email_to] email.to = [email_to]
email_send &= send_email_with_error_log(email, from_name, track_customer_on_error, host=host, email_send &= send_email_with_error_log(
email,
from_name,
track_customer_on_error,
from_email=from_email,
host=host,
port=port, port=port,
host_user=host_user, host_user=host_user,
host_password=host_password, host_password=host_password,
...@@ -169,14 +177,11 @@ def send_email( ...@@ -169,14 +177,11 @@ def send_email(
def send_email_with_error_log( def send_email_with_error_log(
email, from_name=None, track_customer_on_error=False, email, from_name=None, track_customer_on_error=False,
host=None, port=None, host_user=None, host_password=None, use_tls=None): from_email=None, host=None, port=None, host_user=None, host_password=None, use_tls=None):
email_send = False email_send = False
email_to = email.to[0] email_to = email.to[0]
customer, send_mail = send_email_get_customer(email_to, track_customer_on_error) customer, send_mail = send_email_get_customer(email_to, track_customer_on_error)
if send_mail: if send_mail:
from_email, host, host_password, host_user, port, use_tls = send_email_get_connection_param(host, host_password,
host_user, port,
use_tls)
try: try:
with mail.get_connection(host=host, port=port, username=host_user, password=host_password, use_tls=use_tls, use_ssl=not use_tls) as connection: with mail.get_connection(host=host, port=port, username=host_user, password=host_password, use_tls=use_tls, use_ssl=not use_tls) as connection:
email.connection = connection email.connection = connection
...@@ -232,16 +237,16 @@ def send_email_with_error_log( ...@@ -232,16 +237,16 @@ def send_email_with_error_log(
return email_send return email_send
def send_email_get_connection_param(host, host_password, host_user, port, use_tls): def send_email_get_connection_param(test, host, host_password, host_user, port, use_tls):
from repanier.apps import REPANIER_SETTINGS_CONFIG from repanier.apps import REPANIER_SETTINGS_CONFIG
config = REPANIER_SETTINGS_CONFIG config = REPANIER_SETTINGS_CONFIG
if config.email_host_password and not settings.DJANGO_SETTINGS_DEMO: if config.email_is_custom and not test and not settings.DJANGO_SETTINGS_DEMO:
host = config.email_host host = config.email_host
port = config.email_port port = config.email_port
from_email = host_user = config.email_host_user from_email = host_user = config.email_host_user
host_password = config.email_host_password host_password = config.email_host_password
use_tls = config.email_use_tls use_tls = config.email_use_tls
elif host and host_user and not settings.DJANGO_SETTINGS_DEMO: if test and not settings.DJANGO_SETTINGS_DEMO:
from_email = host_user from_email = host_user
else: else:
host = settings.EMAIL_HOST host = settings.EMAIL_HOST
......
...@@ -99,8 +99,11 @@ def login_view(request, template_name='repanier/registration/login.html', ...@@ -99,8 +99,11 @@ def login_view(request, template_name='repanier/registration/login.html',
return HttpResponseRedirect(redirect_to) return HttpResponseRedirect(redirect_to)
else: else:
form = authentication_form(request) form = authentication_form(request)
how_to_register = REPANIER_SETTINGS_CONFIG.safe_translation_getter( try:
'how_to_register', any_language=True, default=EMPTY_STRING) how_to_register = REPANIER_SETTINGS_CONFIG.safe_translation_getter(
'how_to_register', any_language=True, default=EMPTY_STRING)
except:
how_to_register = EMPTY_STRING
current_site = get_current_site(request) current_site = get_current_site(request)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment