Changeset - e7f270f1c28d
[Not reviewed]
default
0 6 1
Branko Majic (branko) - 11 years ago 2013-07-06 00:49:52
branko@majic.rs
CONNT-2: Added built-in support for adding new projects. Also fixed some styling/alignment on index page.
7 files changed with 119 insertions and 4 deletions:
0 comments (0 inline, 0 general)
conntrackt/templates/conntrackt/index.html
Show inline comments
 
@@ -5,10 +5,16 @@
 

	
 
{% block content %}
 
<div class="row">
 
  <h1>Welcome to Conntrackt</h1>
 
  <h1 class="span12">Welcome to Conntrackt</h1>
 
</div>
 
<div class="row">
 
    <div>Below you may find the list of projects that are available to you. Clicking on the project will take you to the summary page for that particular project. Clicking on an entity inside of a project will take you to the summary page of an entity.</div>
 
    <div class="span12">Below you may find the list of projects that are available to you. Clicking on the project will take you to the summary page for that particular project. Clicking on an entity inside of a project will take you to the summary page of an entity.</div>
 
</div>
 
<hr>
 
<div class="row">
 
  <div class="span12">
 
    {% html_link "Add project" "project_add" class="btn btn-primary" %}
 
  </div>
 
</div>
 
<hr>
 
<div class="row">
conntrackt/templates/conntrackt/project_form.html
Show inline comments
 
new file 100644
 
{% extends "conntrackt/base.html" %}
 

	
 
{# For html_link #}
 
{% load conntrackt_tags %}
 
{# For Bootstrapped forms #}
 
{% load crispy_forms_tags %}
 

	
 
{% block content %}
 
<div class="row">
 
  <div class="span6">
 
    <form action="" method="post">
 
      <div class="controls controls-row">
 
      {% csrf_token %}
 
      {{ form | crispy }}
 
      </div>
 
      <div class="controls">
 
        <button type="submit" class="btn btn-primary input-medium pull-right">Add</button>
 
      </div>
 
    </form>
 
  </div>
 
</div>
 
{% endblock content %}
conntrackt/tests/test_views.py
Show inline comments
 
@@ -365,3 +365,54 @@ class ProjectIptablesTest(ViewTest):
 
            self.assertIn(":FORWARD", iptables_file)
 

	
 
        zipped_iptables.close()
 

	
 

	
 
class ProjectCreateViewTest(TestCase):
 

	
 
    def setUp(self):
 
        # Set-up web client.
 
        self.client = Client()
 

	
 
        # Set-up users with different view permissions.
 
        self.user = {}
 
        self.user["fullperms"] = User.objects.create_user("fullperms", "fullperms@example.com", "fullperms")
 
        self.user["fullperms"].user_permissions.add(Permission.objects.get(codename="add_project"))
 
        self.user["noperms"] = User.objects.create_user("noperms", "noperms@example.com", "noperms")
 

	
 

	
 
    def test_permission_denied(self):
 
        """
 
        Tests if permission will be denied for client without sufficient privileges.
 
        """
 

	
 
        self.client.login(username="noperms", password="noperms")
 
        
 
        response = self.client.get(reverse("project_add"))
 
 
 
        self.assertContains(response, "You have insufficient privileges to access this resource. Please contact your local system administrator if you believe you should have been granted access.", status_code=403)
 

	
 
    def test_permission_granted(self):
 
        """
 
        Tests if permission will be granted for user with correct privileges.
 
        """
 

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_add"))
 

	
 
        self.assertEqual(response.status_code, 200)
 

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

	
 
        self.client.login(username="fullperms", password="fullperms")
 

	
 
        response = self.client.get(reverse("project_add"))
 

	
 
        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."')
 

	
conntrackt/urls.py
Show inline comments
 
@@ -3,7 +3,7 @@ from django.conf.urls import patterns, u
 
from django.contrib.auth.views import login, logout
 

	
 
# Application imports.
 
from .views import IndexView, ProjectView, EntityView, entity_iptables, project_iptables
 
from .views import IndexView, ProjectView, ProjectCreateView, EntityView, entity_iptables, project_iptables
 

	
 

	
 
urlpatterns = patterns(
 
@@ -13,6 +13,8 @@ urlpatterns = patterns(
 
    # View for showing information about a project.
 
    url(r'^project/(?P<pk>\d+)/$', ProjectView.as_view(),
 
        name='project'),
 
    # View for creating a new project.
 
    url(r'^project/add/$', ProjectCreateView.as_view(), name="project_add"),
 
    # View for showing information about an entity.
 
    url(r'^entity/(?P<pk>\d+)/$', EntityView.as_view(),
 
        name='entity'),
conntrackt/views.py
Show inline comments
 
@@ -6,7 +6,7 @@ from zipfile import ZipFile, ZIP_DEFLATE
 
from django.contrib.auth.decorators import permission_required
 
from django.http import HttpResponse
 
from django.shortcuts import render_to_response, get_object_or_404
 
from django.views.generic import TemplateView, DetailView
 
from django.views.generic import TemplateView, DetailView, CreateView
 

	
 
# Third-party application imports.
 
from braces.views import MultiplePermissionsRequiredMixin
 
@@ -229,3 +229,34 @@ def project_iptables(request, project_id
 

	
 
    # Finally return the response object.
 
    return response
 

	
 

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

	
 
    model = Project
 
    
 
    # 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
 

	
projtest/projtest/settings.py
Show inline comments
 
@@ -133,6 +133,8 @@ INSTALLED_APPS = (
 
    'braces',
 
    # Enable test-runner.
 
    'discover_runner',
 
    # Better forms, including styling functions.
 
    'crispy_forms',
 
)
 

	
 
# A sample logging configuration. The only tangible logging
requirements/base.txt
Show inline comments
 
django>=1.5
 
South
 
django-braces
 
django-crispy-forms
0 comments (0 inline, 0 general)