浏览代码

[home2] Fix backend Document2 move

Seems like the UUID is unexpected so I switched to doc_id, also fetching the old_directory didn't work for some reason.
Johan Ahlen 9 年之前
父节点
当前提交
b4a07c0
共有 2 个文件被更改,包括 24 次插入10 次删除
  1. 8 5
      desktop/core/src/desktop/api2.py
  2. 16 5
      desktop/core/src/desktop/models.py

+ 8 - 5
desktop/core/src/desktop/api2.py

@@ -160,14 +160,17 @@ def _massage_permissions(document):
 @api_error_handler
 @require_POST
 def move_document(request):
-  source_id = request.POST.get('source_id', 'source_id')
-  destination_id = request.POST.get('destination_id', 'destination_id')
+  source_doc_id = json.loads(request.POST.get('source_doc_id'))
+  destination_doc_id = json.loads(request.POST.get('destination_doc_id'))
 
   # destination exists + is dir?
-  source = Document2.objects.document(request.user, uuid=source_id)
-  destination = Directory.objects.document(request.user, uuid=destination_id)
+  source = Document2.objects.document(request.user, doc_id=source_doc_id)
+  destination = Document2.objects.document(request.user, doc_id=destination_doc_id)
 
-  source.move(destination)
+  if destination.type != 'directory':
+    raise PopupException(_('Destination is not a directory'))
+
+  source.move(destination, request.user)
 
   return JsonResponse({'status': 0})
 

+ 16 - 5
desktop/core/src/desktop/models.py

@@ -864,6 +864,16 @@ class Document2(models.Model):
   def can_read_or_exception(self, user):
     self.doc.get().can_read_or_exception(user)
 
+  def can_write(self, user):
+    perm = self.list_permissions('write')
+    return user.is_superuser or self.owner == user or perm.groups.filter(id__in=user.groups.all()).exists() or user in perm.users.all()
+
+  def can_write_or_exception(self, user):
+    if self.can_write(user):
+      return True
+    else:
+      raise PopupException(_("Document does not exist or you don't have the permission to access it."))
+
   def get_history(self):
     return self.dependencies.filter(is_history=True).order_by('-last_modified')
 
@@ -899,14 +909,15 @@ class Document2(models.Model):
 
     super(Document2, self).save(*args, **kwargs)
 
-  def move(self, directory):
+  def move(self, directory, user):
     # get dir and remove
-    old_directory = self.documents(self.user).get(type='directory', uuid=self.uuid)
-    old_directory.dependencies.remove(self)
+    old_directory = Document2.objects.get(type='directory', dependencies=self.pk)
+    if old_directory.can_write_or_exception(user=user):
+      old_directory.dependencies.remove(self)
 
     # add to new dir
-    destination_directory = self.documents(self.user).get(type='directory', uuid=self.uuid)
-    destination_directory.dependencies.remove(self)
+    if directory.can_write_or_exception(user=user):
+      directory.dependencies.add(self)
 
   def share(self, user, name='read', users=None, groups=None):
     # TODO check in settings if user can sync, re-share, which perms...