File diff 9a15715b4674 → 529cb3e7db9b
conntrackt/forms.py
Show inline comments
 
# Django imports.
 
from django.forms import ModelForm
 
from django.forms.models import inlineformset_factory
 

	
 
# Application imports.
 
from .models import Entity, Interface, Communication
 

	
 

	
 
class EntityForm(ModelForm):
 
class WidgetCSSClassFormMixin(object):
 
    """
 
    Helper form mixin that can be used for applying additional custom CSS
 
    classes to form field widgets.
 

	
 
    The mixin accepts the following class options:
 

	
 
        widget_css_classes - Dictionary describing which additional CSS classes
 
        should be applied to which form widget. Dictinoary keys should be equal
 
        to form widget names, while the value should be a string containing the
 
        extra CSS classes that should be applied to it. In order to apply the
 
        CSS classes to every widget of the form, use the key "ALL"
 
    """
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Assigns the custom CSS classes form widgets, as configured by the
 
        widget_css_classes property.
 
        """
 

	
 
        super(WidgetCSSClassFormMixin, self).__init__(*args, **kwargs)
 

	
 
        for field_name, css_class in self.widget_css_classes.iteritems():
 
            if field_name == "ALL":
 
                for field in self.fields.values():
 
                    if "class" in field.widget.attrs:
 
                        field.widget.attrs["class"] += " " + css_class
 
                    else:
 
                        field.widget.attrs["class"] = css_class
 
            else:
 
                field = self.fields[field_name]
 
                if "class" in field.widget.attrs:
 
                    field.widget.attrs["class"] += " " + css_class
 
                else:
 
                    field.widget.attrs["class"] = css_class
 

	
 

	
 
class PlaceholderFormMixin(object):
 
    """
 
    Helper form mixin that can be used to set-up placeholders for text widgets.
 

	
 
    The mixin accepts the following class options:
 

	
 
        widget_placeholders - Dictionary describing which placeholders should be
 
        applied to which widgets. Keys should be equal to form widget names,
 
        while the values should be the strings that should be set as
 
        placeholders.
 
    """
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Assigns the placeholders to text form widgets, as configured by the
 
        widget_placeholders property.
 
        """
 

	
 
        super(PlaceholderFormMixin, self).__init__(*args, **kwargs)
 

	
 
        for field_name, placeholder in self.widget_placeholders.iteritems():
 
            self.fields[field_name].widget.attrs["placeholder"] = placeholder
 

	
 

	
 
class EntityForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
 
    """
 
    Implements a custom model form for entities with some styling changes.
 
    """
 
@@ -14,24 +74,12 @@ class EntityForm(ModelForm):
 
    class Meta:
 
        model = Entity
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Initialises the form instance. Sets-up some bootstrap CSS classes for
 
        widgets.
 
        """
 

	
 
        super(EntityForm, self).__init__(*args, **kwargs)
 

	
 
        # Update the widgets to be wider.
 
        for field_name, field in self.fields.iteritems():
 
            field.widget.attrs["class"] = "span6"
 

	
 
        # Set-up some placeholders.
 
        self.fields["name"].widget.attrs["placeholder"] = "Entity name"
 
        self.fields["description"].widget.attrs["placeholder"] = "Entity description"
 
    widget_placeholders = {"name": "Entity name",
 
                           "description": "Entity description"}
 
    widget_css_classes = {"ALL": "span6"}
 

	
 

	
 
class InterfaceForm(ModelForm):
 
class InterfaceForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
 
    """
 
    Implements a custom model form for interfaces with some styling changes.
 
    """
 
@@ -39,26 +87,15 @@ class InterfaceForm(ModelForm):
 
    class Meta:
 
        model = Interface
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Initialises the form instance. Sets-up some bootstrap CSS classes for
 
        widgets.
 
        """
 

	
 
        super(InterfaceForm, self).__init__(*args, **kwargs)
 
    widget_placeholders = {"name": "Interface name",
 
                           "description": "Interface description",
 
                           "address": "IP address of interface",
 
                           "netmask": "IP address netmask"}
 

	
 
        # Update the widgets to be wider.
 
        for field_name, field in self.fields.iteritems():
 
            field.widget.attrs["class"] = "span6"
 

	
 
        # Set-up some placeholders.
 
        self.fields["name"].widget.attrs["placeholder"] = "Interface name"
 
        self.fields["description"].widget.attrs["placeholder"] = "Interface description"
 
        self.fields["address"].widget.attrs["placeholder"] = "IP address of interface"
 
        self.fields["netmask"].widget.attrs["placeholder"] = "IP address netmask"
 
    widget_css_classes = {"ALL": "span6"}
 

	
 

	
 
class CommunicationForm(ModelForm):
 
class CommunicationForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
 
    """
 
    Implements a custom model form for communications with some styling changes.
 
    """
 
@@ -66,18 +103,7 @@ class CommunicationForm(ModelForm):
 
    class Meta:
 
        model = Communication
 

	
 
    def __init__(self, *args, **kwargs):
 
        """
 
        Initialises the form instance. Sets-up some bootstrap CSS classes for
 
        widgets.
 
        """
 

	
 
        super(CommunicationForm, self).__init__(*args, **kwargs)
 
    widget_placeholders = {"port": "Port used for communication",
 
                           "description": "Communication description"}
 

	
 
        # Update the widgets to be wider.
 
        for field_name, field in self.fields.iteritems():
 
            field.widget.attrs["class"] = "span6"
 

	
 
        # Set-up some placeholders.
 
        self.fields["port"].widget.attrs["placeholder"] = "Port used for communication"
 
        self.fields["description"].widget.attrs["placeholder"] = "Communication description"
 
    widget_css_classes = {"ALL": "span6"}