1
0

api.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. import subprocess
  2. from itertools import chain
  3. from django.shortcuts import render
  4. from django.template import loader
  5. from django.urls import reverse
  6. from django.utils import timezone
  7. from django.http import HttpResponse, HttpResponseRedirect
  8. from django.contrib import messages
  9. from django.db.models import Q
  10. from django.contrib.auth import authenticate, login, logout
  11. from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
  12. from django.contrib.auth.models import User
  13. from core.views import gen_page_base, gen_page_sys, get_get_value, get_data_value
  14. from .forms import *
  15. from .models import *
  16. def test_last_client_NUnic(type):
  17. if type == "PC":
  18. range_value = "range-pc"
  19. elif type == "IMP":
  20. range_value = "range-imp"
  21. else :
  22. range_value = "blop"
  23. data_range = get_data_value(range_value)
  24. print( "Get new last ID")
  25. range_list = data_range.split(";")
  26. for value in range(int(range_list[0]), int(range_list[1])):
  27. try:
  28. test = PIC.objects.get(PIC_NUnic = value)
  29. except:
  30. print("OK")
  31. break
  32. return value
  33. def switch_get_list(request):
  34. template = loader.get_template('parc_info_api_switch_get_list.html')
  35. get_search = request.GET.get('get_search', '')
  36. if get_search :
  37. #print(get_search)
  38. data_query = Swich.objects.exclude(SW_Archive=True).filter(Q(SW_Nom__icontains=get_search)
  39. |Q(SW_Baie__BAIE_Site_NG__CLIN_Anag__icontains=get_search)
  40. |Q(SW_Baie__BAIE_Nom__icontains=get_search)
  41. |Q(SW_IP__icontains=get_search)
  42. |Q(SW_Commentaires__icontains=get_search)
  43. ).order_by('SW_Baie__BAIE_Site_NG','SW_Baie','SW_Nom')
  44. else :
  45. #print("not get_search")
  46. data_query = Swich.objects.exclude(SW_Archive=True).all().order_by('SW_Baie__BAIE_Site_NG','SW_Baie','SW_Nom')
  47. html = template.render({
  48. 'data_query': data_query,
  49. }, request)
  50. return HttpResponse(html)
  51. def switch_edit_info(request, id):
  52. get_search = request.GET.get('get_search', '')
  53. if not get_search:
  54. get_search = ""
  55. template = loader.get_template('parc_info_api_switch_edit.html')
  56. if id == None or id == '0':
  57. item = Swich()
  58. item.id = 0
  59. else :
  60. item = Swich.objects.get(id = id)
  61. form = Edit_switch_form(instance=item)
  62. html = template.render({
  63. 'get_search':get_search,
  64. 'form': form,
  65. 'item': item,
  66. }, request)
  67. return HttpResponse(html)
  68. def switch_edit_port(request, switch_id, port_id ):
  69. template = loader.get_template('parc_info_api_switch_edit_port.html')
  70. if port_id == None or port_id == '0':
  71. item = Port_Link()
  72. item.PL_Swich = Swich.objects.get(id = switch_id)
  73. item.id = 0
  74. form = Edit_switch_port_form(instance=item)
  75. else :
  76. item = Port_Link.objects.get(id = port_id)
  77. if item.PL_Port_NG == "old":
  78. item.PL_Port_NG = item.PL_Port
  79. form = Edit_switch_port_form(instance=item)
  80. html = template.render({
  81. 'form': form,
  82. 'item': item,
  83. }, request)
  84. return HttpResponse(html)
  85. def switch_update_port(request, switch_id):
  86. template = loader.get_template('parc_info_api_switch_update.html')
  87. item = Swich.objects.get(id = switch_id)
  88. form = Update_switch_port(instance=item)
  89. html = template.render({
  90. 'form': form,
  91. 'item': item,
  92. }, request)
  93. return HttpResponse(html)
  94. def switch_get_info(request, id):
  95. template = loader.get_template('parc_info_api_switch_get.html')
  96. item = Swich.objects.get(id = id)
  97. item.ports = Port_Link.objects.filter(PL_Swich = item.id).order_by('PL_Port_NG')
  98. html = template.render({
  99. 'item': item,
  100. }, request)
  101. return HttpResponse(html)
  102. def srv_get_list(request):
  103. template = loader.get_template('parc_info_api_srv_get_list.html')
  104. get_search = request.GET.get('get_search', '')
  105. if get_search :
  106. #print(get_search)
  107. data_query = PIS.objects.filter(Q(PIS_Nom_netbios__icontains=get_search)
  108. |Q(PIS_Friendly_Name__icontains=get_search)
  109. |Q(PIS_Site__CLIN_Anag__icontains=get_search)
  110. |Q(PIS_Adresse_IP__icontains=get_search)
  111. |Q(PIS_Role__icontains=get_search)
  112. |Q(PIS_SN__icontains=get_search)
  113. |Q(PIS_Adresse_Mac__icontains=get_search)
  114. ).order_by('PIS_Archive','PIS_Site','PIS_Role', 'PIS_Adresse_IP')
  115. else :
  116. #print("not get_search")
  117. data_query = PIS.objects.all().order_by('PIS_Archive','PIS_Site','PIS_Role', 'PIS_Adresse_IP')
  118. html = template.render({
  119. 'data_query': data_query,
  120. }, request)
  121. return HttpResponse(html)
  122. def srv_get_info(request, id_srv):
  123. template = loader.get_template('parc_info_api_srv_get.html')
  124. item = PIS.objects.get(id = id_srv)
  125. html = template.render({
  126. 'item': item,
  127. }, request)
  128. return HttpResponse(html)
  129. def srv_edit_info(request, id_srv):
  130. get_search = request.GET.get('get_search', '')
  131. if not get_search:
  132. get_search = ""
  133. template = loader.get_template('parc_info_api_srv_edit.html')
  134. if id_srv == None or id_srv == '0':
  135. item = PIS()
  136. item.id = 0
  137. else :
  138. item = PIS.objects.get(id = id_srv)
  139. form = Edit_srv_form(instance=item)
  140. html = template.render({
  141. 'get_search':get_search,
  142. 'form': form,
  143. 'item': item,
  144. }, request)
  145. return HttpResponse(html)
  146. def user_get_list(request):
  147. template = loader.get_template('parc_info_api_user_get_list.html')
  148. get_search = request.GET.get('get_search', '')
  149. if get_search :
  150. #print(get_search)
  151. data_query_1 = PIC.objects.exclude(PIC_L_Statut__in = ['INAPTE', 'EN STOCK']).filter(Q(PIC_Nom_netbios__icontains=get_search)
  152. |Q(PIC_Site__CLIN_Anag__icontains=get_search)
  153. |Q(PIC_Adresse_IP__icontains=get_search)
  154. |Q(PIC_Utilisateur__icontains=get_search)
  155. |Q(PIC_Utilisateur_Fq__icontains=get_search)
  156. |Q(PIC_Adresse_Mac__icontains=get_search)
  157. |Q(PIC_Service__SERVICE_Nom__icontains=get_search)
  158. |Q(PIC_SN__icontains=get_search)
  159. |Q(PIC_L_Chassi_Type__icontains=get_search)
  160. ).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')
  161. data_query_2 = PIC.objects.filter(PIC_L_Statut = 'EN STOCK').filter(Q(PIC_Nom_netbios__icontains=get_search)
  162. |Q(PIC_Site__CLIN_Anag__icontains=get_search)
  163. |Q(PIC_Adresse_IP__icontains=get_search)
  164. |Q(PIC_Utilisateur__icontains=get_search)
  165. |Q(PIC_Utilisateur_Fq__icontains=get_search)
  166. |Q(PIC_Adresse_Mac__icontains=get_search)
  167. |Q(PIC_Service__SERVICE_Nom__icontains=get_search)
  168. |Q(PIC_SN__icontains=get_search)
  169. |Q(PIC_L_Chassi_Type__icontains=get_search)
  170. ).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic')
  171. data_query = list(chain(data_query_1, data_query_2))
  172. else :
  173. #print("not get_search")
  174. data_query = PIC.objects.exclude(PIC_L_Statut__in = ['INAPTE', 'EN STOCK']).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')
  175. for data in data_query:
  176. data.accessoir = Accessory_Link.objects.filter(pic = data)
  177. html = template.render({
  178. 'data_query': data_query,
  179. }, request)
  180. return HttpResponse(html)
  181. def user_edit_info(request, id_user):
  182. get_search = request.GET.get('get_search', '')
  183. if not get_search:
  184. get_search = ""
  185. template = loader.get_template('parc_info_api_user_edit.html')
  186. if id_user == None or id_user == '0':
  187. item = PIC()
  188. item.id = 0
  189. else :
  190. item = PIC.objects.get(id = id_user)
  191. if item.PIC_L_Chassi_Type == "IMP":
  192. form = Edit_imp_form(instance=item)
  193. else:
  194. form = Edit_user_mini_form(instance=item)
  195. html = template.render({
  196. 'form': form,
  197. 'item': item,
  198. 'get_search':get_search,
  199. }, request)
  200. return HttpResponse(html)
  201. def user_edit_statut(request, id_user):
  202. get_search = request.GET.get('get_search', '')
  203. if not get_search:
  204. get_search = ""
  205. template = loader.get_template('parc_info_api_user_statut.html')
  206. if id_user == None or id_user == '0':
  207. item = PIC()
  208. item.id = 0
  209. else :
  210. item = PIC.objects.get(id = id_user)
  211. form = Edit_user_statut_form(instance=item)
  212. html = template.render({
  213. 'form': form,
  214. 'item': item,
  215. 'get_search':get_search,
  216. }, request)
  217. return HttpResponse(html)
  218. def user_get_info(request, id_user):
  219. template = loader.get_template('parc_info_api_user_get.html')
  220. item = PIC.objects.get(id = id_user)
  221. item.accessoir = Accessory_Link.objects.filter(pic = item)
  222. html = template.render({
  223. 'item': item,
  224. }, request)
  225. return HttpResponse(html)
  226. def user_get_hist(request, id_user):
  227. template = loader.get_template('parc_info_api_user_get_hist.html')
  228. tmp = PIC.objects.get(id = id_user)
  229. print(id_user)
  230. print(tmp)
  231. data_query = PIC_History.objects.filter(H_PIC = tmp).order_by('-H_Date')
  232. print(data_query)
  233. html = template.render({
  234. 'data_query': data_query,
  235. }, request)
  236. return HttpResponse(html)
  237. def user_start_novnc(request, cl_hostname):
  238. print(cl_hostname)
  239. cl_hostname = cl_hostname + ":5900"
  240. subprocess.Popen(["tmux", "new", "-d", "/root/projects/noVNC-1.3.0/utils/novnc_proxy", "--vnc", cl_hostname, "--timeout", "60","--idle-timeout", "60" ], stdout=subprocess.PIPE)
  241. return HttpResponse("http://SRV-GParc:6080/vnc.html?host=SRV-GParc&port=6080")
  242. def srv_update(request):
  243. data_query = PIS.objects.all()
  244. for item in data_query:
  245. item.save()
  246. return HttpResponse("ok")
  247. def user_update(request):
  248. data_query = PIC.objects.all()
  249. for item in data_query:
  250. # try:
  251. # tmp_service = Services.objects.get(SERVICE_Nom = item.PIC_Service)
  252. # except:
  253. # tmp_service = Services.objects.get(SERVICE_Nom = "VOID")
  254. # item.PIC_Service = tmp_service
  255. # if item.PIC_L_Chassi_Type == "CL" or item.PIC_L_Chassi_Type == "PC" :
  256. # try :
  257. # list_filter_ecran = Accessory_Link.objects.get(pic = item)
  258. # except :
  259. # item.PIC_Accessory.add(Accessory_Link.objects.get(Accessory_N_slugify = "ecran"))
  260. if item.PIC_Site.CLIN_Anag == "GEN":
  261. item.PIC_Site.CLIN_Anag = "HPN"
  262. item.save()
  263. return HttpResponse("ok")