add ConnectedDevice views

This commit is contained in:
Andreas Zweili 2020-02-15 18:50:51 +01:00
parent d7800ca0f7
commit 47b1f81a89
13 changed files with 265 additions and 4 deletions

View File

@ -8,6 +8,10 @@ class CustomersTable(tables.Table):
orderable=False)
computers = tables.LinkColumn('computers', text='Computers', args=[A('pk')],
orderable=False)
connected_devices = tables.LinkColumn('connected_devices',
text='Connected Devices',
args=[A('pk')],
orderable=False)
devices = tables.LinkColumn('devices', text='Devices', args=[A('pk')],
orderable=False)
backups = tables.LinkColumn('backups', text='Backups', args=[A('pk')],

View File

@ -42,6 +42,10 @@ def test_customer_list_view(create_admin_user):
"/customer/"
+ str(customer.id)
+ "/computers/")
and helper.in_content(response,
"/customer/"
+ str(customer.id)
+ "/connected_devices/")
and helper.in_content(response,
"/customer/"
+ str(customer.id)
@ -78,6 +82,10 @@ def test_customer_list_view_multiple_customers(create_admin_user):
"/customer/"
+ str(customer1.id)
+ "/computers/")
and helper.in_content(response,
"/customer/"
+ str(customer1.id)
+ "/connected_devices/")
and helper.in_content(response,
"/customer/"
+ str(customer1.id)
@ -103,6 +111,10 @@ def test_customer_list_view_multiple_customers(create_admin_user):
"/customer/"
+ str(customer2.id)
+ "/computers/")
and helper.in_content(response,
"/customer/"
+ str(customer2.id)
+ "/connected_devices/")
and helper.in_content(response,
"/customer/"
+ str(customer2.id)

View File

@ -18,7 +18,7 @@ def device_view_permission(old_function):
return new_function
def connect_device_view_permission(old_function):
def connected_device_view_permission(old_function):
def new_function(request, pk, *args, **kwargs):
device = get_object_or_404(ConnectedDevice, pk=pk)
user = request.user

View File

@ -66,13 +66,18 @@ class ConnectedDevice(Device):
class Meta:
verbose_name_plural = "Connected Devices"
def get_absolute_url(self):
from django.urls import reverse
return reverse('connected_device', args=[str(self.id)])
class DeviceInNet(models.Model):
device = models.ForeignKey(ConnectedDevice, on_delete=models.CASCADE)
net = models.ForeignKey(Net, on_delete=models.CASCADE)
ip = models.GenericIPAddressField(verbose_name="IP")
nic = models.CharField(max_length=50, blank=True, verbose_name="NIC")
mac_address = models.CharField(max_length=50, blank=True, verbose_name="MAC Address")
mac_address = models.CharField(max_length=50, blank=True,
verbose_name="MAC Address")
ip_status = models.ForeignKey(IpStatus, models.SET_NULL, null=True,
blank=True, verbose_name="IP Status")

View File

@ -1,5 +1,6 @@
import django_tables2 as tables
from .models import ConnectedDevice
from .models import Device
@ -10,3 +11,12 @@ class DevicesTable(tables.Table):
class Meta:
template_name = 'django_tables2/semantic.html'
model = Device
class ConnectedDevicesTable(tables.Table):
id = tables.Column(visible=False)
name = tables.Column('ConnectedDevice', linkify=True)
class Meta:
template_name = 'django_tables2/semantic.html'
model = ConnectedDevice

View File

@ -0,0 +1,56 @@
{% extends "core/base.html" %}
{% block section_title %}{{ device.name }}{% endblock %}
{% block content %}
<div class="ui cards">
<div class="card">
<div class="content">
<div class="header">Description</div>
<div class="description"><p>{{ device.description }}</p></div>
<table class="ui celled table">
</tr>
<tr>
<th><b>Serial Number:</b></th>
<td><code>{{ device.serialnumber }}</code></td>
</tr>
<tr>
<th><b>Category:</b></th>
<td>{{ device.category }}</td>
</tr>
<tr>
<th><b>Owner:</b></th>
<td>{{ device.owner }}</td>
</tr>
<tr>
<th><b>Customer:</b></th>
<td><a href="{% url 'customer' device.customer.id %}">{{ device.customer }}</a></td>
</tr>
<tr>
<th><b>Manufacturer:</b></th>
<td>{{ device.manufacturer }}</td>
</tr>
<tr>
<th><b>Model:</b></th>
<td>{{ device.model }}</td>
</tr>
<tr>
<th><b>Location:</b></th>
<td>{{ device.location }}</td>
</tr>
<tr>
<th><b>Installation Date:</b></th>
<td>{{ device.installation_date }}</td>
</tr>
<tr>
<td><b>IPs:</b></td>
<td>
{% for net_id, ip in device.ips.items %}
<a href="{% url 'net' net_id %}">{{ip}}</a><br>
{% endfor %}
</td>
</tr>
</table>
</div>
</div>
</div>
{% endblock %}

View File

@ -0,0 +1,6 @@
{% extends "core/base.html" %}
{% load render_table from django_tables2 %}
{% block section_title %}List of General Devices{% endblock %}
{% block content %}
{% render_table devices %}
{% endblock %}

View File

@ -7,4 +7,4 @@ pytestmark = pytest.mark.django_db
def test_device_reverse_url():
device = mixer.blend('devices.ConnectedDevice')
assert (device.get_absolute_url()
== "/device/" + str(device.id) + "/")
== "/connected_device/" + str(device.id) + "/")

