Create location form

This commit is contained in:
George Lacey 2021-05-11 16:19:31 +01:00
parent eca02882da
commit 1dd2c0726f
7 changed files with 57 additions and 9 deletions

View File

@ -1,3 +1,4 @@
from .repoform import RepoForm from .repoform import RepoForm
from .archiveform import ArchiveForm from .archiveform import ArchiveForm
from .errorform import ErrorForm from .errorform import ErrorForm
from .locationform import LocationForm

View File

@ -0,0 +1,6 @@
from django import forms
class LocationForm(forms.Form):
label = forms.CharField(label='Label')
path = forms.CharField(label='Path')

View File

@ -3,3 +3,4 @@ from .repo import Repo
from .cache import Cache from .cache import Cache
from .archive import Archive from .archive import Archive
from .error import Error from .error import Error
from .location import Location

View File

@ -24,7 +24,7 @@ DESCRIPTION = {
class Location(models.Model): class Location(models.Model):
label = models.TextField(unique=True) label = models.TextField(unique=True)
path = models.TextField() path = models.TextField()
last_checked = models.DateTimeField() last_checked = models.DateTimeField(null=True)
def __path_type(self): def __path_type(self):
try: try:
@ -65,3 +65,11 @@ class Location(models.Model):
def have_permission(self): def have_permission(self):
return self.__path_type() != ACCESS_DENIED return self.__path_type() != ACCESS_DENIED
def short_description(self):
type = self.__path_type()
type_description = self.type_description()
existence = "exists" if self.exists() else "does not exist"
if self.exists():
return f"{type_description} {self.path} {existence}"

View File

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add Location</title>
</head>
<body>
<form action="location" method="post">
{% csrf_token %}
{{ form }}
<input type="submit" value="Submit">
</form>
</body>
</html>

View File

@ -4,7 +4,8 @@ from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', views.index, name='index'),
path('post/repo', views.get_repo, name='repo'), path('post/repo', views.post_repo, name='repo'),
path('post/archive', views.get_archive, name='archive'), path('post/archive', views.post_archive, name='archive'),
path('post/error', views.get_error, name='error'), path('post/error', views.post_error, name='error'),
path('post/location', views.post_location, name='location'),
] ]

View File

@ -1,9 +1,9 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from .models import Repo, Label, Archive, Cache, Error
from django.urls import reverse from django.urls import reverse
from .forms import RepoForm, ArchiveForm, ErrorForm from .models import Repo, Label, Archive, Cache, Error, Location
from .forms import RepoForm, ArchiveForm, ErrorForm, LocationForm
from django.contrib.auth.decorators import permission_required from django.contrib.auth.decorators import permission_required
from .utility import data from .utility import data
from datetime import datetime, timedelta from datetime import datetime, timedelta
@ -36,7 +36,7 @@ def repo_daily_dict(repo_list, n_days=14):
@permission_required("borg.add_repo") @permission_required("borg.add_repo")
def get_repo(request): def post_repo(request):
if request.method == 'POST': if request.method == 'POST':
form = RepoForm(request.POST) form = RepoForm(request.POST)
if form.is_valid(): if form.is_valid():
@ -64,7 +64,7 @@ def get_repo(request):
@permission_required("borg.add_archive") @permission_required("borg.add_archive")
def get_archive(request): def post_archive(request):
if request.method == 'POST': if request.method == 'POST':
form = ArchiveForm(request.POST) form = ArchiveForm(request.POST)
if form.is_valid(): if form.is_valid():
@ -92,7 +92,7 @@ def get_archive(request):
@permission_required("borg.add_error") @permission_required("borg.add_error")
def get_error(request): def post_error(request):
if request.method == 'POST': if request.method == 'POST':
form = ErrorForm(request.POST) form = ErrorForm(request.POST)
if form.is_valid(): if form.is_valid():
@ -107,3 +107,20 @@ def get_error(request):
form = ErrorForm() form = ErrorForm()
return render(request, 'borg/post/error.html', {'form': form}) return render(request, 'borg/post/error.html', {'form': form})
@permission_required("borg.add_location")
def post_location(request):
if request.method == 'POST':
form = LocationForm(request.POST)
if form.is_valid():
cdata = form.cleaned_data
label, _ = Location.objects.get_or_create(label=cdata['label'],
defaults={"path": cdata["path"]})
label.save()
return HttpResponseRedirect(reverse('index'))
else:
form = LocationForm ()
return render(request, 'borg/post/location.html', {'form': form})