You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2013/05/03 23:57:47 UTC

[17/50] git commit: [#5891] ticket:327 refactoring for comments attachments

[#5891]  ticket:327  refactoring for comments attachments


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

Branch: refs/heads/db/6007
Commit: c40773b77f71335adfe7a300955e5fc99d0266c1
Parents: 57d4e6f
Author: Yuriy Arhipov <yu...@yandex.ru>
Authored: Tue Apr 30 11:15:35 2013 +0400
Committer: Yuriy Arhipov <yu...@yandex.ru>
Committed: Tue Apr 30 11:16:44 2013 +0400

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py         |   35 ++------------------
 Allura/allura/lib/helpers.py                 |   13 ++++++++
 Allura/allura/tests/model/test_discussion.py |   14 ++++++++
 3 files changed, 31 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c40773b7/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index b0bb426..eb34981 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -211,12 +211,7 @@ class ThreadController(BaseController):
         file_info = kw.get('file_info', None)
         p = self.thread.add_post(**kw)
         is_spam = g.spam_checker.check(kw['text'], artifact=p, user=c.user)
-        if hasattr(file_info, 'file'):
-            p.attach(
-                file_info.filename, file_info.file, content_type=file_info.type,
-                post_id=p._id,
-                thread_id=p.thread_id,
-                discussion_id=p.discussion_id)
+        h.attach_to_post(p, file_info)
         if self.thread.artifact:
             self.thread.artifact.mod_date = datetime.utcnow()
         flash('Message posted')
@@ -299,12 +294,7 @@ class PostController(BaseController):
             require_access(self.post, 'moderate')
             post_fields = self.W.edit_post.to_python(kw, None)
             file_info = post_fields.pop('file_info', None)
-            if hasattr(file_info, 'file'):
-                self.post.attach(
-                    file_info.filename, file_info.file, content_type=file_info.type,
-                    post_id=self.post._id,
-                    thread_id=self.post.thread_id,
-                    discussion_id=self.post.discussion_id)
+            h.attach_to_post(self.post, file_info)
             for k,v in post_fields.iteritems():
                 try:
                     setattr(self.post, k, v)
@@ -349,15 +339,7 @@ class PostController(BaseController):
         kw = self.W.edit_post.to_python(kw, None)
         p = self.thread.add_post(parent_id=self.post._id, **kw)
         is_spam = g.spam_checker.check(kw['text'], artifact=p, user=c.user)
-        if hasattr(file_info, 'file'):
-            mime_type = file_info.type
-            if not mime_type or '/' not in mime_type:
-                mime_type = utils.guess_mime_type(file_info.filename)
-            p.attach(
-                file_info.filename, file_info.file, content_type=mime_type,
-                post_id=p._id,
-                thread_id=p.thread_id,
-                discussion_id=p.discussion_id)
+        h.attach_to_post(p, file_info)
         redirect(request.referer)
 
     @h.vardec
@@ -391,16 +373,7 @@ class PostController(BaseController):
     @require_post()
     def attach(self, file_info=None):
         require_access(self.post, 'moderate')
-        if hasattr(file_info, 'file'):
-            mime_type = file_info.type
-            # If mime type was not passed or bogus, guess it
-            if not mime_type or '/' not in mime_type:
-                mime_type = utils.guess_mime_type(file_info.filename)
-            self.post.attach(
-                file_info.filename, file_info.file, content_type=mime_type,
-                post_id=self.post._id,
-                thread_id=self.post.thread_id,
-                discussion_id=self.post.discussion_id)
+        h.attach_to_post(self.post, file_info)
         redirect(request.referer)
 
     @expose()

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c40773b7/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 9ddad18..deed268 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -51,6 +51,7 @@ from allura.lib import exceptions as exc
 from allura.lib.decorators import exceptionless
 from allura.lib import AsciiDammit
 from .security import has_access
+from allura.lib import utils
 
 
 # validates project, subproject, and user names
@@ -702,3 +703,15 @@ def get_first(d, key):
     if isinstance(v, list):
         return v[0] if len(v) > 0 else None
     return v
+
+
+def attach_to_post(post, file_info):
+    if hasattr(file_info, 'file'):
+        mime_type = file_info.type
+        if not mime_type or '/' not in mime_type:
+            mime_type = utils.guess_mime_type(file_info.filename)
+        post.attach(
+            file_info.filename, file_info.file, content_type=mime_type,
+            post_id=post._id,
+            thread_id=post.thread_id,
+            discussion_id=post.discussion_id)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/c40773b7/Allura/allura/tests/model/test_discussion.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/model/test_discussion.py b/Allura/allura/tests/model/test_discussion.py
index 7e9df32..2ab4b2a 100644
--- a/Allura/allura/tests/model/test_discussion.py
+++ b/Allura/allura/tests/model/test_discussion.py
@@ -206,6 +206,20 @@ def test_attachment_methods():
     assert '\nAttachment: fake.txt (37 Bytes; text/plain)' in n.text
 
 @with_setup(setUp, tearDown)
+def test_attach_to_post():
+    test_file = FieldStorage()
+    test_file.name = 'file_info'
+    test_file.filename = 'test.txt'
+    test_file.type = 'text/plain'
+    test_file.file=StringIO('test file\n')
+    d = M.Discussion(shortname='test', name='test')
+    t = M.Thread.new(discussion_id=d._id, subject='Test Thread')
+    test_post = t.post('test post')
+    h.attach_to_post(test_post, test_file)
+    ThreadLocalORMSession.flush_all()
+    assert test_post.attachments.count() == 1, test_post.attachments.count()
+
+@with_setup(setUp, tearDown)
 def test_discussion_delete():
     d = M.Discussion(shortname='test', name='test')
     t = M.Thread.new(discussion_id=d._id, subject='Test Thread')