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@859
|
16 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
|
bgneal@859
|
17 logger = logging.getLogger(__name__)
|
bgneal@859
|
18
|
bgneal@859
|
19
|
bgneal@859
|
20 def _setup_logging():
|
bgneal@859
|
21 logger.setLevel(logging.DEBUG)
|
bgneal@859
|
22 logger.propagate = False
|
bgneal@859
|
23 handler = logging.FileHandler(filename=LOGFILE, encoding='utf-8')
|
bgneal@859
|
24 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
bgneal@859
|
25 handler.setFormatter(formatter)
|
bgneal@859
|
26 logger.addHandler(handler)
|
bgneal@859
|
27
|
bgneal@859
|
28
|
bgneal@859
|
29 class Command(NoArgsCommand):
|
bgneal@859
|
30 help = "Rewrite forum posts and comments to not use http for images"
|
bgneal@859
|
31 option_list = NoArgsCommand.option_list + (
|
bgneal@859
|
32 make_option('--forums',
|
bgneal@859
|
33 action='store_true',
|
bgneal@859
|
34 default=False,
|
bgneal@859
|
35 help="process forum posts (default)"),
|
bgneal@859
|
36 make_option('--comments',
|
bgneal@859
|
37 action='store_true',
|
bgneal@859
|
38 default=False,
|
bgneal@859
|
39 help="process comments"),
|
bgneal@859
|
40 make_option('--start',
|
bgneal@859
|
41 type='int',
|
bgneal@859
|
42 help="object to start at"),
|
bgneal@859
|
43 make_option('--end',
|
bgneal@859
|
44 type='int',
|
bgneal@859
|
45 help="one past the end object"),
|
bgneal@859
|
46 )
|
bgneal@859
|
47
|
bgneal@859
|
48 def handle_noargs(self, **options):
|
bgneal@859
|
49 _setup_logging()
|
bgneal@859
|
50
|
bgneal@859
|
51 do_comments = options['comments']
|
bgneal@859
|
52 do_forums = options['forums']
|
bgneal@859
|
53 if do_comments and do_forums:
|
bgneal@859
|
54 raise CommandError("Please specify --forums or --comments, not both")
|
bgneal@859
|
55 elif not do_comments and not do_forums:
|
bgneal@859
|
56 raise CommandError("Please specify --forums or --comments")
|
bgneal@859
|
57
|
bgneal@859
|
58 logger.info("Arguments received: %s", options)
|