Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
859:fca9281c535c | 860:6e20d3b1e7c2 |
---|---|
11 import os.path | 11 import os.path |
12 | 12 |
13 from django.core.management.base import NoArgsCommand, CommandError | 13 from django.core.management.base import NoArgsCommand, CommandError |
14 from django.conf import settings | 14 from django.conf import settings |
15 | 15 |
16 from comments.models import Comment | |
17 from forums.models import Post | |
18 | |
19 | |
16 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log') | 20 LOGFILE = os.path.join(settings.PROJECT_PATH, 'logs', 'ssl_images.log') |
17 logger = logging.getLogger(__name__) | 21 logger = logging.getLogger(__name__) |
18 | 22 |
19 | 23 |
20 def _setup_logging(): | 24 def _setup_logging(): |
24 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | 28 formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') |
25 handler.setFormatter(formatter) | 29 handler.setFormatter(formatter) |
26 logger.addHandler(handler) | 30 logger.addHandler(handler) |
27 | 31 |
28 | 32 |
33 class CommentFacade(object): | |
34 """Wrapper class to provide uniform access to Comments.""" | |
35 def __init__(self, comment): | |
36 self.comment = comment | |
37 | |
38 @property | |
39 def markdown(self): | |
40 return self.comment.comment | |
41 | |
42 @markdown.setter | |
43 def markdown(self, value): | |
44 self.comment.comment = value | |
45 | |
46 | |
47 class PostFacade(object): | |
48 """Wrapper class to provide uniform access to Forum posts.""" | |
49 def __init__(self, post): | |
50 self.post = post | |
51 | |
52 @property | |
53 def markdown(self): | |
54 return self.post.body | |
55 | |
56 @markdown.setter | |
57 def markdown(self, value): | |
58 self.post.body = value | |
59 | |
60 | |
29 class Command(NoArgsCommand): | 61 class Command(NoArgsCommand): |
30 help = "Rewrite forum posts and comments to not use http for images" | 62 help = "Rewrite forum posts and comments to not use http for images" |
31 option_list = NoArgsCommand.option_list + ( | 63 option_list = NoArgsCommand.option_list + ( |
32 make_option('--forums', | 64 make_option('--forums', |
33 action='store_true', | 65 action='store_true', |
34 default=False, | 66 default=False, |
35 help="process forum posts (default)"), | 67 help="process forum posts"), |
36 make_option('--comments', | 68 make_option('--comments', |
37 action='store_true', | 69 action='store_true', |
38 default=False, | 70 default=False, |
39 help="process comments"), | 71 help="process comments"), |
40 make_option('--start', | 72 make_option('-i', '--i', |
41 type='int', | 73 type='int', |
42 help="object to start at"), | 74 help="first slice index; the i in [i:j]"), |
43 make_option('--end', | 75 make_option('-j', '--j', |
44 type='int', | 76 type='int', |
45 help="one past the end object"), | 77 help="second slice index; the j in [i:j]"), |
46 ) | 78 ) |
47 | 79 |
48 def handle_noargs(self, **options): | 80 def handle_noargs(self, **options): |
49 _setup_logging() | 81 _setup_logging() |
82 logger.info("Starting; arguments received: %s", options) | |
50 | 83 |
51 do_comments = options['comments'] | 84 do_comments = options['comments'] |
52 do_forums = options['forums'] | 85 do_forums = options['forums'] |
53 if do_comments and do_forums: | 86 if do_comments and do_forums: |
54 raise CommandError("Please specify --forums or --comments, not both") | 87 raise CommandError("Please specify --forums or --comments, not both") |
55 elif not do_comments and not do_forums: | 88 elif not do_comments and not do_forums: |
56 raise CommandError("Please specify --forums or --comments") | 89 raise CommandError("Please specify --forums or --comments") |
57 | 90 |
58 logger.info("Arguments received: %s", options) | 91 if do_comments: |
92 qs = Comment.objects.all() | |
93 facade = CommentFacade | |
94 else: | |
95 qs = Post.objects.all() | |
96 facade = PostFacade | |
97 | |
98 i, j = options['i'], options['j'] | |
99 | |
100 if i is not None and i < 0: | |
101 raise CommandError("-i must be >= 0") | |
102 if j is not None and j < 0: | |
103 raise CommandError("-j must be >= 0") | |
104 if j is not None and i is not None and j <= i: | |
105 raise CommandError("-j must be > -i") | |
106 | |
107 if i is not None and j is not None: | |
108 qs = qs[i:j] | |
109 elif i is not None and j is None: | |
110 qs = qs[i:] | |
111 elif i is None and j is not None: | |
112 qs = qs[:j] | |
113 | |
114 s = [] | |
115 for model in qs.iterator(): | |
116 obj = facade(model) | |
117 s.append(obj.markdown) | |
118 | |
119 import pprint | |
120 pprint.pprint(s) |