views.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. from django.template import loader
  4. from django.shortcuts import redirect
  5. from django.conf import settings
  6. from django.urls import reverse
  7. import vyos
  8. from filters.vycontrol_filters import routeunpack
  9. def static_list(request):
  10. if not request.user.is_authenticated:
  11. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  12. all_instances = vyos.instance_getall()
  13. hostname_default = vyos.get_hostname_prefered(request)
  14. static_dict = vyos.get_route_static(hostname_default)
  15. static_list = []
  16. for s in static_dict['route']:
  17. static_list.append({
  18. 'route': s,
  19. 'nexthop': static_dict['route'][s]['next-hop'],
  20. })
  21. template = loader.get_template('static/list.html')
  22. context = {
  23. 'instances': all_instances,
  24. 'hostname_default': hostname_default,
  25. 'static_list' : static_list
  26. }
  27. return HttpResponse(template.render(context, request))
  28. def static_add(request):
  29. if not request.user.is_authenticated:
  30. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  31. all_instances = vyos.instance_getall()
  32. hostname_default = vyos.get_hostname_prefered(request)
  33. static_list = vyos.get_route_static(hostname_default)
  34. error_message = None
  35. if 'subnet' in request.POST and 'nexthop' in request.POST:
  36. return1 = vyos.set_route_static(hostname_default, request.POST['subnet'], request.POST['nexthop'])
  37. if return1 == False:
  38. error_message = 'Cannot add static route.'
  39. else:
  40. return redirect('static:static-list')
  41. ippath = vyos.ip_route(hostname_default)
  42. template = loader.get_template('static/add.html')
  43. context = {
  44. 'instances': all_instances,
  45. 'hostname_default': hostname_default,
  46. 'static_list' : static_list,
  47. 'error_message' : error_message,
  48. }
  49. return HttpResponse(template.render(context, request))
  50. def static_remove(request, route, nexthop):
  51. if not request.user.is_authenticated:
  52. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  53. all_instances = vyos.instance_getall()
  54. hostname_default = vyos.get_hostname_prefered(request)
  55. static_list = vyos.get_route_static(hostname_default)
  56. print(route)
  57. print(routeunpack(route))
  58. if route and nexthop:
  59. return1 = vyos.delete_route_static(hostname_default, routeunpack(route), nexthop)
  60. return redirect('static:static-list')