|
|
|
|
|
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
|
|
|
|
|
|
if conf.enable_2:
|
|
|
kwargs['dev_conf_keys'] = ['delay', 'periode_1', 'width_1', 'ntx_1', 'periode_2', 'width_2', 'ntx_2', 'selector']
|
|
|
else:
|
|
|
kwargs['dev_conf_keys'] = ['delay', 'periode_1', 'width_1', 'ntx_1', 'selector']
|
|
|
|
|
|
kwargs['title'] = 'Configuration'
|
|
|
kwargs['suptitle'] = 'Detail'
|
|
|
|
|
|
kwargs['button'] = 'Edit Configuration'
|
|
|
|
|
|
conf.status_device()
|
|
|
|
|
|
###### 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)
|
|
|
|
|
|
if request.method=='GET':
|
|
|
|
|
|
form = GeneratorConfigurationForm(instance=conf)
|
|
|
|
|
|
elif request.method=='POST':
|
|
|
|
|
|
form = GeneratorConfigurationForm(request.POST, instance=conf)
|
|
|
|
|
|
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'
|
|
|
|
|
|
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')
|