Commit e0580823 by Bence Dányi

firewall_gui: code refactored

Autocomplete functions are dynamically generated
parent b86b3414
...@@ -110,13 +110,7 @@ urlpatterns = patterns('', ...@@ -110,13 +110,7 @@ urlpatterns = patterns('',
url(r'^firewall/vlangroups/(?P<id>\d+)/$', 'firewall_gui.views.show_vlangroup'), url(r'^firewall/vlangroups/(?P<id>\d+)/$', 'firewall_gui.views.show_vlangroup'),
url(r'^firewall/hostgroups/(?P<id>\d+)/$', 'firewall_gui.views.show_hostgroup'), url(r'^firewall/hostgroups/(?P<id>\d+)/$', 'firewall_gui.views.show_hostgroup'),
url(r'^firewall/autocomplete/vlan/$', 'firewall_gui.views.autocomplete_vlan'), url(r'^firewall/autocomplete/(?P<entity>\w+)/$', 'firewall_gui.views.autocomplete'),
url(r'^firewall/autocomplete/vlangroup/$', 'firewall_gui.views.autocomplete_vlangroup'),
url(r'^firewall/autocomplete/host/$', 'firewall_gui.views.autocomplete_host'),
url(r'^firewall/autocomplete/hostgroup/$', 'firewall_gui.views.autocomplete_hostgroup'),
url(r'^firewall/autocomplete/firewall/$', 'firewall_gui.views.autocomplete_firewall'),
url(r'^firewall/autocomplete/domain/$', 'firewall_gui.views.autocomplete_domain'),
url(r'^firewall/autocomplete/record/$', 'firewall_gui.views.autocomplete_record'),
url(r'^firewall/rules/save/$', 'firewall_gui.views.save_rule'), url(r'^firewall/rules/save/$', 'firewall_gui.views.save_rule'),
) )
...@@ -400,47 +400,31 @@ def show_hostgroup(request, id): ...@@ -400,47 +400,31 @@ def show_hostgroup(request, id):
return HttpResponse(json.dumps(group), content_type='application/json') return HttpResponse(json.dumps(group), content_type='application/json')
def autocomplete_vlan(request): def make_autocomplete(entity, name='name'):
def autocomplete(request):
return HttpResponse(json.dumps([{ return HttpResponse(json.dumps([{
'id': vlan.id, 'id': object.id,
'name': vlan.name 'name': getattr(object, name)
} for vlan in Vlan.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json') } for object in entity.objects.filter(**{
name+'__icontains': request.POST['name']
def autocomplete_vlangroup(request): })[:5]]), content_type='application/json')
return HttpResponse(json.dumps([{ return autocomplete
'id': vlangroup.id,
'name': vlangroup.name
} for vlangroup in VlanGroup.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json') def autocomplete(request, entity):
try:
def autocomplete_hostgroup(request): return {
return HttpResponse(json.dumps([{ 'vlan': make_autocomplete(Vlan),
'id': hostgroup.id, 'vlangroup': make_autocomplete(VlanGroup),
'name': hostgroup.name 'host': make_autocomplete(Host, 'hostname'),
} for hostgroup in Group.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json') 'hostgroup': make_autocomplete(Group),
'firewall': make_autocomplete(Firewall),
def autocomplete_host(request): 'domain': make_autocomplete(Domain),
return HttpResponse(json.dumps([{ 'record': make_autocomplete(Record),
'id': host.id, }[entity](request)
'name': host.hostname except Exception as e:
} for host in Host.objects.filter(hostname__icontains=request.POST['name'])[:5]]), content_type='application/json') return HttpResponse('>:-3', status=500)
def autocomplete_firewall(request):
return HttpResponse(json.dumps([{
'id': firewall.id,
'name': firewall.name
} for firewall in Firewall.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json')
def autocomplete_domain(request):
return HttpResponse(json.dumps([{
'id': domain.id,
'name': domain.name
} for domain in Domain.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json')
def autocomplete_record(request):
return HttpResponse(json.dumps([{
'id': record.id,
'name': record.name
} for record in Record.objects.filter(name__icontains=request.POST['name'])[:5]]), content_type='application/json')
def save_rule(request): def save_rule(request):
data = json.loads(request.body) data = json.loads(request.body)
......
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