Mercurial > libervia-backend
comparison frontends/src/jp/common.py @ 2270:07caa12be945
jp (blog, common): added --force-item option to avoid magic and use argument as actual item id:
- creation of new item with item id has been fixed
- --force-item allow to use url as item id, or keyword
- pubsub_item can is now returned by getItemPath. If not None, the id is known and must be used
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 27 Jun 2017 17:00:52 +0200 |
parents | 606ff34d30f2 |
children | 5f0dbf42aa9c |
comparison
equal
deleted
inserted
replaced
2269:606ff34d30f2 | 2270:07caa12be945 |
---|---|
82 """ | 82 """ |
83 self.host = host | 83 self.host = host |
84 self.sat_conf = config.parseMainConf() | 84 self.sat_conf = config.parseMainConf() |
85 self.cat_dir = cat_dir.encode('utf-8') | 85 self.cat_dir = cat_dir.encode('utf-8') |
86 self.use_metadata = use_metadata | 86 self.use_metadata = use_metadata |
87 | |
88 def add_parser_options(self): | |
89 self.parser.add_argument("--force-item", action='store_true', help=_(u"don't use magic and take item argument as an actual item")) | |
87 | 90 |
88 def secureUnlink(self, path): | 91 def secureUnlink(self, path): |
89 """Unlink given path after keeping it for a while | 92 """Unlink given path after keeping it for a while |
90 | 93 |
91 This method is used to prevent accidental deletion of a draft | 94 This method is used to prevent accidental deletion of a draft |
297 - last: retrieve last published item | 300 - last: retrieve last published item |
298 - current: continue current local draft | 301 - current: continue current local draft |
299 - file path | 302 - file path |
300 - item id | 303 - item id |
301 """ | 304 """ |
305 force_item = self.args.force_item | |
306 if force_item and not item: | |
307 self.parser.error(_(u"an item id must be specified if you use --force-item")) | |
302 command = item.lower() | 308 command = item.lower() |
303 pubsub_service = self.args.service | 309 pubsub_service = self.args.service |
304 pubsub_node = self.args.node | 310 pubsub_node = self.args.node |
305 pubsub_item = None | 311 pubsub_item = None |
306 | 312 |
307 if command not in ('new', 'last', 'current'): | 313 if not force_item and command not in ('new', 'last', 'current'): |
308 # we have probably an URL, we try to parse it | 314 # we have probably an URL, we try to parse it |
309 import urlparse | 315 import urlparse |
310 url = self.args.item | 316 url = self.args.item |
311 parsed_url = urlparse.urlsplit(url) | 317 parsed_url = urlparse.urlsplit(url) |
312 if parsed_url.scheme.startswith('http'): | 318 if parsed_url.scheme.startswith('http'): |
349 if pubsub_item is not None: | 355 if pubsub_item is not None: |
350 command = 'edit' # XXX: edit command is only used internaly, it similar to last, but with the item given in the URL | 356 command = 'edit' # XXX: edit command is only used internaly, it similar to last, but with the item given in the URL |
351 else: | 357 else: |
352 command = 'new' | 358 command = 'new' |
353 | 359 |
354 if command in ('new', 'last', 'edit'): | 360 if not force_item and command in ('new', 'last', 'edit'): |
355 # we need a temporary file | 361 # we need a temporary file |
356 tmp_suff = '.' + self.getTmpSuff() | 362 tmp_suff = '.' + self.getTmpSuff() |
357 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) | 363 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) |
358 if command == 'new': | 364 if command == 'new': |
359 self.disp(u'Editing a new item', 2) | 365 self.disp(u'Editing a new item', 2) |
372 content_file_obj.write(content.encode('utf-8')) | 378 content_file_obj.write(content.encode('utf-8')) |
373 content_file_obj.seek(0) | 379 content_file_obj.seek(0) |
374 else: | 380 else: |
375 if self.use_metadata: | 381 if self.use_metadata: |
376 metadata = None | 382 metadata = None |
377 if command == 'current': | 383 if not force_item and command == 'current': |
378 # user wants to continue current draft | 384 # user wants to continue current draft |
379 content_file_path = self.getCurrentFile(self.profile) | 385 content_file_path = self.getCurrentFile(self.profile) |
380 self.disp(u'Continuing edition of current draft', 2) | 386 self.disp(u'Continuing edition of current draft', 2) |
381 content_file_obj = open(content_file_path, 'r+b') | 387 content_file_obj = open(content_file_path, 'r+b') |
382 elif os.path.isfile(self.args.item): | 388 elif not force_item and os.path.isfile(self.args.item): |
383 # there is an existing draft that we use | 389 # there is an existing draft that we use |
384 content_file_path = os.path.expanduser(self.args.item) | 390 content_file_path = os.path.expanduser(self.args.item) |
385 content_file_obj = open(content_file_path, 'r+b') | 391 content_file_obj = open(content_file_path, 'r+b') |
386 else: | 392 else: |
387 # last chance, it should be an item | 393 # last chance, it should be an item |
388 tmp_suff = '.' + self.getTmpSuff() | 394 tmp_suff = '.' + self.getTmpSuff() |
389 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) | 395 content_file_obj, content_file_path = self.getTmpFile(tmp_suff) |
390 | 396 pubsub_item = self.args.item |
391 if self.use_metadata: | 397 |
392 content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item) | 398 try: |
399 # we try to get existing item | |
400 if self.use_metadata: | |
401 content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item) | |
402 else: | |
403 content = self.getItemData(pubsub_service, pubsub_node, self.args.item) | |
404 except Exception as e: | |
405 # FIXME: ugly but we have not good may to check errors in bridge | |
406 if u'item-not-found' in unicode(e): | |
407 # item doesn't exist, we create a new one with requested id | |
408 metadata = None | |
409 self.disp(_(u'item "{item_id}" not found, we create a new item with this id').format(item_id=pubsub_item), 2) | |
393 else: | 410 else: |
394 content = self.getItemData(pubsub_service, pubsub_node, self.args.item) | 411 # item exists, we write content if content file |
395 content_file_obj.write(content.encode('utf-8')) | 412 content_file_obj.write(content.encode('utf-8')) |
396 content_file_obj.seek(0) | 413 content_file_obj.seek(0) |
414 self.disp(_(u'item "{item_id}" found, we edit it').format(item_id=pubsub_item), 2) | |
397 | 415 |
398 if self.use_metadata: | 416 if self.use_metadata: |
399 return pubsub_service, pubsub_node, content_file_path, content_file_obj, metadata | 417 return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj, metadata |
400 else: | 418 else: |
401 return pubsub_service, pubsub_node, content_file_path, content_file_obj | 419 return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj |