incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject git commit: [#4887] Screenshot sorting
Date Thu, 18 Jul 2013 20:50:30 GMT
Updated Branches:
  refs/heads/tv/4887 [created] f51188ae5


[#4887] Screenshot sorting

Signed-off-by: Tim Van Steenburgh <tvansteenburgh@gmail.com>


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

Branch: refs/heads/tv/4887
Commit: f51188ae522202d7a3d9cd38e904719fc91fffea
Parents: 5167a9d
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Thu Jul 18 20:50:09 2013 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Thu Jul 18 20:50:09 2013 +0000

----------------------------------------------------------------------
 Allura/allura/ext/admin/admin_main.py           | 26 +++++++++-
 Allura/allura/lib/widgets/project_list.py       |  4 ++
 .../lib/widgets/resources/css/screenshots.css   | 21 ++++++++
 .../lib/widgets/resources/js/screenshots.js     | 10 ++++
 Allura/allura/model/project.py                  |  3 +-
 .../templates/widgets/project_screenshots.html  | 50 ++++++++++++--------
 Allura/allura/tests/functional/test_admin.py    | 22 +++++++++
 7 files changed, 112 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/ext/admin/admin_main.py
----------------------------------------------------------------------
diff --git a/Allura/allura/ext/admin/admin_main.py b/Allura/allura/ext/admin/admin_main.py
index 1083d86..f8c8983 100644
--- a/Allura/allura/ext/admin/admin_main.py
+++ b/Allura/allura/ext/admin/admin_main.py
@@ -449,14 +449,20 @@ class ProjectAdminController(BaseController):
     @validate(W.screenshot_admin)
     def add_screenshot(self, screenshot=None, caption=None, **kw):
         require_access(c.project, 'update')
-        if len(c.project.get_screenshots()) >= 6:
+        screenshots = c.project.get_screenshots()
+        if len(screenshots) >= 6:
             flash('You may not have more than 6 screenshots per project.','error')
         elif screenshot is not None and screenshot != '':
             M.AuditLog.log('add screenshot')
+            sort = 1 + max([ss.sort or 0 for ss in screenshots] or [0])
             M.ProjectFile.save_image(
                 screenshot.filename, screenshot.file, content_type=screenshot.type,
                 save_original=True,
-                original_meta=dict(project_id=c.project._id,category='screenshot',caption=caption),
+                original_meta=dict(
+                    project_id=c.project._id,
+                    category='screenshot',
+                    caption=caption,
+                    sort=sort),
                 square=True, thumbnail_size=(150,150),
                 thumbnail_meta=dict(project_id=c.project._id,category='screenshot_thumb'))
             g.post_event('project_updated')
@@ -464,6 +470,22 @@ class ProjectAdminController(BaseController):
 
     @expose()
     @require_post()
+    def sort_screenshots(self, **kw):
+        """Sort project screenshots.
+
+        Called via ajax when screenshots are reordered via drag/drop on
+        the Screenshots admin page.
+
+        ``kw`` is a mapping of (screenshot._id, sort_order) pairs.
+
+        """
+        for s in c.project.get_screenshots():
+            if str(s._id) in kw:
+                s.sort = int(kw[str(s._id)])
+        g.post_event('project_updated')
+
+    @expose()
+    @require_post()
     def delete_screenshot(self, id=None, **kw):
         require_access(c.project, 'update')
         if id is not None and id != '':

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/lib/widgets/project_list.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/project_list.py b/Allura/allura/lib/widgets/project_list.py
index 6d6649d..a5a327f 100644
--- a/Allura/allura/lib/widgets/project_list.py
+++ b/Allura/allura/lib/widgets/project_list.py
@@ -163,3 +163,7 @@ class ProjectScreenshots(ew_core.Widget):
         ew_core.Widget.defaults,
         project=None,
         edit=False)
+
+    def resources(self):
+        yield ew.JSLink('js/screenshots.js')
+        yield ew.CSSLink('css/screenshots.css')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/lib/widgets/resources/css/screenshots.css
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/css/screenshots.css b/Allura/allura/lib/widgets/resources/css/screenshots.css
new file mode 100644
index 0000000..08be2aa
--- /dev/null
+++ b/Allura/allura/lib/widgets/resources/css/screenshots.css
@@ -0,0 +1,21 @@
+.sortable {
+    float: left;
+}
+.sortable .screenshot {
+    margin: 0 0 10px 10px;
+    border: 1px solid #eee;
+    background: #fff;
+    float: left;
+}
+.sortable .screenshot .image {
+    margin-left: 0;
+    float: left;
+}
+.sortable .screenshot .image img {
+    display: block;
+}
+.sortable .screenshot .controls {
+    margin: 50px 15px 0 20px;
+    width: 190px;
+    float: left;
+}

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/lib/widgets/resources/js/screenshots.js
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/widgets/resources/js/screenshots.js b/Allura/allura/lib/widgets/resources/js/screenshots.js
new file mode 100644
index 0000000..032a17c
--- /dev/null
+++ b/Allura/allura/lib/widgets/resources/js/screenshots.js
@@ -0,0 +1,10 @@
+$(function() {
+  $('.sortable').sortable({cursor: 'move'}).bind('sortupdate', function(e) {
+    var params = {'_session_id': $.cookie('_session_id')};
+    $(this).find('.screenshot').each(function(i) {
+      params[$(this).data('ss-id')] = i;
+    });
+    $.post('sort_screenshots', params);
+  });
+});
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/model/project.py
----------------------------------------------------------------------
diff --git a/Allura/allura/model/project.py b/Allura/allura/model/project.py
index b889515..435cbbb 100644
--- a/Allura/allura/model/project.py
+++ b/Allura/allura/model/project.py
@@ -57,6 +57,7 @@ class ProjectFile(File):
     project_id=FieldProperty(S.ObjectId)
     category=FieldProperty(str)
     caption=FieldProperty(str)
+    sort=FieldProperty(int)
 
 class ProjectCategory(MappedClass):
     class __mongometa__:
@@ -287,7 +288,7 @@ class Project(MappedClass, ActivityNode, ActivityObject):
     def get_screenshots(self):
         return ProjectFile.query.find(dict(
                 project_id=self._id,
-                category='screenshot')).all()
+                category='screenshot')).sort('sort').all()
 
     @LazyProperty
     def icon(self):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/templates/widgets/project_screenshots.html
