markup webdav: move code, clean up

This commit is contained in:
2020-02-24 16:56:49 -08:00
parent c66827452d
commit bdbbd449c4
3 changed files with 41 additions and 43 deletions

View File

@ -1,8 +1,10 @@
from django.urls import path from django.urls import path
from . import views from . import views
from .webdav import MarkupDavView
urlpatterns = [ urlpatterns = [
path('submit', views.submit, name='markup_submit'), path('submit', views.submit, name='markup_submit'),
#path('fail', views.fail, name='markup_fail'), #path('fail', views.fail, name='markup_fail'),
path('dav<path:path>', MarkupDavView.as_view()),
] ]

View File

@ -1,21 +1,22 @@
import datetime import datetime
import os import os
import shutil import shutil
from sys import getfilesystemencoding
import logging import logging
from sys import getfilesystemencoding
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django_http_auth.decorators import http_basic_auth
from djangodav.acls import FullAcl from djangodav.acls import FullAcl
from djangodav.base.resources import BaseDavResource, MetaEtagMixIn from djangodav.base.resources import BaseDavResource, MetaEtagMixIn
from djangodav.locks import DummyLock from djangodav.locks import DummyLock
from djangodav.utils import url_join from djangodav.utils import url_join
from djangodav.views import DavView from djangodav.views import DavView
from django.utils.decorators import method_decorator from procat2.models import Catalog
from django_http_auth.decorators import http_basic_auth
from .models import Catalog # from procat2.settings import ASSET_DIR
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -23,34 +24,37 @@ fs_encoding = getfilesystemencoding()
@method_decorator(http_basic_auth, name='dispatch') @method_decorator(http_basic_auth, name='dispatch')
class AuthDavView(DavView): class MarkupDavView(DavView):
def __init__(self): def __init__(self):
log.info(f"AuthDavView init")
super(DavView, self).__init__(resource_class=MarkupDavResource, lock_class=DummyLock, acl_class=FullAcl) super(DavView, self).__init__(resource_class=MarkupDavResource, lock_class=DummyLock, acl_class=FullAcl)
class MarkupDavResource(MetaEtagMixIn, BaseDavResource): 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): def __str__(self):
return f"<MarkupDavResource {self.path}>" return f"<MarkupDavResource '{self.user}' '{self.path}'>"
def get_children(self): def get_children(self):
"""Return an iterator of all direct children of this resource.""" """Return an iterator of all direct children of this resource."""
# make sure the current object is a directory # make sure the current object is a directory
path = self.get_path() path = self.get_path()
log.info(f"get_children of {path}") log.info(f"get_children of '{path}'")
children = [] children = []
if path == '/': if path == '/':
#children = ['My Catalogs', 'For Markup'] children = [self.MY_CATS_FOLDER, self.MARKUP_SUBMIT_FOLDER]
children = ['MyCatalogs'] elif path == f'/{self.MY_CATS_FOLDER}/':
elif path == '/MyCatalogs': children = self.user_catalogs()
# TODO return user's catalogs elif path == f'/{self.MARKUP_SUBMIT_FOLDER}/':
# get User
children = None
elif path == '/For Markup':
children = None children = None
# else: # else:
# path = self.get_abs_path() # path = self.get_abs_path()
@ -58,15 +62,16 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource):
# children = os.listdir(path) # children = os.listdir(path)
for child in children: for child in children:
try: is_unicode = isinstance(child, str)
is_unicode = isinstance(child, str) if not is_unicode:
except NameError: # Python 3 fix child = child.decode(fs_encoding)
is_unicode = isinstance(child, str) child_resource = self.clone(path=url_join(*(self.path + [child])), user=self.user)
if not is_unicode: log.info(f'returning kid {child_resource}')
child = child.decode(fs_encoding) yield child_resource
log.info(f'returning kid {self.clone(url_join(*(self.path + [child])))}')
yield self.clone(url_join(*(self.path + [child])))
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): def get_abs_path(self):
"""Return the absolute path of the resource. Used internally to interface with """Return the absolute path of the resource. Used internally to interface with
@ -100,32 +105,24 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource):
def is_collection(self): def is_collection(self):
"""Return True if this resource is a directory (collection in WebDAV parlance).""" """Return True if this resource is a directory (collection in WebDAV parlance)."""
path = '/'.join(self.path) path = '/'.join(self.path)
# log.info(f"is_collection {self.path}")
if path == '': if path in [self.ROOT_FOLDER, self.MY_CATS_FOLDER, self.MARKUP_SUBMIT_FOLDER]:
return True
elif path == 'MyCatalogs':
return True
elif path == 'For Markup':
return True return True
log.info(f"is_collection {self.path} FALLTHROUGH")
return False return False
# return os.path.isdir(self.get_abs_path()) # return os.path.isdir(self.get_abs_path())
@property @property
def is_object(self): def is_object(self):
"""Return True if this resource is a file (resource in WebDAV parlance).""" """Return True if this resource is a file (resource in WebDAV parlance)."""
#path = self.get_path()
path = '/'.join(self.path) path = '/'.join(self.path)
# log.info(f"is_object {path}") log.info(f"is_object {path}")
if path == '': if self.is_collection:
return False
elif path == 'MyCatalogs':
return False
elif path == 'For Markup':
return False return False
# TODO test catalog pdf
return True return True
#return os.path.isfile(self.get_abs_path()) #return os.path.isfile(self.get_abs_path())
@ -134,10 +131,12 @@ class MarkupDavResource(MetaEtagMixIn, BaseDavResource):
"""Return True if this resource exists.""" """Return True if this resource exists."""
path = '/'.join(self.path) path = '/'.join(self.path)
log.info(f"exists {path}") log.info(f"exists {path}")
if path in ['', 'MyCatalogs', 'For Markup']:
if self.is_collection:
return True 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): # def write(self, content, temp_file=None, range_start=None):
# raise NotImplementedError # raise NotImplementedError

View File

@ -26,7 +26,6 @@ from products.views import search_products, all_models
from .forms import UserCreationForm from .forms import UserCreationForm
from .views import login_guest, lazy_convert_done from .views import login_guest, lazy_convert_done
from .resource import AuthDavView
urlpatterns = [ urlpatterns = [
@ -54,8 +53,6 @@ urlpatterns = [
path('quickinfo/', include('quickinfo.urls')), path('quickinfo/', include('quickinfo.urls')),
path('markup/', include('markup.urls')), path('markup/', include('markup.urls')),
path('dav<path:path>', AuthDavView.as_view()),
] ]
if settings.DJDT: if settings.DJDT: