Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Lagerregal/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@
NPM_ROOT_PATH = os.getcwd()

NPM_FILE_PATTERNS = {
'alpinejs': [
'dist/alpine.js',
],
'bootstrap': [
'dist/js/bootstrap.min.js',
],
Expand Down
1 change: 0 additions & 1 deletion Lagerregal/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@
path('devices_ajax/devicedetails/<int:device>/', login_required(devicedata_ajax.DeviceDetails.as_view()), name="device-details"),
path('devices_ajax/devicedetails/<int:device>/json', login_required(devicedata_ajax.DeviceDetailsJson.as_view()), name="device-details-json"),
path('devices_ajax/devicesoftware/<int:device>/', login_required(devicedata_ajax.DeviceSoftware.as_view()), name="device-software"),
path('devices_ajax/initialize_device/', login_required(devices_ajax.InitializeAutomaticDevice.as_view()), name="init-automatic-device"),
]

urlpatterns += format_suffix_patterns([
Expand Down
20 changes: 0 additions & 20 deletions devices/ajax.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,23 +228,3 @@ def get(self, request):
]

return HttpResponse(json.dumps(data), content_type='application/json')


class InitializeAutomaticDevice(View):

def post(self, request):
print(request.POST)
device = Device()
device.name = request.POST["name"]
device.department_id = request.POST["department"]
device.devicetype_id = request.POST["device_type"]
device.operating_system = request.POST["operating_system"]
device.creator = self.request.user
device.save()
device.hostname = "{0}-{1}-{2:06d}".format(device.department.short_name, request.POST["operating_system"], device.pk)
device.save()

return HttpResponse(json.dumps({
"id": device.id,
"hostname": device.hostname
}), content_type='application/json')
50 changes: 38 additions & 12 deletions devices/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import re

from django import forms
from django.conf import settings
from django.contrib.auth.models import Group
from django.db.models import Count
from django.db.utils import OperationalError
Expand All @@ -11,6 +10,7 @@

from django_select2.forms import Select2MultipleWidget
from django_select2.forms import Select2Widget
from reversion import revisions as reversion

from devicegroups.models import Devicegroup
from devices.models import Device
Expand Down Expand Up @@ -328,24 +328,50 @@ def __init__(self, *args, **kwargs):
pass


class DeviceFormAutomatic(forms.Form):
class DeviceFormAutomatic(forms.ModelForm):
error_css_class = 'has-error'

# initial fields
name = forms.CharField(max_length=200, required=False)
devicetype = forms.ModelChoiceField(Type.objects.filter(automatic_data=True), required=False)
department = forms.ModelChoiceField(Department.objects.filter(short_name__isnull=False), required=False)
operating_system = forms.ChoiceField(choices=settings.OPERATING_SYSTEMS)

# optional fields
serialnumber = forms.CharField(max_length=100, required=False)
inventorynumber = forms.CharField(max_length=100, required=False)
room = forms.ModelChoiceField(Room.objects.select_related("building").all(), required=False)
ipaddresses = forms.ModelMultipleChoiceField(
IpAddress.objects.filter(device=None, user=None),
required=False,
widget=Select2MultipleWidget(attrs={"data-token-separators": '[",", " "]'}))

class Meta:
model = Device
fields = [
'name',
'devicetype',
'department',
'operating_system',
'serialnumber',
'inventorynumber',
'room',
]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['devicetype'].required = True
self.fields['devicetype'].queryset = Type.objects.filter(automatic_data=True)
self.fields['department'].required = True
self.fields['operating_system'].required = True

def save(self):
device = super().save()

device.hostname = "{0}-{1}-{2:06d}".format(
self.cleaned_data["department"].short_name,
self.cleaned_data["operating_system"],
device.pk,
)
device.save()

reversion.set_comment(_("Assigned to Device {0}").format(device.name))
for ipaddress in self.cleaned_data['ipaddresses']:
ipaddress.device = device
ipaddress.save()

return device


class AddForm(forms.ModelForm):
error_css_class = 'has-error'
Expand Down
77 changes: 23 additions & 54 deletions devices/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import csv
import datetime
import json
import time

from django.conf import settings
Expand Down Expand Up @@ -38,7 +37,6 @@
from reversion import revisions as reversion
from reversion.models import Version

from api.serializers import DeviceIDSerializer
from devices.forms import VIEWSORTING
from devices.forms import VIEWSORTING_DEVICES
from devices.forms import DeviceForm
Expand Down Expand Up @@ -453,7 +451,7 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"].fields["department"].queryset = self.request.user.departments.all()
context["form"].fields["emailtemplate"].queryset = MailTemplate.objects.all()
context['actionstring'] = "Create new Device"
context['actionstring'] = _("Create new device")
context["breadcrumbs"] = [
(reverse("device-list"), _("Devices")),
("", _("Create new device"))]
Expand Down Expand Up @@ -507,61 +505,32 @@ class DeviceCreateAutomatic(PermissionRequiredMixin, FormView):
form_class = DeviceFormAutomatic
permission_required = 'devices.add_device'

def get_success_url(self):
pk = self.request.GET.get("id", None)
return reverse("device-detail", kwargs={"pk": pk})

def get_initial(self):
initial = super().get_initial()
if self.request.user.main_department:
initial["department"] = self.request.user.main_department
return initial
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
if "id" in self.request.GET:
kwargs["instance"] = get_object_or_404(Device, pk=self.request.GET["id"])
elif self.request.user.main_department:
kwargs["initial"]["department"] = self.request.user.main_department
return kwargs

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["form"].fields["department"].queryset = self.request.user.departments.filter(short_name__isnull=False)
existing_id = self.request.GET.get("id", None)
if existing_id is not None:
device = get_object_or_404(Device, pk=existing_id)
serializer = DeviceIDSerializer(device).data
context["device_json"] = json.dumps(serializer)
else:
context["device_json"] = "{}"
context['actionstring'] = "Create new Device"
context['actionstring'] = _("Create new device")
context["breadcrumbs"] = [
(reverse("device-list"), _("Devices")),
("", _("Create new device"))]
return context

def form_valid(self, form):
if form.cleaned_data["department"]:
if not form.cleaned_data["department"] in self.request.user.departments.filter(short_name__isnull=False):
return HttpResponseBadRequest()
reversion.set_comment(_("Created"))
r = super().form_valid(form)
existing_id = self.request.GET.get("id", None)
if existing_id is not None:
device = get_object_or_404(Device, pk=existing_id)
if "id" not in self.request.GET:
device = form.save()
return HttpResponseRedirect("{}?id={}".format(self.request.path, device.pk))
else:
device = Device()
device.name = form.cleaned_data["name"]
device.department = form.cleaned_data["department"]
device.devicetype = form.cleaned_data["devicetype"]
device.operating_system = form.cleaned_data["operating_system"]
device.serialnumber = form.cleaned_data["serialnumber"]
device.inventorynumber = form.cleaned_data["inventorynumber"]
device.room = form.cleaned_data["room"]
device.save()

ipaddresses = form.cleaned_data["ipaddresses"]

reversion.set_comment(_("Assigned to Device {0}").format(device.name))
for ipaddress in ipaddresses:
ipaddress.device = device
ipaddress.save()

messages.success(self.request, _('Device was successfully saved.'))
return r
reversion.set_comment(_("Created"))
device = form.save()
messages.success(self.request, _("Device was successfully saved."))
return HttpResponseRedirect(reverse("device-detail", kwargs={"pk": device.pk}))


class DeviceUpdate(PermissionRequiredMixin, UpdateView):
Expand Down Expand Up @@ -789,7 +758,7 @@ class DeviceReturn(PermissionRequiredMixin, FormView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['actionstring'] = "Mark device as returned"
context['actionstring'] = _("Mark device as returned")

# get lending object with given pk
lending = get_object_or_404(Lending, pk=self.kwargs["lending"])
Expand Down Expand Up @@ -1179,7 +1148,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Create new Room"
context['actionstring'] = _("Create new room")
context['type'] = "room"
context["breadcrumbs"] = [
(reverse("room-list"), _("Rooms")),
Expand All @@ -1197,7 +1166,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Update"
context['actionstring'] = _("Update")
context["breadcrumbs"] = [
(reverse("room-list"), _("Rooms")),
(reverse("room-detail", kwargs={"pk": context["object"].pk}), context["object"].name),
Expand Down Expand Up @@ -1317,7 +1286,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Create new Building"
context['actionstring'] = _("Create new building")
context['type'] = "building"
context["breadcrumbs"] = [
(reverse("building-list"), _("Buildings")),
Expand All @@ -1335,7 +1304,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Update"
context['actionstring'] = _("Update")
context["breadcrumbs"] = [
(reverse("building-list"), _("Buildings")),
(reverse("building-detail", kwargs={"pk": context["object"].pk}), context["object"].name),
Expand Down Expand Up @@ -1455,7 +1424,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Create new Manufacturer"
context['actionstring'] = _("Create new manufacturer")
context['type'] = "manufacturer"
context["breadcrumbs"] = [
(reverse("manufacturer-list"), _("Manufacturers")),
Expand All @@ -1473,7 +1442,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['actionstring'] = "Update"
context['actionstring'] = _("Update")
context["breadcrumbs"] = [
(reverse("manufacturer-list"), _("Manufacturers")),
(reverse("manufacturer-detail", kwargs={"pk": context["object"].pk}), context["object"].name),
Expand Down
4 changes: 2 additions & 2 deletions network/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class IpAddressCreate(PermissionRequiredMixin, CreateView):

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['actionstring'] = "Create new"
context['actionstring'] = _("Create new IP-Address")
context["form"].fields["department"].queryset = self.request.user.departments.all()
if self.request.user.main_department:
context["form"].fields["department"].initial = self.request.user.main_department
Expand All @@ -129,7 +129,7 @@ def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super().get_context_data(**kwargs)
context["form"].fields["department"].queryset = self.request.user.departments.all()
context['actionstring'] = "Update"
context['actionstring'] = _("Update")
context["breadcrumbs"] = [
(reverse("ipaddress-list"), _("IP-Addresses")),
(reverse("ipaddress-detail", kwargs={"pk": self.object.pk}), self.object.address),
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "lagerregal",
"license": "BSD-3-Clause",
"dependencies": {
"alpinejs": "^2.8.2",
"bootstrap": "^4.6.0",
"bootswatch": "^4.6.0",
"datatables.net": "^1.10.24",
Expand Down
4 changes: 1 addition & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8"/>
<meta http-equiv="Content-Security-Policy" content="default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval';
script-src 'self' 'unsafe-inline';
style-src 'self' http://fonts.googleapis.com https://fonts.googleapis.com;
connect-src 'self' https://localhost:41951 https://127.0.0.1:41951;
font-src 'self' http://fonts.gstatic.com https://fonts.gstatic.com;
Expand All @@ -20,8 +20,6 @@

<link rel="shortcut icon" href="{% static "images/favicon.ico" %}">
<link rel="apple-touch-icon" href="{% static "images/apple-touch-icon.png" %}">

<script src="{% static "alpinejs/dist/alpine.js" %}"></script>
</head>
<body class="{% block body_class%}{% endblock %}">
{% if not nochrome %}
Expand Down
Loading