Changeset - c5b0177debb4
[Not reviewed]
default
0 3 0
Branko Majic (branko) - 11 years ago 2013-07-24 21:22:05
branko@majic.rs
CONNT-13: Implemented the ProjectForm model form. Updated tests.
3 files changed with 18 insertions and 61 deletions:
0 comments (0 inline, 0 general)
conntrackt/forms.py
Show inline comments
 
# Django imports.
 
from django.forms import ModelForm
 

	
 
# Application imports.
 
from .models import Entity, Interface, Communication
 
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
 
@@ -98,12 +98,26 @@ class InterfaceForm(WidgetCSSClassFormMi
 
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"}
conntrackt/tests/test_views.py
Show inline comments
 
@@ -370,64 +370,33 @@ class ProjectIptablesTest(PermissionTest
 
            self.assertIn(":INPUT", iptables_file)
 
            self.assertIn(":OUTPUT", iptables_file)
 
            self.assertIn(":FORWARD", iptables_file)
 

	
 
        zipped_iptables.close()
 

	
 

	
 
class ProjectCreateViewTest(PermissionTestMixin, TestCase):
 

	
 
    view_class = ProjectCreateView
 
    sufficient_permissions = ("add_project",)
 

	
 
    def test_form_styling(self):
 
        """
 
        Tests if proper form styling is being applied.
 
        """
 

	
 
        # Get the view.
 
        view = ProjectCreateView.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 Project"')
 
        self.assertContains(response, 'placeholder="Description for new project."')
 

	
 

	
 
class ProjectUpdateViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = ProjectUpdateView
 
    sufficient_permissions = ("change_project",)
 
    permission_test_view_kwargs = {"pk": 1}
 

	
 
    def test_form_styling(self):
 
        """
 
        Tests if proper form styling is being applied.
 
        """
 

	
 
        # Get the view.
 
        view = ProjectUpdateView.as_view()
 

	
 
        # Get the response.
 
        response = generate_get_response(view, None, pk=1)
 

	
 
        self.assertContains(response, 'class="span6 textinput')
 
        self.assertContains(response, 'class="span6 textarea')
 
        self.assertContains(response, 'placeholder="Project name"')
 
        self.assertContains(response, 'placeholder="Description for project."')
 

	
 
    def test_context(self):
 
        """
 
        Verifies that the context is properly set-up when the view is called for
 
        specific project.
 
        """
 

	
 
        # Get the view.
 
        view = ProjectUpdateView.as_view()
 

	
 
        # Get the response.
 
        response = generate_get_response(view, None, pk=1)
conntrackt/views.py
Show inline comments
 
@@ -5,25 +5,25 @@ from zipfile import ZipFile, ZIP_DEFLATE
 
# Django imports.
 
from django.contrib.auth.decorators import permission_required
 
from django.contrib import messages
 
from django.core.urlresolvers import reverse, reverse_lazy
 
from django.http import HttpResponse
 
from django.shortcuts import render_to_response, get_object_or_404
 
from django.views.generic import TemplateView, DetailView, CreateView, UpdateView, DeleteView
 

	
 
# Third-party application imports.
 
from braces.views import MultiplePermissionsRequiredMixin
 

	
 
# Application imports.
 
from .forms import EntityForm, InterfaceForm, CommunicationForm
 
from .forms import EntityForm, InterfaceForm, CommunicationForm, ProjectForm
 
from .models import Project, Entity, Location, Interface, Communication
 
from .utils import generate_entity_iptables
 

	
 

	
 
class IndexView(MultiplePermissionsRequiredMixin, TemplateView):
 
    """
 
    Custom view used for rendering the index page.
 
    """
 

	
 
    template_name = 'conntrackt/index.html'
 

	
 
    # Required permissions.
 
@@ -244,79 +244,53 @@ def project_iptables(request, project_id
 
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
 

	
 
    # Finally return the response object.
 
    return response
 

	
 

	
 
class ProjectCreateView(MultiplePermissionsRequiredMixin, CreateView):
 
    """
 
    View for creating a new project.
 
    """
 

	
 
    model = Project
 
    form_class = ProjectForm
 
    template_name_suffix = "_create_form"
 

	
 
    # Required permissions.
 
    permissions = {
 
        "all": ("conntrackt.add_project",),
 
        }
 

	
 
    # Raise authorisation denied exception for unmet permissions.
 
    raise_exception = True
 

	
 
    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(ProjectCreateView, self).get_form(form_class)
 
        form.fields["name"].widget.attrs["class"] = "span6"
 
        form.fields["name"].widget.attrs["placeholder"] = "New Project"
 
        form.fields["description"].widget.attrs["class"] = "span6"
 
        form.fields["description"].widget.attrs["placeholder"] = "Description for new project."
 

	
 
        return form
 

	
 

	
 
class ProjectUpdateView(MultiplePermissionsRequiredMixin, UpdateView):
 
    """
 
    View for modifying an existing project.
 
    """
 

	
 
    model = Project
 
    form_class = ProjectForm
 
    template_name_suffix = "_update_form"
 

	
 
    # Required permissions.
 
    permissions = {
 
        "all": ("conntrackt.change_project",),
 
        }
 

	
 
    # Raise authorisation denied exception for unmet permissions.
 
    raise_exception = True
 

	
 
    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(ProjectUpdateView, self).get_form(form_class)
 
        form.fields["name"].widget.attrs["class"] = "span6"
 
        form.fields["name"].widget.attrs["placeholder"] = "Project name"
 
        form.fields["description"].widget.attrs["class"] = "span6"
 
        form.fields["description"].widget.attrs["placeholder"] = "Description for project."
 

	
 
        return form
 

	
 

	
 
class ProjectDeleteView(MultiplePermissionsRequiredMixin, DeleteView):
 
    """
 
    View for deleting a project.
 
    """
 

	
 
    model = Project
 

	
 
    # Required permissions.
 
    permissions = {
 
        "all": ("conntrackt.delete_project",),
 
        }
0 comments (0 inline, 0 general)