views.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. def index(request):
  9. if not request.user.is_authenticated:
  10. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  11. all_instances = vyos.instance_getall()
  12. hostname_default = vyos.get_hostname_prefered(request)
  13. static_dict = vyos.get_route_static(hostname_default)
  14. static_list = []
  15. for s in static_dict['route']:
  16. static_list.append({
  17. 'route': s,
  18. 'nexthop': static_dict['route'][s]['next-hop'],
  19. })
  20. template = loader.get_template('static/list.html')
  21. context = {
  22. 'instances': all_instances,
  23. 'hostname_default': hostname_default,
  24. 'static_list' : static_list
  25. }
  26. return HttpResponse(template.render(context, request))
  27. def static(request):
  28. if not request.user.is_authenticated:
  29. return redirect('%s?next=%s' % (reverse('registration-login'), request.path))
  30. all_instances = vyos.instance_getall()
  31. hostname_default = vyos.get_hostname_prefered(request)
  32. static_list = vyos.get_route_static(hostname_default)
  33. error_message = None
  34. if 'subnet' in request.POST and 'nexthop' in request.POST:
  35. return1 = vyos.set_route_static(hostname_default, request.POST['subnet'], request.POST['nexthop'])
  36. if return1 == False:
  37. error_message = 'Cannot add static route.'
  38. else:
  39. return redirect('static:static-list')
  40. ippath = vyos.ip_route(hostname_default)
  41. template = loader.get_template('static/static.html')
  42. context = {
  43. 'instances': all_instances,
  44. 'hostname_default': hostname_default,
  45. 'static_list' : static_list,
  46. 'error_message' : error_message,
  47. }
  48. return HttpResponse(template.render(context, request))