allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [6/7] allura git commit: [#7998] ticket:883 change way to calling attach functions, add is_ajax func
Date Fri, 08 Jan 2016 16:36:13 GMT
[#7998] ticket:883 change way to calling attach functions, add is_ajax func


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

Branch: refs/heads/master
Commit: 7b8316447a6d5bbf5610333588c7c9a1f2fb3b30
Parents: a2d5424
Author: Denis Kotov <deniskkotov@gmail.com>
Authored: Tue Jan 5 19:57:40 2016 +0200
Committer: Denis Kotov <deniskkotov@gmail.com>
Committed: Thu Jan 7 21:33:35 2016 +0200

----------------------------------------------------------------------
 Allura/allura/controllers/attachments.py        |  3 ++
 Allura/allura/lib/utils.py                      |  7 +++++
 .../forgewiki/templates/wiki/page_edit.html     | 30 +++++++++++---------
 ForgeWiki/forgewiki/wiki_main.py                |  6 ++--
 4 files changed, 31 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/7b831644/Allura/allura/controllers/attachments.py
----------------------------------------------------------------------
diff --git a/Allura/allura/controllers/attachments.py b/Allura/allura/controllers/attachments.py
index 6766d02..61c0bf2 100644
--- a/Allura/allura/controllers/attachments.py
+++ b/Allura/allura/controllers/attachments.py
@@ -22,6 +22,7 @@ from tg import expose, request, redirect
 from ming.utils import LazyProperty
 
 from allura.lib.security import require_access
+from allura.lib.utils import is_ajax
 from .base import BaseController
 
 
@@ -110,6 +111,8 @@ class AttachmentController(BaseController):
     def index(self, delete=False, **kw):
         if request.method == 'POST':
             self.handle_post(delete, **kw)
+            if is_ajax(request):
+                return
             redirect(request.referer)
         if self.artifact.deleted:
             raise exc.HTTPNotFound

http://git-wip-us.apache.org/repos/asf/allura/blob/7b831644/Allura/allura/lib/utils.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 17721b0..90ad4cc 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -655,3 +655,10 @@ def unique_attachments(attachments):
     for _, atts in groupby(attachments, op.attrgetter('filename')):
         result.append(max(atts, key=op.attrgetter('_id')))
     return result
+
+
+
+def is_ajax(request):
+    if request.headers.get('X-Requested-With', None) == 'XMLHttpRequest':
+        return True
+    return False

http://git-wip-us.apache.org/repos/asf/allura/blob/7b831644/ForgeWiki/forgewiki/templates/wiki/page_edit.html
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/templates/wiki/page_edit.html b/ForgeWiki/forgewiki/templates/wiki/page_edit.html
index be2caae..b587075 100644
--- a/ForgeWiki/forgewiki/templates/wiki/page_edit.html
+++ b/ForgeWiki/forgewiki/templates/wiki/page_edit.html
@@ -74,9 +74,9 @@
     {{lib.csrf_token()}}
 </form>
 <div class="grid-19">
-  {{c.attachment_list.display(onsubmit='del_attach(this); return false;', attachments=page.attachments,
edit_mode=page_exists and h.has_access(page, 'edit')())}}
+  {{c.attachment_list.display(attachments=page.attachments, edit_mode=page_exists and h.has_access(page,
'edit')())}}
   {% if page_exists and h.has_access(page, 'edit')() %}
-    {{c.attachment_add.display(onsubmit='send_attach(); return false;', name='file_info')}}
+    {{c.attachment_add.display(name='file_info')}}
   {% endif %}
 </div>
 {{c.confirmation.display(content='')}}
@@ -115,14 +115,14 @@
                      '<a href="' + attachment_url + '">' +
                      '<img src="' + attachment_url + '/thumb" alt="Thumbnail"/>' +
                      '</a><br/>' + filename +
-                     '<form method="post" action="' + attachment_url + '" onsubmit="del_attach(this);
return false;">' +
+                     '<form method="post" action="' + attachment_url + '>' +
                        '<input type="hidden" name="delete" value="True"/>' +
                        '<input type="submit" value="Delete File"/>' +
                      '</form>' +
                    '</div>';
       $(new_attach).prependTo($('.attachment_images'));
     } else {
-      var new_attach ='<div><form method="post" action="' + attachment_url + '"
onsubmit="del_attach(this); return false;">' +
+      var new_attach ='<div><form method="post" action="' + attachment_url + '">'
+
                         '<a href="' + attachment_url + '">' + filename + '</a>
(' + filesize + ' bytes)' +
                         '<input type="hidden" name="delete" value="True"/>' +
                         '<span>' +
@@ -132,7 +132,9 @@
       $(new_attach).prependTo($('.attachment_files'));
     };
   };
-  function send_attach() {
+
+  $('#attachment_form').on('submit', function(event) {
+    event.preventDefault();
     var page_url = '{{page.url()}}';
     $.ajax({
       type: "POST",
@@ -140,7 +142,7 @@
       data: new FormData($('#attachment_form')[0]),
       processData: false,
       contentType: false,
-      success: function(data) {
+      success: function() {
         var i;
         for (i = 0; i < $("#file_info")[0].files.length; i++){
           var attachment_name = $("#file_info")[0].files[i].name;
@@ -149,19 +151,21 @@
         }
       }
     });
-  };
+  });
 
-  function del_attach(elem) {
-    var attr_url = $(elem).attr('action');
+  $('.attachments').on('submit', 'form', function(event) {
+    event.preventDefault();
+    elem = $(this);
+    var attr_url = elem.attr('action');
     $.ajax({
       type: "POST",
       url: attr_url,
-      data: $(elem).serialize() +"&_session_id=" + $.cookie('_session_id'),
-      success:  function() {
-        $(elem).parent().remove();
+      data: elem.serialize() +"&_session_id=" + $.cookie('_session_id'),
+      success:  function() {;
+        elem.parent().remove();
       }
     });
-  };
+  });
   {% endif %}
 </script>
 {% endblock %}

http://git-wip-us.apache.org/repos/asf/allura/blob/7b831644/ForgeWiki/forgewiki/wiki_main.py
----------------------------------------------------------------------
diff --git a/ForgeWiki/forgewiki/wiki_main.py b/ForgeWiki/forgewiki/wiki_main.py
index 31de5b8..560af82 100644
--- a/ForgeWiki/forgewiki/wiki_main.py
+++ b/ForgeWiki/forgewiki/wiki_main.py
@@ -37,6 +37,7 @@ from allura.app import Application, SitemapEntry, DefaultAdminController,
Config
 from allura.lib.search import search_app
 from allura.lib.decorators import require_post
 from allura.lib.security import require_access, has_access
+from allura.lib.utils import is_ajax
 from allura.lib import exceptions as forge_exc
 from allura.controllers import AppDiscussionController, BaseController, AppDiscussionRestController
 from allura.controllers import DispatchIndex
@@ -754,8 +755,9 @@ class PageController(BaseController, FeedController):
             raise exc.HTTPNotFound
         require_access(self.page, 'edit')
         self.page.add_multiple_attachments(file_info)
-        if request.headers.get('X-Requested-With', None) != 'XMLHttpRequest':
-            redirect(request.referer)
+        if is_ajax(request):
+            return
+        redirect(request.referer)
 
     @expose('json:')
     @require_post()


Mime
View raw message