Ver código fonte

[doc2] Support creating a directory on the new home page

Romain Rigaux 10 anos atrás
pai
commit
7120cfc

+ 9 - 3
desktop/core/src/desktop/api2.py

@@ -33,6 +33,7 @@ from desktop.lib.export_csvxls import make_response
 from desktop.lib.i18n import smart_str, force_unicode
 from desktop.models import Document2, Document, Directory
 from desktop.lib.exceptions_renderable import PopupException
+from hadoop.fs.hadoopfs import Hdfs
 
 
 LOG = logging.getLogger(__name__)
@@ -72,8 +73,8 @@ def get_documents2(request):
   path = request.GET.get('path', '/') # Expects path to be a Directory for now
 
   try:
-    file_doc = Document2.objects.directory(user=request.user, name=path)
-  except Document2.DoesNotExist, e:
+    file_doc = Directory.objects.get(owner=request.user, name=path) # TODO perms
+  except Directory.DoesNotExist, e:
     if path == '/':
       file_doc = Directory.objects.create(name='/', type='directory', owner=request.user)
       file_doc.dependencies.add(*Document2.objects.filter(owner=request.user).exclude(id=file_doc.id))
@@ -105,9 +106,14 @@ def move_document(request):
 @api_error_handler
 @require_POST
 def create_directory(request):
-  path = request.POST.get('path')
+  parent_path = json.loads(request.POST.get('parent_path'))
+  name = json.loads(request.POST.get('name'))
 
+  parent_dir = Directory.objects.get(owner=request.user, name=parent_path)
+  
+  path = Hdfs.normpath(parent_path + '/' + name)
   file_doc = Directory.objects.create(name=path, type='directory', owner=request.user)
