Changeset - 897541fd8978
[Not reviewed]
default
0 3 0
Branko Majic (branko) - 12 years ago 2013-08-02 21:58:45
branko@majic.rs
CONNT-12: Integrated the 'redirect to next' mixin into views.
3 files changed with 28 insertions and 21 deletions:
0 comments (0 inline, 0 general)
conntrackt/templates/conntrackt/location_widget.html
Show inline comments
 
{% load conntrackt_tags %}
 
  <table class="table table-striped">
 
    <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>
 
      <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>
 
      <td>{% html_link '<i class="icon-edit"></i>' 'entity_update' entity.id class="btn btn-link"  %}</td>
 
      <td>{% html_link '<i class="icon-remove"></i>' 'entity_delete' entity.id class="btn btn-link"  %}</td>
 
      <td>{% html_link '<i class="icon-edit"></i>' 'entity_update' entity.id class="btn btn-link" get="next="|add:request.path %}</td>
 
      <td>{% html_link '<i class="icon-remove"></i>' 'entity_delete' entity.id class="btn btn-link" get="next="|add:request.path %}</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>{% html_link "Add entity" "entity_create" class="btn btn-primary btn-mini" get="project="|add:project_id|add:"&location="|add:location_id|add:"&next="|add:request.path %}</td>
 
      <td></td>
 
      <td></td>
 
      <td></td>
 
    </tr>
 
    {% endwith %}
 
  </table>
 

	
conntrackt/templates/conntrackt/project_detail.html
Show inline comments
 
@@ -2,47 +2,47 @@
 

	
 
