views.py
138 lines
| 4.2 KiB
| text/x-python
|
PythonLexer
r348 | |||
import json | |||
from django.contrib import messages | |||
from django.utils.safestring import mark_safe | |||
from django.shortcuts import render, redirect, get_object_or_404, HttpResponse | |||
from django.contrib.auth.decorators import login_required | |||
from apps.main.models import Experiment, Device | |||
from apps.main.views import sidebar | |||
from .models import GeneratorConfiguration | |||
from .forms import GeneratorConfigurationForm, GeneratorImportForm | |||
def conf(request, conf_id): | |||
conf = get_object_or_404(GeneratorConfiguration, pk=conf_id) | |||
kwargs = {} | |||
kwargs['dev_conf'] = conf | |||
r350 | kwargs['dev_conf_keys'] = ['periode', 'delay', 'width', 'selector'] | ||
r348 | |||
kwargs['title'] = 'Configuration' | |||
kwargs['suptitle'] = 'Detail' | |||
kwargs['button'] = 'Edit Configuration' | |||
r350 | |||
conf.status_device() | |||
r348 | ###### SIDEBAR ###### | ||
kwargs.update(sidebar(conf=conf)) | |||
return render(request, 'generator_conf.html', kwargs) | |||
@login_required | |||
def conf_edit(request, conf_id): | |||
conf = get_object_or_404(GeneratorConfiguration, pk=conf_id) | |||
print(conf) | |||
#print("fin de carga de params") | |||
if request.method=='GET': | |||
print("GET case") | |||
form = GeneratorConfigurationForm(instance=conf) | |||
print(form) | |||
elif request.method=='POST': | |||
#print("ingreso a post conf edit") | |||
line_data = {} | |||
conf_data = {} | |||
clock_data = {} | |||
extras = [] | |||
print("Inicio impresion POST#####") | |||
print(request.POST.items) | |||
print("Fin impresion de POST items#####") | |||
#classified post fields | |||
for label,value in request.POST.items(): | |||
if label=='csrfmiddlewaretoken': | |||
continue | |||
if label.count('|')==0: | |||
if label in ('mode', 'multiplier', 'divisor', 'reference', 'frequency'): | |||
clock_data[label] = value | |||
else: | |||
conf_data[label] = value | |||
continue | |||
elif label.split('|')[0]!='-1': | |||
extras.append(label) | |||
continue | |||
#print(label) | |||
x, pk, name = label.split('|') | |||
if name=='codes': | |||
value = [s for s in value.split('\r\n') if s] | |||
if pk in line_data: | |||
line_data[pk][name] = value | |||
else: | |||
line_data[pk] = {name:value} | |||
#print(line_data[pk]) | |||
#update conf | |||
form = GeneratorConfigurationForm(conf_data, instance=conf) | |||
#print(request.POST.items()) | |||
if form.is_valid(): | |||
form.save() | |||
messages.success(request, 'Generator configuration successfully updated') | |||
return redirect(conf.get_absolute_url()) | |||
kwargs = {} | |||
kwargs['dev_conf'] = conf | |||
kwargs['form'] = form | |||
kwargs['edit'] = True | |||
kwargs['title'] = 'Generator Configuration' | |||
kwargs['suptitle'] = 'Edit' | |||
kwargs['button'] = 'Update' | |||
print(kwargs) | |||
print(form) | |||
return render(request, 'generator_conf_edit.html', kwargs) | |||
def import_file(request, conf_id): | |||
conf = get_object_or_404(GeneratorConfiguration, pk=conf_id) | |||
if request.method=='POST': | |||
form = GeneratorImportForm(request.POST, request.FILES) | |||
if form.is_valid(): | |||
try: | |||
data = conf.import_from_file(request.FILES['file_name']) | |||
conf.dict_to_parms(data) | |||
messages.success(request, 'Configuration "%s" loaded succesfully' % request.FILES['file_name']) | |||
return redirect(conf.get_absolute_url_edit()) | |||
except Exception as e: | |||
messages.error(request, 'Error parsing file: "%s" - %s' % (request.FILES['file_name'], repr(e))) | |||
else: | |||
messages.warning(request, 'Your current configuration will be replaced') | |||
form = GeneratorImportForm() | |||
kwargs = {} | |||
kwargs['form'] = form | |||
kwargs['title'] = 'Generator Configuration' | |||
kwargs['suptitle'] = 'Import file' | |||
kwargs['button'] = 'Upload' | |||
kwargs['previous'] = conf.get_absolute_url() | |||
return render(request, 'generator_import.html', kwargs) | |||
def conf_raw(request, conf_id): | |||
conf = get_object_or_404(GeneratorConfiguration, pk=conf_id) | |||
raw = conf.write_device(raw=True) | |||
return HttpResponse(raw, content_type='application/json') |