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@866
|
29 MODEL_CHOICES = ['comments', 'posts']
|
bgneal@866
|
30
|
bgneal@863
|
31 quit_flag = False
|
bgneal@863
|
32
|
bgneal@863
|
33
|
bgneal@863
|
34 def signal_handler(signum, frame):
|
bgneal@863
|
35 """SIGINT signal handler"""
|
bgneal@863
|
36 global quit_flag
|
bgneal@863
|
37 quit_flag = True
|
bgneal@863
|
38
|
bgneal@859
|
39
|
bgneal@859
|
40 def _setup_logging():
|
bgneal@859
|
41 logger.setLevel(logging.DEBUG)
|
bgneal@859
|
42 logger.propagate = False
|
bgneal@859
|
43 handler = logging.FileHandler(filename=LOGFILE, encoding='utf-8')
|
bgneal@859
|
44 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
|
bgneal@859
|
45 handler.setFormatter(formatter)
|
bgneal@859
|
46 logger.addHandler(handler)
|
bgneal@859
|
47
|
bgneal@859
|
48
|
bgneal@866
|
49 def replace_image_markup(match):
|
bgneal@866
|
50 return match.group(0)
|
bgneal@860
|
51
|
bgneal@860
|
52
|
bgneal@866
|
53 def process_post(text):
|
bgneal@863
|
54 """Process the post object:
|
bgneal@863
|
55
|
bgneal@863
|
56 A regex substitution is run on the post's text field. This fixes up image
|
bgneal@863
|
57 links, getting rid of plain old http sources; either converting to https
|
bgneal@863
|
58 or relative style links (if the link is to SG101).
|
bgneal@863
|
59
|
bgneal@863
|
60 We also do a search for Markdown image reference markup. We aren't expecting
|
bgneal@863
|
61 these, but we will log something if we see any.
|
bgneal@863
|
62
|
bgneal@863
|
63 """
|
bgneal@866
|
64 return IMAGE_LINK_RE.sub(replace_image_markup, text)
|
bgneal@863
|
65
|
bgneal@863
|
66
|
bgneal@859
|
67 class Command(NoArgsCommand):
|
bgneal@859
|
68 help = "Rewrite forum posts and comments to not use http for images"
|
bgneal@859
|
69 option_list = NoArgsCommand.option_list + (
|
bgneal@866
|
70 make_option('-m', '--model',
|
bgneal@866
|
71 choices=MODEL_CHOICES,
|
bgneal@866
|
72 help="which model to update; must be one of {{{}}}".format(
|
bgneal@866
|
73 ', '.join(MODEL_CHOICES))),
|
bgneal@860
|
74 make_option('-i', '--i',
|
bgneal@859
|
75 type='int',
|
bgneal@863
|
76 help="optional first slice index; the i in [i:j]"),
|
bgneal@860
|
77 make_option('-j', '--j',
|
bgneal@859
|
78 type='int',
|
bgneal@863
|
79 help="optional second slice index; the j in [i:j]"),
|
bgneal@859
|
80 )
|
bgneal@859
|
81
|
bgneal@859
|
82 def handle_noargs(self, **options):
|
bgneal@859
|
83 _setup_logging()
|
bgneal@860
|
84 logger.info("Starting; arguments received: %s", options)
|
bgneal@859
|
85
|
bgneal@866
|
86 if options['model'] not in MODEL_CHOICES:
|
bgneal@866
|
87 raise CommandError('Please choose a --model option')
|
bgneal@859
|
88
|
bgneal@866
|
89 if options['model'] == 'comments':
|
bgneal@860
|
90 qs = Comment.objects.all()
|
bgneal@866
|
91 text_attr = 'comment'
|
bgneal@860
|
92 else:
|
bgneal@860
|
93 qs = Post.objects.all()
|
bgneal@866
|
94 text_attr = 'body'
|
bgneal@860
|
95
|
bgneal@860
|
96 i, j = options['i'], options['j']
|
bgneal@860
|
97
|
bgneal@860
|
98 if i is not None and i < 0:
|
bgneal@860
|
99 raise CommandError("-i must be >= 0")
|
bgneal@860
|
100 if j is not None and j < 0:
|
bgneal@860
|
101 raise CommandError("-j must be >= 0")
|
bgneal@860
|
102 if j is not None and i is not None and j <= i:
|
bgneal@860
|
103 raise CommandError("-j must be > -i")
|
bgneal@860
|
104
|
bgneal@860
|
105 if i is not None and j is not None:
|
bgneal@860
|
106 qs = qs[i:j]
|
bgneal@860
|
107 elif i is not None and j is None:
|
bgneal@860
|
108 qs = qs[i:]
|
bgneal@860
|
109 elif i is None and j is not None:
|
bgneal@860
|
110 qs = qs[:j]
|
bgneal@860
|
111
|
bgneal@863
|
112 # Install signal handler for ctrl-c
|
bgneal@863
|
113 signal.signal(signal.SIGINT, signal_handler)
|
bgneal@863
|
114
|
bgneal@860
|
115 s = []
|
bgneal@860
|
116 for model in qs.iterator():
|
bgneal@863
|
117 if quit_flag:
|
bgneal@863
|
118 logger.warning("SIGINT received, exiting")
|
bgneal@866
|
119 txt = getattr(model, text_attr)
|
bgneal@866
|
120 new_txt = process_post(txt)
|
bgneal@866
|
121 s.append(new_txt)
|
bgneal@860
|
122
|
bgneal@860
|
123 import pprint
|
bgneal@860
|
124 pprint.pprint(s)
|