{# For html_link #}
 
{% load conntrackt_tags %}
 

	
 
{% block content %}
 
<div class="row">
 
  <h1 class="span12">{{project.name}}</h1>
 
</div>
 
<hr>
 

	
 
{% if project.description %}
 
<div class="row">
 
  <div class="span12">
 
    {{project.description}}
 
  </div>
 
</div>
 
<hr>
 
{% endif %}
 

	
 
<div class="row">
 
  <div class="span12">
 
    {% html_link "Edit" "project_update" project.id class="btn btn-primary" %}
 
    {% html_link "Remove" "project_delete" project.id class="btn btn-primary" %}
 
    {% with project_id=project.id|slugify %}
 
    {% html_link "Add entity" "entity_create" class="btn btn-primary" get="project="|add:project_id %}
 
    {% html_link "Add communication" "communication_create" class="btn btn-primary" get="project="|add:project_id %}
 
    {% html_link "Add entity" "entity_create" class="btn btn-primary" get="project="|add:project_id|add:"&next="|add:request.path %}
 
    {% html_link "Add communication" "communication_create" class="btn btn-primary" get="project="|add:project_id|add:"&next="|add:request.path %}
 
    {% endwith %}
 
  </div>
 
</div>
 
<hr>
 
{% if location_entities %}
 
<div class="row">
 
  {% for location, entities in location_entities %}
 
  <div class="span4">
 
    <div class="well">{% include "conntrackt/location_widget.html" with location=location entities=entities %}</div>
 
  </div>
 
  {% endfor %}
 
</div>
 
<div class="row">
 
  <div class="span12">
 
    <h2>Communications diagram</h2>
 
    <img src="{% url "project_diagram" project.id %}" width="100%">
 
  </div>
 
</div>
 
{% endif %}
 
{% endblock %}
 

	
conntrackt/views.py
Show inline comments
 
@@ -248,213 +248,213 @@ def project_iptables(request, project_id
 
        filename = '%s-iptables.zip' % (project.name.lower().replace(" ", "_"))
 

	
 
    # Render iptables rules for each entity, placing them in the ZIP archive.
 
    for entity in entities:
 
        entity_iptables = generate_entity_iptables(entity)
 
        zipped_iptables.writestr("%s-iptables.conf" % entity.name.lower().replace(" ", "_"), entity_iptables)
 

	
 
    # Close the archive, and flush the buffer.
 
    zipped_iptables.close()
 
    buff.flush()
 

	
 
    # Write the contents of our buffer (ZIP archive) to response content, and
 
    # close the IO string.
 
    response.write(buff.getvalue())
 
    buff.close()
 

	
 
    # Set the Content-Disposition so the browser would know it should download
 
    # the archive, and suggest the filename.
 
    response['Content-Disposition'] = 'attachment; filename="%s"' % filename
 

	
 
    # Finally return the response object.
 
    return response
 

	
 

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

	
 
    model = Project
 
    form_class = ProjectForm
 
    headline = "Add new project"
 
    template_name = "conntrackt/create_form.html"
 

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

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

	
 

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

	
 
    model = Project
 
    form_class = ProjectForm
 
    template_name = "conntrackt/update_form.html"
 

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

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

	
 
    def get_headline(self):
 
        """
 
        Set headline based on project name.
 
        """
 

	
 
        return "Update project %s" % self.object.name
 

	
 

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

	
 
    model = Project
 
    template_name = "conntrackt/delete_form.html"
 

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

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

	
 
    def get_headline(self):
 
        """
 
        Set headline based on project name.
 
        """
 

	
 
        return "Delete project %s" % self.object.name
 

	
 

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

	
 
    model = Location
 
    form_class = LocationForm
 
    headline = "Add new location"
 
    template_name = "conntrackt/create_form.html"
 

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

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

	
 
    success_url = reverse_lazy("index")
 

	
 

	
 
class LocationUpdateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
class LocationUpdateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
    """
 
    View for modifying an existing location.
 
    """
 

	
 
    model = Location
 
    form_class = LocationForm
 
    template_name = "conntrackt/update_form.html"
 

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

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

	
 
    success_url = reverse_lazy("index")
 

	
 
    def get_headline(self):
 
        """
 
        Set headline based on location name.
 
        """
 

	
 
        return "Update location %s" % self.object.name
 

	
 

	
 
class LocationDeleteView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
class LocationDeleteView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
    """
 
    View for deleting a location.
 
    """
 

	
 
    model = Location
 
    template_name = "conntrackt/delete_form.html"
 

	
 
    # 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.
 
        """
 

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

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

	
 
    def get_headline(self):
 
        """
 
        Set headline based on location name.
 
        """
 

	
 
        return "Delete location %s" % self.object.name
 

	
 

	
 
class EntityCreateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
class EntityCreateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
    """
 
    View for creating a new entity.
 
    """
 

	
 
    model = Entity
 
    form_class = EntityForm
 
    headline = "Add new entity"
 
    template_name = "conntrackt/create_form.html"
 

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

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

	
 
    def get_form(self, form_class):
 
        """
 
        Returns an instance of form that can be used by the view.
 

	
 
        The method will limit the project or location select inputs if request
 
        contained this information.
 
        """
 
@@ -468,120 +468,120 @@ class EntityCreateView(SetHeadlineMixin,
 
            form.fields["project"].widget.attrs["readonly"] = True
 

	
 
        # Limit the location selection if required.
 
        location_id = self.request.GET.get("location", None)
 
        if location_id:
 
            form.fields["location"].queryset = Location.objects.filter(pk=location_id)
 
            form.fields["location"].widget.attrs["readonly"] = True
 

	
 
        return form
 

	
 
    def get_initial(self):
 
        """
 
        Returns initial values that should be pre-selected (if they were
 
        specified through a GET parameter).
 
        """
 

	
 
        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 EntityUpdateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
class EntityUpdateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
    """
 
    View for updating an existing entity.
 
    """
 

	
 
    model = Entity
 
    form_class = EntityForm
 
    template_name = "conntrackt/update_form.html"
 

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

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

	
 
    def get_headline(self):
 
        """
 
        Set headline based on entity name.
 
        """
 

	
 
        return "Update entity %s" % self.object.name
 

	
 

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

	
 
    model = Entity
 
    template_name = "conntrackt/delete_form.html"
 

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

	
 
    def get_headline(self):
 
        """
 
        Set headline based on entity name.
 
        """
 

	
 
        return "Delete entity %s" % self.object.name
 

	
 

	
 
class InterfaceCreateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
class InterfaceCreateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
    """
 
    View for creating a new interface.
 
    """
 

	
 
    model = Interface
 
    form_class = InterfaceForm
 
    headline = "Add new interface"
 
    template_name = "conntrackt/create_form.html"
 

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

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

	
 
    def get_form(self, form_class):
 
        """
 
        Returns an instance of form that can be used by the view.
 

	
 
        The method will limit the entity select input if request contained this
 
        information.
 
        """
 
@@ -598,49 +598,49 @@ class InterfaceCreateView(SetHeadlineMix
 

	
 
    def get_initial(self):
 
        """
 
        Returns initial values that should be pre-selected (if they were
 
        specified through a GET parameter).
 
        """
 

	
 
        initial = super(InterfaceCreateView, self).get_initial()
 

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

	
 
        return initial
 

	
 
    def get_success_url(self):
 
        """
 
        Returns the URL to which the user should be redirected after an
 
        interface has been created.
 

	
 
        The URL in this case will be set to entity's details page.
 
        """
 

	
 
        return reverse("entity", args=(self.object.entity.pk,))
 

	
 

	
 
class InterfaceUpdateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
class InterfaceUpdateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
    """
 
    View for updating an existing interface.
 
    """
 

	
 
    model = Interface
 
    form_class = InterfaceForm
 
    template_name = "conntrackt/update_form.html"
 

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

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

	
 
    def get_form(self, form_class):
 
        """
 
        Returns an instance of form that can be used by the view.
 

	
 
        The method will limit the entities that can be selected for the
 
        interface to the ones that belong to the same project as the currently
 
        set entity.
 
        """
 
@@ -649,95 +649,95 @@ class InterfaceUpdateView(SetHeadlineMix
 

	
 
        # Limit the entities to same project.
 
        form.fields["entity"].queryset = Entity.objects.filter(project=self.object.entity.project)
 

	
 
        return form
 

	
 
    def get_success_url(self):
 
        """
 
        Returns the URL to which the user should be redirected after an
 
        interface has been updated.
 

	
 
        The URL in this case will be set to entity's details page.
 
        """
 

	
 
        return reverse("entity", args=(self.object.entity.pk,))
 

	
 
    def get_headline(self):
 
        """
 
        Set headline based on interface name.
 
        """
 

	
 
        return "Update interface %s" % self.object.name
 

	
 

	
 
class InterfaceDeleteView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
class InterfaceDeleteView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
    """
 
    View for deleting an interface.
 
    """
 

	
 
    model = Interface
 
    template_name = "conntrackt/delete_form.html"
 

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

	
 
    # 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
 
        interface deletion.
 
        """
 

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

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

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

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

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

	
 
        return super(InterfaceDeleteView, self).delete(*args, **kwargs)
 

	
 
    def get_headline(self):
 
        """
 
        Set headline based on interface name.
 
        """
 

	
 
        return "Delete interface %s" % self.object.name
 

	
 

	
 
class CommunicationCreateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
class CommunicationCreateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, CreateView):
 
    """
 
    View for creating a new communication.
 
    """
 

	
 
    model = Communication
 
    form_class = CommunicationForm
 
    headline = "Add new communication"
 
    template_name = "conntrackt/create_form.html"
 

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

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

	
 
    def get_form(self, form_class):
 
        """
 
        Returns an instance of form that can be used by the view.
 

	
 
        The method will limit the source and destination interface selection to
 
        interfaces belonging to the same project as provided entity ID (if any
 
        was provided).
 
@@ -777,52 +777,59 @@ class CommunicationCreateView(SetHeadlin
 
            try:
 
                interface = Interface.objects.filter(entity=from_entity)[0]
 
                initial["source"] = interface.id
 
            except IndexError:
 
                pass
 

	
 
        if to_entity:
 
            try:
 
                interface = Interface.objects.filter(entity=to_entity)[0]
 
                initial["destination"] = interface.id
 
            except IndexError:
 
                pass
 

	
 
        return initial
 

	
 
    def get_success_url(self):
 
        """
 
        Returns the URL to which the user should be redirected after a
 
        communication has been created.
 

	
 
        The URL will either point to value provided via GET parameter "next", or
 
        to project page to which the communication belongs.
 
        """
 

	
 
        return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,)))
 
        # We must set the success URL to something first.
 
        self.success_url = reverse("project", args=(self.object.source.entity.project.pk,))
 

	
 
        # This will override the URL if parameter "next" was provided (from
 
        # RedirectToNextMixin).
 
        success_url = super(CommunicationCreateView, self).get_success_url()
 

	
 
        return success_url
 

	
 

	
 
class CommunicationUpdateView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
class CommunicationUpdateView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, UpdateView):
 
    """
 
    View for updating an existing communication.
 
    """
 

	
 
    model = Communication
 
    form_class = CommunicationForm
 
    template_name = "conntrackt/update_form.html"
 

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

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

	
 
    def get_form(self, form_class):
 
        """
 
        Returns an instance of form that can be used by the view.
 

	
 
        The method will limit the source and destination interfaces that can be
 
        selected for the communication. Both will be limited to interfaces
 
        coming from entities that belong to the same project as current
 
        communication's source interface.
 
@@ -835,49 +842,49 @@ class CommunicationUpdateView(SetHeadlin
 
        form.fields["source"].queryset = Interface.objects.filter(entity__project=project)
 
        form.fields["destination"].queryset = Interface.objects.filter(entity__project=project)
 

	
 
        return form
 

	
 
    def get_success_url(self):
 
        """
 
        Returns the URL to which the user should be redirected after a
 
        communication has been created.
 

	
 
        The URL will either point to value provided via GET parameter "next", or
 
        to project page to which the communication belongs.
 
        """
 

	
 
        return self.request.GET.get("next", reverse("project", args=(self.object.source.entity.project.pk,)))
 

	
 
    def get_headline(self):
 
        """
 
        Set headline based on communication.
 
        """
 

	
 
        return "Update communication %s" % self.object
 

	
 

	
 
class CommunicationDeleteView(SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
class CommunicationDeleteView(RedirectToNextMixin, SetHeadlineMixin, MultiplePermissionsRequiredMixin, DeleteView):
 
    """
 
    View for deleting an communication.
 
    """
 

	
 
    model = Communication
 
    template_name = "conntrackt/delete_form.html"
 

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

	
 
    # 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
 
        communication deletion.
 
        """
 

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

	
 
        return super(CommunicationDeleteView, self).post(*args, **kwargs)
0 comments (0 inline, 0 general)