annotate core/management/commands/ssl_images.py @ 862:22a2362a8c9a

Merge with upstream.
author Brian Neal <bgneal@gmail.com>
date Mon, 01 Dec 2014 18:39:40 -0600
parents 6e20d3b1e7c2
children 0ffdb434d2dd
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@859 12
bgneal@859 13 from django.core.management.base import NoArgsCommand, CommandError
bgneal@859 14 from django.conf import settings
bgneal@859 15
bgneal@860 16 from comments.models import Comment
bgneal@860 17 from forums.models import Post
bgneal@860 18
bgneal@860 19
bgneal@859 20 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
bgneal@859 21 logger = logging.getLogger(__name__)
bgneal@859 22
bgneal@859 23
bgneal@859 24 def _setup_logging():
bgneal@859 25 logger.setLevel(logging.DEBUG)
bgneal@859 26 logger.propagate = False
bgneal@859 27 handler = logging.FileHandler(filename=LOGFILE, encoding='utf-8')
bgneal@859 28 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
bgneal@859 29 handler.setFormatter(formatter)
bgneal@859 30 logger.addHandler(handler)
bgneal@859 31
bgneal@859 32
bgneal@860 33 class CommentFacade(object):
bgneal@860 34 """Wrapper class to provide uniform access to Comments."""
bgneal@860 35 def __init__(self, comment):
bgneal@860 36 self.comment = comment
bgneal@860 37
bgneal@860 38 @property
bgneal@860 39 def markdown(self):
bgneal@860 40 return self.comment.comment
bgneal@860 41
bgneal@860 42 @markdown.setter
bgneal@860 43 def markdown(self, value):
bgneal@860 44 self.comment.comment = value
bgneal@860 45
bgneal@860 46
bgneal@860 47 class PostFacade(object):
bgneal@860 48 """Wrapper class to provide uniform access to Forum posts."""
bgneal@860 49 def __init__(self, post):
bgneal@860 50 self.post = post
bgneal@860 51
bgneal@860 52 @property
bgneal@860 53 def markdown(self):
bgneal@860 54 return self.post.body
bgneal@860 55
bgneal@860 56 @markdown.setter
bgneal@860 57 def markdown(self, value):
bgneal@860 58 self.post.body = value
bgneal@860 59
bgneal@860 60
bgneal@859 61 class Command(NoArgsCommand):
bgneal@859 62 help = "Rewrite forum posts and comments to not use http for images"
bgneal@859 63 option_list = NoArgsCommand.option_list + (
bgneal@859 64 make_option('--forums',
bgneal@859 65 action='store_true',
bgneal@859 66 default=False,
bgneal@860 67 help="process forum posts"),
bgneal@859 68 make_option('--comments',
bgneal@859 69 action='store_true',
bgneal@859 70 default=False,
bgneal@859 71 help="process comments"),
bgneal@860 72 make_option('-i', '--i',
bgneal@859 73 type='int',
bgneal@860 74 help="first slice index; the i in [i:j]"),
bgneal@860 75 make_option('-j', '--j',
bgneal@859 76 type='int',
bgneal@860 77 help="second slice index; the j in [i:j]"),
bgneal@859 78 )
bgneal@859 79
bgneal@859 80 def handle_noargs(self, **options):
bgneal@859 81 _setup_logging()
bgneal@860 82 logger.info("Starting; arguments received: %s", options)
bgneal@859 83
bgneal@859 84 do_comments = options['comments']
bgneal@859 85 do_forums = options['forums']
bgneal@859 86 if do_comments and do_forums:
bgneal@859 87 raise CommandError("Please specify --forums or --comments, not both")
bgneal@859 88 elif not do_comments and not do_forums:
bgneal@859 89 raise CommandError("Please specify --forums or --comments")
bgneal@859 90
bgneal@860 91 if do_comments:
bgneal@860 92 qs = Comment.objects.all()
bgneal@860 93 facade = CommentFacade
bgneal@860 94 else:
bgneal@860 95 qs = Post.objects.all()
bgneal@860 96 facade = PostFacade
bgneal@860 97
bgneal@860 98 i, j = options['i'], options['j']
bgneal@860 99
bgneal@860 100 if i is not None and i < 0:
bgneal@860 101 raise CommandError("-i must be >= 0")
bgneal@860 102 if j is not None and j < 0:
bgneal@860 103 raise CommandError("-j must be >= 0")
bgneal@860 104 if j is not None and i is not None and j <= i:
bgneal@860 105 raise CommandError("-j must be > -i")
bgneal@860 106
bgneal@860 107 if i is not None and j is not None:
bgneal@860 108 qs = qs[i:j]
bgneal@860 109 elif i is not None and j is None:
bgneal@860 110 qs = qs[i:]
bgneal@860 111 elif i is None and j is not None:
bgneal@860 112 qs = qs[:j]
bgneal@860 113
bgneal@860 114 s = []
bgneal@860 115 for model in qs.iterator():
bgneal@860 116 obj = facade(model)
bgneal@860 117 s.append(obj.markdown)
bgneal@860 118
bgneal@860 119 import pprint
bgneal@860 120 pprint.pprint(s)