----------------------------------------------------------------------
diff --git a/Allura/allura/templates/widgets/project_screenshots.html b/Allura/allura/templates/widgets/project_screenshots.html
index 14c2d6f..01d9849 100644
--- a/Allura/allura/templates/widgets/project_screenshots.html
+++ b/Allura/allura/templates/widgets/project_screenshots.html
@@ -16,26 +16,34 @@
        specific language governing permissions and limitations
        under the License.
 -#}
-{% for ss in project.get_screenshots() %}
-  <div class="grid-6" style="text-align: center">
-    <a href="{{project.url()}}screenshot/{{ss.filename}}"><img src="{{project.url()}}screenshot/{{ss.filename}}/thumb"
alt="Screenshot thumbnail"/></a>
-    {% if not edit %}
-    <br>
-    {{ss.caption}}
+{% set screenshots = project.get_screenshots() %}
+{% if screenshots.__len__() > 1 %}
+<p>Drag screenshots to sort.</p>
+{% endif %}
+<div class="sortable">
+  {% for ss in screenshots %}
+  <div data-ss-id="{{ ss._id }}" class="screenshot">
+    <div class="image">
+      <a href="{{project.url()}}screenshot/{{ss.filename}}"><img src="{{project.url()}}screenshot/{{ss.filename}}/thumb"
alt="Screenshot thumbnail"/></a>
+      {% if not edit %}
+      <br>
+      {{ss.caption}}
+      {% endif %}
+    </div>
+    {% if edit %}
+    <div class="controls">
+      <form action="edit_screenshot" method="post">
+        <input type="hidden" value="{{ss._id}}" name="id">
+        <input type="text" value="{{ss.caption}}" name="caption"><br>
+        <input type="submit" value="Save Changes">
+      </form>
+      <form action="delete_screenshot" method="post">
+        <input type="hidden" value="{{ss._id}}" name="id">
+        <input type="submit" value="Delete">
+      </form>
+    </div>
     {% endif %}
   </div>
-  {% if edit %}
-  <div class="grid-12" style="margin-top:50px; min-height: 110px">
-    <form action="edit_screenshot" method="post">
-      <input type="hidden" value="{{ss._id}}" name="id">
-      <input type="text" value="{{ss.caption}}" name="caption"><br>
-      <input type="submit" value="Save Changes">
-    </form>
-    <form action="delete_screenshot" method="post">
-      <input type="hidden" value="{{ss._id}}" name="id">
-      <input type="submit" value="Delete">
-    </form>
-  </div>
-  {% endif %}
-{% endfor %}
-<div style="clear:both">&nbsp;</div>
\ No newline at end of file
+  {% endfor %}
+</div>
+<div style="clear:both">&nbsp;</div>

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/f51188ae/Allura/allura/tests/functional/test_admin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/functional/test_admin.py b/Allura/allura/tests/functional/test_admin.py
index 8b1d703..7d630c9 100644
--- a/Allura/allura/tests/functional/test_admin.py
+++ b/Allura/allura/tests/functional/test_admin.py
@@ -263,6 +263,28 @@ class TestProjectAdmin(TestController):
         #r = self.app.get('/p/test/home/')
         #assert 'aaa' not in r
 
+    def test_sort_screenshots(self):
+        for file_name in ('admin_24.png', 'admin_32.png'):
+            file_path = os.path.join(allura.__path__[0], 'nf', 'allura',
+                    'images', file_name)
+            file_data = file(file_path).read()
+            upload = ('screenshot', file_name, file_data)
+            self.app.post('/admin/add_screenshot', params=dict(
+                    caption=file_name),
+                    upload_files=[upload])
+
+        p_nbhd = M.Neighborhood.query.get(name='Projects')
+        project = M.Project.query.get(shortname='test',
+                neighborhood_id=p_nbhd._id)
+        # first uploaded is first by default
+        screenshots = project.get_screenshots()
+        assert_equals(screenshots[0].filename, 'admin_24.png')
+        # reverse order
+        params = dict((str(ss._id), len(screenshots) - 1 - i)
+                for i, ss in enumerate(screenshots))
+        self.app.post('/admin/sort_screenshots', params)
+        assert_equals(project.get_screenshots()[0].filename, 'admin_32.png')
+
     def test_project_delete_undelete(self):
         # create a subproject
         with audits('create subproject sub-del-undel'):


Mime
View raw message