Added setting to enable webdav (default: disabled), cleaned up the code somewhat.

This commit is contained in:
Jonas Winkler 2018-07-11 14:59:47 +02:00
parent 38bb1f9672
commit f2c32d840e
4 changed files with 54 additions and 46 deletions

4
paperless.conf.example Normal file → Executable file
View File

@ -193,3 +193,7 @@ PAPERLESS_EMAIL_SECRET=""
# positive integer, but if you don't define one in paperless.conf, a default of # positive integer, but if you don't define one in paperless.conf, a default of
# 100 will be used. # 100 will be used.
#PAPERLESS_LIST_PER_PAGE=100 #PAPERLESS_LIST_PER_PAGE=100
# Enable WebDAV support for Paperless. Default is false.
#PAPERLESS_ENABLE_WEBDAV="true"

View File

@ -103,7 +103,6 @@ class PaperlessDavResource(MetaEtagMixIn, BaseDavResource):
def __init__(self, path, **kwargs): def __init__(self, path, **kwargs):
super(PaperlessDavResource, self).__init__(path) super(PaperlessDavResource, self).__init__(path)
if 'document' in kwargs: if 'document' in kwargs:
print("using document from kwargs")
# this greatly reduces the amount of database requests. # this greatly reduces the amount of database requests.
self.document = kwargs.pop('document') self.document = kwargs.pop('document')
else: else:
@ -147,7 +146,7 @@ class PaperlessDavResource(MetaEtagMixIn, BaseDavResource):
yield self.clone(url_join(*(self.path + [child]))) yield self.clone(url_join(*(self.path + [child])))
for doc in self.documents: for doc in self.documents:
yield self.clone(url_join(*(self.path + [doc.title])), document=doc) yield self.clone(url_join(*(self.path + [doc.file_name])), document=doc)
def write(self, content, temp_file=None): def write(self, content, temp_file=None):
raise NotImplementedError() raise NotImplementedError()
@ -174,8 +173,8 @@ def parse_path(path):
2. provide a database filter that returns a set of documents to be displayed, applying filters if necessary. 2. provide a database filter that returns a set of documents to be displayed, applying filters if necessary.
3. provide a set of "folders" that act as filters to narrow down the list of documents. 3. provide a set of "folders" that act as filters to narrow down the list of documents.
This is achieved by implementing a state machine. This machine processes the path segment by segment and switched This is achieved by implementing a state machine. This machine processes the path segment by segment and switches
states as the path is processed. Depending on the state, only certain path segments are allowed. states as the path is processed. Depending on the state, only certain path segments are allowed
:param path: :param path:
:return: :return:
""" """
@ -184,10 +183,12 @@ def parse_path(path):
year_selected = False year_selected = False
month_selected = False month_selected = False
day_selected = False day_selected = False
show_documents = True show_documents = False
def get_filter_children(): def get_filter_children(is_root=False):
filters = [] filters = []
if is_root:
filters.append("show_all_documents")
if not year_selected: if not year_selected:
filters.append('year') filters.append('year')
elif not month_selected: elif not month_selected:
@ -203,72 +204,72 @@ def parse_path(path):
path_queue = [x for x in path.split('/') if x] path_queue = [x for x in path.split('/') if x]
filter = Document.objects.all() filter = Document.objects.all()
children = get_filter_children() children = get_filter_children(True)
document = None document = None
exists = True exists = True
current_rule = 'select_filter' current_state = 'select_filter'
while len(path_queue) > 0: while len(path_queue) > 0:
path_segment = path_queue.pop(0) path_segment = path_queue.pop(0)
show_documents = False
children = []
next_state = ''
if current_rule == 'select_filter': if current_state == 'select_filter' and path_segment == 'year':
show_documents = False next_state = 'select_year'
if path_segment == 'year': children = [str(d.year) for d in filter.dates('created', 'year')]
next_rule = 'select_year' elif current_state == 'select_filter' and path_segment == 'month':
children = [str(d.year) for d in filter.dates('created', 'year')] next_state = 'select_month'
elif path_segment == 'month': children = [str(d.month) for d in filter.dates('created', 'month')]
next_rule = 'select_month' elif current_state == 'select_filter' and path_segment == 'day':
children = [str(d.month) for d in filter.dates('created', 'month')] next_state = 'select_day'
elif path_segment == 'day': children = [str(d.day) for d in filter.dates('created', 'day')]
next_rule = 'select_day' elif current_state == 'select_filter' and path_segment == 'correspondent':
children = [str(d.day) for d in filter.dates('created', 'day')] next_state = 'select_correspondent'
elif path_segment == 'correspondent': children = [c.name for c in Correspondent.objects.filter(documents__in=filter).distinct()]
next_rule = 'select_correspondent' elif current_state == 'select_filter' and path_segment == 'tag':
children = [c.name for c in Correspondent.objects.filter(documents__in=filter)] next_state = 'select_tag'
elif path_segment == 'tag': children = [t.name for t in Tag.objects.filter(documents__in=filter).distinct() if t.name not in used_tags]
next_rule = 'select_tag' elif current_state == 'select_filter' and path_segment == 'show_all_documents':
children = [t.name for t in Tag.objects.filter(documents__in=filter) if t.name not in used_tags] show_documents = True
else: elif current_state == 'select_tag':
next_rule = 'document' next_state = 'select_filter'
children = []
try:
document = Document.objects.get(title=path_segment)
except:
exists = False
elif current_rule == 'select_tag':
next_rule = 'select_filter'
filter = filter.filter(tags__name=path_segment) filter = filter.filter(tags__name=path_segment)
used_tags.append(path_segment) used_tags.append(path_segment)
children = get_filter_children() children = get_filter_children()
show_documents = True show_documents = True
elif current_rule == 'select_correspondent': elif current_state == 'select_correspondent':
next_rule = 'select_filter' next_state = 'select_filter'
filter = filter.filter(correspondent__name=path_segment) filter = filter.filter(correspondent__name=path_segment)
correspondent_selected = True correspondent_selected = True
children = get_filter_children() children = get_filter_children()
show_documents = True show_documents = True
elif current_rule == 'select_year': elif current_state == 'select_year':
next_rule = 'select_filter' next_state = 'select_filter'
filter = filter.filter(created__year=path_segment) filter = filter.filter(created__year=path_segment)
year_selected = True year_selected = True
children = get_filter_children() children = get_filter_children()
show_documents = True show_documents = True
elif current_rule == 'select_month': elif current_state == 'select_month':
next_rule = 'select_filter' next_state = 'select_filter'
filter = filter.filter(created__month=path_segment) filter = filter.filter(created__month=path_segment)
month_selected = True month_selected = True
children = get_filter_children() children = get_filter_children()
show_documents = True show_documents = True
elif current_rule == 'select_day': elif current_state == 'select_day':
next_rule = 'select_filter' next_state = 'select_filter'
filter = filter.filter(created__day=path_segment) filter = filter.filter(created__day=path_segment)
day_selected = True day_selected = True
children = get_filter_children() children = get_filter_children()
show_documents = True show_documents = True
else: else:
raise ValueError() try:
#TODO: this is pretty slow and sketchy.
document = [d for d in Document.objects.all() if d.file_name == path_segment][0]
except IndexError:
exists = False
current_rule = next_rule current_state = next_state
return exists, filter if show_documents else [], document, children return exists, filter if show_documents else [], document, children

