Files
@ c5b0177debb4
Branch filter:
Location: conntrackt/conntrackt/forms.py - annotation
c5b0177debb4
4.0 KiB
text/x-python
CONNT-13: Implemented the ProjectForm model form. Updated tests.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | 7086387253d4 7086387253d4 7086387253d4 7086387253d4 c5b0177debb4 7086387253d4 7086387253d4 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 7086387253d4 7086387253d4 7086387253d4 7086387253d4 7086387253d4 7086387253d4 7086387253d4 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 2a0c8cb4797c 2a0c8cb4797c 529cb3e7db9b 2a0c8cb4797c 2a0c8cb4797c 2a0c8cb4797c 2a0c8cb4797c 2a0c8cb4797c 2a0c8cb4797c 2a0c8cb4797c 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 529cb3e7db9b 2a0c8cb4797c 529cb3e7db9b 744593ed423d 744593ed423d 529cb3e7db9b 744593ed423d 744593ed423d 744593ed423d 744593ed423d 744593ed423d 744593ed423d 744593ed423d 529cb3e7db9b 529cb3e7db9b 744593ed423d 529cb3e7db9b c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 c5b0177debb4 | # Django imports.
from django.forms import ModelForm
# Application imports.
from .models import Project, Entity, Interface, Communication
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.
"""
class Meta:
model = Entity
widget_placeholders = {"name": "Entity name",
"description": "Entity description"}
widget_css_classes = {"ALL": "span6"}
class InterfaceForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
"""
Implements a custom model form for interfaces with some styling changes.
"""
class Meta:
model = Interface
widget_placeholders = {"name": "Interface name",
"description": "Interface description",
"address": "IP address of interface",
"netmask": "IP address netmask"}
widget_css_classes = {"ALL": "span6"}
class CommunicationForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
"""
Implements a custom model form for communications with some styling changes.
"""
class Meta:
model = Communication
widget_placeholders = {"port": "Port used for communication",
"description": "Communication description"}
widget_css_classes = {"ALL": "span6"}
class ProjectForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm):
"""
Implements a custom model form for projects with some styling changes.
"""
class Meta:
model = Project
widget_placeholders = {"name": "Project name",
"description": "Project description"}
widget_css_classes = {"ALL": "span6"}
|