|
|
from django import forms
|
|
|
from django.utils.safestring import mark_safe
|
|
|
from apps.main.models import Device, Experiment, Configuration
|
|
|
from django.template.defaultfilters import default
|
|
|
|
|
|
FILE_FORMAT = (
|
|
|
('json', 'json'),
|
|
|
)
|
|
|
|
|
|
def add_empty_choice(choices, pos=0, label='-----'):
|
|
|
if len(choices)>0:
|
|
|
choices = list(choices)
|
|
|
choices.insert(0, (0, label))
|
|
|
return choices
|
|
|
else:
|
|
|
return [(0, label)]
|
|
|
|
|
|
class DatepickerWidget(forms.widgets.TextInput):
|
|
|
def render(self, name, value, attrs=None, renderer=None):
|
|
|
input_html = super(DatepickerWidget, self).render(name, value, attrs)
|
|
|
|
|
|
html = '<div class="input-group date">'+input_html+'<span class="input-group-addon"><i class="far fa-calendar-alt"></i></span></div>'
|
|
|
return mark_safe(html)
|
|
|
|
|
|
class DateRangepickerWidget(forms.widgets.TextInput):
|
|
|
def render(self, name, value, attrs=None, renderer=None):
|
|
|
start = attrs['start_date']
|
|
|
end = attrs['end_date']
|
|
|
html = '''<div class="col-md-6 input-group date" style="float:inherit">
|
|
|
<input class="form-control" id="id_start_date" name="start_date" placeholder="Start" title="" type="text" value="{}">
|
|
|
<span class="input-group-addon"><i class="far fa-calendar-alt"></i></span>
|
|
|
</div>
|
|
|
<div class="col-md-6 input-group date" style="float:inherit">
|
|
|
<input class="form-control" id="id_end_date" name="end_date" placeholder="End" title="" type="text" value="{}">
|
|
|
<span class="input-group-addon"><i class="far fa-calendar-alt"></i></span>
|
|
|
</div>'''.format(start, end)
|
|
|
return mark_safe(html)
|
|
|
|
|
|
class TimepickerWidget(forms.widgets.TextInput):
|
|
|
def render(self, name, value, attrs=None, renderer=None):
|
|
|
input_html = super(TimepickerWidget, self).render(name, value, attrs)
|
|
|
|
|
|
html = '<div class="input-group time">'+input_html+'<span class="input-group-addon"><i class="far fa-clock"></i></span></div>'
|
|
|
return mark_safe(html)
|
|
|
|
|
|
|
|
|
class ExperimentForm(forms.ModelForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(ExperimentForm, self).__init__(*args, **kwargs)
|
|
|
#self.fields['start_time'].widget = TimepickerWidget(self.fields['start_time'].widget.attrs)
|
|
|
#self.fields['end_time'].widget = TimepickerWidget(self.fields['end_time'].widget.attrs)
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
exp = super(ExperimentForm, self).save(*args, **kwargs)
|
|
|
exp.name = exp.name.replace(' ', '')
|
|
|
exp.save()
|
|
|
return exp
|
|
|
|
|
|
class Meta:
|
|
|
model = Experiment
|
|
|
exclude = ['task', 'status', 'author', 'hash']
|
|
|
|
|
|
class ExperimentEditionForm(forms.ModelForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(ExperimentEditionForm, self).__init__(*args, **kwargs)
|
|
|
#self.fields['start_time'].widget = TimepickerWidget(self.fields['start_time'].widget.attrs)
|
|
|
#self.fields['end_time'].widget = TimepickerWidget(self.fields['end_time'].widget.attrs)
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
|
|
exp = super(ExperimentEditionForm, self).save(*args, **kwargs)
|
|
|
exp.name = exp.name.replace(' ', '')
|
|
|
exp.save()
|
|
|
return exp
|
|
|
|
|
|
class Meta:
|
|
|
model = Experiment
|
|
|
exclude = ['pedestal', 'generator', 'reception_rx', 'transmission_tx', 'task', 'status', 'author', 'hash']
|
|
|
|
|
|
|
|
|
class DeviceForm(forms.ModelForm):
|
|
|
class Meta:
|
|
|
model = Device
|
|
|
exclude = ['status']
|
|
|
|
|
|
class ConfigurationForm(forms.ModelForm):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
super(ConfigurationForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
if 'initial' in kwargs and 'experiment' in kwargs['initial'] and kwargs['initial']['experiment'] not in (0, '0'):
|
|
|
self.fields['experiment'].widget.attrs['disabled'] = 'disabled'
|
|
|
|
|
|
class Meta:
|
|
|
model = Configuration
|
|
|
exclude = ['type', 'created_date', 'programmed_date', 'parameters', 'author', 'hash']
|
|
|
|
|
|
class UploadFileForm(forms.Form):
|
|
|
|
|
|
file = forms.FileField()
|
|
|
|
|
|
class DownloadFileForm(forms.Form):
|
|
|
|
|
|
format = forms.ChoiceField(choices= ((0, 'json'),) )
|
|
|
|
|
|
def __init__(self, device_type, *args, **kwargs):
|
|
|
|
|
|
super(DownloadFileForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
self.fields['format'].choices = FILE_FORMAT
|
|
|
|
|
|
|
|
|
class NewForm(forms.Form):
|
|
|
|
|
|
# create_from = forms.ChoiceField(choices=((0, '-----'),
|
|
|
# (1, 'Empty (blank)'),
|
|
|
# (2, 'Template')))
|
|
|
# choose_template = forms.ChoiceField()
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
#template_choices = kwargs.pop('template_choices', [])
|
|
|
super(NewForm, self).__init__(*args, **kwargs)
|
|
|
#self.fields['choose_template'].choices = add_empty_choice(template_choices)
|
|
|
|
|
|
|
|
|
class FilterForm(forms.Form):
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
extra_fields = kwargs.pop('extra_fields', [])
|
|
|
super(FilterForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
for field in extra_fields:
|
|
|
if 'range_date' in field:
|
|
|
self.fields[field] = forms.CharField(required=False)
|
|
|
self.fields[field].widget = DateRangepickerWidget()
|
|
|
if 'initial' in kwargs:
|
|
|
self.fields[field].widget.attrs = {'start_date':kwargs['initial'].get('start_date', ''),
|
|
|
'end_date':kwargs['initial'].get('end_date', '')}
|
|
|
elif field in ('historical', ) or 'my ' in field:
|
|
|
self.fields[field] = forms.BooleanField(required=False)
|
|
|
else:
|
|
|
self.fields[field] = forms.CharField(required=False)
|
|
|
|
|
|
class ChangeIpForm(forms.Form):
|
|
|
|
|
|
ip_address = forms.GenericIPAddressField()
|
|
|
mask = forms.GenericIPAddressField(initial='255.255.255.0')
|
|
|
gateway = forms.GenericIPAddressField(initial='0.0.0.0')
|
|
|
dns = forms.GenericIPAddressField(initial='0.0.0.0')
|
|
|
|