diff core/management/commands/ssl_images.py @ 860:6e20d3b1e7c2

Improve command line support for ssl_images.
author Brian Neal <bgneal@gmail.com>
date Tue, 25 Nov 2014 21:02:50 -0600
parents fca9281c535c
children 0ffdb434d2dd
line wrap: on
line diff
--- a/core/management/commands/ssl_images.py	Tue Nov 25 20:06:40 2014 -0600
+++ b/core/management/commands/ssl_images.py	Tue Nov 25 21:02:50 2014 -0600
@@ -13,6 +13,10 @@
 from django.core.management.base import NoArgsCommand, CommandError
 from django.conf import settings
 
+from comments.models import Comment
+from forums.models import Post
+
+
 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log')
 logger = logging.getLogger(__name__)
 
@@ -26,27 +30,56 @@
     logger.addHandler(handler)
 
 
+class CommentFacade(object):
+    """Wrapper class to provide uniform access to Comments."""
+    def __init__(self, comment):
+        self.comment = comment
+
+    @property
+    def markdown(self):
+        return self.comment.comment
+
+    @markdown.setter
+    def markdown(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 markdown(self):
+        return self.post.body
+
+    @markdown.setter
+    def markdown(self, value):
+        self.post.body = value
+
+
 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 (default)"),
+                help="process forum posts"),
             make_option('--comments',
                 action='store_true',
                 default=False,
                 help="process comments"),
-            make_option('--start',
+            make_option('-i', '--i',
                 type='int',
-                help="object to start at"),
-            make_option('--end',
+                help="first slice index; the i in [i:j]"),
+            make_option('-j', '--j',
                 type='int',
-                help="one past the end object"),
+                help="second slice index; the j in [i:j]"),
             )
 
     def handle_noargs(self, **options):
         _setup_logging()
+        logger.info("Starting; arguments received: %s", options)
 
         do_comments = options['comments']
         do_forums = options['forums']
@@ -55,4 +88,33 @@
         elif not do_comments and not do_forums:
             raise CommandError("Please specify --forums or --comments")
 
-        logger.info("Arguments received: %s", options)
+        if do_comments:
+            qs = Comment.objects.all()
+            facade = CommentFacade
+        else:
+            qs = Post.objects.all()
+            facade = PostFacade
+
+        i, j = options['i'], options['j']
+
+        if i is not None and i < 0:
+            raise CommandError("-i must be >= 0")
+        if j is not None and j < 0:
+            raise CommandError("-j must be >= 0")
+        if j is not None and i is not None and j <= i:
+            raise CommandError("-j must be > -i")
+
+        if i is not None and j is not None:
+            qs = qs[i:j]
+        elif i is not None and j is None:
+            qs = qs[i:]
+        elif i is None and j is not None:
+            qs = qs[:j]
+
+        s = []
+        for model in qs.iterator():
+            obj = facade(model)
+            s.append(obj.markdown)
+
+        import pprint
+        pprint.pprint(s)