Files @ 2a0c8cb4797c
Branch filter:

Location: conntrackt/conntrackt/forms.py

branko
CONNT-5: Added custom model form for Interface. Applied small improvement to EntityForm for styling. Implemented adding interfaces to entities from the entity details page. Updated representation of interfaces on entity details page. Updated view tests for the new functionality. Minor fix to EntityView.
# Django imports.
from django.forms import ModelForm
from django.forms.models import inlineformset_factory

# Application imports.
from .models import Entity, Interface


class EntityForm(ModelForm):
    """
    Implements a custom model form for entities with some styling changes.
    """

    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"


class InterfaceForm(ModelForm):
    """
    Implements a custom model form for interfaces with some styling changes.
    """

    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)

        # 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"