View File

@ -0,0 +1,78 @@
import pytest
from mixer.backend.django import mixer
from django.test import Client
from core.tests import helper
pytestmark = pytest.mark.django_db
def test_connected_device_detail_view_not_logged_in():
response = Client().get('/connected_device/1/')
assert response.status_code == 302 and 'login' in response.url
def test_connected_device_detail_view(create_admin_user):
fixture = create_admin_user()
mixer.blend('devices.DeviceCategory')
mixer.blend('devices.HardwareModel')
mixer.blend('customers.Owner')
mixer.blend('customers.Location')
connected_device = mixer.blend('devices.ConnectedDevice',
customer=fixture['customer'],
owner=mixer.SELECT,
category=mixer.SELECT,
manufacturer=mixer.SELECT,
hardware_model=mixer.SELECT,
location=mixer.SELECT)
client = Client()
client.login(username="novartis-admin", password="password")
response = client.get('/connected_device/'
+ str(connected_device.id)
+ '/')
assert (response.status_code == 200
and helper.in_content(response, connected_device)
and helper.in_content(response, connected_device.serialnumber)
and helper.in_content(response, connected_device.category)
and helper.in_content(response, connected_device.owner)
and helper.in_content(response, connected_device.customer)
and helper.in_content(response, connected_device.manufacturer)
and helper.in_content(response, connected_device.hardware_model)
and helper.in_content(response, connected_device.location))
def test_connected_device_detail_view_not_found(create_admin_user):
create_admin_user()
client = Client()
client.login(username="novartis-admin", password="password")
response = client.get('/connected_device/100/')
assert response.status_code == 404
def test_connected_device_detail_view_no_permission(create_admin_user):
create_admin_user()
customer = mixer.blend('customers.Customer')
connected_device = mixer.blend('devices.ConnectedDevice',
customer=customer)
client = Client()
client.login(username="novartis-admin", password="password")
response = client.get('/connected_device/'
+ str(connected_device.id)
+ '/')
assert response.status_code == 403
def test_connected_device_detail_view_net_relation(create_admin_user):
create_admin_user()
device = mixer.blend('devices.ConnectedDevice', customer=mixer.SELECT)
net = mixer.blend('nets.Net', customer=mixer.SELECT)
device_in_net = mixer.blend('devices.DeviceInNet',
device=device,
net=net,
ip="10.7.89.100")
client = Client()
client.login(username="novartis-admin", password="password")
response = client.get('/connected_device/' + str(device.id) + '/')
assert (response.status_code == 200
and helper.in_content(response, device_in_net.ip))

