##// END OF EJS Templates
test svn 2...
test svn 2 git-svn-id: http://jro-dev.igp.gob.pe/svn/jro_hard/radarsys/trunk/webapp@177 aa17d016-51d5-4e8b-934c-7b2bbb1bbe71

File last commit:

r119:a764d9ef7917
r156:8d9f2b4cb89f
Show More
widgets.py
282 lines | 11.3 KiB | text/x-python | PythonLexer
Juan C. Espinoza
- Update rc app...
r79
import ast
import json
Juan C. Espinoza
Update RC app (add support for mix configurations, bug plotting window line, )...
r107 from itertools import chain
Juan C. Espinoza
- Update rc app...
r79
from django import forms
from django.utils.safestring import mark_safe
Juan C. Espinoza
Update RC app (add support for mix configurations, bug plotting window line, )...
r107 from django.utils.encoding import force_unicode
from django.utils.html import conditional_escape
Juan C. Espinoza
- Update rc app...
r79
class KmUnitWidget(forms.widgets.TextInput):
def render(self, label, value, attrs=None):
if isinstance(value, (int, float)):
unit = int(value*attrs['km2unit'])
elif isinstance(value, basestring):
units = []
values = [s for s in value.split(',') if s]
for val in values:
units.append('{0:.0f}'.format(float(val)*attrs['km2unit']))
unit = ','.join(units)
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
Juan C. Espinoza
- Fix input form for delays in RCLine...
r119 if attrs['id'] in ('id_delays',):
input_type = 'text'
else:
input_type = 'number'
Juan C. Espinoza
- Update rc app...
r79
if 'line' in attrs:
label += '_{0}'.format(attrs['line'].pk)
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 html = '''<div class="col-md-12 col-no-padding">
Juan C. Espinoza
- Fix input form for delays in RCLine...
r119 <div class="col-md-5 col-no-padding"><input type="{0}" step="any" {1} class="form-control" id="id_{2}" name="{3}" value="{4}"></div>
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-1 col-no-padding">Km</div>
Juan C. Espinoza
- Fix input form for delays in RCLine...
r119 <div class="col-md-5 col-no-padding"><input type="{0}" {1} class="form-control" id="id_{2}_unit" value="{5}"></div>
<div class="col-md-1 col-no-padding">Units</div></div><br>'''.format(input_type, disabled, label, name, value, unit)
Juan C. Espinoza
- Update rc app...
r79
script = '''<script type="text/javascript">
$(document).ready(function () {{
km_fields.push("id_{label}");
unit_fields.push("id_{label}_unit");
$("#id_{label}").change(function() {{
Juan C. Espinoza
- Fix input form for delays in RCLine...
r119 $("#id_{label}_unit").val(str2unit($(this).val()));
Juan C. Espinoza
- Update rc app...
r79 $("#id_{label}").val(str2km($("#id_{label}_unit").val()));
}});
$("#id_{label}_unit").change(function() {{
Juan C. Espinoza
- Fix input form for delays in RCLine...
r119 $(this).val(parseFloat($(this).val()));
Juan C. Espinoza
- Update rc app...
r79 $("#id_{label}").val(str2km($(this).val()));
}});
}});
</script>'''.format(label=label)
if disabled:
return mark_safe(html)
else:
return mark_safe(html+script)
class UnitKmWidget(forms.widgets.TextInput):
def render(self, label, value, attrs=None):
if isinstance(value, (int, float)):
km = value/attrs['km2unit']
elif isinstance(value, basestring):
kms = []
values = [s for s in value.split(',') if s]
for val in values:
kms.append('{0:.0f}'.format(float(val)/attrs['km2unit']))
km = ','.join(kms)
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
if 'line' in attrs:
label += '_{0}'.format(attrs['line'].pk)
html = '''<div class="col-md-12 col-no-padding">
<div class="col-md-5 col-no-padding"><input type="number" {0} class="form-control" id="id_{1}_unit" name="{2}" value="{3}"></div>
<div class="col-md-1 col-no-padding">Units</div>
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-5 col-no-padding"><input type="number" step="any" {4} class="form-control" id="id_{5}" value="{6}"></div>
Juan C. Espinoza
- Update rc app...
r79 <div class="col-md-1 col-no-padding">Km</div></div>'''.format(disabled, label, name, value, disabled, label, km)
script = '''<script type="text/javascript">
$(document).ready(function () {{
km_fields.push("id_{label}");
unit_fields.push("id_{label}_unit");
$("#id_{label}").change(function() {{
$("#id_{label}_unit").val(str2unit($(this).val()));
}});
$("#id_{label}_unit").change(function() {{
$("#id_{label}").val(str2km($(this).val()));
}});
}});
</script>'''.format(label=label)
if disabled:
return mark_safe(html)
else:
return mark_safe(html+script)
class KmUnitHzWidget(forms.widgets.TextInput):
def render(self, label, value, attrs=None):
unit = float(value)*attrs['km2unit']
if unit%10==0:
unit = int(unit)
hz = 150000*float(value)**-1
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
if 'line' in attrs:
label += '_{0}'.format(attrs['line'].pk)
html = '''<div class="col-md-12 col-no-padding">
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-3 col-no-padding"><input type="number" step="any" {0} class="form-control" id="id_{1}" name="{2}" value="{3}"></div>
Juan C. Espinoza
- Update rc app...
r79 <div class="col-md-1 col-no-padding">Km</div>
<div class="col-md-3 col-no-padding"><input type="number" {4} class="form-control" id="id_{1}_unit" value="{5}"></div>
<div class="col-md-1 col-no-padding">Units</div>
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-3 col-no-padding"><input type="number" step="any" {4} class="form-control" id="id_{1}_hz" value="{6}"></div>
Juan C. Espinoza
- Update rc app...
r79 <div class="col-md-1 col-no-padding">Hz</div>
</div>'''.format(disabled, label, name, value, disabled, unit, hz)
script = '''<script type="text/javascript">
$(document).ready(function () {{
km_fields.push("id_{label}");
unit_fields.push("id_{label}_unit");
$("#id_{label}").change(function() {{
$("#id_{label}_unit").val(str2unit($(this).val()));
$("#id_{label}_hz").val(str2hz($(this).val()));
updateDc();
}});
$("#id_{label}_unit").change(function() {{
$(this).val(Math.round(parseFloat($(this).val())/10)*10);
$("#id_{label}").val(str2km($(this).val()));
$("#id_{label}_hz").val(str2hz($("#id_{label}").val()));
updateDc();
}});
$("#id_{label}_hz").change(function() {{
$("#id_{label}").val(str2hz($(this).val()));
$("#id_{label}_unit").val(str2unit($("#id_{label}").val()));
updateDc();
}});
}});
</script>'''.format(label=label)
if disabled:
return mark_safe(html)
else:
return mark_safe(html+script)
class KmUnitDcWidget(forms.widgets.TextInput):
def render(self, label, value, attrs=None):
unit = int(float(value)*attrs['km2unit'])
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
label += '_{0}'.format(attrs['line'].pk)
Juan C. Espinoza
Update RC app (add support for mix configurations, bug plotting window line, )...
r107 dc = float(json.loads(attrs['line'].params)['pulse_width'])*100/attrs['line'].rc_configuration.ipp
Juan C. Espinoza
- Update rc app...
r79
html = '''<div class="col-md-12 col-no-padding">
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-3 col-no-padding"><input type="number" step="any" {0} class="form-control" id="id_{1}" name="{2}" value="{3}"></div>
Juan C. Espinoza
- Update rc app...
r79 <div class="col-md-1 col-no-padding">Km</div>
<div class="col-md-3 col-no-padding"><input type="number" {4} class="form-control" id="id_{1}_unit" value="{5}"></div>
<div class="col-md-1 col-no-padding">Units</div>
Juan C. Espinoza
- Add sequence mode in mix configurations....
r116 <div class="col-md-3 col-no-padding"><input type="number" step="any" {4} class="form-control" id="id_{1}_dc" value="{6}"></div>
Juan C. Espinoza
- Update rc app...
r79 <div class="col-md-1 col-no-padding">DC[%]</div>
</div>'''.format(disabled, label, name, value, disabled, unit, dc)
script = '''<script type="text/javascript">
$(document).ready(function () {{
km_fields.push("id_{label}");
unit_fields.push("id_{label}_unit");
dc_fields.push("id_{label}");
$("#id_{label}").change(function() {{
$("#id_{label}_unit").val(str2unit($(this).val()));
$("#id_{label}_dc").val(str2dc($("#id_{label}").val()));
}});
$("#id_{label}_unit").change(function() {{
$("#id_{label}").val(str2km($(this).val()));
$("#id_{label}_dc").val(str2dc($("#id_{label}").val()));
}});
$("#id_{label}_dc").change(function() {{
Juan C. Espinoza
Update RC app (add support for mix configurations, bug plotting window line, )...
r107 $("#id_{label}").val(parseFloat($(this).val())*100/parseFloat($("#id_ipp").val()));
Juan C. Espinoza
- Update rc app...
r79 $("#id_{label}_unit").val(str2unit($("#id_{label}").val()));
}});
}});
</script>'''.format(label=label)
if disabled:
return mark_safe(html)
else:
return mark_safe(html+script)
class DefaultWidget(forms.widgets.TextInput):
def render(self, label, value, attrs=None):
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
html = '<div class="col-md-12 col-no-padding"><div class="col-md-5 col-no-padding"><input {0} type="text" class="form-control" id="id_{1}" name="{2}" value="{3}"></div></div>'.format(disabled, label, name, value)
return mark_safe(html)
class HiddenWidget(forms.widgets.HiddenInput):
def render(self, label, value, attrs=None):
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = self.attrs.get('name', label)
html = '<input {0} type="hidden" class="form-control" id="id_{1}" name="{2}" value="{3}">'.format(disabled, label, name, value)
return mark_safe(html)
class CodesWidget(forms.widgets.Textarea):
def render(self, label, value, attrs=None):
disabled = 'disabled' if attrs.get('disabled', False) else ''
name = attrs.get('name', label)
if '[' in value:
value = ast.literal_eval(value)
if isinstance(value, list):
codes = '\r\n'.join(value)
else:
codes = value
html = '<textarea rows="5" {0} class="form-control" id="id_{1}" name="{2}" style="white-space:nowrap; overflow:scroll;">{3}</textarea>'.format(disabled, label, name, codes)
return mark_safe(html)
Juan C. Espinoza
Update RC app (add support for mix configurations, bug plotting window line, )...
r107
class HCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, name=name)
output = [u'<br><ul>']
# Normalize to strings
str_values = set([force_unicode(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
output.append(u'<span><label%s>%s %s</label></span>' % (label_for, rendered_cb, option_label))
output.append(u'</div><br>')
return mark_safe(u'\n'.join(output))
Juan C. Espinoza
- Update rc app...
r79