Changeset - b9fabc69b505
[Not reviewed]
default
0 4 0
Branko Majic (branko) - 11 years ago 2013-07-13 21:13:50
branko@majic.rs
CONNT-4: Implemented delete view for entities, and integrated it with the rest of application.
4 files changed with 69 insertions and 7 deletions:
0 comments (0 inline, 0 general)
conntrackt/templates/conntrackt/entity_detail.html
Show inline comments
 
@@ -5,13 +5,22 @@
 

	
 
{% block content %}
 
{# Use a bit shorter variable names. #}
 
{% with project=entity.project location=entity.location %}
 

	
 
{% if entity %}
 
<h1>{{entity.name}}</h1>
 
<div class="row">
 
  <h1 class="span12">{{entity.name}}</h1>
 
</div>
 
<hr>
 
<div class="row">
 
  <div class="span12">
 
    {% html_link "Remove" "entity_delete" entity.id class="btn btn-primary" %}
 
  </div>
 
</div>
 
<hr>
 
<dl>
 
  <dt>Project</dt><dd>{% html_link project.name 'project'  project.id  %}</dd>
 
  <dt>Location</dt><dd>{{location.name}}</dd>
 
  <dt>Incoming communications</dt><dd><ul class="unstyled">{% for interface in entity.interface_set.all %}
 
    {% for communication in interface.destination_set.all %}
 
    <li>{{communication.source}} - {{communication.protocol}}: {{communication.port}}</li>
conntrackt/templates/conntrackt/location_widget.html
Show inline comments
 
{% load conntrackt_tags %}
 
  <table class="table table-striped">
 
    <tr><td><strong>{{location.name}}</strong></td><td>{% html_link '<i class="icon-book"></i>' 'project_location_iptables' project.id location.id class="btn btn-link" %}</td></tr>
 
    <tr>
 
      <td style="width:99%"><strong>{{location.name}}</strong></td>
 
      <td>{% html_link '<i class="icon-book"></i>' 'project_location_iptables' project.id location.id class="btn btn-link" %}</td>
 
      <td></td>
 
    </tr>
 
{% for entity in entities %}
 
    <tr><td>{% html_link entity.name 'entity' entity.id class="btn btn-link" %}</td><td>{% html_link '<i class="icon-list"></i>' 'entity_iptables' entity.id class="btn btn-link" %}</td></tr>
 
    <tr>
 
      <td>{% html_link entity.name 'entity' entity.id class="btn btn-link" %}</td>
 
      <td>{% html_link '<i class="icon-list"></i>' 'entity_iptables' entity.id class="btn btn-link" %}</td>
 
      <td>{% html_link '<i class="icon-remove"></i>' 'entity_delete' entity.id class="btn btn-link"  %}</td>
 
    </tr>
 
{% endfor %}
 
    {% with project_id=project.id|slugify location_id=location.id|slugify %}
 
    <tr><td>{% html_link "Add entity" "entity_create" class="btn btn-primary btn-mini" get="project="|add:project_id|add:"&location="|add:location_id %}</td><td></td></tr>
 
    <tr>
 
      <td>{% html_link "Add entity" "entity_create" class="btn btn-primary btn-mini" get="project="|add:project_id|add:"&location="|add:location_id %}</td>
 
      <td></td>
 
      <td></td>
 
    </tr>
 
    {% endwith %}
 
  </table>
 

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

	
 
# Application imports.
 
from .views import IndexView, EntityView, entity_iptables, project_iptables
 
from .views import ProjectView, ProjectCreateView, ProjectUpdateView, ProjectDeleteView
 
from .views import LocationCreateView, LocationUpdateView, LocationDeleteView
 
from .views import EntityCreateView
 
from .views import EntityCreateView, EntityDeleteView
 

	
 

	
 
urlpatterns = patterns(
 
    'conntrackt.views',
 
    # Homepage/index view.
 
    url(r'^$', IndexView.as_view(), name="index"),
 
@@ -34,12 +34,14 @@ urlpatterns = patterns(
 

	
 
    # View for showing information about an entity.
 
    url(r'^entity/(?P<pk>\d+)/$', EntityView.as_view(),
 
        name='entity'),
 
    # View for creating a new entity.
 
    url(r'^entity/add/$', EntityCreateView.as_view(), name="entity_create"),
 
    # View for deleting an entity.
 
    url(r'^entity/(?P<pk>\d+)/remove/$', EntityDeleteView.as_view(), name="entity_delete"),
 

	
 
    # View for rendering iptables rules for a specific entity.
 
    url(r'^entity/(?P<pk>\d+)/iptables/$', entity_iptables, name="entity_iptables"),
 
    # View for rendering zip file with iptables rules for all entities in a project.
 
    url(r'^project/(?P<project_id>\d+)/iptables/$', project_iptables, name="project_iptables"),
 
    # View for rendering zip file with iptables rules for all entities in a project for a specific location.
conntrackt/views.py
Show inline comments
 
@@ -2,13 +2,13 @@
 
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_lazy
 
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
 
@@ -83,13 +83,13 @@ class ProjectView(MultiplePermissionsReq
 

	
 
        # Set-up an array that will contaion (location, entities) tuples.
 
        location_entities = []
 

	
 
        # Add the (location, entities) tuple for each location that has entities
 
        # belonging to the related project.
 
        for location in Location.objects.filter(entity__project=self.object).distinct():
 
        for location in Location.objects.filter(entity__project=self.object).distinct().order_by("name"):
 
            entities = Entity.objects.filter(project=self.object, location=location)
 
            location_entities.append((location, entities))
 

	
 
        # Add the (location, entities) tuples to context.
 
        context['location_entities'] = location_entities
 

	
 
@@ -470,6 +470,45 @@ class EntityCreateView(MultiplePermissio
 
        initial = super(EntityCreateView, self).get_initial()
 

	
 
        initial["project"] = self.request.GET.get("project", None)
 
        initial["location"] = self.request.GET.get("location", None)
 

	
 
        return initial
 

	
 

	
 
class EntityDeleteView(MultiplePermissionsRequiredMixin, DeleteView):
 
    """
 
    View for deleting an entity.
 
    """
 

	
 
    model = Entity
 

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

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

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

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

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

	
 

	
 
    def delete(self, *args, **kwargs):
 
        """
 
        Deletes the object. This method is overridden in order to obtain the
 
        project ID for success URL.
 

	
 
        @TODO: Fix this once Django 1.6 comes out with fix from ticket 19044.
 
        """
 

	
 
        self.success_url = reverse("project", args=(self.get_object().project.id,))
 

	
 
        return super(EntityDeleteView, self).delete(*args, **kwargs)
0 comments (0 inline, 0 general)