View File

@ -0,0 +1,66 @@
import pytest
from django.test import Client
from mixer.backend.django import mixer
from core.tests import helper
from customers.models import Customer
pytestmark = pytest.mark.django_db
def test_customer_device_table_not_logged_in():
response = Client().get('/customer/1/devices/')
assert response.status_code == 302 and 'login' in response.url
def test_customer_device_table(create_admin_user):
fixture = create_admin_user()
customer = fixture['customer']
client = Client()
client.login(username="novartis-admin", password="password")
device = mixer.blend('devices.ConnectedDevice', customer=mixer.SELECT)
response = client.get('/customer/'
+ str(customer.id)
+ '/connected_devices/')
assert (response.status_code == 200
and helper.in_content(response, device))
def test_customer_device_table_no_device(create_admin_user):
fixture = create_admin_user()
customer = fixture['customer']
client = Client()
client.login(username="novartis-admin", password="password")
response = client.get('/customer/'
+ str(customer.id)
+ '/connected_devices/')
assert (response.status_code == 200
and helper.not_in_content(response, "Novartis PC"))
def test_customer_device_table_no_permission(create_admin_user):
create_admin_user()
customer = Customer.objects.create(name='Nestle')
client = Client()
client.login(username="novartis-admin", password="password")
mixer.blend('devices.ConnectedDevice', customer=customer)
response = client.get('/customer/'
+ str(customer.id)
+ '/connected_devices/')
assert response.status_code == 403
def test_customer_device_table_multiple_devices(create_admin_user):
fixture = create_admin_user()
customer = fixture['customer']
client = Client()
client.login(username="novartis-admin", password="password")
device1 = mixer.blend('devices.ConnectedDevice', customer=mixer.SELECT)
device2 = mixer.blend('devices.ConnectedDevice', customer=mixer.SELECT)
response = client.get('/customer/'
+ str(customer.id)
+ '/connected_devices/')
assert (response.status_code == 200
and helper.in_content(response, device1)
and helper.in_content(response, device2))

View File

@ -4,7 +4,12 @@ from . import views
urlpatterns = [
path('connected_device/<int:pk>/', views.connected_device_detail_view,
name='connected_device'),
path('customer/<int:pk>/devices/', views.devices_table_view,
name='devices'),
path('customer/<int:pk>/connected_devices/',
views.connected_devices_table_view,
name='connected_devices'),
path('device/<int:pk>/', views.device_detail_view, name='device'),
]

View File

@ -7,7 +7,10 @@ from django_tables2 import RequestConfig
from customers.decorators import customer_view_permission
from .decorators import device_view_permission
from .decorators import connected_device_view_permission
from .models import ConnectedDevice
from .tables import ConnectedDevicesTable
from .models import Device
from .tables import DevicesTable
@ -26,3 +29,19 @@ def devices_table_view(request, pk):
table = DevicesTable(Device.objects.filter(customer=pk))
RequestConfig(request).configure(table)
return render(request, 'devices/device_list.html', {'devices': table})
@login_required
@connected_device_view_permission
def connected_device_detail_view(request, pk):
device = get_object_or_404(ConnectedDevice, pk=pk)
return render(request, 'devices/connected_device_details.html',
{'device': device})
@login_required
@customer_view_permission
def connected_devices_table_view(request, pk):
table = ConnectedDevicesTable(ConnectedDevice.objects.filter(customer=pk))
RequestConfig(request).configure(table)
return render(request, 'devices/connected_device_list.html', {'devices': table})

View File

@ -15,7 +15,7 @@ class NetsTable(tables.Table):
class NetDetailTable(tables.Table):
device = tables.Column('Computer', linkify=True)
device = tables.Column(linkify=True)
ip = tables.Column()
net = tables.Column(visible=False)