Skip to content

Commit

Permalink
refactoring and bugfixes related to advanced_form
Browse files Browse the repository at this point in the history
* advanced_form is no a seperate class AdvancedEditForm(forms.Form)
* templates changed to actually post into the advanced form instead of regular one
* try/expect put around gitlog in object_list_types
* clean_data is now 10 times more efficient for advanced_form
  • Loading branch information
Pall Sigurdsson committed Aug 9, 2012
1 parent 10a3aa9 commit 46888b6
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 101 deletions.
114 changes: 75 additions & 39 deletions adagios/objectbrowser/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def clean(self,value):
for i in value:
if i not in tmp:
tmp.append(i)
return self.__prefix + tmp
value = self.__prefix + ','.join(tmp)
return value
def prepare_value(self, value):
"""
Takes a comma separated string, removes + if it is prefixed so. Returns a list
Expand Down Expand Up @@ -90,35 +91,35 @@ class PynagForm(forms.Form):
name = forms.CharField(required=False, label="Object Name")
use = forms.CharField(required=False, label="Use")
def clean(self):
for k,v in self.cleaned_data.items():
if k in MULTICHOICE_FIELDS and self.simple == False:
cleaned_data = super(self.__class__, self).clean()
for k,v in cleaned_data.items():
if k in MULTICHOICE_FIELDS:
# Put the + back in there if needed
if self.pynag_object.get(k,'').startswith('+'):
v = self.cleaned_data[k] = "+%s"%(v)
# TODO: What are we doing in the following line?
if k not in self.data.keys(): self.cleaned_data.pop(k)
# If value is empty and attribute was never defined to begin with
elif k in self.undefined_attributes and v == '': self.cleaned_data.pop(k)
# If value is the same as it was before
elif v == self.pynag_object[k]: self.cleaned_data.pop(k)
# If value is empty and pynag object is defines as None
elif v == '' and self.pynag_object[k] is None: self.cleaned_data.pop(k)
# If we get here, something is supposed to be modified
else:
self.cleaned_data[k] = smart_str(v)
return self.cleaned_data
v = cleaned_data[k] = "+%s"%(v)
return cleaned_data
def save(self):
for k,v in self.cleaned_data.items():
k,v = str(k), str(v)
if self.pynag_object[k] != v:
self.pynag_object[k] = v
self.fields[k] = self.get_pynagField(k, css_tag="defined_attribute")
self.fields[k].value = v
for k in self.changed_data:
# Ignore fields that did not appear in the POST at all
if k not in self.data and k not in MULTICHOICE_FIELDS:
continue
# If value is empty, we assume it is to be removed
value = self.cleaned_data[k]
if value == '':
value = None
# Sometimes attributes slide in changed_data without having
# been modified, lets ignore those
if self.pynag_object[k] == value:
continue
# If we reach here, it is save to modify our pynag object.
self.pynag_object[k] = value
# Additionally, update the field for the return form
self.fields[k] = self.get_pynagField(k, css_tag="defined_attribute")
self.fields[k].value = value
self.pynag_object.save()
def __init__(self, pynag_object, simple=False,*args, **kwargs):
def __init__(self, pynag_object ,*args, **kwargs):
self.pynag_object = pynag_object
self.simple = simple
super(forms.Form,self).__init__(*args, **kwargs)
super(self.__class__,self).__init__(*args, **kwargs)
# Lets find out what attributes to create
object_type = pynag_object['object_type']
defined_attributes = sorted( self.pynag_object._defined_attributes.keys() )
Expand All @@ -132,22 +133,21 @@ def __init__(self, pynag_object, simple=False,*args, **kwargs):
self.undefined_attributes.append( i )
# Find out which attributes to show
for field_name in defined_attributes + inherited_attributes + self.undefined_attributes:
if self.simple:
self.fields[field_name] = self.get_pynagField(field_name)
else:
self.fields[field_name] = self.get_pynagField(field_name)
self.fields[field_name] = self.get_pynagField(field_name)
return
def get_pynagField(self, field_name, css_tag=""):
""" Takes a given field_name and returns a forms.Field that is appropriate for this field """
# Lets figure out what type of field this is, default to charfield
object_type = self.pynag_object['object_type']
definitions = object_definitions.get( object_type ) or {}
options = definitions.get(field_name) or {}

# Find out what type of field to create from the field_name
# If this is the advanved_edit form, then all fields are charfields
if self.simple == True:
field = forms.CharField()

# Find out what type of field to create from the field_name.
# Lets assume charfield in the beginning
field = forms.CharField()

if False == True:
pass
elif field_name in ('contact_groups','contactgroups','contactgroup_members'):
all_groups = Model.Contactgroup.objects.filter(contactgroup_name__contains="")
choices = map(lambda x: (x.contactgroup_name, x.contactgroup_name), all_groups)
Expand Down Expand Up @@ -180,10 +180,7 @@ def get_pynagField(self, field_name, css_tag=""):
field = PynagChoiceField(choices=NOTIFICATION_OPTIONS)
elif options.get('value') == '[0/1]':
field = forms.CharField(widget=PynagRadioWidget)
else:
# Fallback to a default charfield
field = forms.CharField()


# No prettyprint for macros
if field_name.startswith('_'):
field.label = field_name
Expand All @@ -210,7 +207,46 @@ def add_css_tag(self, field, css_tag):
field.css_tag = ''
field.widget.attrs['class'] += " " + css_tag
field.css_tag += " " + css_tag



class AdvancedEditForm(forms.Form):
""" A form for pynag.Model.Objectdefinition
This form will display a charfield for every attribute of the objectdefinition
"Every" attribute means:
* Every defined attribute
* Every inherited attribute
* Every attribute that is defined in nagios object definition html
"""
register = forms.CharField(required=False)
name = forms.CharField(required=False, label="Object Name")
use = forms.CharField(required=False, label="Use")
__prefix = "advanced" # This prefix will go on every field
def save(self):
for k in self.changed_data:
value = self.cleaned_data[k]
# same as original, lets ignore that
if self.pynag_object[k] == value:
continue
if value == '':
value = None

# If we reach here, it is save to modify our pynag object.
self.pynag_object[k] = value
self.pynag_object.save()
def __init__(self, pynag_object ,*args, **kwargs):
self.pynag_object = pynag_object
super(self.__class__,self).__init__(*args, prefix=self.__prefix, **kwargs)

# Lets find out what attributes to create
object_type = pynag_object['object_type']
all_attributes = sorted( object_definitions.get(object_type).keys() )
for field_name in self.pynag_object.keys() + all_attributes:
if field_name == 'meta': continue
self.fields[field_name] = forms.CharField(required=False,label=field_name)

class GeekEditObjectForm(forms.Form):
definition= forms.CharField( widget=forms.Textarea(attrs={ 'wrap':'off', 'cols':'80'}) )
def __init__(self,pynag_object=None, *args,**kwargs):
Expand Down
36 changes: 11 additions & 25 deletions adagios/objectbrowser/templates/edit_contact.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<li><a href="#notifications" data-toggle="tab">Notifications</a></li>
<li><a href="#advanced" data-toggle="tab">Advanced</a></li>
</ul>
<form class="form-horizontal" action="#" method="POST">
{% csrf_token %}
<div class="tab-content">
<div class="tab-content">
<form class="form-horizontal" action="#" method="POST">
{% csrf_token %}
<div class="tab-pane" id="general">
<fieldset>

Expand Down Expand Up @@ -123,34 +123,20 @@
</div>

</div>
</form>
<div class="tab-pane" id="advanced">
<form action="#" method="post">

{% csrf_token %}
{% for field in form %}


<fieldset>
<label class="control-label" for="{{ field.name }}">{{ field.label }}{% if field.help %} <abbr rel="tooltip" title="{{ field.label }}" data-content="{{ field.help_text }}"><i class="icon-question-sign"></i></abbr> {% endif %}</label>
<div class="controls" id="advanced_field">
{{ field }}
</div>
</fieldset>
<!--
<div class="{{ field.field.widget.attrs.class }}">
<p><label> {{ field.label }} </label>
{{ field }} {{ field.errors }} {{ field.help_text }} </p>
</div> -->
{% endfor %}
<!-- All Attributes -->
<h2>All Attributes</h2>
<form action="{% url objectbrowser.views.advanced_edit my_object.id %}" method="post" class="form-horizontal">{% csrf_token %}
{% include "bootstrap_fields.html" with fields=advanced_form %}
<div class="form-actions">
<button class="btn btn-primary" type="submit">Save changes</button>
<button class="btn">Cancel</button>
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
</form>
</div>
</div>

</form>


{% else %}
<p>Object not found </p>
Expand Down
13 changes: 12 additions & 1 deletion adagios/objectbrowser/templates/edit_host.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,19 @@
<button class="btn">Cancel</button>
</div>
</div>
</div>
</form>
<div class="tab-pane" id="advanced">
<!-- All Attributes -->
<h2>All Attributes</h2>
<form action="{% url objectbrowser.views.advanced_edit my_object.id %}" method="post" class="form-horizontal">{% csrf_token %}
{% include "bootstrap_fields.html" with fields=advanced_form %}
<div class="form-actions">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
</div> <!-- End of advanced tab -->

</div>
{% else %}
<p>Object not found </p>
{% endif %}
Expand Down
21 changes: 1 addition & 20 deletions adagios/objectbrowser/templates/edit_object.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,19 @@
<ul class="nav nav-tabs" id="objecttab">
<li><a href="#advanced" data-toggle="tab">Advanced</a></li>
</ul>
<form class="form-horizontal" action="#" method="POST">
{% csrf_token %}
<div class="tab-content">

<div class="tab-pane" id="advanced">
<!-- All Attributes -->
<h2>All Attributes</h2>
<form action="#" method="post" class="form-horizontal">{% csrf_token %}
<form action="{% url objectbrowser.views.advanced_edit my_object.id %}" method="post" class="form-horizontal">{% csrf_token %}
{% include "bootstrap_fields.html" with fields=advanced_form %}
<div class="form-actions">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>


<!-- Display all members of a specific group -->
{% if effective_members %}
<div class="effective_members">
<fieldset id="id_effective_members" class="attribute_{{object_type}}">
<legend>Effective Members</legend>
{% for m in effective_members %}
<p>
<label><a href="id={{ m.id }}">{{m.description}}</a></label>
</p>
{% endfor %}
</fieldset>
</div>
{% endif %}

</div> <!-- End of advanced tab -->
</div>
</form>

{% else %}
<p>Object not found </p>
Expand Down
16 changes: 8 additions & 8 deletions adagios/objectbrowser/templates/edit_service.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<li><a href="#notifications" data-toggle="tab">Notifications</a></li>
<li><a href="#advanced" data-toggle="tab">Advanced</a></li>
</ul>
<form class="form-horizontal" action="#" method="POST">
{% csrf_token %}
<div class="tab-content">
<div class="tab-content">


<div class="tab-pane" id="general">
<fieldset>
<form class="form-horizontal" action="#" method="POST">{% csrf_token %} <fieldset>

<label class="control-label" for="host_name" rel="tooltip" title="{{ form.host_name.label }}" data-content="eg web01.example.com">{{ form.host_name.label }}</label>
<div class="controls"> <!-- class inherited -->
Expand Down Expand Up @@ -109,7 +109,6 @@
</div>
</div>


<div class="tab-pane" id="notifications">
<fieldset>

Expand Down Expand Up @@ -150,17 +149,18 @@
</div>
</div>

</form>

<div class="tab-pane" id="advanced">
<form action="#" method="post" class="form-horizontal">{% csrf_token %}
<form action="{% url objectbrowser.views.advanced_edit my_object.id %}" method="post" class="form-horizontal">{% csrf_token %}
{% include "bootstrap_fields.html" with fields=advanced_form %}
<div class="form-actions">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>
</div>
</div>
</form>
</div>

{% else %}
<p>Object not found </p>
{% endif %}
Expand Down
2 changes: 1 addition & 1 deletion adagios/objectbrowser/templates/edit_timeperiod.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="tab-content">

<div class="tab-pane" id="timeperiod">
<form action="#" method="post">
<form action="{% url objectbrowser.views.geek_edit my_object.id %}" method="post">

{% csrf_token %}
<p>
Expand Down

0 comments on commit 46888b6

Please sign in to comment.