markup webdav: move code, clean up
This commit is contained in:
@ -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<path:path>', MarkupDavView.as_view()),
|
||||
]
|
||||
|
||||
@ -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"<MarkupDavResource {self.path}>"
|
||||
return f"<MarkupDavResource '{self.user}' '{self.path}'>"
|
||||
|
||||
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])))
|
||||
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,9 +131,11 @@ 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:
|
||||
|
||||
# TODO test catalog pdf
|
||||
return os.path.exists(self.get_abs_path())
|
||||
|
||||
# def write(self, content, temp_file=None, range_start=None):
|
||||
@ -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<path:path>', AuthDavView.as_view()),
|
||||
]
|
||||
|
||||
if settings.DJDT:
|
||||
|
||||
Reference in New Issue
Block a user