allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hei...@apache.org
Subject [6/9] allura git commit: [#8051] ticket:895 Add opportunity to export attachments in ForgeDiscussion
Date Tue, 08 Mar 2016 21:35:15 GMT
[#8051] ticket:895 Add opportunity to export attachments in ForgeDiscussion


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/51096ed1
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/51096ed1
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/51096ed1

Branch: refs/heads/master
Commit: 51096ed157096f5cbf30fc8d05d8520fe516aad1
Parents: bccd8c5
Author: Denis Kotov <deniskkotov@gmail.com>
Authored: Thu Jan 28 23:45:34 2016 +0200
Committer: Heith Seewald <heiths@gmail.com>
Committed: Tue Mar 8 14:11:57 2016 -0500

----------------------------------------------------------------------
 Allura/allura/model/discuss.py                |  4 +--
 ForgeDiscussion/forgediscussion/forum_main.py | 32 ++++++++++++++++++++--
 ForgeWiki/forgewiki/model/wiki.py             |  2 +-
 ForgeWiki/forgewiki/wiki_main.py              |  2 +-
 4 files changed, 34 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/51096ed1/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index e13ba3d..921b059 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -63,13 +63,13 @@ class Discussion(Artifact, ActivityObject):
     threads = RelationProperty('Thread', via='discussion_id')
     posts = RelationProperty('Post', via='discussion_id')
 
-    def __json__(self, limit=None, posts_limit=None):
+    def __json__(self, limit=None, posts_limit=None, is_export=False):
         return dict(
             _id=str(self._id),
             shortname=self.shortname,
             name=self.name,
             description=self.description,
-            threads=[t.__json__(limit=posts_limit) for t
+            threads=[t.__json__(limit=posts_limit, is_export=is_export) for t
                      in self.thread_class().query.find(dict(discussion_id=self._id)).limit(limit
or 0)]
         )
 

http://git-wip-us.apache.org/repos/asf/allura/blob/51096ed1/ForgeDiscussion/forgediscussion/forum_main.py
----------------------------------------------------------------------
diff --git a/ForgeDiscussion/forgediscussion/forum_main.py b/ForgeDiscussion/forgediscussion/forum_main.py
index 276eb32..5d584e8 100644
--- a/ForgeDiscussion/forgediscussion/forum_main.py
+++ b/ForgeDiscussion/forgediscussion/forum_main.py
@@ -19,6 +19,7 @@
 import logging
 import urllib
 import json
+import os
 
 # Non-stdlib imports
 from pylons import tmpl_context as c, app_globals as g
@@ -34,6 +35,7 @@ from allura.app import Application, ConfigOption, SitemapEntry, DefaultAdminCont
 from allura.lib import helpers as h
 from allura.lib.decorators import require_post
 from allura.lib.security import require_access, has_access
+from allura.lib.utils import JSONForExport
 
 # Local imports
 from forgediscussion import model as DM
@@ -237,13 +239,39 @@ class ForgeDiscussionApp(Application):
 
     def bulk_export(self, f, export_path='', with_attachments=False):
         f.write('{"forums": [')
-        forums = DM.Forum.query.find(dict(app_config_id=self.config._id))
+        forums = list(DM.Forum.query.find(dict(app_config_id=self.config._id)))
+        if with_attachments:
+            GenericJSON = JSONForExport
+            for forum in forums:
+                self.export_attachments(forum.threads, export_path)
+        else:
+            GenericJSON = jsonify.GenericJSON
         for i, forum in enumerate(forums):
             if i > 0:
                 f.write(',')
-            json.dump(forum, f, cls=jsonify.GenericJSON, indent=2)
+            json.dump(forum, f, cls=GenericJSON, indent=2)
         f.write(']}')
 
+    def export_attachments(self, threads, export_path):
+        for thread in threads:
+            attachment_path = self.get_attachemnt_path(export_path, str(thread.artifact._id),
thread._id)
+            if not os.path.exists(attachment_path):
+                os.makedirs(attachment_path)
+            for post in thread.query_posts(status='ok'):
+                post_path = os.path.join(
+                    attachment_path,
+                    post.slug
+                )
+                if not os.path.exists(post_path):
+                    os.makedirs(post_path)
+                for attachment in post.attachments:
+                    path = os.path.join(
+                        post_path,
+                        attachment.filename
+                    )
+                    with open(path, 'w') as fl:
+                        fl.write(attachment.rfile().read())
+
 
 class ForumAdminController(DefaultAdminController):
 

http://git-wip-us.apache.org/repos/asf/allura/blob/51096ed1/ForgeWiki/forgewiki/model/wiki.py
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/model/wiki.py b/ForgeWiki/forgewiki/model/wiki.py
index 5d75136..5b14618 100644
--- a/ForgeWiki/forgewiki/model/wiki.py
+++ b/ForgeWiki/forgewiki/model/wiki.py
@@ -128,7 +128,7 @@ class Page(VersionedArtifact, ActivityObject):
         return [dict(bytes=attach.length,
                      path=os.path.join(
                          self.app_config.options.mount_point,
-                         self.title,
+                         str(self._id),
                          attach.filename)) for attach in self.attachments]
 
     def attachments_for_json(self):

http://git-wip-us.apache.org/repos/asf/allura/blob/51096ed1/ForgeWiki/forgewiki/wiki_main.py
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/wiki_main.py b/ForgeWiki/forgewiki/wiki_main.py
index 54ca706..2a24130 100644
--- a/ForgeWiki/forgewiki/wiki_main.py
+++ b/ForgeWiki/forgewiki/wiki_main.py
@@ -357,7 +357,7 @@ The wiki uses [Markdown](%s) syntax.
 
     def export_attachments(self, pages, export_path):
         for page in pages:
-            attachment_path = self.get_attachemnt_path(export_path, page.title)
+            attachment_path = self.get_attachemnt_path(export_path, str(page._id))
             if not os.path.exists(attachment_path):
                 os.makedirs(attachment_path)
             for attachment in page.attachments:


Mime
View raw message