add backups app

This commit is contained in:
Andreas Zweili 2020-01-11 17:01:21 +01:00
parent e167aefc03
commit da624cabc3
11 changed files with 296 additions and 0 deletions

0
backups/__init__.py Normal file
View File

65
backups/admin.py Normal file
View File

@ -0,0 +1,65 @@
from django.contrib import admin
from .models import (
Backup,
BackupMethod,
Notification,
NotificationType,
NotificationFromBackup,
TargetDevice,
)
class BackupMethodAdmin(admin.ModelAdmin):
def get_model_perms(self, request):
"""
Return empty perms dict thus hiding the model from admin index.
"""
return {}
class NotificationTypeAdmin(admin.ModelAdmin):
def get_model_perms(self, request):
"""
Return empty perms dict thus hiding the model from admin index.
"""
return {}
class NotificationAdmin(admin.ModelAdmin):
def get_model_perms(self, request):
"""
Return empty perms dict thus hiding the model from admin index.
"""
return {}
class TargetDeviceInLine(admin.StackedInline):
model = TargetDevice
extra = 0
verbose_name_plural = 'Target Devices'
class NotificationForBackupInLine(admin.StackedInline):
model = NotificationFromBackup
extra = 0
verbose_name_plural = 'Notifications'
class BackupAdmin(admin.ModelAdmin):
inlines = (TargetDeviceInLine, NotificationForBackupInLine)
class TargetDeviceAdmin(admin.ModelAdmin):
def get_model_perms(self, request):
"""
Return empty perms dict thus hiding the model from admin index.
"""
return {}
admin.site.register(Backup, BackupAdmin)
admin.site.register(BackupMethod, BackupMethodAdmin)
admin.site.register(Notification, NotificationAdmin)
admin.site.register(NotificationType, NotificationTypeAdmin)
admin.site.register(TargetDevice, TargetDeviceAdmin)

5
backups/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class BackupsConfig(AppConfig):
name = 'backups'

17
backups/decorators.py Normal file
View File

@ -0,0 +1,17 @@
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404
from .models import Backup
def backup_view_permission(old_fuction):
def new_function(request, pk, *args, **kwargs):
backup = get_object_or_404(Backup, pk=pk)
user = request.user
if user.has_perm('customers.view_customer', backup.computer.customer):
return old_fuction(request, pk)
else:
return HttpResponseForbidden(
"You're not allowed to access this device."
)
return new_function

View File

54
backups/models/backup.py Normal file
View File

@ -0,0 +1,54 @@
from django.db import models
from core.models import Category, Weekday
from .computer import Computer
from .notification import Notification
from .software import Software
class BackupMethod(Category):
description = models.TextField()
class Meta:
verbose_name_plural = "Backup Methods"
class Backup(models.Model):
name = models.CharField(max_length=50)
computer = models.ForeignKey(Computer, related_name='source_computer',
on_delete=models.CASCADE)
method = models.ForeignKey(BackupMethod, models.SET_NULL, blank=True,
null=True)
software = models.ForeignKey(Software, models.SET_NULL, blank=True,
null=True)
source_path = models.CharField(max_length=200, blank=True)
exec_time = models.TimeField()
exec_day = models.ForeignKey(Weekday, models.SET_NULL, blank=True,
null=True)
target_device = models.ManyToManyField(Computer, through='TargetDevice')
def __str__(self):
return str(self.name)
def get_absolute_url(self):
from django.urls import reverse
return reverse('backup', args=[str(self.id)])
class TargetDevice(models.Model):
device = models.ForeignKey(Computer, models.SET_NULL, blank=True,
null=True)
backup = models.ForeignKey(Backup, on_delete=models.CASCADE)
target_path = models.CharField(max_length=200, blank=True)
def __str__(self):
return str(self.device)
class Meta:
verbose_name_plural = "Target Devices"
class NotificationFromBackup(models.Model):
backup = models.ForeignKey(Backup, on_delete=models.CASCADE)
notification = models.ForeignKey(Notification, on_delete=models.CASCADE)

View File

