changeset 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 2fae89f30b8d
files frontends/src/jp/cmd_blog.py frontends/src/jp/common.py
diffstat 2 files changed, 36 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/jp/cmd_blog.py	Tue Jun 27 16:23:28 2017 +0200
+++ b/frontends/src/jp/cmd_blog.py	Tue Jun 27 17:00:52 2017 +0200
@@ -301,6 +301,7 @@
         self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item"))
         self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item"))
         self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments"))
+        common.BaseEdit.add_parser_options(self)
 
     def buildMetadataFile(self, content_file_path, mb_data=None):
         """Build a metadata file using json
@@ -351,9 +352,10 @@
     def edit(self, content_file_path, content_file_obj,
              mb_data=None):
         """Edit the file contening the content using editor, and publish it"""
-        self.item_ori_mb_data = mb_data
+        if mb_data is not None:
+            self.pubsub_item = mb_data['id']
         # we first create metadata file
-        meta_ori, meta_file_path = self.buildMetadataFile(content_file_path, self.item_ori_mb_data)
+        meta_ori, meta_file_path = self.buildMetadataFile(content_file_path, mb_data)
 
         # do we need a preview ?
         if self.args.preview:
@@ -369,8 +371,8 @@
     def publish(self, content, mb_data):
         mb_data['content_rich'] =  content
 
-        if self.item_ori_mb_data is not None:
-            mb_data['id'] = self.item_ori_mb_data['id']
+        if self.pubsub_item is not None:
+            mb_data['id'] = self.pubsub_item
 
         self.host.bridge.mbSend(self.pubsub_service, self.pubsub_node, mb_data, self.profile)
         self.disp(u"Blog item published")
@@ -398,7 +400,7 @@
         SYNTAX_EXT.update(config.getConfig(self.sat_conf, 'jp', CONF_SYNTAX_EXT, {}))
         self.current_syntax = None
 
-        self.pubsub_service, self.pubsub_node, content_file_path, content_file_obj, mb_data = self.getItemPath(self.args.item)
+        self.pubsub_service, self.pubsub_node, self.pubsub_item, content_file_path, content_file_obj, mb_data = self.getItemPath(self.args.item)
 
         self.edit(content_file_path, content_file_obj, mb_data=mb_data)
 
--- a/frontends/src/jp/common.py	Tue Jun 27 16:23:28 2017 +0200
+++ b/frontends/src/jp/common.py	Tue Jun 27 17:00:52 2017 +0200
@@ -85,6 +85,9 @@
         self.cat_dir = cat_dir.encode('utf-8')
         self.use_metadata = use_metadata
 
+    def add_parser_options(self):
+        self.parser.add_argument("--force-item", action='store_true', help=_(u"don't use magic and take item argument as an actual item"))
+
     def secureUnlink(self, path):
         """Unlink given path after keeping it for a while
 
@@ -299,12 +302,15 @@
                 - file path
                 - item id
         """
+        force_item = self.args.force_item
+        if force_item and not item:
+            self.parser.error(_(u"an item id must be specified if you use --force-item"))
         command = item.lower()
         pubsub_service = self.args.service
         pubsub_node = self.args.node
         pubsub_item = None
 
-        if command not in ('new', 'last', 'current'):
+        if not force_item and command not in ('new', 'last', 'current'):
             # we have probably an URL, we try to parse it
             import urlparse
             url = self.args.item
@@ -351,7 +357,7 @@
                 else:
                     command = 'new'
 
-        if command in ('new', 'last', 'edit'):
+        if not force_item and command in ('new', 'last', 'edit'):
             # we need a temporary file
             tmp_suff = '.' + self.getTmpSuff()
             content_file_obj, content_file_path = self.getTmpFile(tmp_suff)
@@ -374,12 +380,12 @@
         else:
             if self.use_metadata:
                 metadata = None
-            if command == 'current':
+            if not force_item and command == 'current':
                 # user wants to continue current draft
                 content_file_path = self.getCurrentFile(self.profile)
                 self.disp(u'Continuing edition of current draft', 2)
                 content_file_obj = open(content_file_path, 'r+b')
-            elif os.path.isfile(self.args.item):
+            elif not force_item and os.path.isfile(self.args.item):
                 # there is an existing draft that we use
                 content_file_path = os.path.expanduser(self.args.item)
                 content_file_obj = open(content_file_path, 'r+b')
@@ -387,15 +393,27 @@
                 # last chance, it should be an item
                 tmp_suff = '.' + self.getTmpSuff()
                 content_file_obj, content_file_path = self.getTmpFile(tmp_suff)
+                pubsub_item = self.args.item
 
-                if self.use_metadata:
-                    content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                try:
+                    # we try to get existing item
+                    if self.use_metadata:
+                        content, metadata = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                    else:
+                        content = self.getItemData(pubsub_service, pubsub_node, self.args.item)
+                except Exception as e:
+                    # FIXME: ugly but we have not good may to check errors in bridge
+                    if u'item-not-found' in unicode(e):
+                        # item doesn't exist, we create a new one with requested id
+                        metadata = None
+                        self.disp(_(u'item "{item_id}" not found, we create a new item with this id').format(item_id=pubsub_item), 2)
                 else:
-                    content = self.getItemData(pubsub_service, pubsub_node, self.args.item)
-                content_file_obj.write(content.encode('utf-8'))
-                content_file_obj.seek(0)
+                    # item exists, we write content if content file
+                    content_file_obj.write(content.encode('utf-8'))
+                    content_file_obj.seek(0)
+                    self.disp(_(u'item "{item_id}" found, we edit it').format(item_id=pubsub_item), 2)
 
         if self.use_metadata:
-            return pubsub_service, pubsub_node, content_file_path, content_file_obj, metadata
+            return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj, metadata
         else:
-            return pubsub_service, pubsub_node, content_file_path, content_file_obj
+            return pubsub_service, pubsub_node, pubsub_item, content_file_path, content_file_obj