From 6da4c36c68622293d5a5d4d3af9270a89af7736c Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 12 Dec 2018 10:34:12 +0100 Subject: [PATCH] improved html tag stripping --- rowers/emails.py | 10 +++++++++- rowers/plannedsessions.py | 4 ++++ rowers/tasks.py | 3 ++- rowers/templates/race_submit.html | 2 +- rowers/views.py | 2 +- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/rowers/emails.py b/rowers/emails.py index 1203a3bd..8e4605d3 100644 --- a/rowers/emails.py +++ b/rowers/emails.py @@ -53,6 +53,14 @@ def textify(html): # Strip single spaces in the beginning of each line return text_only.replace('\n ', '\n').strip() +def htmlstripnobr(html): + safe_html = re.sub('[ \t]+', ' ', strip_tags(html)) + return safe_html + +def htmlstrip(html): + safe_html = re.sub('[ \t]+', ' ', strip_tags(html)) + return newlinetobr(safe_html) + def newlinetobr(html): html = html.replace('\n\n','
') return html.replace('\n','
') @@ -65,7 +73,7 @@ def send_template_email(from_email,to_email,subject, html_content = htmly.render(context) text_content = textify(html_content) - html_content = newlinetobr(html_content) + # html_content = newlinetobr(html_content) if 'cc' in kwargs: msg = EmailMultiAlternatives(subject, text_content, from_email, to_email,cc=kwargs['cc']) diff --git a/rowers/plannedsessions.py b/rowers/plannedsessions.py index 4a1da8e4..1bf1ab8a 100644 --- a/rowers/plannedsessions.py +++ b/rowers/plannedsessions.py @@ -25,6 +25,8 @@ from rowers.models import ( from rowers.courses import get_time_course +from rowers.emails import htmlstrip,htmlstripnobr + import rowers.mytypes as mytypes import metrics @@ -616,6 +618,7 @@ def update_plannedsession(ps,cd): def update_indoorvirtualrace(ps,cd): for attr, value in cd.items(): if attr == 'comment': + value = htmlstripnobr(value) value.replace("\r\n", " "); value.replace("\n", " "); setattr(ps, attr, value) @@ -666,6 +669,7 @@ def update_indoorvirtualrace(ps,cd): def update_virtualrace(ps,cd): for attr, value in cd.items(): if attr == 'comment': + value = htmlstripnobr(value) value.replace("\r\n", " "); value.replace("\n", " "); setattr(ps, attr, value) diff --git a/rowers/tasks.py b/rowers/tasks.py index d1f6855e..8601baa0 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -40,6 +40,7 @@ from django.utils import timezone from django.utils.html import strip_tags from utils import deserialize_list,ewmovingaverage,wavg +from emails import htmlstrip from HTMLParser import HTMLParser class MLStripper(HTMLParser): @@ -819,7 +820,7 @@ def handle_send_disqualification_email( 'username':username, 'reason':reason, 'siteurl':siteurl, - 'message': strip_tags(message), + 'message': htmlstrip(message), 'racename':racename, } diff --git a/rowers/templates/race_submit.html b/rowers/templates/race_submit.html index c4fd6468..e5e3fdab 100644 --- a/rowers/templates/race_submit.html +++ b/rowers/templates/race_submit.html @@ -39,7 +39,7 @@

{% csrf_token %} - +

diff --git a/rowers/views.py b/rowers/views.py index 61577504..8a32085c 100644 --- a/rowers/views.py +++ b/rowers/views.py @@ -186,7 +186,7 @@ import pandas as pd import numpy as np import matplotlib.pyplot as plt -from rowers.emails import send_template_email +from rowers.emails import send_template_email,htmlstrip from pytz import timezone as tz,utc from timezonefinder import TimezoneFinder