@ -0,0 +1,18 @@
from django.db import models
from core.models import Category
class NotificationType(Category):
pass
class Notification(models.Model):
name = models.CharField(max_length=50)
description = models.TextField(blank=True)
recipient = models.EmailField()
notification_type = models.ForeignKey(NotificationType, models.SET_NULL,
blank=True, null=True)
def __str__(self):
return self.name

14
backups/tables.py Normal file
View File

@ -0,0 +1,14 @@
import django_tables2 as tables
from .models import Backup
class BackupsTable(tables.Table):
id = tables.Column(visible=False)
name = tables.Column('Backup', linkify=True)
computer = tables.Column('Computer', linkify=True)
target_device = tables.ManyToManyColumn(linkify_item=True)
class Meta:
template_name = 'django_tables2/semantic.html'
model = Backup

View File

@ -0,0 +1,82 @@
{% extends "inventory/base.html" %}
{% block section_title %}{{ backup.name }}{% endblock %}
{% block content %}
<div class="ui cards">
<div class="card">
<div class="content">
<div class="header">Description</div>
<div class="description"><p>{{ backup.description }}</p></div>
<table class="ui celled table">
<tr>
<td><b>Computer:</b></td>
<td><a href="{% url 'computer' backup.computer.id %}">{{ backup.computer }}</a></td>
</tr>
<tr>
<td><b>Backup Method:</b></td>
<td>{{ backup.method }}</td>
</tr>
<tr>
<td><b>Backup Software:</b></td>
<td>{{ backup.software }}</td>
</tr>
<tr>
<td><b>Exec Time:</b></td>
<td>{{ backup.exec_time }}</td>
</tr>
<tr>
<td><b>Exec Day:</b></td>
<td>{{ backup.exec_day }}</td>
</tr>
</table>
</div>
</div>
{% if target_device_list %}
<div class="card">
<div class="content">
<div class="header">Target Devices</div>
{% for device in target_device_list %}
<table class="ui celled table">
<tr>
<td><b>Target Device:</b></td>
<td><a href="{% url 'computer' device.device.id %}">{{ device.device }}</a></td>
</tr>
<tr>
<td><b>Target Path:</b></td>
<td>{{ device.target_path }}</td>
</tr>
</table>
{% endfor %}
</div>
</div>
{% endif %}
{% if notifications %}
<div class="card">
<div class="content">
<div class="header">Notifications</div>
{% for notification in notifications %}
<table class="ui celled table">
<tr>
<td><b>Name:</b></td>
<td>{{ notification.notification }}</td>
</tr>
<tr>
<td><b>Description:</b></td>
<td>{{ notification.notification.description }}</td>
</tr>
<tr>
<td><b>Recipient:</b></td>
<td>{{ notification.notification.recipient }}</td>
</tr>
<tr>
<td><b>Type:</b></td>
<td>{{ notification.notification.notification_type }}</td>
</tr>
</table>
{% endfor %}
</div>
</div>
{% endif %}
</div>
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends "inventory/base.html" %}
{% load render_table from django_tables2 %}
{% block section_title %}List of Backups{% endblock %}
{% block content %}
{% render_table backups %}
{% endblock %}

35
backups/views.py Normal file
View File

@ -0,0 +1,35 @@
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django_tables2 import RequestConfig
from inventory.models import Computer
from customer.decorators import customer_view_permission
from .decorators import backup_view_permission
from .models import Backup
from .models import NotificationFromBackup
from .models import TargetDevice
from .tables import BackupsTable
@login_required
@customer_view_permission
def backups_table_view(request, pk):
computers = Computer.objects.filter(customer=pk)
table = BackupsTable(Backup.objects.filter(computer__in=computers))
RequestConfig(request).configure(table)
return render(request, 'inventory/backup_list.html', {'backups': table})
@login_required
@backup_view_permission
def backup_detail_view(request, pk):
backup = get_object_or_404(Backup, pk=pk)
target_device_list = TargetDevice.objects.filter(backup=backup)
notifications = NotificationFromBackup.objects.filter(backup=backup)
return render(request, 'inventory/backup_details.html',
{'backup': backup,
'target_device_list': target_device_list,
'notifications': notifications})