diff core/management/commands/ssl_images.py @ 894:101728976f9c

Check html for <img src="http:...">. Older Smiley code generated absolute URLs for smiley images. Check for this and if found, save the model to force regeneration of HTML.
author Brian Neal <bgneal@gmail.com>
date Wed, 18 Feb 2015 21:20:31 -0600
parents ae146e30d588
children e7c549e4dbf7
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Tue Feb 17 18:59:06 2015 -0600
+++ b/core/management/commands/ssl_images.py	Wed Feb 18 21:20:31 2015 -0600
@@ -20,6 +20,7 @@
 
 from django.core.management.base import NoArgsCommand, CommandError
 from django.conf import settings
+from lxml import etree
 import markdown.inlinepatterns
 from PIL import Image
 
@@ -280,6 +281,21 @@
     return IMAGE_LINK_RE.sub(replace_image_markup, text)
 
 
+def html_check(html):
+    """Return True if the given HTML fragment has <img> tags with src attributes
+    that use http, and False otherwise.
+    """
+    if not html:
+        return False
+
+    root = etree.HTML(html)
+    for img in root.iter('img'):
+        src = img.get('src')
+        if src and src.lower().startswith('http:'):
+            return True
+    return False
+
+
 class Command(NoArgsCommand):
     help = "Rewrite forum posts and comments to not use http for images"
     option_list = NoArgsCommand.option_list + (
@@ -362,5 +378,11 @@
                 logger.debug("changed:  %s", new_txt)
                 setattr(model, text_attr, new_txt)
                 model.save()
+            elif html_check(model.html):
+                # Check for content generated with older smiley code that used
+                # absolute URLs for the smiley images. If True, then just save
+                # the model again to force updated HTML to be created.
+                logger.info("Older Smiley HTML detected, forcing a save")
+                model.save()
 
         logger.info("ssl_images exiting")