-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
142 lines (125 loc) · 5.18 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# ------------------------------------------------------------------------------
#
# Project: Master Inventory <http://github.com/ESA-MInv/minv>
# Authors: Fabian Schindler <fabian.schindler@eox.at>
#
# ------------------------------------------------------------------------------
# Copyright (C) 2016 European Space Agency
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies of this Software or works derived from this Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ------------------------------------------------------------------------------
from os.path import join, basename
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from minv.inventory import models
from minv.inventory import forms
from minv.inventory.backup import (
backup, restore, get_available_backups, BASE_PATH
)
def get_available_backups_list(add_no_selected=False):
files = [
(bck[0], "%s (%s)" % bck)
for bck in get_available_backups()
]
return files if not add_no_selected else [("", "----")] + files
@login_required(login_url="login")
def backup_view(request):
""" Django view to show and create backups.
"""
if request.method == "POST":
form = forms.BackupForm(get_available_backups_list(True), request.POST)
if form.is_valid():
data = form.cleaned_data
backup_type = data.get("backup_type")
subject = data.get("subject", [])
configuration = "configuration" in subject
logs = "logfiles" in subject
try:
diff = data.get("differential_file")
interval = data.get("interval")
if backup_type == "incremental" and not interval:
raise Exception(
"Backup type is incremental, but no interval specified"
)
elif backup_type == "incremental":
diff = None
if backup_type == "differential" and not diff:
raise Exception(
"Backup type is differential, but no base backup file "
"selected."
)
elif backup_type == "differential":
diff = join(BASE_PATH, diff)
interval = None
if backup_type == "full":
diff = None
interval = None
backup_path = backup(logs, configuration, False, diff, interval)
except Exception as exc:
messages.error(
request, "Failed to perform backup, error was: %s" % exc
)
else:
messages.info(
request, "Created %s backup of %s: %s." % (
data["backup_type"], ", ".join(data["subject"]),
basename(backup_path)
)
)
form = forms.BackupForm(
get_available_backups_list(True), request.POST
)
else:
form = forms.BackupForm(get_available_backups_list(True))
return render(
request, "inventory/backup.html", {
"collections": models.Collection.objects.all(),
"form": form
}
)
@login_required(login_url="login")
def restore_view(request):
""" Django view to restore previously created backups
"""
if request.method == "POST":
form = forms.RestoreForm(get_available_backups_list(), request.POST)
if form.is_valid():
filename = form.cleaned_data["backup"]
try:
restore(join(BASE_PATH, filename))
except Exception as exc:
messages.error(
request,
"Failed to restore backup from '%s', error was: %s"
% (filename, exc)
)
else:
messages.info(
request,
"Restored backup from '%s'." % filename
)
else:
form = forms.RestoreForm(get_available_backups_list())
return render(
request, "inventory/restore.html", {
"collections": models.Collection.objects.all(),
"form": form
}
)