mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-26 18:38:38 -06:00
Fixes with new API endpoint
Rewrote js to send data to the new endpoint Made preferences API endpoint accept JSON structures
This commit is contained in:
parent
a0e62c6c87
commit
8280b66a7c
@ -6,7 +6,6 @@ from django.core.paginator import EmptyPage, PageNotAnInteger
|
||||
from django.db import transaction
|
||||
from django.db.models import Count, F, Prefetch
|
||||
from django.forms import ModelMultipleChoiceField, MultipleHiddenInput, modelformset_factory
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404, redirect, render
|
||||
from django.utils.html import escape
|
||||
from django.utils.safestring import mark_safe
|
||||
@ -330,21 +329,9 @@ class RackElevationListView(generic.ObjectListView):
|
||||
'reverse': reverse,
|
||||
'rack_face': rack_face,
|
||||
'filter_form': forms.RackElevationFilterForm(request.GET),
|
||||
'show_images': request.user.config.get('rack_elevation.show_images', False)
|
||||
'show_images': request.user.config.get('rack_elevation.show_images', True)
|
||||
})
|
||||
|
||||
def post(self, request):
|
||||
# Update the user's rack_elevation configuration
|
||||
if "show_images" in request.POST:
|
||||
value = False
|
||||
if request.POST.get("show_images") == 'true' or request.POST.get("show_images") == 'True':
|
||||
value = True
|
||||
preference_name = "rack_elevation.show_images"
|
||||
request.user.config.set(preference_name, value, commit=True)
|
||||
return HttpResponse("Your preferences have been updated.")
|
||||
else:
|
||||
return HttpResponse("No valid parameters was provided")
|
||||
|
||||
|
||||
class RackView(generic.ObjectView):
|
||||
queryset = Rack.objects.prefetch_related('site__region', 'tenant__group', 'group', 'role')
|
||||
@ -372,6 +359,7 @@ class RackView(generic.ObjectView):
|
||||
|
||||
device_count = Device.objects.restrict(request.user, 'view').filter(rack=instance).count()
|
||||
|
||||
print(request.user.config.get('rack_elevation.show_images'))
|
||||
return {
|
||||
'device_count': device_count,
|
||||
'reservations': reservations,
|
||||
@ -379,8 +367,8 @@ class RackView(generic.ObjectView):
|
||||
'nonracked_devices': nonracked_devices,
|
||||
'next_rack': next_rack,
|
||||
'prev_rack': prev_rack,
|
||||
'show_images': request.user.config.get('rack_elevation.show_images', False)
|
||||
})
|
||||
'show_images': request.user.config.get('rack_elevation.show_images', True)
|
||||
}
|
||||
|
||||
|
||||
class RackEditView(generic.ObjectEditView):
|
||||
|
@ -20,13 +20,13 @@ function setImages(visible){
|
||||
$('#toggle_device_images').click(function() {
|
||||
var selected = $(this).attr('selected');
|
||||
$.ajax({
|
||||
url: "/dcim/rack-elevations/",
|
||||
type: 'POST',
|
||||
url: "/api/users/config/",
|
||||
type: 'PATCH',
|
||||
headers:{"X-CSRFToken": $("#csrfmiddlewaretoken").val()},
|
||||
data: {
|
||||
'show_images': !selected,
|
||||
'csrfmiddlewaretoken': $("#csrfmiddlewaretoken").val()
|
||||
'rack_elevation': JSON.stringify({'show_images': !selected})
|
||||
},
|
||||
datatype: "json"
|
||||
datatype: "application/json"
|
||||
});
|
||||
|
||||
setImages(!selected);
|
||||
|
@ -56,7 +56,7 @@
|
||||
<div class="pull-right noprint">
|
||||
<input type="hidden" id="csrfmiddlewaretoken" value="{{ csrf_token }}">
|
||||
<button id="toggle_device_images" class="btn btn-default toggle-images" {% if show_images %} selected="selected" {% endif %}>
|
||||
<span class="mdi mdi-checkbox-marked-circle-outline" aria-hidden="true"></span> Show Images
|
||||
<span class="mdi mdi-checkbox-{% if show_images %}marked{% else %}blank{% endif %}-circle-outline" aria-hidden="true"></span> Show Images
|
||||
</button>
|
||||
{% custom_links object %}
|
||||
</div>
|
||||
|
@ -6,7 +6,7 @@
|
||||
<div class="btn-toolbar pull-right noprint" role="toolbar">
|
||||
<input type="hidden" id="csrfmiddlewaretoken" value="{{ csrf_token }}">
|
||||
<button id="toggle_device_images" class="btn btn-default toggle-images" {% if show_images %} selected="selected" {% endif %}>
|
||||
<span class="mdi mdi mdi-checkbox-marked-circle-outline" aria-hidden="true"></span> Show Images
|
||||
<span class="mdi mdi-checkbox-{% if show_images %}marked{% else %}blank{% endif %}-circle-outline" aria-hidden="true"></span> Show Images
|
||||
</button>
|
||||
<div class="btn-group" role="group">
|
||||
<a href="{% url 'dcim:rack_elevation_list' %}{% querystring request face='front' %}" class="btn btn-default{% if rack_face == 'front' %} active{% endif %}">Front</a>
|
||||
|
@ -1,5 +1,7 @@
|
||||
import json
|
||||
from django.contrib.auth.models import Group, User
|
||||
from django.db.models import Count
|
||||
from django.http import HttpResponse
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.routers import APIRootView
|
||||
@ -73,8 +75,16 @@ class UserConfigViewSet(ViewSet):
|
||||
Update the UserConfig for the currently authenticated User.
|
||||
"""
|
||||
# TODO: How can we validate this data?
|
||||
|
||||
parsed_dict = {}
|
||||
for item in request.data.items():
|
||||
try:
|
||||
parsed_dict[item[0]] = json.loads(item[1])
|
||||
except:
|
||||
parsed_dict[item[0]] = item[1]
|
||||
|
||||
userconfig = self.get_queryset().first()
|
||||
userconfig.data = deepmerge(userconfig.data, request.data)
|
||||
userconfig.data = deepmerge(userconfig.data, parsed_dict)
|
||||
userconfig.save()
|
||||
|
||||
return Response(userconfig.data)
|
||||
|
@ -173,6 +173,7 @@ def deepmerge(original, new):
|
||||
Deep merge two dictionaries (new into original) and return a new dict
|
||||
"""
|
||||
merged = OrderedDict(original)
|
||||
print(original, new)
|
||||
for key, val in new.items():
|
||||
if key in original and isinstance(original[key], dict) and isinstance(val, dict):
|
||||
merged[key] = deepmerge(original[key], val)
|
||||
|
Loading…
Reference in New Issue
Block a user