incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [05/50] [abbrv] git commit: [#5891] ticket:327 removed add_attach from helpers, and put it to Post
Date Tue, 07 May 2013 22:27:43 GMT
[#5891]  ticket:327  removed add_attach from helpers, and put it to Post


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

Branch: refs/heads/tv/3854
Commit: 5c8db5459133394fbfe82828fcab82d42bd785ad
Parents: c40773b
Author: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Authored: Tue Apr 30 15:10:11 2013 +0400
Committer: Yuriy Arhipov <yuriyarhipovua@yandex.ru>
Committed: Tue Apr 30 15:10:11 2013 +0400

----------------------------------------------------------------------
 Allura/allura/controllers/discuss.py         |    8 ++++----
 Allura/allura/lib/helpers.py                 |   15 +--------------
 Allura/allura/model/discuss.py               |   12 ++++++++++++
 Allura/allura/tests/model/test_discussion.py |    7 +++++--
 4 files changed, 22 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5c8db545/Allura/allura/controllers/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index eb34981..218f9b4 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -211,7 +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)
-        h.attach_to_post(p, file_info)
+        p.add_attachment(file_info)
         if self.thread.artifact:
             self.thread.artifact.mod_date = datetime.utcnow()
         flash('Message posted')
@@ -294,7 +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)
-            h.attach_to_post(self.post, file_info)
+            self.post.add_attachment(file_info)
             for k,v in post_fields.iteritems():
                 try:
                     setattr(self.post, k, v)
@@ -339,7 +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)
-        h.attach_to_post(p, file_info)
+        p.add_attachment(file_info)
         redirect(request.referer)
 
     @h.vardec
@@ -373,7 +373,7 @@ class PostController(BaseController):
     @require_post()
     def attach(self, file_info=None):
         require_access(self.post, 'moderate')
-        h.attach_to_post(self.post, file_info)
+        self.post.add_attachment(file_info)
         redirect(request.referer)
 
     @expose()

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5c8db545/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index deed268..29cf44e 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -51,7 +51,6 @@ 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,16 +701,4 @@ def get_first(d, key):
     v = d.get(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)
+    return v
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5c8db545/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index d169e1b..4375077 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -31,6 +31,7 @@ from ming.utils import LazyProperty
 from allura.lib import helpers as h
 from allura.lib import security
 from allura.lib.security import require_access, has_access
+from allura.lib import utils
 from allura.model.notification import Notification, Mailbox
 from .artifact import Artifact, ArtifactReference, VersionedArtifact, Snapshot, Message,
Feed
 from .attachments import BaseAttachment
@@ -513,6 +514,17 @@ class Post(Message, VersionedArtifact, ActivityObject):
         return self.attachment_class().query.find(dict(
             post_id=self._id, type='attachment'))
 
+    def add_attachment(self, 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)
+            self.attach(
+                file_info.filename, file_info.file, content_type=mime_type,
+                post_id=self._id,
+                thread_id=self.thread_id,
+                discussion_id=self.discussion_id)
+
     def last_edit_by(self):
         return User.query.get(_id=self.last_edit_by_id) or User.anonymous()
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/5c8db545/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 2ab4b2a..35c5594 100644
--- a/Allura/allura/tests/model/test_discussion.py
+++ b/Allura/allura/tests/model/test_discussion.py
@@ -206,7 +206,7 @@ def test_attachment_methods():
     assert '\nAttachment: fake.txt (37 Bytes; text/plain)' in n.text
 
 @with_setup(setUp, tearDown)
-def test_attach_to_post():
+def test_add_attachment():
     test_file = FieldStorage()
     test_file.name = 'file_info'
     test_file.filename = 'test.txt'
@@ -215,9 +215,12 @@ def test_attach_to_post():
     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)
+    test_post.add_attachment(test_file)
     ThreadLocalORMSession.flush_all()
     assert test_post.attachments.count() == 1, test_post.attachments.count()
+    attach = test_post.attachments.first()
+    assert attach.filename == 'test.txt', attach.filename
+    assert attach.content_type == 'text/plain', attach.content_type
 
 @with_setup(setUp, tearDown)
 def test_discussion_delete():


Mime
View raw message