Commit 7402ce79 by Kálmán Viktor

network: display rules in host view

parent 493c99ac
...@@ -45,6 +45,38 @@ class HostTable(Table): ...@@ -45,6 +45,38 @@ class HostTable(Table):
order_by = ('vlan', 'hostname', ) order_by = ('vlan', 'hostname', )
class SmallRuleTable(Table):
rule = TemplateColumn(
template_name="network/columns/host-rule.html",
attrs={"th": {"style": "display: none;"}}
)
action = TemplateColumn(
template_name="network/columns/host-rule-action.html",
attrs={
"th": {"style": "display: none;"},
"cell": {"style": "text-align: center; vertical-align: middle;"}
}
)
class Meta:
model = Rule
attrs = {'class': 'table table-striped table-bordered table-condensed'}
fields = ('rule', 'action', )
class SmallGroupRuleTable(Table):
rule = TemplateColumn(
template_name="network/columns/host-rule.html",
attrs={"th": {"style": "display: none;"}}
)
class Meta:
model = Rule
attrs = {'class': 'table table-striped table-bordered table-condensed'}
fields = ('rule', )
# inheritance by copy-paste # inheritance by copy-paste
class SmallHostTable(Table): class SmallHostTable(Table):
hostname = LinkColumn('network.host', args=[A('pk')]) hostname = LinkColumn('network.host', args=[A('pk')])
......
{% load i18n %}
{% load l10n %}
<span style="color: #FF0000;">[{{ record.r_type }}]</span>
{% if record.direction == "1" %}{{ record.foreign_network }}{% else %}{{ record.r_type }}{% endif %}
{#<span style="color: #0000FF;"> ▸ </span>#}
<i class="icon-arrow-right"></i>
{% if record.direction == "0" %}{{ record.foreign_network }}{% else %}{{ record.r_type }}{% endif %}
<span style="color: #00FF00;">
{% if record.proto %}
proto={{ record.proto }}
{% endif %}
{% if record.sport %}
sport={{ record.sport }}
{% endif %}
{% if record.dport %}
dport={{ record.dport }}
{% endif %}
{{ record.description }}
{% load i18n %}
{% load l10n %}
<a href="{% url network.rule_delete pk=record.pk %}?from={{ request.path }}"><i class="icon-remove"></i></a>
<a href="{% url network.rule pk=record.pk %}"><i class="icon-pencil"></i></a>
{% load i18n %}
{% load l10n %}
{# <span style="color: #FF0000;">[{{ record.r_type }}]</span> #}
{% if record.direction == "1" %}{{ record.foreign_network }}{% else %}{{ record.r_type }}{% endif %}
{#<span style="color: #0000FF;"> ▸ </span>#}
<i class="icon-arrow-right"></i>
{% if record.direction == "0" %}{{ record.foreign_network }}{% else %}{{ record.r_type }}{% endif %}
{% if record.accept %}
<span class="label label-info" style="margin-left: 5px;">ACCEPT</span>
{% endif %}
{% if record.extra %}
<span class="label label-inverse">{{ record.extra }}</span>
{% endif %}
{% if record.nat %}
<span class="label label-success">NAT [{{ record.dport }}:{{record.nat_dport}}]</span>
{% endif %}
{% extends "network/base.html" %}
{% load i18n %}
{% load l10n %}
{% block content %}
<div class="page-heading">
<h1>Remove <small></small></h1>
</div>
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to remove hostgroup <strong>"{{ group }}"</strong>
from <strong>"{{ host }}"</strong>?</p>
<input type="hidden" value="{{ request.GET.from }}" name="next" />
<input type="submit" value="Confirm" />
</form>
{% endblock %}
{% extends "network/base.html" %}
{% load i18n %}
{% load l10n %}
{% block content %}
<div class="page-heading">
<h1>Delete <small>delete this rule</small></h1>
</div>
<form action="" method="post">{% csrf_token %}
<p>Are you sure you want to delete <strong>"{{ object }}"</strong>?</p>
<input type="hidden" value="{{ request.GET.from }}" name="next" />
<input type="submit" value="Confirm" />
</form>
{% endblock %}
...@@ -10,5 +10,44 @@ ...@@ -10,5 +10,44 @@
<h1>{{ form.hostname.value }} <small>details of host</small></h1> <h1>{{ form.hostname.value }} <small>details of host</small></h1>
</div> </div>
{% crispy form %}
<div class="row">
<div class="span8">
{% crispy form %}
</div>
<div class="span4">
<h3>Rules</h3>
{% render_table rule_list %}
<h3>Groups</h3>
{% for group in group_rule_list %}
<h4>{{ group.name }}
<a href="{% url network.remove_host_group pk=host_pk group_pk=group.pk %}?from={{ request.path }}">
<i class="icon-remove" style="vertical-align: middle;"></i>
</a>
<a href="{% url network.group group.pk %}">
<i class="icon-pencil" style="vertical-align: middle;"></i>
</a>
</h4>
{% render_table group.table %}
{% endfor %}
<h3>Add host group</h3>
{% if not_used_groups|length == 0 %}
No more groups to add!
{% else %}
<form action="{% url network.add_host_group pk=host_pk %}" method="POST">
{% csrf_token %}
<div class="input-append">
<select name="group">
{% for rest in not_used_groups %}
<option value="{{ rest.pk }}">{{ rest }}</option>
{% endfor %}
</select>
<input type="submit" value="Add group" class="btn"></input>
</div><!-- input append -->
</form>
{% endif %}
</div><!-- span4 -->
</div><!-- row -->
{% endblock %} {% endblock %}
...@@ -3,7 +3,8 @@ from django.conf.urls import patterns, url ...@@ -3,7 +3,8 @@ from django.conf.urls import patterns, url
from .views import (IndexView, HostList, HostDetail, VlanList, VlanDetail, from .views import (IndexView, HostList, HostDetail, VlanList, VlanDetail,
DomainList, DomainDetail, GroupList, GroupDetail, DomainList, DomainDetail, GroupList, GroupDetail,
RecordList, RecordDetail, BlacklistList, BlacklistDetail, RecordList, RecordDetail, BlacklistList, BlacklistDetail,
RuleList, RuleDetail, VlanGroupList, VlanGroupDetail) RuleList, RuleDetail, VlanGroupList, VlanGroupDetail,
RuleDelete, remove_host_group, add_host_group)
urlpatterns = patterns( urlpatterns = patterns(
...@@ -32,4 +33,10 @@ urlpatterns = patterns( ...@@ -32,4 +33,10 @@ urlpatterns = patterns(
name='network.vlan_group_list'), name='network.vlan_group_list'),
url('^vlangroups/(?P<pk>\d+)/$', VlanGroupDetail.as_view(), url('^vlangroups/(?P<pk>\d+)/$', VlanGroupDetail.as_view(),
name='network.vlan_group'), name='network.vlan_group'),
url('^rules/delete/(?P<pk>\d+)/$', RuleDelete.as_view(),
name="network.rule_delete"),
url('^hosts/(?P<pk>\d+)/remove/(?P<group_pk>\d+)/$', remove_host_group,
name='network.remove_host_group'),
url('^hosts/(?P<pk>\d+)/add/$', add_host_group,
name='network.add_host_group')
) )
from django.views.generic import TemplateView from django.views.generic import TemplateView, UpdateView, DeleteView
from django.views.generic import UpdateView
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, redirect
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
...@@ -8,7 +8,7 @@ from firewall.models import (Host, Vlan, Domain, Group, Record, Blacklist, ...@@ -8,7 +8,7 @@ from firewall.models import (Host, Vlan, Domain, Group, Record, Blacklist,
Rule, VlanGroup) Rule, VlanGroup)
from .tables import (HostTable, VlanTable, SmallHostTable, DomainTable, from .tables import (HostTable, VlanTable, SmallHostTable, DomainTable,
GroupTable, RecordTable, BlacklistTable, RuleTable, GroupTable, RecordTable, BlacklistTable, RuleTable,
VlanGroupTable) VlanGroupTable, SmallRuleTable, SmallGroupRuleTable)
from .forms import (HostForm, VlanForm, DomainForm, GroupForm, RecordForm, from .forms import (HostForm, VlanForm, DomainForm, GroupForm, RecordForm,
BlacklistForm, RuleForm, VlanGroupForm) BlacklistForm, RuleForm, VlanGroupForm)
...@@ -126,6 +126,32 @@ class HostDetail(UpdateView): ...@@ -126,6 +126,32 @@ class HostDetail(UpdateView):
template_name = "network/host-edit.html" template_name = "network/host-edit.html"
form_class = HostForm form_class = HostForm
def get_context_data(self, **kwargs):
context = super(HostDetail, self).get_context_data(**kwargs)
# own rules
q = Rule.objects.filter(host=self.object).all()
context['rule_list'] = SmallRuleTable(q)
# rules from host groups
group_rule_list = []
for group in self.object.groups.all():
q = Rule.objects.filter(hostgroup=group).all()
group_rule_list.append({
'table': SmallGroupRuleTable(q),
'name': unicode(group),
'pk': group.pk
})
context['group_rule_list'] = group_rule_list
# available groups
rest = Group.objects.exclude(pk__in=self.object.groups.all()).all()
context['not_used_groups'] = rest
# set host pk (we need this for URL-s)
context['host_pk'] = self.kwargs['pk']
return context
def get_success_url(self): def get_success_url(self):
if 'pk' in self.kwargs: if 'pk' in self.kwargs:
return reverse_lazy('network.host', kwargs=self.kwargs) return reverse_lazy('network.host', kwargs=self.kwargs)
...@@ -171,6 +197,17 @@ class RuleDetail(UpdateView): ...@@ -171,6 +197,17 @@ class RuleDetail(UpdateView):
return reverse_lazy('network.rule', kwargs=self.kwargs) return reverse_lazy('network.rule', kwargs=self.kwargs)
class RuleDelete(DeleteView):
model = Rule
template_name = "network/confirm/rule_delete.html"
def get_success_url(self):
if 'next' in self.request.POST:
return self.request.POST['next']
else:
return reverse_lazy('network.rule_list')
class VlanList(SingleTableView): class VlanList(SingleTableView):
model = Vlan model = Vlan
table_class = VlanTable table_class = VlanTable
...@@ -207,3 +244,32 @@ class VlanGroupDetail(UpdateView): ...@@ -207,3 +244,32 @@ class VlanGroupDetail(UpdateView):
form_class = VlanGroupForm form_class = VlanGroupForm
success_url = reverse_lazy('network.vlan_group_list') success_url = reverse_lazy('network.vlan_group_list')
def remove_host_group(request, **kwargs):
host = Host.objects.get(pk=kwargs['pk'])
group = Group.objects.get(pk=kwargs['group_pk'])
# for get we show the confirmation page
if request.method == "GET":
return render(request,
'network/confirm/remove_host_group.html',
{
'group': group.name,
'host': host.hostname
})
# for post we actually remove the group from the host
elif request.method == "POST":
host.groups.remove(group)
return redirect(reverse_lazy('network.host',
kwargs={'pk': kwargs['pk']}))
def add_host_group(request, **kwargs):
group_pk = request.POST.get('group')
if request.method == "POST" and group_pk:
host = Host.objects.get(pk=kwargs['pk'])
group = Group.objects.get(pk=group_pk)
host.groups.add(group)
return redirect(reverse_lazy('network.host', kwargs=kwargs))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment