Нема описа

views.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from django.shortcuts import render
  2. from backend.mongodb import db
  3. from exfo.lib import Exfo
  4. from pprint import pprint
  5. from ttp import ttp
  6. from django.http import JsonResponse
  7. exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  8. exfo.login()
  9. def index(request):
  10. collection = db['mascot']
  11. mascot_details = collection.find({})
  12. rapi = exfo.list_api()
  13. sla = exfo.call_api("sla")
  14. data_to_parse = """
  15. interface Loopback0
  16. description Router-id-loopback
  17. ip address 192.168.0.113/24
  18. !
  19. interface Vlan778
  20. description CPE_Acces_Vlan
  21. ip address 2002::fd37/124
  22. ip vrf CPE1
  23. !
  24. """
  25. ttp_template = """
  26. interface {{ interface }}
  27. ip address {{ ip }}/{{ mask }}
  28. description {{ description }}
  29. ip vrf {{ vrf }}
  30. """
  31. template = """
  32. <input load="text">
  33. interface Loopback0
  34. description Router-id-loopback
  35. ip address 192.168.0.113/24
  36. !
  37. interface Loopback1
  38. description Router-id-loopback
  39. ip address 192.168.0.1/24
  40. !
  41. interface Vlan778
  42. ip address 2002::fd37/124
  43. ip vrf CPE1
  44. !
  45. interface Vlan779
  46. ip address 2002::bbcd/124
  47. ip vrf CPE2
  48. !
  49. </input>
  50. <group name="loopbacks_new**.{{ interface }}">
  51. interface {{ interface | contains("Loop") }}
  52. ip address {{ ip }}/{{ mask }}
  53. description {{ description }}
  54. ip vrf {{ vrf }}
  55. </group>
  56. <group name="vlans*">
  57. interface {{ interface | contains("Vlan") }}
  58. ip address {{ ip }}/{{ mask }}
  59. description {{ description }}
  60. ip vrf {{ vrf }}
  61. </group>
  62. """
  63. parser = ttp(template=template)
  64. parser.parse()
  65. # form excel table and save in file
  66. parser.result(
  67. format="excel",
  68. filename="excel_out_test_excel_formatter_update.xlsx",
  69. returner="file",
  70. update=True,
  71. url="./Output/",
  72. table=[
  73. {
  74. "headers": ["interface", "ip", "mask", "vrf", "description"],
  75. "path": "loopbacks_new",
  76. "key": "interface",
  77. "tab_name": "loopbacks_new",
  78. },
  79. {"path": "vlans"},
  80. ],
  81. )
  82. # create parser object and parse data using template:
  83. # parser = ttp(data=data_to_parse, template=ttp_template)
  84. # parser.parse()
  85. # # print result in JSON format
  86. # results = parser.result(format='xlsx')[0]
  87. # pprint(results)
  88. return render(request, 'backend/index.html', {'objs': mascot_details, 'output': rapi.json(),\
  89. 'sla': sla.json()})
  90. #Define Collection
  91. def remote(request):
  92. cmd = request.GET.get('cmd', None)
  93. # exfo = Exfo("administrator", "exf0w0rxC@t4dm!n")
  94. # exfo.login()
  95. r = exfo.call_remote_api(cmd)
  96. pprint(r.json())
  97. # return JsonResponse(r.json())
  98. return render(request, 'backend/remote_render.html', {'res': r})