View File

@ -280,3 +280,5 @@ FY_END = os.getenv("PAPERLESS_FINANCIAL_YEAR_END")
# Specify the default date order (for autodetected dates) # Specify the default date order (for autodetected dates)
DATE_ORDER = os.getenv("PAPERLESS_DATE_ORDER", "DMY") DATE_ORDER = os.getenv("PAPERLESS_DATE_ORDER", "DMY")
ENABLE_WEBDAV = bool(os.getenv("PAPERLESS_ENABLE_WEBDAV", "NO").lower() in ("yes", "y", "1", "t", "true"))

View File

@ -50,14 +50,15 @@ urlpatterns = [
# The Django admin # The Django admin
url(r"admin/", admin.site.urls), url(r"admin/", admin.site.urls),
url(r'^dav(?P<path>.*)$', SecuredDavView.as_view(resource_class=PaperlessDavResource, lock_class=DummyLock, acl_class=FullAcl)),
# Redirect / to /admin # Redirect / to /admin
url(r"^$", RedirectView.as_view( url(r"^$", RedirectView.as_view(
permanent=True, url=reverse_lazy("admin:index"))), permanent=True, url=reverse_lazy("admin:index"))),
] + static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.ENABLE_WEBDAV:
urlpatterns.append(url(r'^dav(?P<path>.*)$', SecuredDavView.as_view(resource_class=PaperlessDavResource, lock_class=DummyLock, acl_class=FullAcl)))
# Text in each page's <h1> (and above login form). # Text in each page's <h1> (and above login form).
admin.site.site_header = 'Paperless' admin.site.site_header = 'Paperless'
# Text at the end of each page's <title>. # Text at the end of each page's <title>.