allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [1/4] allura git commit: [#8003] ticket:877 Show/serve only most recent attachment if file name matches
Date Tue, 05 Jan 2016 23:14:45 GMT
Repository: allura
Updated Branches:
  refs/heads/master a784bde6d -> 996d82b12


[#8003] ticket:877 Show/serve only most recent attachment if file name matches


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

Branch: refs/heads/master
Commit: 9e952a61c6e60bd3ee793b00347f9fa020617432
Parents: a784bde
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Thu Dec 24 16:30:20 2015 +0200
Committer: Igor Bondarenko <jetmind2@gmail.com>
Committed: Thu Dec 24 16:30:20 2015 +0200

----------------------------------------------------------------------
 Allura/allura/controllers/attachments.py | 10 ++++++----
 Allura/allura/lib/utils.py               | 16 +++++++++++++++-
 Allura/allura/model/artifact.py          |  3 ++-
 Allura/allura/model/discuss.py           |  3 ++-
 Allura/allura/tests/test_utils.py        | 22 ++++++++++++++++++++++
 5 files changed, 47 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/9e952a61/Allura/allura/controllers/attachments.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/attachments.py b/Allura/allura/controllers/attachments.py
index 4260988..a175bf9 100644
--- a/Allura/allura/controllers/attachments.py
+++ b/Allura/allura/controllers/attachments.py
@@ -71,8 +71,9 @@ class AttachmentController(BaseController):
     def attachment(self):
         metadata = self.AttachmentClass.metadata_for(self.artifact)
         metadata['type'] = 'attachment'
-        attachment = self.AttachmentClass.query.get(
-            filename=self.filename, **metadata)
+        attachment = self.AttachmentClass.query.find(
+            dict(filename=self.filename, **metadata)
+        ).sort('_id', -1).limit(1).first()
         if attachment is None:
             raise exc.HTTPNotFound
         return attachment
@@ -81,8 +82,9 @@ class AttachmentController(BaseController):
     def thumbnail(self):
         metadata = self.AttachmentClass.metadata_for(self.artifact)
         metadata['type'] = 'thumbnail'
-        attachment = self.AttachmentClass.query.get(
-            filename=self.filename, **metadata)
+        attachment = self.AttachmentClass.query.find(
+            dict(filename=self.filename, **metadata)
+        ).sort('_id', -1).limit(1).first()
         if attachment is None:
             raise exc.HTTPNotFound
         return attachment

http://git-wip-us.apache.org/repos/asf/allura/blob/9e952a61/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index eb273d2..d01e115 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -30,6 +30,7 @@ import mimetypes
 import re
 import magic
 from itertools import groupby
+import operator as op
 import collections
 
 import tg
@@ -638,4 +639,17 @@ def skip_mod_date(model_cls):
     try:
         yield
     finally:
-        session(model_cls)._get().skip_mod_date = skip_mod_date
\ No newline at end of file
+        session(model_cls)._get().skip_mod_date = skip_mod_date
+
+
+def unique_attachments(attachments):
+    """Given a list of :class:`allura.model.attachments.BaseAttachment` return
+    a list where each filename present only once. If original list contains
+    multiple attachmnets with the same filename the most recent one (i.e. with
+    max :class:`bson.ObjectId`) will make it to the resulting list."""
+    if not attachments:
+        return []
+    result = []
+    for _, atts in groupby(attachments, op.attrgetter('filename')):
+        result.append(max(atts, key=op.attrgetter('_id')))
+    return result

http://git-wip-us.apache.org/repos/asf/allura/blob/9e952a61/Allura/allura/model/artifact.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/artifact.py b/Allura/allura/model/artifact.py
index 4792f44..09a3a38 100644
--- a/Allura/allura/model/artifact.py
+++ b/Allura/allura/model/artifact.py
@@ -411,8 +411,9 @@ class Artifact(MappedClass, SearchIndexable):
 
     @LazyProperty
     def attachments(self):
-        return self.attachment_class().query.find(dict(
+        atts = self.attachment_class().query.find(dict(
             app_config_id=self.app_config_id, artifact_id=self._id, type='attachment')).all()
+        return utils.unique_attachments(atts)
 
     def delete(self):
         """Delete this Artifact.

http://git-wip-us.apache.org/repos/asf/allura/blob/9e952a61/Allura/allura/model/discuss.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/discuss.py b/Allura/allura/model/discuss.py
index a7642a7..69bf556 100644
--- a/Allura/allura/model/discuss.py
+++ b/Allura/allura/model/discuss.py
@@ -594,8 +594,9 @@ class Post(Message, VersionedArtifact, ActivityObject):
 
     @LazyProperty
     def attachments(self):
-        return self.attachment_class().query.find(dict(
+        atts = self.attachment_class().query.find(dict(
             post_id=self._id, type='attachment')).all()
+        return utils.unique_attachments(atts)
 
     def add_multiple_attachments(self, file_info):
         if isinstance(file_info, list):

http://git-wip-us.apache.org/repos/asf/allura/blob/9e952a61/Allura/allura/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_utils.py b/Allura/allura/tests/test_utils.py
index 413165a..afbd2ec 100644
--- a/Allura/allura/tests/test_utils.py
+++ b/Allura/allura/tests/test_utils.py
@@ -24,6 +24,7 @@ import datetime as dt
 from ming.odm import session
 from os import path
 
+from bson import ObjectId
 from webob import Request
 from mock import Mock, patch
 from nose.tools import (
@@ -333,3 +334,24 @@ def test_skip_mod_date():
     with utils.skip_mod_date(M.Artifact):
         assert getattr(session(M.Artifact)._get(), 'skip_mod_date', None) is True
     assert getattr(session(M.Artifact)._get(), 'skip_mod_date', None) is False
+
+
+class FakeAttachment(object):
+    def __init__(self, filename):
+        self._id = ObjectId()
+        self.filename = filename
+
+
+def unique_attachments():
+    ua = utils.unique_attachments
+    assert_equal([], ua(None))
+    assert_equal([], ua([]))
+
+    pic1 = FakeAttachment('pic.png')
+    pic2 = FakeAttachment('pic.png')
+    file1 = FakeAttachment('file.txt')
+    file2 = FakeAttachment('file.txt')
+    other = FakeAttachment('other')
+    attachments = [pic1, pic2, file1, file2, other]
+    expected = [pic2, file2, other]
+    assert_equal(expected, ua(attachments))


Mime
View raw message