Changeset - c153097ca199
[Not reviewed]
default
0 3 0
Branko Majic (branko) - 11 years ago 2013-07-24 21:25:10
branko@majic.rs
CONNT-14: Implemented the LocationForm model form. Updated tests.
3 files changed with 18 insertions and 62 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 Project, Entity, Interface, Communication
 
from .models import Project, Location, 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)
 

	
 
@@ -100,24 +100,38 @@ class CommunicationForm(WidgetCSSClassFo
 
    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"}
 

	
 

	
 
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"}
conntrackt/tests/test_views.py
Show inline comments
 
@@ -432,89 +432,57 @@ class ProjectDeleteViewTest(PermissionTe
 
        """
 
        Tests if the message gets added when the project is deleted.
 
        """
 

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

	
 
        # Generate the request.
 
        request = RequestFactory().post("/fake-path/")
 
        request.user = mock.Mock()
 
        request._dont_enforce_csrf_checks = True
 
        request._messages = FakeMessages()
 

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

	
 
        self.assertIn("Project Test Project 1 has been removed.", request._messages.messages)
 

	
 

	
 
class LocationCreateViewTest(PermissionTestMixin, TestCase):
 

	
 
    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):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = LocationUpdateView
 
    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
 
        specific location.
 
        """
 

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

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

	
 
        self.assertEqual(response.context_data["location"].name, "Test Location 1")
 

	
 

	
 
class LocationDeleteViewTest(PermissionTestMixin, TestCase):
 

	
 
    fixtures = ['test-data.json']
 

	
 
    view_class = LocationDeleteView
 
    sufficient_permissions = ("delete_location",)
 
    permission_test_view_kwargs = {"pk": "1"}
 

	
 
    def test_context(self):
conntrackt/views.py
Show inline comments
 
# Standard library imports.
 
from StringIO import StringIO
 
from zipfile import ZipFile, ZIP_DEFLATED
 

	
 
# 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, ProjectForm
 
from .forms import ProjectForm, LocationForm, EntityForm, InterfaceForm, CommunicationForm
 
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.
 
    permissions = {
 
        "all": ("conntrackt.view",),
 
        }
 
    # Raise authorisation denied exception for unmet permissions.
 
    raise_exception = True
 

	
 
    def get_context_data(self, **kwargs):
 
        """
 
        Returns the context data that should be used for rendering of the
 
        template.
 

	
 
        Adds the 'projects' context object containing all of the projects
 
@@ -296,107 +296,81 @@ class ProjectDeleteView(MultiplePermissi
 
        }
 

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

	
 
    success_url = reverse_lazy("index")
 

	
 
    def post(self, *args, **kwargs):
 
        """
 
        Add a success message that will be displayed to the user to confirm the
 
        project deletion.
 
        """
 

	
 
        messages.success(self.request, "Project %s has been removed." % self.get_object().name, extra_tags="alert alert-success")
 

	
 
        return super(ProjectDeleteView, self).post(*args, **kwargs)
 

	
 

	
 
class LocationCreateView(MultiplePermissionsRequiredMixin, CreateView):
 
    """
 
    View for creating a new location.
 
    """
 

	
 
    model = Location
 
    form_class = LocationForm
 
    template_name_suffix = "_create_form"
 

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

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

	
 
    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):
 
    """
 
    View for modifying an existing location.
 
    """
 

	
 
    model = Location
 
    form_class = LocationForm
 
    template_name_suffix = "_update_form"
 

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

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

	
 
    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):
 
    """
 
    View for deleting a location.
 
    """
 

	
 
    model = Location
 

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

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

	
 
    success_url = reverse_lazy("index")
 

	
 
    def post(self, *args, **kwargs):
 
        """
 
        Add a success message that will be displayed to the user to confirm the
 
        location deletion.
 
        """
 

	
0 comments (0 inline, 0 general)