comparison core/management/commands/ssl_images.py @ 859:fca9281c535c

Started work on command to update posts for SSL.
author Brian Neal <bgneal@gmail.com>
date Tue, 25 Nov 2014 20:06:40 -0600
parents
children 6e20d3b1e7c2
comparison
equal deleted inserted replaced
858:a4d685cdecdd 859:fca9281c535c
1 """
2 ssl_images is a custom manage.py command to convert forum post and comment
3 images to https. It does this by rewriting the markup:
4 - Images with src = http://surfguitar101.com/something are rewritten to be
5 /something.
6 - Non SG101 images that use http: are downloaded, resized, and uploaded to
7 an S3 bucket. The src attribute is replaced with the new S3 URL.
8 """
9 import logging
10 from optparse import make_option
11 import os.path
12
13 from django.core.management.base import NoArgsCommand, CommandError
14 from django.conf import settings
15
16 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
17 logger = logging.getLogger(__name__)
18
19
20 def _setup_logging():
21 logger.setLevel(logging.DEBUG)
22 logger.propagate = False
23 handler = logging.FileHandler(filename=LOGFILE, encoding='utf-8')
24 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
25 handler.setFormatter(formatter)
26 logger.addHandler(handler)
27
28
29 class Command(NoArgsCommand):
30 help = "Rewrite forum posts and comments to not use http for images"
31 option_list = NoArgsCommand.option_list + (
32 make_option('--forums',
33 action='store_true',
34 default=False,
35 help="process forum posts (default)"),
36 make_option('--comments',
37 action='store_true',
38 default=False,
39 help="process comments"),
40 make_option('--start',
41 type='int',
42 help="object to start at"),
43 make_option('--end',
44 type='int',
45 help="one past the end object"),
46 )
47
48 def handle_noargs(self, **options):
49 _setup_logging()
50
51 do_comments = options['comments']
52 do_forums = options['forums']
53 if do_comments and do_forums:
54 raise CommandError("Please specify --forums or --comments, not both")
55 elif not do_comments and not do_forums:
56 raise CommandError("Please specify --forums or --comments")
57
58 logger.info("Arguments received: %s", options)