diff --git a/conntrackt/forms.py b/conntrackt/forms.py --- a/conntrackt/forms.py +++ b/conntrackt/forms.py @@ -2,7 +2,7 @@ from django.forms import ModelForm # Application imports. -from .models import Project, Entity, Interface, Communication +from .models import Project, Location, Entity, Interface, Communication class WidgetCSSClassFormMixin(object): @@ -121,3 +121,17 @@ class ProjectForm(WidgetCSSClassFormMixi "description": "Project description"} widget_css_classes = {"ALL": "span6"} + + +class LocationForm(WidgetCSSClassFormMixin, PlaceholderFormMixin, ModelForm): + """ + Implements a custom model form for projects with some styling changes. + """ + + class Meta: + model = Location + + widget_placeholders = {"name": "Location name", + "description": "Location description"} + + widget_css_classes = {"ALL": "span6"} diff --git a/conntrackt/tests/test_views.py b/conntrackt/tests/test_views.py --- a/conntrackt/tests/test_views.py +++ b/conntrackt/tests/test_views.py @@ -453,22 +453,6 @@ class LocationCreateViewTest(PermissionT view_class = LocationCreateView sufficient_permissions = ("add_location",) - def test_form_styling(self): - """ - Tests if proper form styling is being applied. - """ - - # Get the view. - view = LocationCreateView.as_view() - - # Get the response. - response = generate_get_response(view) - - self.assertContains(response, 'class="span6 textinput') - self.assertContains(response, 'class="span6 textarea') - self.assertContains(response, 'placeholder="New Location"') - self.assertContains(response, 'placeholder="Description for new location."') - class LocationUpdateViewTest(PermissionTestMixin, TestCase): @@ -478,22 +462,6 @@ class LocationUpdateViewTest(PermissionT sufficient_permissions = ("change_location",) permission_test_view_kwargs = {"pk": 1} - def test_form_styling(self): - """ - Tests if proper form styling is being applied. - """ - - # Get the view. - view = LocationUpdateView.as_view() - - # Get the response. - response = generate_get_response(view, pk=1) - - self.assertContains(response, 'class="span6 textinput') - self.assertContains(response, 'class="span6 textarea') - self.assertContains(response, 'placeholder="Location name"') - self.assertContains(response, 'placeholder="Description for location."') - def test_context(self): """ Verifies that the context is properly set-up when the view is called for diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -14,7 +14,7 @@ from django.views.generic import Templat from braces.views import MultiplePermissionsRequiredMixin # Application imports. -from .forms import EntityForm, InterfaceForm, CommunicationForm, ProjectForm +from .forms import ProjectForm, LocationForm, EntityForm, InterfaceForm, CommunicationForm from .models import Project, Entity, Location, Interface, Communication from .utils import generate_entity_iptables @@ -317,6 +317,7 @@ class LocationCreateView(MultiplePermiss """ model = Location + form_class = LocationForm template_name_suffix = "_create_form" # Required permissions. @@ -329,20 +330,6 @@ class LocationCreateView(MultiplePermiss success_url = reverse_lazy("index") - def get_form(self, form_class): - """ - Implements an override for the default form constructed for the create - view that includes some better styling of input widgets. - """ - - form = super(LocationCreateView, self).get_form(form_class) - form.fields["name"].widget.attrs["class"] = "span6" - form.fields["name"].widget.attrs["placeholder"] = "New Location" - form.fields["description"].widget.attrs["class"] = "span6" - form.fields["description"].widget.attrs["placeholder"] = "Description for new location." - - return form - class LocationUpdateView(MultiplePermissionsRequiredMixin, UpdateView): """ @@ -350,6 +337,7 @@ class LocationUpdateView(MultiplePermiss """ model = Location + form_class = LocationForm template_name_suffix = "_update_form" # Required permissions. @@ -362,20 +350,6 @@ class LocationUpdateView(MultiplePermiss success_url = reverse_lazy("index") - def get_form(self, form_class): - """ - Implements an override for the default form constructed for the create - view that includes some better styling of input widgets. - """ - - form = super(LocationUpdateView, self).get_form(form_class) - form.fields["name"].widget.attrs["class"] = "span6" - form.fields["name"].widget.attrs["placeholder"] = "Location name" - form.fields["description"].widget.attrs["class"] = "span6" - form.fields["description"].widget.attrs["placeholder"] = "Description for location." - - return form - class LocationDeleteView(MultiplePermissionsRequiredMixin, DeleteView): """