浏览代码

[home2] Concept of directories

Romain Rigaux 10 年之前
父节点
当前提交
78d1b24bac

+ 26 - 1
desktop/core/src/desktop/api2.py

@@ -29,7 +29,7 @@ from django.utils import html
 from desktop.lib.django_util import JsonResponse
 from desktop.lib.export_csvxls import make_response
 from desktop.lib.i18n import smart_str
-from desktop.models import Document2, Document
+from desktop.models import Document2, Document, Directory
 from django.http import HttpResponse
 
 
@@ -47,6 +47,31 @@ def get_documents(request):
   return JsonResponse({'documents': [doc.to_dict() for doc in Document2.objects.filter(**filters)]})
 
 
+def get_documents2(request):
+  path = request.GET.get('path', '/') # Expects path to be a Directory for now
+
+  filters = {
+      'owner': request.user,
+      'name': path,
+      'type': 'directory'
+  }
+
+  try:
+    file_doc = Directory.objects.get(**filters)
+  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))
+    else:
+      raise e
+
+  return JsonResponse({
+      'file': file_doc.to_dict(),
+      'documents': [doc.to_dict() for doc in file_doc.documents()],
+      'path': path
+  })
+
+
 def get_document(request):
   if request.GET.get('id'):
     doc = Document2.objects.get(id=request.GET['id'])

+ 15 - 0
desktop/core/src/desktop/models.py

@@ -813,6 +813,8 @@ class Document2(models.Model):
       return reverse('oozie:edit_bundle') + '?bundle=' + str(self.id)
     elif self.type.startswith('query'):
       return reverse('notebook:editor') + '?editor=' + str(self.id)
+    elif self.type == 'directory':
+      return '/home2' + '?path=' + self.name
     elif self.type == 'notebook':
       return reverse('notebook:notebook') + '?notebook=' + str(self.id)
     elif self.type == 'search-dashboard':
@@ -874,6 +876,19 @@ class Document2(models.Model):
     super(Document2, self).save(*args, **kwargs)
 
 
+class Directory(Document2):
+  # e.g. name = '/' or '/dir1/dir2/f3'
+
+  class Meta:
+    proxy = True
+
+  def parent(self):
+    return Document2.objects.get(type='directory', dependencies=[self.pk])
+
+  def documents(self):
+    return self.dependencies.all()
+
+
 def get_data_link(meta):
   link = None
 

+ 4 - 2
desktop/core/src/desktop/static/desktop/js/home2.vm.js

@@ -15,11 +15,13 @@
 // limitations under the License.
 
 
-function HomeViewModel(json_docs) {
+function HomeViewModel(data) {
   var self = this;
 
-  var ALL_DOCUMENTS = json_docs;
+  var ALL_DOCUMENTS = data.documents;
   self.documents = ko.mapping.fromJS(ALL_DOCUMENTS);
+  self.path = ko.mapping.fromJS(data.path);
+
   self.page = ko.observable(1);
   self.documentsPerPage = ko.observable(50);
 

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

@@ -139,6 +139,7 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
       <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">
         <h2 class="card-heading simple">${_('My Documents')}</h2>
+        <span data-bind="text: path"></span>
 
         <div class="card-body">
           <p>
@@ -206,10 +207,12 @@ ${ commonheader(_('Welcome Home'), "home", user) | n,unicode }
 <script type="text/javascript" charset="utf-8">
   var viewModel, shareViewModel, JSON_USERS_GROUPS;
 
-  $(document).ready(function () {
-    $.get("/desktop/api2/docs/", function(data) {
-      viewModel = new HomeViewModel(data.documents);
-      ko.applyBindings(viewModel, $('#documentList')[0]);
+  $(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]);
     });
   });
 </script>

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

@@ -18,7 +18,6 @@
 from __future__ import absolute_import
 
 import logging
-import os
 import re
 
 # FIXME: This could be replaced with hooking into the `AppConfig.ready()`
@@ -39,7 +38,6 @@ from django.conf.urls.static import static
 from django.contrib import admin
 
 from desktop import appmanager
-from desktop import metrics
 from desktop.conf import METRICS
 
 # Django expects handler404 and handler500 to be defined.
@@ -102,6 +100,7 @@ dynamic_patterns += patterns('desktop.api',
 
 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/export$', 'export_documents'),
   (r'^desktop/api2/doc/import$', 'import_documents'),