comparison frontends/src/jp/cmd_blog.py @ 1834:6209de5e3e25

jp (blog): import now show progression + value to add to sat.conf for URLs redirection
author Goffi <goffi@goffi.org>
date Sun, 24 Jan 2016 18:24:01 +0100
parents 74014a9cc203
children cdecf553e051
comparison
equal deleted inserted replaced
1833:a123e881f9e5 1834:6209de5e3e25
19 19
20 20
21 import base 21 import base
22 from sat.core.i18n import _ 22 from sat.core.i18n import _
23 from sat.core.constants import Const as C 23 from sat.core.constants import Const as C
24 import json
25 URL_REDIRECT_PREFIX = 'url_redirect_'
24 26
25 __commands__ = ["Blog"] 27 __commands__ = ["Blog"]
26 28
27 29
28 class Import(base.CommandBase): 30 class Import(base.CommandBase):
29 def __init__(self, host): 31 def __init__(self, host):
30 super(Import, self).__init__(host, 'import', help=_(u'import an external blog')) 32 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog'))
31 33
32 def add_parser_options(self): 34 def add_parser_options(self):
33 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list")) 35 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
34 self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host")) 36 self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host"))
35 self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)")) 37 self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)"))
40 self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE', 42 self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE',
41 help=_(u"PubSub service where the items must be uploaded (default: server)")) 43 help=_(u"PubSub service where the items must be uploaded (default: server)"))
42 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?', 44 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?',
43 help=_(u"importer data location (see importer description), nothing to show importer description")) 45 help=_(u"importer data location (see importer description), nothing to show importer description"))
44 46
47 def onProgressStarted(self, metadata):
48 self.disp(_(u'Blog upload started'),2)
49
50 def onProgressFinished(self, metadata):
51 self.disp(_(u'Blog uploaded successfully'),2)
52 redirections = {k[len(URL_REDIRECT_PREFIX):]:v for k,v in metadata.iteritems()
53 if k.startswith(URL_REDIRECT_PREFIX)}
54 if redirections:
55 conf = u'\n'.join([
56 u'url_redirections_profile = {}'.format(self.profile),
57 u"url_redirections_dict = {}".format(
58 # we need to add ' ' before each new line and to double each '%' for ConfigParser
59 u'\n '.join(json.dumps(redirections, indent=1, separators=(',',': ')).replace(u'%', u'%%').split(u'\n'))),
60 ])
61 self.disp(_(u'\nTo redirect old URLs to new ones, put the following lines in your sat.conf file, in [libervia] section:\n\n{conf}'.format(conf=conf)))
62
63 def onProgressError(self, error_msg):
64 self.disp(_(u'Error while uploading blog: {}').format(error_msg),error=True)
65
66 def error(self, failure):
67 self.disp(_("Error while trying to upload a blog: {reason}").format(reason=failure), error=True)
68 self.host.quit(1)
69
45 def connected(self): 70 def connected(self):
46 """Send files to jabber contact""" 71 """Send files to jabber contact"""
72 self.need_loop=True
47 super(Import, self).connected() 73 super(Import, self).connected()
74
48 if self.args.location is None: 75 if self.args.location is None:
49 for name in ('option', 'service', 'no_images_upload'): 76 for name in ('option', 'service', 'no_images_upload'):
50 if getattr(self.args, name): 77 if getattr(self.args, name):
51 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name)) 78 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
52 if self.args.importer is None: 79 if self.args.importer is None:
72 options['upload_images'] = C.BOOL_FALSE 99 options['upload_images'] = C.BOOL_FALSE
73 if self.args.upload_ignore_host: 100 if self.args.upload_ignore_host:
74 self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set") 101 self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set")
75 elif self.args.upload_ignore_host: 102 elif self.args.upload_ignore_host:
76 options['upload_ignore_host'] = self.args.upload_ignore_host 103 options['upload_ignore_host'] = self.args.upload_ignore_host
77 try: 104 def gotId(id_):
78 self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile) 105 self.progress_id = id_
79 except Exception as e: 106 self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile,
80 print e 107 callback=gotId, errback=self.error)
81 self.host.quit(1)
82 108
83 109
84 class Blog(base.CommandBase): 110 class Blog(base.CommandBase):
85 subcommands = (Import,) 111 subcommands = (Import,)
86 112