annotate core/management/commands/ssl_images.py @ 863:0ffdb434d2dd

Started fleshing out the ssl_images command.
author Brian Neal <bgneal@gmail.com>
date Tue, 02 Dec 2014 21:16:46 -0600
parents 6e20d3b1e7c2
children 98adae6e13a1
rev   line source
bgneal@859 1 """
bgneal@859 2 ssl_images is a custom manage.py command to convert forum post and comment
bgneal@859 3 images to https. It does this by rewriting the markup:
bgneal@859 4 - Images with src = http://surfguitar101.com/something are rewritten to be
bgneal@859 5 /something.
bgneal@859 6 - Non SG101 images that use http: are downloaded, resized, and uploaded to
bgneal@859 7 an S3 bucket. The src attribute is replaced with the new S3 URL.
bgneal@859 8 """
bgneal@859 9 import logging
bgneal@859 10 from optparse import make_option
bgneal@859 11 import os.path
bgneal@863 12 import re
bgneal@863 13 import signal
bgneal@859 14
bgneal@859 15 from django.core.management.base import NoArgsCommand, CommandError
bgneal@859 16 from django.conf import settings
bgneal@863 17 import markdown.inlinepatterns
bgneal@859 18
bgneal@860 19 from comments.models import Comment
bgneal@860 20 from forums.models import Post
bgneal@860 21
bgneal@860 22
bgneal@859 23 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
bgneal@859 24 logger = logging.getLogger(__name__)
bgneal@859 25
bgneal@863 26 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE)
bgneal@863 27 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE)
bgneal@863 28
bgneal@863 29 quit_flag = False
bgneal@863 30
bgneal@863 31
bgneal@863 32 def signal_handler(signum, frame):
bgneal@863 33 """SIGINT signal handler"""
bgneal@863 34 global quit_flag
bgneal@863 35 quit_flag = True
bgneal@863 36
bgneal@859 37
bgneal@859 38 def _setup_logging():
bgneal@859 39 logger.setLevel(logging.DEBUG)
bgneal@859 40 logger.propagate = False
bgneal@859 41 handler = logging.FileHandler(filename=LOGFILE, encoding='utf-8')
bgneal@859 42 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
bgneal@859 43 handler.setFormatter(formatter)
bgneal@859 44 logger.addHandler(handler)
bgneal@859 45
bgneal@859 46
bgneal@860 47 class CommentFacade(object):
bgneal@860 48 """Wrapper class to provide uniform access to Comments."""
bgneal@860 49 def __init__(self, comment):
bgneal@860 50 self.comment = comment
bgneal@860 51
bgneal@860 52 @property
bgneal@863 53 def text(self):
bgneal@860 54 return self.comment.comment
bgneal@860 55
bgneal@863 56 @text.setter
bgneal@863 57 def text(self, value):
bgneal@860 58 self.comment.comment = value
bgneal@860 59
bgneal@860 60
bgneal@860 61 class PostFacade(object):
bgneal@860 62 """Wrapper class to provide uniform access to Forum posts."""
bgneal@860 63 def __init__(self, post):
bgneal@860 64 self.post = post
bgneal@860 65
bgneal@860 66 @property
bgneal@863 67 def text(self):
bgneal@860 68 return self.post.body
bgneal@860 69
bgneal@863 70 @text.setter
bgneal@863 71 def text(self, value):
bgneal@860 72 self.post.body = value
bgneal@860 73
bgneal@860 74
bgneal@863 75 def process_post(post):
bgneal@863 76 """Process the post object:
bgneal@863 77
bgneal@863 78 A regex substitution is run on the post's text field. This fixes up image
bgneal@863 79 links, getting rid of plain old http sources; either converting to https
bgneal@863 80 or relative style links (if the link is to SG101).
bgneal@863 81
bgneal@863 82 We also do a search for Markdown image reference markup. We aren't expecting
bgneal@863 83 these, but we will log something if we see any.
bgneal@863 84
bgneal@863 85 """
bgneal@863 86
bgneal@863 87
bgneal@859 88 class Command(NoArgsCommand):
bgneal@859 89 help = "Rewrite forum posts and comments to not use http for images"
bgneal@859 90 option_list = NoArgsCommand.option_list + (
bgneal@859 91 make_option('--forums',
bgneal@859 92 action='store_true',
bgneal@859 93 default=False,
bgneal@860 94 help="process forum posts"),
bgneal@859 95 make_option('--comments',
bgneal@859 96 action='store_true',
bgneal@859 97 default=False,
bgneal@859 98 help="process comments"),
bgneal@860 99 make_option('-i', '--i',
bgneal@859 100 type='int',
bgneal@863 101 help="optional first slice index; the i in [i:j]"),
bgneal@860 102 make_option('-j', '--j',
bgneal@859 103 type='int',
bgneal@863 104 help="optional second slice index; the j in [i:j]"),
bgneal@859 105 )
bgneal@859 106
bgneal@859 107 def handle_noargs(self, **options):
bgneal@859 108 _setup_logging()
bgneal@860 109 logger.info("Starting; arguments received: %s", options)
bgneal@859 110
bgneal@859 111 do_comments = options['comments']
bgneal@859 112 do_forums = options['forums']
bgneal@859 113 if do_comments and do_forums:
bgneal@859 114 raise CommandError("Please specify --forums or --comments, not both")
bgneal@859 115 elif not do_comments and not do_forums:
bgneal@859 116 raise CommandError("Please specify --forums or --comments")
bgneal@859 117
bgneal@860 118 if do_comments:
bgneal@860 119 qs = Comment.objects.all()
bgneal@860 120 facade = CommentFacade
bgneal@860 121 else:
bgneal@860 122 qs = Post.objects.all()
bgneal@860 123 facade = PostFacade
bgneal@860 124
bgneal@860 125 i, j = options['i'], options['j']
bgneal@860 126
bgneal@860 127 if i is not None and i < 0:
bgneal@860 128 raise CommandError("-i must be >= 0")
bgneal@860 129 if j is not None and j < 0:
bgneal@860 130 raise CommandError("-j must be >= 0")
bgneal@860 131 if j is not None and i is not None and j <= i:
bgneal@860 132 raise CommandError("-j must be > -i")
bgneal@860 133
bgneal@860 134 if i is not None and j is not None:
bgneal@860 135 qs = qs[i:j]
bgneal@860 136 elif i is not None and j is None:
bgneal@860 137 qs = qs[i:]
bgneal@860 138 elif i is None and j is not None:
bgneal@860 139 qs = qs[:j]
bgneal@860 140
bgneal@863 141 # Install signal handler for ctrl-c
bgneal@863 142 signal.signal(signal.SIGINT, signal_handler)
bgneal@863 143
bgneal@860 144 s = []
bgneal@860 145 for model in qs.iterator():
bgneal@863 146 if quit_flag:
bgneal@863 147 logger.warning("SIGINT received, exiting")
bgneal@860 148 obj = facade(model)
bgneal@863 149 process_post(obj)
bgneal@863 150 s.append(obj.text)
bgneal@860 151
bgneal@860 152 import pprint
bgneal@860 153 pprint.pprint(s)