|
|
import os
|
|
|
import json
|
|
|
from queue import Empty
|
|
|
|
|
|
from django import forms
|
|
|
from django.utils.safestring import mark_safe
|
|
|
from apps.main.models import Device
|
|
|
from apps.main.forms import add_empty_choice
|
|
|
from .models import USRPTXConfiguration
|
|
|
|
|
|
def create_choices_from_model(model, conf_id, all_choice=False):
|
|
|
|
|
|
instance = globals()[model]
|
|
|
choices = instance.objects.all().values_list('pk', 'name')
|
|
|
|
|
|
return choices
|
|
|
|
|
|
def validators_pulse_2(data):
|
|
|
info = {}
|
|
|
if data['enable_2'] is True and None in data.values():
|
|
|
for key, value in data.items():
|
|
|
if value is None:
|
|
|
info[key] = "Ensure this value isn't empty."
|
|
|
raise forms.ValidationError(info)
|
|
|
|
|
|
def validators_textarea(data):
|
|
|
info = {}
|
|
|
|
|
|
if data['code_type_1'].pk == 19 and data['code_1'] == '':
|
|
|
info['code_1'] = "Ensure this value isn't empty."
|
|
|
|
|
|
try:
|
|
|
if data['code_type_2'].pk == 19 and data['code_2'] == '':
|
|
|
info['code_2'] = "Ensure this value isn't empty."
|
|
|
except:
|
|
|
pass
|
|
|
|
|
|
if info != {}:
|
|
|
raise forms.ValidationError(info)
|
|
|
|
|
|
class USRPTXConfigurationForm(forms.ModelForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(USRPTXConfigurationForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
|
|
if instance and instance.pk:
|
|
|
|
|
|
devices = Device.objects.filter(device_type__name='usrp_tx')
|
|
|
#if instance.experiment:
|
|
|
#self.fields['experiment'].widget.attrs['read_only'] = True
|
|
|
#self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)]
|
|
|
#self.fields['device'].widget.choices = [(device.id, device) for device in devices]
|
|
|
|
|
|
if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'):
|
|
|
self.fields['experiment'].widget.attrs['readonly'] = True
|
|
|
|
|
|
class Meta:
|
|
|
model = USRPTXConfiguration
|
|
|
exclude = ('type', 'parameters', 'status', 'total_units', 'author', 'hash')
|
|
|
|
|
|
def clean(self):
|
|
|
form_data = super(USRPTXConfigurationForm, self).clean()
|
|
|
|
|
|
kwargs_check_pulse2 = {}
|
|
|
kwargs_check_pulse2['enable_2'] = form_data.get('enable_2')
|
|
|
kwargs_check_pulse2['pulse_2'] = form_data.get('pulse_2')
|
|
|
kwargs_check_pulse2['code_type_2'] = form_data.get('code_type_2')
|
|
|
kwargs_check_pulse2['repetitions_2'] = form_data.get('repetitions_2')
|
|
|
validators_pulse_2(kwargs_check_pulse2)
|
|
|
|
|
|
kwargs_check_textarea = {}
|
|
|
kwargs_check_textarea['code_type_1'] = form_data.get('code_type_1')
|
|
|
kwargs_check_textarea['code_1'] = form_data.get('code_1')
|
|
|
kwargs_check_textarea['code_type_2'] = form_data.get('code_type_2')
|
|
|
kwargs_check_textarea['code_2'] = form_data.get('code_2')
|
|
|
validators_textarea(kwargs_check_textarea)
|
|
|
|
|
|
return form_data
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
conf = super(USRPTXConfigurationForm, self).save(*args, **kwargs)
|
|
|
conf.save()
|
|
|
return conf
|
|
|
|
|
|
class USRPTXEditionForm(forms.ModelForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(USRPTXEditionForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
instance = getattr(self, 'instance', None)
|
|
|
|
|
|
if instance and instance.pk:
|
|
|
|
|
|
devices = Device.objects.filter(device_type__name='usrp_tx')
|
|
|
#if instance.experiment:
|
|
|
#self.fields['experiment'].widget.attrs['read_only'] = True
|
|
|
#self.fields['experiment'].widget.choices = [(instance.experiment.id, instance.experiment)]
|
|
|
#self.fields['device'].widget.choices = [(device.id, device) for device in devices]
|
|
|
|
|
|
if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'):
|
|
|
self.fields['experiment'].widget.attrs['readonly'] = True
|
|
|
|
|
|
class Meta:
|
|
|
model = USRPTXConfiguration
|
|
|
exclude = ('template', 'device', 'label', 'ip_address', 'daughterboard', 'antenna', 'type', 'parameters', 'status', 'total_units', 'author', 'hash')
|
|
|
|
|
|
def clean(self):
|
|
|
form_data = super(USRPTXEditionForm, self).clean()
|
|
|
return form_data
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
conf = super(USRPTXEditionForm, self).save(*args, **kwargs)
|
|
|
conf.save()
|
|
|
return conf
|
|
|
|
|
|
class ExtFileField(forms.FileField):
|
|
|
"""
|
|
|
Same as forms.FileField, but you can specify a file extension whitelist.
|
|
|
|
|
|
>>> from django.core.files.uploadedfile import SimpleUploadedFile
|
|
|
>>>
|
|
|
>>> t = ExtFileField(ext_whitelist=(".pdf", ".txt"))
|
|
|
>>>
|
|
|
>>> t.clean(SimpleUploadedFile('filename.pdf', 'Some File Content'))
|
|
|
>>> t.clean(SimpleUploadedFile('filename.txt', 'Some File Content'))
|
|
|
>>>
|
|
|
>>> t.clean(SimpleUploadedFile('filename.exe', 'Some File Content'))
|
|
|
Traceback (most recent call last):
|
|
|
...
|
|
|
ValidationError: [u'Not allowed filetype!']
|
|
|
"""
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
extensions = kwargs.pop("extensions")
|
|
|
self.extensions = [i.lower() for i in extensions]
|
|
|
|
|
|
super(ExtFileField, self).__init__(*args, **kwargs)
|
|
|
|
|
|
def clean(self, *args, **kwargs):
|
|
|
data = super(ExtFileField, self).clean(*args, **kwargs)
|
|
|
filename = data.name
|
|
|
ext = os.path.splitext(filename)[1]
|
|
|
ext = ext.lower()
|
|
|
if ext not in self.extensions:
|
|
|
raise forms.ValidationError('Not allowed file type: %s' % ext)
|
|
|
|
|
|
class USRPTXImportForm(forms.Form):
|
|
|
|
|
|
file_name = ExtFileField(extensions=['.racp', '.json', '.dat'])
|