+  parent_dir.dependencies.add(file_doc)
 
   return JsonResponse({
       'status': 0,

+ 8 - 6
desktop/core/src/desktop/models.py

@@ -742,7 +742,7 @@ class Document2Manager(models.Manager):
 
   # TODO permissions
   def documents(self, user):
-    return Document2.objects.filter(owner=user)
+    return Document2.objects.filter(owner=user).order_by('-last_modified')
 
   def directory(self, user, path):
     return self.documents(user).get(type='directory', name=path)
@@ -751,7 +751,7 @@ class Document2Manager(models.Manager):
     return self.get(uuid=uuid, version=version, is_history=is_history)
 
   def get_history(self, user, doc_type):
-    return self.filter(owner=user, type=doc_type, is_history=True).order_by('-last_modified') # To do perm sharing
+    return self.filter(owner=user, type=doc_type, is_history=True).order_by('-last_modified')
 
 
 def uuid_default():
@@ -896,20 +896,22 @@ class Document2(models.Model):
     destination_directory = self.documents(self.user).get(type='directory', uuid=self.uuid)
     destination_directory.dependencies.remove(self)
 
-    # ok return true
-
 
 class Directory(Document2):
   # e.g. name = '/' or '/dir1/dir2/f3'
 
   class Meta:
     proxy = True
+    
+  def save(self, *args, **kwargs):
+    super(Directory, self).save(*args, **kwargs)
+    # TODO unique_together = ('owner', 'name')
 
   def parent(self):
     return Document2.objects.get(type='directory', dependencies=[self.pk])
 
-  def documents(self):
-    return self.dependencies.all()
+  def documents(self): 
+    return self.dependencies.all() # TODO perms
 
 
 def get_data_link(meta):

+ 28 - 12
desktop/core/src/desktop/static/desktop/js/home2.vm.js

@@ -18,9 +18,9 @@
 function HomeViewModel(data) {
   var self = this;
 
-  var ALL_DOCUMENTS = data.documents;
-  self.documents = ko.mapping.fromJS(ALL_DOCUMENTS);
-  self.path = ko.mapping.fromJS(data.path);
+  self.documents = ko.observableArray();
+  self.path = ko.mapping.fromJS('/');
+  self.mkdirFormPath = ko.observable('');
 
   self.page = ko.observable(1);
   self.documentsPerPage = ko.observable(50);
@@ -69,13 +69,29 @@ function HomeViewModel(data) {
     self.page(1);
   });
 
-  self.getDocById = function (docId) {
-    var _doc = null;
-    $.each(ALL_DOCUMENTS, function (id, doc) {
-      if (doc.id == docId) {
-        _doc = doc;
-      }
-    });
-    return _doc;
-  }
+
+  self.loadDocuments = function(path) {
+	$.get("/desktop/api2/docs2/", {
+	   path: path
+	}, function(data) {
+	  self.documents(data.documents);    	
+	});
+  };
+
+  self.mkdir = function() {
+    $.post("/desktop/api2/doc/mkdir", {
+	    parent_path: ko.mapping.toJSON(self.path),
+	    name: ko.mapping.toJSON(self.mkdirFormPath)
+      }, function (data) {
+        if (data.status == 0) {
+          self.loadDocuments(self.path()); // TODO proper refresh
+          self.mkdirFormPath('');
+        }
+        else {
+          fail(data.message);
+        }
+     }).fail(function (xhr) {
+        fail(xhr.responseText);
+     });
+  };
 }

+ 8 - 7
desktop/core/src/desktop/templates/home2.mako

@@ -137,9 +137,12 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
 
     <div class="span10">
       <div class="card card-home" style="margin-top: 0">
-        <input id="searchInput" type="text" placeholder="Search for name, description, etc..." class="input-xlarge search-query" style="margin-left: 20px;margin-top: 5px">
+        <input id="searchInput" type="text" placeholder="${ _('Search for name, description, etc...') }" class="input-xlarge search-query" style="margin-left: 20px;margin-top: 5px">
         <h2 class="card-heading simple">${_('My Documents')}</h2>
         <span data-bind="text: path"></span>
+        <br/>
+        <input data-bind="value: mkdirFormPath"></input>
+        <a href="javascript(0);" class="btn" data-bind="click: mkdir"><i class="fa fa-plus-circle"></i> ${ _('Create Directory') }</a>
 
         <div class="card-body">
           <p>
@@ -208,12 +211,10 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
   var viewModel, shareViewModel, JSON_USERS_GROUPS;
 
   $(document).ready(function() {
-    $.get("/desktop/api2/docs2/", {
-      'path': location.getParameter('path') ? location.getParameter('path') : '/'
-      }, function(data) {
-        viewModel = new HomeViewModel(data);
-        ko.applyBindings(viewModel, $('#documentList')[0]);
-    });
+    viewModel = new HomeViewModel();
+    ko.applyBindings(viewModel, $('#documentList')[0]);
+
+    viewModel.loadDocuments(location.getParameter('path') ? location.getParameter('path') : '/');
   });
 </script>
 

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

@@ -102,6 +102,11 @@ dynamic_patterns += patterns('desktop.api2',
   (r'^desktop/api2/docs/?$', 'get_documents'),
   (r'^desktop/api2/docs2/?$', 'get_documents2'),
   (r'^desktop/api2/doc/get$', 'get_document'),
+
+  (r'^desktop/api2/doc/move$', 'move_document'),
+  (r'^desktop/api2/doc/mkdir$', 'create_directory'),
+  (r'^desktop/api2/doc/delete$', 'delete_document'),
+
   (r'^desktop/api2/doc/export$', 'export_documents'),
   (r'^desktop/api2/doc/import$', 'import_documents'),
 )

+ 1 - 1
desktop/libs/notebook/src/notebook/templates/notebooks.mako

@@ -263,4 +263,4 @@ ${ commonshare() | n,unicode }
 </script>
 
 
-${commonfooter(messages) | n,unicode}
+${ commonfooter(request, messages) | n,unicode }