diff core/management/commands/ssl_images.py @ 866:98adae6e13a1

More WIP on ssl_images command.
author Brian Neal <bgneal@gmail.com>
date Thu, 04 Dec 2014 20:32:43 -0600
parents 0ffdb434d2dd
children 64a5acb83937
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Wed Dec 03 19:24:53 2014 -0600
+++ b/core/management/commands/ssl_images.py	Thu Dec 04 20:32:43 2014 -0600
@@ -26,6 +26,8 @@
 IMAGE_LINK_RE = re.compile(markdown.inlinepatterns.IMAGE_LINK_RE)
 IMAGE_REF_RE = re.compile(markdown.inlinepatterns.IMAGE_REFERENCE_RE)
 
+MODEL_CHOICES = ['comments', 'posts']
+
 quit_flag = False
 
 
@@ -44,35 +46,11 @@
     logger.addHandler(handler)
 
 
-class CommentFacade(object):
-    """Wrapper class to provide uniform access to Comments."""
-    def __init__(self, comment):
-        self.comment = comment
+def replace_image_markup(match):
+    return match.group(0)
 
-    @property
-    def text(self):
-        return self.comment.comment
 
-    @text.setter
-    def text(self, value):
-        self.comment.comment = value
-
-
-class PostFacade(object):
-    """Wrapper class to provide uniform access to Forum posts."""
-    def __init__(self, post):
-        self.post = post
-
-    @property
-    def text(self):
-        return self.post.body
-
-    @text.setter
-    def text(self, value):
-        self.post.body = value
-
-
-def process_post(post):
+def process_post(text):
     """Process the post object:
 
     A regex substitution is run on the post's text field. This fixes up image
@@ -83,19 +61,16 @@
     these, but we will log something if we see any.
 
     """
+    return IMAGE_LINK_RE.sub(replace_image_markup, text)
 
 
 class Command(NoArgsCommand):
     help = "Rewrite forum posts and comments to not use http for images"
     option_list = NoArgsCommand.option_list + (
-            make_option('--forums',
-                action='store_true',
-                default=False,
-                help="process forum posts"),
-            make_option('--comments',
-                action='store_true',
-                default=False,
-                help="process comments"),
+            make_option('-m', '--model',
+                choices=MODEL_CHOICES,
+                help="which model to update; must be one of {{{}}}".format(
+                                                    ', '.join(MODEL_CHOICES))),
             make_option('-i', '--i',
                 type='int',
                 help="optional first slice index; the i in [i:j]"),
@@ -108,19 +83,15 @@
         _setup_logging()
         logger.info("Starting; arguments received: %s", options)
 
-        do_comments = options['comments']
-        do_forums = options['forums']
-        if do_comments and do_forums:
-            raise CommandError("Please specify --forums or --comments, not both")
-        elif not do_comments and not do_forums:
-            raise CommandError("Please specify --forums or --comments")
+        if options['model'] not in MODEL_CHOICES:
+            raise CommandError('Please choose a --model option')
 
-        if do_comments:
+        if options['model'] == 'comments':
             qs = Comment.objects.all()
-            facade = CommentFacade
+            text_attr = 'comment'
         else:
             qs = Post.objects.all()
-            facade = PostFacade
+            text_attr = 'body'
 
         i, j = options['i'], options['j']
 
@@ -145,9 +116,9 @@
         for model in qs.iterator():
             if quit_flag:
                 logger.warning("SIGINT received, exiting")
-            obj = facade(model)
-            process_post(obj)
-            s.append(obj.text)
+            txt = getattr(model, text_attr)
+            new_txt = process_post(txt)
+            s.append(new_txt)
 
         import pprint
         pprint.pprint(s)