changeset 863:0ffdb434d2dd

Started fleshing out the ssl_images command.
author Brian Neal <bgneal@gmail.com>
date Tue, 02 Dec 2014 21:16:46 -0600 (2014-12-03)
parents 22a2362a8c9a
children 08bae2b1d2d1
files core/management/commands/ssl_images.py
diffstat 1 files changed, 42 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Mon Dec 01 18:39:40 2014 -0600
+++ b/core/management/commands/ssl_images.py	Tue Dec 02 21:16:46 2014 -0600
@@ -9,9 +9,12 @@
 import logging
 from optparse import make_option
 import os.path
+import re
+import signal
 
 from django.core.management.base import NoArgsCommand, CommandError
 from django.conf import settings
+import markdown.inlinepatterns
 
 from comments.models import Comment
 from forums.models import Post
@@ -20,6 +23,17 @@
 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
 logger = logging.getLogger(__name__)
 
+IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE)
+IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE)
+
+quit_flag = False
+
+
+def signal_handler(signum, frame):
+    """SIGINT signal handler"""
+    global quit_flag
+    quit_flag = True
+
 
 def _setup_logging():
     logger.setLevel(logging.DEBUG)
@@ -36,11 +50,11 @@
         self.comment = comment
 
     @property
-    def markdown(self):
+    def text(self):
         return self.comment.comment
 
-    @markdown.setter
-    def markdown(self, value):
+    @text.setter
+    def text(self, value):
         self.comment.comment = value
 
 
@@ -50,14 +64,27 @@
         self.post = post
 
     @property
-    def markdown(self):
+    def text(self):
         return self.post.body
 
-    @markdown.setter
-    def markdown(self, value):
+    @text.setter
+    def text(self, value):
         self.post.body = value
 
 
+def process_post(post):
+    """Process the post object:
+
+    A regex substitution is run on the post's text field. This fixes up image
+    links, getting rid of plain old http sources; either converting to https
+    or relative style links (if the link is to SG101).
+
+    We also do a search for Markdown image reference markup. We aren't expecting
+    these, but we will log something if we see any.
+
+    """
+
+
 class Command(NoArgsCommand):
     help = "Rewrite forum posts and comments to not use http for images"
     option_list = NoArgsCommand.option_list + (
@@ -71,10 +98,10 @@
                 help="process comments"),
             make_option('-i', '--i',
                 type='int',
-                help="first slice index; the i in [i:j]"),
+                help="optional first slice index; the i in [i:j]"),
             make_option('-j', '--j',
                 type='int',
-                help="second slice index; the j in [i:j]"),
+                help="optional second slice index; the j in [i:j]"),
             )
 
     def handle_noargs(self, **options):
@@ -111,10 +138,16 @@
         elif i is None and j is not None:
             qs = qs[:j]
 
+        # Install signal handler for ctrl-c
+        signal.signal(signal.SIGINT, signal_handler)
+
         s = []
         for model in qs.iterator():
+            if quit_flag:
+                logger.warning("SIGINT received, exiting")
             obj = facade(model)
-            s.append(obj.markdown)
+            process_post(obj)
+            s.append(obj.text)
 
         import pprint
         pprint.pprint(s)