views.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. import vyos
  6. from .models import Instance
  7. def index(request):
  8. #interfaces = vyos.get_interfaces()
  9. all_instances = vyos.instance_getall()
  10. hostname_default = vyos.get_hostname_prefered(request)
  11. template = loader.get_template('config/instance.html')
  12. context = {
  13. #'interfaces': interfaces,
  14. 'instances': all_instances,
  15. 'hostname_default': hostname_default,
  16. }
  17. return HttpResponse(template.render(context, request))
  18. def instances(request):
  19. all_instances = vyos.instance_getall()
  20. hostname_default = vyos.get_hostname_prefered(request)
  21. template = loader.get_template('config/instances.html')
  22. context = {
  23. 'instances': all_instances,
  24. 'hostname_default': hostname_default,
  25. }
  26. return HttpResponse(template.render(context, request))
  27. def instance_add(request):
  28. #interfaces = vyos.get_interfaces()
  29. all_instances = vyos.instance_getall()
  30. hostname_default = vyos.get_hostname_prefered(request)
  31. if len(request.POST) > 0:
  32. instance = Instance()
  33. instance.alias = request.POST['alias']
  34. instance.hostname = request.POST['hostname']
  35. instance.port = request.POST['port']
  36. instance.key = request.POST['key']
  37. if 'https' in request.POST:
  38. instance.https = request.POST['https']
  39. else:
  40. instance.https = False
  41. instance_id = instance.save()
  42. else:
  43. instance_id = 0
  44. template = loader.get_template('config/instance_add.html')
  45. context = {
  46. 'hostname_default': hostname_default,
  47. 'instance_id': instance_id,
  48. 'instances': all_instances,
  49. }
  50. return HttpResponse(template.render(context, request))
  51. def instance_conntry(request, hostname):
  52. all_instances = vyos.instance_getall()
  53. hostname_default = vyos.get_hostname_prefered(request)
  54. # permcheck
  55. instance = Instance.objects.get(hostname=hostname)
  56. connected = vyos.conntry(hostname)
  57. if connected == True:
  58. request.session['hostname'] = hostname
  59. template = loader.get_template('config/instance_conntry.html')
  60. context = {
  61. 'instance': instance,
  62. "connected": connected,
  63. 'instances': all_instances,
  64. 'hostname_default': hostname_default,
  65. }
  66. return HttpResponse(template.render(context, request))
  67. def instance_default(request, hostname):
  68. all_instances = vyos.instance_getall()
  69. # permcheck
  70. instance = Instance.objects.get(hostname=hostname)
  71. connected = vyos.conntry(hostname)
  72. # show some error when not connected
  73. if connected == True:
  74. request.session['hostname'] = hostname
  75. instance.main = True
  76. instance.save()
  77. return redirect('config:instances')
  78. def instance_remove(request, hostname):
  79. all_instances = vyos.instance_getall()
  80. # permcheck
  81. instance = Instance.objects.get(hostname=hostname)
  82. hostname_default = vyos.get_hostname_prefered(request)
  83. if hostname_default != hostname:
  84. instance.delete()
  85. return redirect('config:instances')