diff --git a/conntrackt/views.py b/conntrackt/views.py --- a/conntrackt/views.py +++ b/conntrackt/views.py @@ -14,8 +14,8 @@ from django.views.generic import Templat from braces.views import MultiplePermissionsRequiredMixin # Application imports. -from .forms import EntityForm -from .models import Project, Entity, Location +from .forms import EntityForm, InterfaceForm +from .models import Project, Entity, Location, Interface from .utils import generate_entity_iptables @@ -126,7 +126,7 @@ class EntityView(MultiplePermissionsRequ """ # Call the parent class method. - context = super(DetailView, self).get_context_data(**kwargs) + context = super(EntityView, self).get_context_data(**kwargs) # Add the rendered iptables rules to the context. context['entity_iptables'] = generate_entity_iptables(self.object) @@ -529,7 +529,6 @@ class EntityDeleteView(MultiplePermissio return super(EntityDeleteView, self).post(*args, **kwargs) - def delete(self, *args, **kwargs): """ Deletes the object. This method is overridden in order to obtain the @@ -541,3 +540,61 @@ class EntityDeleteView(MultiplePermissio self.success_url = reverse("project", args=(self.get_object().project.id,)) return super(EntityDeleteView, self).delete(*args, **kwargs) + + +class InterfaceCreateView(MultiplePermissionsRequiredMixin, CreateView): + """ + View for creating a new interface. + """ + + model = Interface + form_class = InterfaceForm + template_name_suffix = "_create_form" + + # 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. + """ + + form = super(InterfaceCreateView, self).get_form(form_class) + + # Limit the entity selection if required. + entity_id = self.request.GET.get("entity", None) + if entity_id: + form.fields["entity"].queryset = Entity.objects.filter(pk=entity_id) + form.fields["entity"].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(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,))