added server side directory adding, validation and redirecting, show directory path and mention 'files' near filecount in panel header

This commit is contained in:
Xevion
2020-10-31 18:34:54 -05:00
parent 4cc1402d21
commit 688ffe88dd
3 changed files with 27 additions and 5 deletions

View File

@@ -17,7 +17,7 @@
<br>
Additionally, files can be matched <i>recursively</i> if you so wish. Please note that this functionality may create anomalous behavior around duplicate files.
</p>
<form action="{% url 'add_submit' %}">
<form action="{% url 'add_submit' %}" method="post">
{% csrf_token %}
<div class="field">
<label class="label">Directory</label>

View File

@@ -2,9 +2,9 @@
{% block content %}
<div class="panel">
<div class="panel-heading">
Files
{{ directory.path }}
<span style="font-weight: 400; font-style: italic; font-size: 70%;">
{{ files|length }}
{{ files|length }} files
</span>
<span style="vertical-align: middle;" class="panel-icon">
<a href="{% url 'index' %}">

View File

@@ -1,7 +1,8 @@
import os
from django.http import FileResponse, HttpResponse
from django.http import FileResponse, HttpResponseRedirect
from django.shortcuts import render, get_object_or_404
from django.urls import reverse
from viewer.helpers import extra_listdir
from viewer.models import ServedDirectory
@@ -61,4 +62,25 @@ def add(request):
def submit_new(request):
return HttpResponse('')
try:
s = ServedDirectory(
path=request.POST['path'],
regex_pattern=request.POST.get('regex'),
regex=request.POST.get('regex') is not None,
match_filename=request.POST.get('match_filename', False),
recursive=request.POST.get('recursive', False)
)
if not os.path.isdir(request.POST['path']):
raise ValueError('A invalid Directory was specified in the request.')
s.save()
except KeyError:
return render(request, 'message.html', status=403,
context={'title': 'Invalid Options',
'message': 'The POST request you sent did not have the options required to complete '
'the request.'})
except ValueError:
return render(request, 'message.html', status=400,
context={'title': 'Invalid Directory',
'message': 'The directory you specified was not a valid directory, either it doesn\'t '
'exist or it isn\'t a directory.'})
return HttpResponseRedirect(reverse('browse', args=(s.id,)))