comparison 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
comparison
equal deleted inserted replaced
862:22a2362a8c9a 863:0ffdb434d2dd
7 an S3 bucket. The src attribute is replaced with the new S3 URL. 7 an S3 bucket. The src attribute is replaced with the new S3 URL.
8 """ 8 """
9 import logging 9 import logging
10 from optparse import make_option 10 from optparse import make_option
11 import os.path 11 import os.path
12 import re
13 import signal
12 14
13 from django.core.management.base import NoArgsCommand, CommandError 15 from django.core.management.base import NoArgsCommand, CommandError
14 from django.conf import settings 16 from django.conf import settings
17 import markdown.inlinepatterns
15 18
16 from comments.models import Comment 19 from comments.models import Comment
17 from forums.models import Post 20 from forums.models import Post
18 21
19 22
20 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log') 23 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
21 logger = logging.getLogger(__name__) 24 logger = logging.getLogger(__name__)
25
26 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE)
27 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE)
28
29 quit_flag = False
30
31
32 def signal_handler(signum, frame):
33 """SIGINT signal handler"""
34 global quit_flag
35 quit_flag = True
22 36
23 37
24 def _setup_logging(): 38 def _setup_logging():
25 logger.setLevel(logging.DEBUG) 39 logger.setLevel(logging.DEBUG)
26 logger.propagate = False 40 logger.propagate = False
34 """Wrapper class to provide uniform access to Comments.""" 48 """Wrapper class to provide uniform access to Comments."""
35 def __init__(self, comment): 49 def __init__(self, comment):
36 self.comment = comment 50 self.comment = comment
37 51
38 @property 52 @property
39 def markdown(self): 53 def text(self):
40 return self.comment.comment 54 return self.comment.comment
41 55
42 @markdown.setter 56 @text.setter
43 def markdown(self, value): 57 def text(self, value):
44 self.comment.comment = value 58 self.comment.comment = value
45 59
46 60
47 class PostFacade(object): 61 class PostFacade(object):
48 """Wrapper class to provide uniform access to Forum posts.""" 62 """Wrapper class to provide uniform access to Forum posts."""
49 def __init__(self, post): 63 def __init__(self, post):
50 self.post = post 64 self.post = post
51 65
52 @property 66 @property
53 def markdown(self): 67 def text(self):
54 return self.post.body 68 return self.post.body
55 69
56 @markdown.setter 70 @text.setter
57 def markdown(self, value): 71 def text(self, value):
58 self.post.body = value 72 self.post.body = value
73
74
75 def process_post(post):
76 """Process the post object:
77
78 A regex substitution is run on the post's text field. This fixes up image
79 links, getting rid of plain old http sources; either converting to https
80 or relative style links (if the link is to SG101).
81
82 We also do a search for Markdown image reference markup. We aren't expecting
83 these, but we will log something if we see any.
84
85 """
59 86
60 87
61 class Command(NoArgsCommand): 88 class Command(NoArgsCommand):
62 help = "Rewrite forum posts and comments to not use http for images" 89 help = "Rewrite forum posts and comments to not use http for images"
63 option_list = NoArgsCommand.option_list + ( 90 option_list = NoArgsCommand.option_list + (
69 action='store_true', 96 action='store_true',
70 default=False, 97 default=False,
71 help="process comments"), 98 help="process comments"),
72 make_option('-i', '--i', 99 make_option('-i', '--i',
73 type='int', 100 type='int',
74 help="first slice index; the i in [i:j]"), 101 help="optional first slice index; the i in [i:j]"),
75 make_option('-j', '--j', 102 make_option('-j', '--j',
76 type='int', 103 type='int',
77 help="second slice index; the j in [i:j]"), 104 help="optional second slice index; the j in [i:j]"),
78 ) 105 )
79 106
80 def handle_noargs(self, **options): 107 def handle_noargs(self, **options):
81 _setup_logging() 108 _setup_logging()
82 logger.info("Starting; arguments received: %s", options) 109 logger.info("Starting; arguments received: %s", options)
109 elif i is not None and j is None: 136 elif i is not None and j is None:
110 qs = qs[i:] 137 qs = qs[i:]
111 elif i is None and j is not None: 138 elif i is None and j is not None:
112 qs = qs[:j] 139 qs = qs[:j]
113 140
141 # Install signal handler for ctrl-c
142 signal.signal(signal.SIGINT, signal_handler)
143
114 s = [] 144 s = []
115 for model in qs.iterator(): 145 for model in qs.iterator():
146 if quit_flag:
147 logger.warning("SIGINT received, exiting")
116 obj = facade(model) 148 obj = facade(model)
117 s.append(obj.markdown) 149 process_post(obj)
150 s.append(obj.text)
118 151
119 import pprint 152 import pprint
120 pprint.pprint(s) 153 pprint.pprint(s)