comparison 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
comparison
equal deleted inserted replaced
893:3aecf9058130 894:101728976f9c
18 import urlparse 18 import urlparse
19 import uuid 19 import uuid
20 20
21 from django.core.management.base import NoArgsCommand, CommandError 21 from django.core.management.base import NoArgsCommand, CommandError
22 from django.conf import settings 22 from django.conf import settings
23 from lxml import etree
23 import markdown.inlinepatterns 24 import markdown.inlinepatterns
24 from PIL import Image 25 from PIL import Image
25 26
26 from comments.models import Comment 27 from comments.models import Comment
27 from forums.models import Post 28 from forums.models import Post
278 279
279 """ 280 """
280 return IMAGE_LINK_RE.sub(replace_image_markup, text) 281 return IMAGE_LINK_RE.sub(replace_image_markup, text)
281 282
282 283
284 def html_check(html):
285 """Return True if the given HTML fragment has <img> tags with src attributes
286 that use http, and False otherwise.
287 """
288 if not html:
289 return False
290
291 root = etree.HTML(html)
292 for img in root.iter('img'):
293 src = img.get('src')
294 if src and src.lower().startswith('http:'):
295 return True
296 return False
297
298
283 class Command(NoArgsCommand): 299 class Command(NoArgsCommand):
284 help = "Rewrite forum posts and comments to not use http for images" 300 help = "Rewrite forum posts and comments to not use http for images"
285 option_list = NoArgsCommand.option_list + ( 301 option_list = NoArgsCommand.option_list + (
286 make_option('-m', '--model', 302 make_option('-m', '--model',
287 choices=MODEL_CHOICES, 303 choices=MODEL_CHOICES,
360 model_name, n + i, model.pk) 376 model_name, n + i, model.pk)
361 logger.debug("original: %s", txt) 377 logger.debug("original: %s", txt)
362 logger.debug("changed: %s", new_txt) 378 logger.debug("changed: %s", new_txt)
363 setattr(model, text_attr, new_txt) 379 setattr(model, text_attr, new_txt)
364 model.save() 380 model.save()
381 elif html_check(model.html):
382 # Check for content generated with older smiley code that used
383 # absolute URLs for the smiley images. If True, then just save
384 # the model again to force updated HTML to be created.
385 logger.info("Older Smiley HTML detected, forcing a save")
386 model.save()
365 387
366 logger.info("ssl_images exiting") 388 logger.info("ssl_images exiting")