From bdbbd449c470975257cd9e5cdbeb13c869c4eb8f Mon Sep 17 00:00:00 2001 From: Seth Ladygo Date: Mon, 24 Feb 2020 16:56:49 -0800 Subject: [PATCH] markup webdav: move code, clean up --- markup/urls.py | 2 + procat2/resource.py => markup/webdav.py | 79 ++++++++++++------------- procat2/urls.py | 3 - 3 files changed, 41 insertions(+), 43 deletions(-) rename procat2/resource.py => markup/webdav.py (75%) diff --git a/markup/urls.py b/markup/urls.py index b4dd0c3..9b722e8 100644 --- a/markup/urls.py +++ b/markup/urls.py @@ -1,8 +1,10 @@ from django.urls import path from . import views +from .webdav import MarkupDavView urlpatterns = [ path('submit', views.submit, name='markup_submit'), #path('fail', views.fail, name='markup_fail'), + path('dav', MarkupDavView.as_view()), ] diff --git a/procat2/resource.py b/markup/webdav.py similarity index 75% rename from procat2/resource.py rename to markup/webdav.py index 61be048..72b0843 100644 --- a/procat2/resource.py +++ b/markup/webdav.py @@ -1,21 +1,22 @@ import datetime import os import shutil -from sys import getfilesystemencoding import logging +from sys import getfilesystemencoding from django.utils.decorators import method_decorator +from django_http_auth.decorators import http_basic_auth + from djangodav.acls import FullAcl from djangodav.base.resources import BaseDavResource, MetaEtagMixIn from djangodav.locks import DummyLock from djangodav.utils import url_join from djangodav.views import DavView -from django.utils.decorators import method_decorator -from django_http_auth.decorators import http_basic_auth +from procat2.models import Catalog -from .models import Catalog +# from procat2.settings import ASSET_DIR log = logging.getLogger(__name__) @@ -23,34 +24,37 @@ fs_encoding = getfilesystemencoding() @method_decorator(http_basic_auth, name='dispatch') -class AuthDavView(DavView): +class MarkupDavView(DavView): def __init__(self): - log.info(f"AuthDavView init") super(DavView, self).__init__(resource_class=MarkupDavResource, lock_class=DummyLock, acl_class=FullAcl) class MarkupDavResource(MetaEtagMixIn, BaseDavResource): - root = '/opt/imagebank/mkbeta/webdav' # TODO replace with settings var + ROOT_FOLDER = '' + MY_CATS_FOLDER = 'My Catalogs' + MARKUP_SUBMIT_FOLDER = 'For Markup' + #root = '/opt/imagebank/mkbeta/webdav' # TODO replace with settings var + + def __init__(self, path=None, user=None): + super().__init__(path) + self.user = user def __str__(self): - return f"" + return f"" def get_children(self): """Return an iterator of all direct children of this resource.""" # make sure the current object is a directory path = self.get_path() - log.info(f"get_children of {path}") + log.info(f"get_children of '{path}'") children = [] if path == '/': - #children = ['My Catalogs', 'For Markup'] - children = ['MyCatalogs'] - elif path == '/MyCatalogs': - # TODO return user's catalogs - # get User - children = None - elif path == '/For Markup': + children = [self.MY_CATS_FOLDER, self.MARKUP_SUBMIT_FOLDER] + elif path == f'/{self.MY_CATS_FOLDER}/': + children = self.user_catalogs() + elif path == f'/{self.MARKUP_SUBMIT_FOLDER}/': children = None # else: # path = self.get_abs_path() @@ -58,15 +62,16 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource): # children = os.listdir(path) for child in children: - try: - is_unicode = isinstance(child, str) - except NameError: # Python 3 fix - is_unicode = isinstance(child, str) - if not is_unicode: - child = child.decode(fs_encoding) - log.info(f'returning kid {self.clone(url_join(*(self.path + [child])))}') - yield self.clone(url_join(*(self.path + [child]))) + is_unicode = isinstance(child, str) + if not is_unicode: + child = child.decode(fs_encoding) + child_resource = self.clone(path=url_join(*(self.path + [child])), user=self.user) + log.info(f'returning kid {child_resource}') + yield child_resource + def user_catalogs(self): + cats = Catalog.objects.filter(owner=self.user).order_by('-updated') + return [c.name for c in cats] def get_abs_path(self): """Return the absolute path of the resource. Used internally to interface with @@ -100,32 +105,24 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource): def is_collection(self): """Return True if this resource is a directory (collection in WebDAV parlance).""" path = '/'.join(self.path) + # log.info(f"is_collection {self.path}") - if path == '': - return True - elif path == 'MyCatalogs': - return True - elif path == 'For Markup': + if path in [self.ROOT_FOLDER, self.MY_CATS_FOLDER, self.MARKUP_SUBMIT_FOLDER]: return True - log.info(f"is_collection {self.path} FALLTHROUGH") return False # return os.path.isdir(self.get_abs_path()) @property def is_object(self): """Return True if this resource is a file (resource in WebDAV parlance).""" - #path = self.get_path() path = '/'.join(self.path) - # log.info(f"is_object {path}") + log.info(f"is_object {path}") - if path == '': - return False - elif path == 'MyCatalogs': - return False - elif path == 'For Markup': + if self.is_collection: return False + # TODO test catalog pdf return True #return os.path.isfile(self.get_abs_path()) @@ -134,10 +131,12 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource): """Return True if this resource exists.""" path = '/'.join(self.path) log.info(f"exists {path}") - if path in ['', 'MyCatalogs', 'For Markup']: + + if self.is_collection: return True - else: - return os.path.exists(self.get_abs_path()) + + # TODO test catalog pdf + return os.path.exists(self.get_abs_path()) # def write(self, content, temp_file=None, range_start=None): # raise NotImplementedError diff --git a/procat2/urls.py b/procat2/urls.py index 4945479..d4539d4 100644 --- a/procat2/urls.py +++ b/procat2/urls.py @@ -26,7 +26,6 @@ from products.views import search_products, all_models from .forms import UserCreationForm from .views import login_guest, lazy_convert_done -from .resource import AuthDavView urlpatterns = [ @@ -54,8 +53,6 @@ urlpatterns = [ path('quickinfo/', include('quickinfo.urls')), path('markup/', include('markup.urls')), - - path('dav', AuthDavView.as_view()), ] if settings.DJDT: