Эх сурвалжийг харах

HUE-5660 [home] Restore documents from Trash to /home

Right click on Trash documents will have the restore functionality to move them to /home
krish 8 жил өмнө
parent
commit
3eb397b8b8

+ 21 - 0
desktop/core/src/desktop/api2.py

@@ -348,6 +348,27 @@ def delete_document(request):
       'status': 0,
   })
 
+@api_error_handler
+@require_POST
+def restore_document(request):
+  """
+  Accepts a uuid
+
+  Restores the document to /home
+  """
+  uuids = json.loads(request.POST.get('uuids'))
+
+  if not uuids:
+    raise PopupException(_('restore_document requires comma separated uuids'))
+
+  for uuid in uuids.split(','):
+    document = Document2.objects.get_by_uuid(user=request.user, uuid=uuid, perm_type='write')
+    document.restore()
+
+  return JsonResponse({
+      'status': 0,
+  })
+
 
 @api_error_handler
 @require_POST

+ 4 - 1
desktop/core/src/desktop/models.py

@@ -1249,7 +1249,10 @@ class Document2(models.Model):
 
       self.move(parent_trash_dir, self.owner)
 
-  # TODO: restore
+  def restore(self):
+   # Currently restoring any doucment to /home
+   home_dir = Document2.objects.get_home_directory(self.owner)
+   self.move(home_dir, self.owner)
 
   def can_read(self, user):
     perm = self.get_permission('read')

+ 15 - 0
desktop/core/src/desktop/static/desktop/js/apiHelper.js

@@ -763,6 +763,21 @@ var ApiHelper = (function () {
     }, options);
   };
 
+  /**
+   * @param {Object} options
+   * @param {Function} options.successCallback
+   * @param {Function} [options.errorCallback]
+   * @param {boolean} [options.silenceErrors]
+   *
+   * @param {string} options.uuid
+   */
+  ApiHelper.prototype.restoreDocument = function (options) {
+    var self = this;
+    self.simplePost("/desktop/api2/doc/restore", {
+      uuids: ko.mapping.toJSON(options.uuids),
+    }, options);
+  };
+
   /**
    *
    * @param {Object} options

+ 34 - 0
desktop/core/src/desktop/static/desktop/js/document/hueFileEntry.js

@@ -513,6 +513,13 @@ var HueFileEntry = (function () {
     huePubSub.publish('open.link', $(event.target).attr('href'));
   };
 
+  HueFileEntry.prototype.showRestoreConfirmation = function () {
+    var self = this;
+    if (self.selectedEntries().length > 0 && (self.superuser || !self.sharedWithMeSelected())) {
+      $('#restoreFromTrashModal').modal('show');
+    }
+  };
+
   HueFileEntry.prototype.showDeleteConfirmation = function () {
     var self = this;
     if (self.selectedEntries().length > 0 && (self.superuser || !self.sharedWithMeSelected())) {
@@ -651,6 +658,33 @@ var HueFileEntry = (function () {
     };
   };
 
+  HueFileEntry.prototype.restoreFromTrash = function () {
+    var self = this;
+    if (self.app === 'documents') {
+      if (self.selectedEntries().indexOf(self) !== -1) {
+        self.activeEntry(self.parent);
+      }
+
+      if (self.selectedEntries().length > 0) {
+        var uuids = self.selectedEntries().map(function(entry) {
+          return entry.definition().uuid;
+        }).join(',');
+        self.apiHelper.restoreDocument({
+          uuids: uuids,
+          successCallback: function () {
+            self.activeEntry().load();
+          },
+          errorCallback: function () {
+            self.activeEntry().load();
+          }
+        });
+      } else {
+        self.activeEntry().load();
+      }
+      $('#restoreFromTrashModal').modal('hide');
+    }
+  };
+
   HueFileEntry.prototype.createDirectory = function (name) {
     var self = this;
     if (name && self.app === 'documents') {

+ 20 - 0
desktop/core/src/desktop/templates/document_browser.mako

@@ -537,6 +537,25 @@ from desktop.views import _ko
       <!-- /ko -->
     </div>
 
+    <div id="restoreFromTrashModal" data-keyboard="true" class="modal hide fade" tabindex="-1">
+      <!-- ko with: activeEntry -->
+      <div class="modal-header">
+        <!-- ko if: selectedEntries().length > 0 -->
+        <h3> ${ _('Restore these document(s) to Home directory?') } </h3>
+        <!-- /ko -->
+      </div>
+      <div class="modal-body">
+        <ul data-bind="foreach: selectedEntries()">
+          <li> <span data-bind="text: $data.definition().name"></span> </li>
+        </ul>
+      </div>
+      <div class="modal-footer">
+        <input type="button" class="btn" data-dismiss="modal" value="${ _('Cancel') }">
+        <input type="submit" data-bind="click: function() { restoreFromTrash() }" class="btn btn-danger" value="${_('Yes')}"/>
+      </div>
+      <!-- /ko -->
+    </div>
+
     <div id="deleteEntriesModal" data-keyboard="true" class="modal hide fade" tabindex="-1">
       <!-- ko with: activeEntry -->
       <div class="modal-header">
@@ -704,6 +723,7 @@ from desktop.views import _ko
               <ul class="hue-context-menu">
                 <!-- ko if: isTrashed -->
                 <li><a href="javascript:void(0);" data-bind="click: function() { $parent.getSelectedDocsWithDependents(); $parent.showDeleteConfirmation(); }"><i class="fa fa-fw fa-times"></i> ${ _('Delete') } <span data-bind="visible: $parent.selectedEntries().length > 1, text: '(' + $parent.selectedEntries().length + ')'"></span></a></li>
+                <li><a href="javascript:void(0);" data-bind="click: function() { $parent.showRestoreConfirmation(); }"><i class="fa fa-fw fa-undo"></i> ${ _('Restore to Home') } <span data-bind="visible: $parent.selectedEntries().length > 1, text: '(' + $parent.selectedEntries().length + ')'"></span></a></li>
                 <!-- /ko -->
                 <!-- ko ifnot: isTrashed -->
                 <!-- ko if: isDirectory -->

+ 1 - 0
desktop/core/src/desktop/urls.py

@@ -131,6 +131,7 @@ dynamic_patterns += patterns('desktop.api2',
   (r'^desktop/api2/doc/mkdir/?$', 'create_directory'),
   (r'^desktop/api2/doc/update/?$', 'update_document'),
   (r'^desktop/api2/doc/delete/?$', 'delete_document'),
+  (r'^desktop/api2/doc/restore/?$', 'restore_document'),
   (r'^desktop/api2/doc/share/?$', 'share_document'),
 
   (r'^desktop/api2/doc/export/?$', 'export_documents'),