Skip to content
Snippets Groups Projects

Resolve "configuration panel"

Merged Piotr Gawron requested to merge 87-configuration-panel into master
14 files
+ 308
3
Compare changes
  • Side-by-side
  • Inline
Files
14
+ 58
0
 
from django.contrib.auth.decorators import login_required
 
from django.http import JsonResponse
 
 
from web.models import ConfigurationItem
 
 
 
@login_required
 
def configuration_items(request):
 
# id of the query from dataTable: https://datatables.net/manual/server-side
 
draw = int(request.GET.get("draw", "-1"))
 
 
start = int(request.GET.get("start", "0"))
 
length = int(request.GET.get("length", "10"))
 
 
items = ConfigurationItem.objects.all()
 
count = items.count()
 
count_filtered = count
 
 
sliced_items = items[start:(start + length)]
 
 
data = []
 
for configuration_item in sliced_items:
 
data.append({
 
"id": configuration_item.id,
 
"name": configuration_item.name,
 
"value": configuration_item.value
 
})
 
return JsonResponse({
 
"draw": draw,
 
"recordsTotal": count,
 
"recordsFiltered": count_filtered,
 
"data": data
 
})
 
 
 
@login_required
 
def update_configuration_item(request):
 
id = int(request.GET.get("id", "-1"))
 
value = request.GET.get("value", None)
 
 
if (id is None) or (value is None):
 
return JsonResponse({
 
"status": "error",
 
"message": "id and value are obligatory"
 
})
 
 
items = ConfigurationItem.objects.filter(id=id)
 
if len(items) == 0:
 
return JsonResponse({
 
"status": "error",
 
"message": "item with given id doesn't exist"
 
})
 
item = items[0]
 
item.value = value
 
item.save()
 
return JsonResponse({
 
"status": "ok",
 
})
Loading