def __init__(self, pynag_object, *args, **kwargs): self.pynag_object = p = pynag_object super(PynagForm, self).__init__(*args, **kwargs) # Lets find out what attributes to create object_type = pynag_object['object_type'] defined_attributes = sorted(p._defined_attributes.keys()) inherited_attributes = sorted(p._inherited_attributes.keys()) all_attributes = sorted(object_definitions.get(object_type).keys()) all_attributes += ['name', 'use', 'register'] # Special hack for macros # If this is a post and any post data looks like a nagios macro # We will generate a field for it on the fly macros = [ x for x in list(self.data.keys()) if x.startswith('$') and x.endswith('$') ] for field_name in macros: # if field_name.startswith('$ARG'): # self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') if object_type == 'service' and field_name.startswith('$_SERVICE'): self.fields[field_name] = self.get_pynagField( field_name, css_tag='defined') elif object_type == 'host' and field_name.startswith('$_HOST'): self.fields[field_name] = self.get_pynagField( field_name, css_tag='defined') # Calculate what attributes are "undefined" self.undefined_attributes = [] for i in all_attributes: if i in defined_attributes: continue if i in inherited_attributes: continue self.undefined_attributes.append(i) # Find out which attributes to show for field_name in defined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') for field_name in inherited_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag="inherited") for field_name in self.undefined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='undefined') # If no initial values were provided, use the one in our pynag object. if not self.initial: self.initial = pynag_object._defined_attributes self.initial.update(pynag_object._changes) for name, field in list(self.fields.items()): if name in self.initial: field.initial = self.initial[name] elif name in self.pynag_object: field.initial = self.pynag_object[name]
def __init__(self, pynag_object ,*args, **kwargs): self.pynag_object = pynag_object super(AdvancedEditForm,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 help_text = "" if field_name in object_definitions[object_type]: help_text=object_definitions[object_type][field_name].get('help_text', "No help available for this item") self.fields[field_name] = forms.CharField(required=False,label=field_name, help_text=help_text) self.fields.keyOrder = sorted( self.fields.keys() )
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 help_text = "" if field_name in object_definitions[object_type]: help_text=object_definitions[object_type][field_name].get('help_text', "No help available for this item") self.fields[field_name] = forms.CharField(required=False,label=field_name, help_text=help_text) self.fields.keyOrder = sorted( self.fields.keys() )
def __init__(self, pynag_object, *args, **kwargs): self.pynag_object = p = pynag_object super(PynagForm, self).__init__(*args, **kwargs) # Lets find out what attributes to create object_type = pynag_object['object_type'] defined_attributes = sorted(p._defined_attributes.keys()) inherited_attributes = sorted(p._inherited_attributes.keys()) all_attributes = sorted(object_definitions.get(object_type).keys()) all_attributes += ['name', 'use', 'register'] # Special hack for macros # If this is a post and any post data looks like a nagios macro # We will generate a field for it on the fly macros = filter(lambda x: x.startswith('$') and x.endswith('$'), self.data.keys()) for field_name in macros: # if field_name.startswith('$ARG'): # self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') if object_type == 'service' and field_name.startswith('$_SERVICE'): self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') elif object_type == 'host' and field_name.startswith('$_HOST'): self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') # Calculate what attributes are "undefined" self.undefined_attributes = [] for i in all_attributes: if i in defined_attributes: continue if i in inherited_attributes: continue self.undefined_attributes.append(i) # Find out which attributes to show for field_name in defined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') for field_name in inherited_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag="inherited") for field_name in self.undefined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='undefined') # If no initial values were provided, use the one in our pynag object. if not self.initial: self.initial = pynag_object._defined_attributes self.initial.update(pynag_object._changes) for name, field in self.fields.items(): if name in self.initial: field.initial = self.initial[name] elif name in self.pynag_object: field.initial = self.pynag_object[name]
def __init__(self, pynag_object, *args, **kwargs): self.pynag_object = pynag_object super(PynagForm, 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()) inherited_attributes = sorted( self.pynag_object._inherited_attributes.keys()) all_attributes = sorted(object_definitions.get(object_type).keys()) all_attributes += ['name', 'use', 'register'] # Special hack for macros # If this is a post and any post data looks like a nagios macro # We will generate a field for it on the fly macros = filter(lambda x: x.startswith('$') and x.endswith('$'), self.data.keys()) for field_name in macros: # if field_name.startswith('$ARG'): # self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') if object_type == 'service' and field_name.startswith('$_SERVICE'): self.fields[field_name] = self.get_pynagField( field_name, css_tag='defined') elif object_type == 'host' and field_name.startswith('$_HOST'): self.fields[field_name] = self.get_pynagField( field_name, css_tag='defined') # Calculate what attributes are "undefined" self.undefined_attributes = [] for i in all_attributes: if i in defined_attributes: continue if i in inherited_attributes: continue self.undefined_attributes.append(i) # Find out which attributes to show for field_name in defined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') for field_name in inherited_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag="inherited") for field_name in self.undefined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='undefined') return
def __init__(self, pynag_object ,*args, **kwargs): self.pynag_object = pynag_object 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() ) inherited_attributes = sorted( self.pynag_object._inherited_attributes.keys() ) all_attributes = sorted( object_definitions.get(object_type).keys() ) all_attributes += ['name','use','register'] # Calculate what attributes are "undefined" self.undefined_attributes = [] for i in all_attributes: if i in defined_attributes: continue if i in inherited_attributes: continue self.undefined_attributes.append( i ) # Find out which attributes to show for field_name in defined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='defined') for field_name in inherited_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag="inherited") for field_name in self.undefined_attributes: self.fields[field_name] = self.get_pynagField(field_name, css_tag='undefined') return
def get_pynagField(self, field_name, css_tag="", required=None): """ Takes a given field_name and returns a forms.Field that is appropriate for this field Arguments: field_name -- Name of the field to add, example "host_name" css_tag -- String will make its way as a css attribute in the resulting html required -- If True, make field required. If None, let pynag decide """ # 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. # 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 = sorted( map(lambda x: (x.contactgroup_name, x.contactgroup_name), all_groups) ) field = PynagChoiceField(choices=choices, inline_help_text="No %s selected" % (field_name)) elif field_name == 'use': all_objects = self.pynag_object.objects.filter(name__contains='') choices = map(lambda x: (x.name, x.name), all_objects) field = PynagChoiceField(choices=sorted(choices), inline_help_text="No %s selected" % (field_name)) elif field_name in ('servicegroups','servicegroup_members'): all_groups = Model.Servicegroup.objects.filter(servicegroup_name__contains='') choices = map(lambda x: (x.servicegroup_name, x.servicegroup_name), all_groups) field = PynagChoiceField(choices=sorted(choices), inline_help_text="No %s selected" % (field_name)) elif field_name in ('hostgroups', 'hostgroup_members'): all_groups = Model.Hostgroup.objects.filter(hostgroup_name__contains='') choices = map(lambda x: (x.hostgroup_name, x.hostgroup_name), all_groups) field = PynagChoiceField(choices=sorted(choices), inline_help_text="No %s selected" % (field_name)) elif field_name == 'members' and object_type == 'hostgroup': all_groups = Model.Host.objects.filter(host_name__contains='') choices = map(lambda x: (x.host_name, x.host_name), all_groups) field = PynagChoiceField(choices=sorted(choices), inline_help_text="No %s selected" % (field_name)) elif field_name == 'host_name' and object_type == 'service': all_groups = Model.Host.objects.filter(host_name__contains='') choices = map(lambda x: (x.host_name, x.host_name), all_groups) field = PynagChoiceField(choices=sorted(choices), inline_help_text="No %s selected" % (field_name)) elif field_name in ('contacts','members'): all_objects = Model.Contact.objects.filter(contact_name__contains='') choices = map(lambda x: (x.contact_name, x.contact_name), all_objects) field = PynagChoiceField(choices=sorted(choices),inline_help_text="No %s selected" % (field_name)) elif field_name.endswith('_period'): all_objects = Model.Timeperiod.objects.filter(timeperiod_name__contains='') choices = [('','')] + map(lambda x: (x.timeperiod_name, x.timeperiod_name), all_objects) field = forms.ChoiceField(choices=sorted(choices)) elif field_name.endswith('notification_commands'): all_objects = Model.Command.objects.filter(command_name__contains='') choices = [('','')] + map(lambda x: (x.command_name, x.command_name), all_objects) field = forms.ChoiceField(choices=sorted(choices)) #elif field_name == 'check_command': # all_objects = Model.Command.objects.all # choices = [('','')] + map(lambda x: (x.command_name, x.command_name), all_objects) # field = forms.ChoiceField(choices=sorted(choices)) elif field_name.endswith('notification_options') and self.pynag_object.object_type =='host': field = PynagChoiceField(choices=HOST_NOTIFICATION_OPTIONS,inline_help_text="No %s selected" % (field_name)) elif field_name.endswith('notification_options') and self.pynag_object.object_type =='service': field = PynagChoiceField(choices=SERVICE_NOTIFICATION_OPTIONS,inline_help_text="No %s selected" % (field_name)) elif options.get('value') == '[0/1]': field = forms.CharField(widget=PynagRadioWidget) # Lets see if there is any help text available for our field if field_name in object_definitions[object_type]: help_text=object_definitions[object_type][field_name].get('help_text', "No help available for this item") field.help_text = help_text # No prettyprint for macros if field_name.startswith('_'): field.label = field_name # If any CSS tag was given, add it to the widget self.add_css_tag(field=field, css_tag=css_tag) if options.has_key('required'): self.add_css_tag(field=field, css_tag=options['required']) field.required = options['required'] == 'required' else: field.required = False # At the moment, our database of required objects is incorrect # So if caller did not specify if field is required, we will not # make it required if required is None: field.required = False else: field.required = required # Put inherited value in the placeholder inherited_value = self.pynag_object._inherited_attributes.get(field_name) if inherited_value is not None: self.add_placeholder(field, '%s (inherited from template)' % (inherited_value)) if field_name in MULTICHOICE_FIELDS: self.add_css_tag(field=field, css_tag="multichoice") return field
def get_pynagField(self, field_name, css_tag="", required=None): """ Takes a given field_name and returns a forms.Field that is appropriate for this field Arguments: field_name -- Name of the field to add, example "host_name" css_tag -- String will make its way as a css attribute in the resulting html required -- If True, make field required. If None, let pynag decide """ # 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. # Lets assume charfield in the beginning field = forms.CharField() if False is True: pass elif field_name in ('contact_groups', 'contactgroups', 'contactgroup_members'): field = PynagAutoCompleteField(object_type='contactgroup', complete='shortname') elif field_name == 'use': field = PynagAutoCompleteField(object_type=object_type, complete="name") elif field_name in ('servicegroups', 'servicegroup_members'): field = PynagAutoCompleteField(object_type='servicegroup') elif field_name in ('hostgroups', 'hostgroup_members', 'hostgroup_name') and object_type != 'hostgroup': field = PynagAutoCompleteField(object_type='hostgroup') elif field_name == 'members' and object_type == 'hostgroup': field = PynagAutoCompleteField(object_type='host') elif field_name == 'host_name' and object_type == 'service': field = PynagAutoCompleteField(object_type='host') elif field_name in ('contacts', 'members'): field = PynagAutoCompleteField(object_type='contact') elif field_name.endswith('_period'): all_objects = Model.Timeperiod.objects.filter( timeperiod_name__contains='') choices = [('', '')] + map( lambda x: (x.timeperiod_name, x.timeperiod_name), all_objects) field = forms.ChoiceField(choices=sorted(choices)) elif field_name.endswith('notification_commands'): all_objects = Model.Command.objects.filter( command_name__contains='') choices = [('', '')] + map( lambda x: (x.command_name, x.command_name), all_objects) field = PynagChoiceField(choices=sorted(choices)) # elif field_name == 'check_command': # all_objects = Model.Command.objects.all # choices = [('','')] + map(lambda x: (x.command_name, x.command_name), all_objects) # field = forms.ChoiceField(choices=sorted(choices)) elif field_name.endswith('notification_options' ) and self.pynag_object.object_type == 'host': field = PynagChoiceField( choices=HOST_NOTIFICATION_OPTIONS, inline_help_text=_("No %(field_name)s selected") % {'field_name': field_name}) elif field_name.endswith( 'notification_options' ) and self.pynag_object.object_type == 'service': field = PynagChoiceField( choices=SERVICE_NOTIFICATION_OPTIONS, inline_help_text=_("No %(field_name)s selected") % {'field_name': field_name}) elif options.get('value') == '[0/1]': field = forms.CharField(widget=PynagRadioWidget) # Lets see if there is any help text available for our field if field_name in object_definitions[object_type]: help_text = object_definitions[object_type][field_name].get( 'help_text', _("No help available for this item")) field.help_text = help_text # No prettyprint for macros if field_name.startswith('_'): field.label = field_name # If any CSS tag was given, add it to the widget self.add_css_tag(field=field, css_tag=css_tag) if 'required' in options: self.add_css_tag(field=field, css_tag=options['required']) field.required = options['required'] == 'required' else: field.required = False # At the moment, our database of required objects is incorrect # So if caller did not specify if field is required, we will not # make it required if required is None: field.required = False else: field.required = required # Put inherited value in the placeholder inherited_value = self.pynag_object._inherited_attributes.get( field_name) if inherited_value is not None: self.add_placeholder( field, _('%(inherited_value)s (inherited from template)') % {'inherited_value': inherited_value}) if field_name in MULTICHOICE_FIELDS: self.add_css_tag(field=field, css_tag="multichoice") return field