Commit 4d6fcb78 authored by Patrick's avatar Patrick

Use common send mail even for password reset to avoid : error: [Errno 101] Network is unreachable

parent 3a123e59
......@@ -114,17 +114,24 @@ def send_email(email=None, from_name=EMPTY_STRING, track_customer_on_error=False
customer = None
for email_to in send_email_to:
email.to = [email_to]
send_email_with_error_log(email, from_name, track_customer_on_error)
time.sleep(1)
def send_email_with_error_log(email, from_name=None, track_customer_on_error=False):
send_mail = True
if track_customer_on_error:
# If the email is conained both in user__email and customer__email2
# select the customer based on user__email
# select the customer based on user__email or customer__email2
email_to = email.to[0]
customer = models.Customer.objects.filter(user__email=email_to).order_by('?').first()
if customer is None:
customer = models.Customer.objects.filter(email2=email_to).order_by('?').first()
send_email_with_error_log(email, from_name, customer)
time.sleep(1)
def send_email_with_error_log(email, from_name=EMPTY_STRING, customer=None):
if customer is None or customer.valid_email is False:
send_mail = False
print("################################## send_email customer.valid_email == False")
else:
customer = None
if send_mail:
with mail.get_connection() as connection:
email.connection = connection
message = EMPTY_STRING
......@@ -152,7 +159,7 @@ def send_email_with_error_log(email, from_name=EMPTY_STRING, customer=None):
valid_email = False
print("################################## send_email error")
print(error_str)
time.sleep(2)
time.sleep(1)
connection = mail.get_connection()
connection.open()
mail_admins("ERROR", "%s\n%s" % (message, error_str), connection=connection)
......@@ -161,15 +168,15 @@ def send_email_with_error_log(email, from_name=EMPTY_STRING, customer=None):
valid_email = None
print("################################## send_email error")
print(error_str)
time.sleep(2)
time.sleep(1)
connection = mail.get_connection()
connection.open()
mail_admins("ERROR", "%s\n%s" % (message, error_str), connection=connection)
connection.close()
print("##################################")
if customer is not None:
customer.valid_email = valid_email
customer.save(update_fields=['valid_email'])
print("##################################")
def send_email_to_who(is_email_send, board=False):
......@@ -1493,8 +1500,7 @@ def reorder_offer_items(permanence_id):
is_box=False,
translations__language_code=language_code
).order_by(
"department_for_customer__tree_id",
"department_for_customer__lft",
"department_for_customer",
"translations__long_name",
"order_average_weight",
"producer__short_profile_name"
......@@ -1511,8 +1517,7 @@ def reorder_offer_items(permanence_id):
producer__sort_products_by_reference=True,
translations__language_code=language_code
).order_by(
"department_for_customer__tree_id",
"department_for_customer__lft",
"department_for_customer",
"reference"
)
for offer_item in reorder_queryset:
......@@ -1526,7 +1531,7 @@ def reorder_offer_items(permanence_id):
is_box=False,
translations__language_code=language_code
).order_by(
"department_for_customer__tree_id",
"department_for_customer",
# "department_for_customer__lft",
"translations__long_name",
"order_average_weight",
......
......@@ -23,6 +23,7 @@ from repanier.picture.const import SIZE_M
from repanier.picture.widgets import AjaxPictureWidget
from repanier.widget.select_bootstrap import SelectBootstrapWidget
from repanier.widget.select_producer_order_unit import SelectProducerOrderUnitWidget
from repanier.tools import send_email_with_error_log
class AuthRepanierLoginForm(AuthenticationForm):
......@@ -71,7 +72,8 @@ class AuthRepanierPasswordResetForm(PasswordResetForm):
html_email = loader.render_to_string(html_email_template_name, context)
email_message.attach_alternative(html_email, 'text/html')
email_message.send()
# email_message.send()
send_email_with_error_log(email_message, track_customer_on_error=True)
# From Django 1.8, this let the user enter name or email to recover
def get_users(self, email):
......
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