comparison core/management/commands/ssl_images.py @ 1012:fc528d4509b0

Prevent mixed content in UserProfiles. Modified sslimages to update UserProfiles. Added another command to check/re-save UserProfiles.
author Brian Neal <bgneal@gmail.com>
date Fri, 27 Nov 2015 16:56:33 -0600
parents 65b2bc9cb3cc
children
comparison
equal deleted inserted replaced
1011:164a39d985ef 1012:fc528d4509b0
23 import lxml.html 23 import lxml.html
24 import markdown.inlinepatterns 24 import markdown.inlinepatterns
25 from PIL import Image 25 from PIL import Image
26 import requests 26 import requests
27 27
28 from bio.models import UserProfile
28 from comments.models import Comment 29 from comments.models import Comment
29 from forums.models import Post 30 from forums.models import Post
30 from core.download import download_file 31 from core.download import download_file
31 from core.functions import remove_file 32 from core.functions import remove_file
32 from core.s3 import S3Bucket 33 from core.s3 import S3Bucket
41 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE, 42 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE,
42 re.DOTALL | re.UNICODE) 43 re.DOTALL | re.UNICODE)
43 44
44 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com']) 45 SG101_HOSTS = set(['www.surfguitar101.com', 'surfguitar101.com'])
45 WHITELIST_HOSTS = set(settings.USER_IMAGES_SOURCES) 46 WHITELIST_HOSTS = set(settings.USER_IMAGES_SOURCES)
46 MODEL_CHOICES = ['comments', 'posts', 'news'] 47 MODEL_CHOICES = ['comments', 'posts', 'news', 'profiles']
47 48
48 PHOTO_MAX_SIZE = (660, 720) 49 PHOTO_MAX_SIZE = (660, 720)
49 PHOTO_BASE_URL = settings.HOT_LINK_PHOTOS_BASE_URL 50 PHOTO_BASE_URL = settings.HOT_LINK_PHOTOS_BASE_URL
50 PHOTO_BUCKET_NAME = settings.HOT_LINK_PHOTOS_BUCKET 51 PHOTO_BUCKET_NAME = settings.HOT_LINK_PHOTOS_BUCKET
51 52
326 logger.info("Starting; arguments received: %s", options) 327 logger.info("Starting; arguments received: %s", options)
327 328
328 if options['model'] not in MODEL_CHOICES: 329 if options['model'] not in MODEL_CHOICES:
329 raise CommandError('Please choose a --model option') 330 raise CommandError('Please choose a --model option')
330 331
332 save_kwargs = {}
331 if options['model'] == 'comments': 333 if options['model'] == 'comments':
332 qs = Comment.objects.all() 334 qs = Comment.objects.all()
333 text_attrs = ['comment'] 335 text_attrs = ['comment']
334 model_name = 'Comment' 336 model_name = 'Comment'
335 elif options['model'] == 'posts': 337 elif options['model'] == 'posts':
336 qs = Post.objects.all() 338 qs = Post.objects.all()
337 text_attrs = ['body'] 339 text_attrs = ['body']
338 model_name = 'Post' 340 model_name = 'Post'
341 elif options['model'] == 'profiles':
342 qs = UserProfile.objects.all()
343 text_attrs = ['profile_text', 'signature']
344 model_name = 'UserProfile'
345 save_kwargs = {'content_update': True}
339 else: 346 else:
340 qs = Story.objects.all() 347 qs = Story.objects.all()
341 text_attrs = ['short_text', 'long_text'] 348 text_attrs = ['short_text', 'long_text']
342 model_name = 'Story' 349 model_name = 'Story'
343 350
401 model_name, n + i, model.pk) 408 model_name, n + i, model.pk)
402 logger.debug(u"original: %s", txt) 409 logger.debug(u"original: %s", txt)
403 logger.debug(u"changed: %s", new_txt) 410 logger.debug(u"changed: %s", new_txt)
404 setattr(model, text_attr, new_txt) 411 setattr(model, text_attr, new_txt)
405 save_flag = True 412 save_flag = True
406 elif not html_based and html_check(model.html): 413 elif not html_based and hasattr(model, 'html') and html_check(model.html):
407 # Check for content generated with older smiley code that used 414 # Check for content generated with older smiley code that used
408 # absolute URLs for the smiley images. If True, then just save 415 # absolute URLs for the smiley images. If True, then just save
409 # the model again to force updated HTML to be created. 416 # the model again to force updated HTML to be created.
410 logger.info("Older Smiley HTML detected, forcing a save") 417 logger.info("Older Smiley HTML detected, forcing a save")
411 save_flag = True 418 save_flag = True
412 419
413 if save_flag: 420 if save_flag:
414 model.save() 421 model.save(**save_kwargs)
415 count += 1 422 count += 1
416 423
417 time_finished = datetime.datetime.now() 424 time_finished = datetime.datetime.now()
418 elapsed = time_finished - time_started 425 elapsed = time_finished - time_started
419 logger.info("ssl_images exiting; number of objects: %d; elapsed: %s", 426 logger.info("ssl_images exiting; number of objects: %d; elapsed: %s",