From 1dd2c0726f855e734dfe997786dba66d871d0199 Mon Sep 17 00:00:00 2001 From: George Lacey Date: Tue, 11 May 2021 16:19:31 +0100 Subject: [PATCH] Create location form --- borgweb/borg/forms/__init__.py | 1 + borgweb/borg/forms/locationform.py | 6 +++++ borgweb/borg/models/__init__.py | 1 + borgweb/borg/models/location.py | 10 ++++++- .../borg/templates/borg/post/location.html | 14 ++++++++++ borgweb/borg/urls.py | 7 ++--- borgweb/borg/views.py | 27 +++++++++++++++---- 7 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 borgweb/borg/forms/locationform.py create mode 100644 borgweb/borg/templates/borg/post/location.html diff --git a/borgweb/borg/forms/__init__.py b/borgweb/borg/forms/__init__.py index 02dcb0a..b5ecdb2 100644 --- a/borgweb/borg/forms/__init__.py +++ b/borgweb/borg/forms/__init__.py @@ -1,3 +1,4 @@ from .repoform import RepoForm from .archiveform import ArchiveForm from .errorform import ErrorForm +from .locationform import LocationForm diff --git a/borgweb/borg/forms/locationform.py b/borgweb/borg/forms/locationform.py new file mode 100644 index 0000000..5c61bda --- /dev/null +++ b/borgweb/borg/forms/locationform.py @@ -0,0 +1,6 @@ +from django import forms + + +class LocationForm(forms.Form): + label = forms.CharField(label='Label') + path = forms.CharField(label='Path') diff --git a/borgweb/borg/models/__init__.py b/borgweb/borg/models/__init__.py index dcdd438..1e8675d 100644 --- a/borgweb/borg/models/__init__.py +++ b/borgweb/borg/models/__init__.py @@ -3,3 +3,4 @@ from .repo import Repo from .cache import Cache from .archive import Archive from .error import Error +from .location import Location diff --git a/borgweb/borg/models/location.py b/borgweb/borg/models/location.py index 1faae7b..69f4ed4 100644 --- a/borgweb/borg/models/location.py +++ b/borgweb/borg/models/location.py @@ -24,7 +24,7 @@ DESCRIPTION = { class Location(models.Model): label = models.TextField(unique=True) path = models.TextField() - last_checked = models.DateTimeField() + last_checked = models.DateTimeField(null=True) def __path_type(self): try: @@ -65,3 +65,11 @@ class Location(models.Model): def have_permission(self): 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}" diff --git a/borgweb/borg/templates/borg/post/location.html b/borgweb/borg/templates/borg/post/location.html new file mode 100644 index 0000000..118fbe4 --- /dev/null +++ b/borgweb/borg/templates/borg/post/location.html @@ -0,0 +1,14 @@ + + + + + Add Location + + +
+ {% csrf_token %} + {{ form }} + +
+ + \ No newline at end of file diff --git a/borgweb/borg/urls.py b/borgweb/borg/urls.py index eff5865..2b77895 100644 --- a/borgweb/borg/urls.py +++ b/borgweb/borg/urls.py @@ -4,7 +4,8 @@ from . import views urlpatterns = [ path('', views.index, name='index'), - path('post/repo', views.get_repo, name='repo'), - path('post/archive', views.get_archive, name='archive'), - path('post/error', views.get_error, name='error'), + path('post/repo', views.post_repo, name='repo'), + path('post/archive', views.post_archive, name='archive'), + path('post/error', views.post_error, name='error'), + path('post/location', views.post_location, name='location'), ] diff --git a/borgweb/borg/views.py b/borgweb/borg/views.py index 9f9bdcb..0c12115 100644 --- a/borgweb/borg/views.py +++ b/borgweb/borg/views.py @@ -1,9 +1,9 @@ from django.shortcuts import render, get_object_or_404 from django.http import HttpResponseRedirect -from .models import Repo, Label, Archive, Cache, Error 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 .utility import data from datetime import datetime, timedelta @@ -36,7 +36,7 @@ def repo_daily_dict(repo_list, n_days=14): @permission_required("borg.add_repo") -def get_repo(request): +def post_repo(request): if request.method == 'POST': form = RepoForm(request.POST) if form.is_valid(): @@ -64,7 +64,7 @@ def get_repo(request): @permission_required("borg.add_archive") -def get_archive(request): +def post_archive(request): if request.method == 'POST': form = ArchiveForm(request.POST) if form.is_valid(): @@ -92,7 +92,7 @@ def get_archive(request): @permission_required("borg.add_error") -def get_error(request): +def post_error(request): if request.method == 'POST': form = ErrorForm(request.POST) if form.is_valid(): @@ -107,3 +107,20 @@ def get_error(request): form = ErrorForm() 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})