1. Yann Malet
  2. byteflow

Commits

tony  committed 090a087

#74 fix - correct html specialchars quoting in comment email notify

  • Participants
  • Parent commits f52ec11
  • Branches default
  • Tags 0.7.1

Comments (0)

Files changed (3)

File apps/discussion/models.py

View file
  • Ignore whitespace
         return strip_tags(self.body_html)[:50]
     get_clean_html.allow_tags = True
 
+    @property
+    def mail_body(self):
+        """
+        Only for case when user types char code in comment. F.e. user
+        types € - it'll be rendered in HTML correctly, but we also
+        need it in mail messages.
+        """
+        from lib.html import descape
+        return descape(self.body)
+
     def save(self):
         if self.body:
             self.body = self.body.strip()

File apps/lib/html.py

View file
  • Ignore whitespace
+import htmlentitydefs
+import re
+
+pattern = re.compile("&(\w+?);")
+
+def descape_entity(m, defs=htmlentitydefs.name2codepoint):
+    # callback: translate one entity to its ISO Latin value
+    try:
+        return unichr(defs[m.group(1)])
+    except KeyError:
+        return m.group(0) # use as is
+
+def descape(string):
+    return pattern.sub(descape_entity, string)
+

File apps/watchlist/mail.py

View file
  • Ignore whitespace
 DEFAULT_COMMENT_BODY = '''Author: {{ comment.user.first_name|default:comment.user.username }}
 
 Comment text:
-{{ comment.body }}
+{{ comment.mail_body|safe }}
 
 Reply: {{ site_url }}{{ comment.get_absolute_url }}