api.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. get_filtre = request.GET.get('get_filtre', '')
  150. if get_search:
  151. #print(get_search)
  152. data_query_1 = PIC.objects.exclude(PIC_L_Statut__in = ['INAPTE', 'EN STOCK']).filter(Q(PIC_Nom_netbios__icontains=get_search)
  153. |Q(PIC_Site__CLIN_Anag__icontains=get_search)
  154. |Q(PIC_Adresse_IP__icontains=get_search)
  155. |Q(PIC_Utilisateur__icontains=get_search)
  156. |Q(PIC_Utilisateur_Fq__icontains=get_search)
  157. |Q(PIC_Adresse_Mac__icontains=get_search)
  158. |Q(PIC_Service__SERVICE_Nom__icontains=get_search)
  159. |Q(PIC_SN__icontains=get_search)
  160. |Q(PIC_L_Chassi_Type__icontains=get_search)
  161. ).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')
  162. data_query_2 = PIC.objects.filter(PIC_L_Statut = 'EN STOCK').filter(Q(PIC_Nom_netbios__icontains=get_search)
  163. |Q(PIC_Site__CLIN_Anag__icontains=get_search)
  164. |Q(PIC_Adresse_IP__icontains=get_search)
  165. |Q(PIC_Utilisateur__icontains=get_search)
  166. |Q(PIC_Utilisateur_Fq__icontains=get_search)
  167. |Q(PIC_Adresse_Mac__icontains=get_search)
  168. |Q(PIC_Service__SERVICE_Nom__icontains=get_search)
  169. |Q(PIC_SN__icontains=get_search)
  170. |Q(PIC_L_Chassi_Type__icontains=get_search)
  171. ).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic')
  172. data_query = list(chain(data_query_1, data_query_2))
  173. elif get_filtre != "ALL" :
  174. data_query = PIC.objects.exclude(PIC_L_Statut = 'INAPTE').filter(PIC_L_Chassi_Type = get_filtre).order_by('PIC_Site__CLIN_Anag','PIC_Service__SERVICE_Nom','PIC_NUnic','PIC_L_Statut')
  175. else :
  176. #print("not get_search")
  177. 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')
  178. for data in data_query:
  179. data.accessoir = Accessory_Link.objects.filter(pic = data)
  180. html = template.render({
  181. 'data_query': data_query,
  182. }, request)
  183. return HttpResponse(html)
  184. def user_edit_info(request, id_user):
  185. get_search = request.GET.get('get_search', '')
  186. if not get_search:
  187. get_search = ""
  188. template = loader.get_template('parc_info_api_user_edit.html')
  189. if id_user == None or id_user == '0':
  190. item = PIC()
  191. item.id = 0
  192. else :
  193. item = PIC.objects.get(id = id_user)
  194. if item.PIC_L_Chassi_Type == "IMP":
  195. form = Edit_imp_form(instance=item)
  196. else:
  197. form = Edit_user_mini_form(instance=item)
  198. html = template.render({
  199. 'form': form,
  200. 'item': item,
  201. 'get_search':get_search,
  202. }, request)
  203. return HttpResponse(html)
  204. def user_edit_statut(request, id_user):
  205. get_search = request.GET.get('get_search', '')
  206. if not get_search:
  207. get_search = ""
  208. template = loader.get_template('parc_info_api_user_statut.html')
  209. if id_user == None or id_user == '0':
  210. item = PIC()
  211. item.id = 0
  212. else :
  213. item = PIC.objects.get(id = id_user)
  214. form = Edit_user_statut_form(instance=item)
  215. html = template.render({
  216. 'form': form,
  217. 'item': item,
  218. 'get_search':get_search,
  219. }, request)
  220. return HttpResponse(html)
  221. def user_get_info(request, id_user):
  222. template = loader.get_template('parc_info_api_user_get.html')
  223. item = PIC.objects.get(id = id_user)
  224. item.accessoir = Accessory_Link.objects.filter(pic = item)
  225. html = template.render({
  226. 'item': item,
  227. }, request)
  228. return HttpResponse(html)
  229. def user_get_hist(request, id_user):
  230. template = loader.get_template('parc_info_api_user_get_hist.html')
  231. tmp = PIC.objects.get(id = id_user)
  232. print(id_user)
  233. print(tmp)
  234. data_query = PIC_History.objects.filter(H_PIC = tmp).order_by('-H_Date')
  235. print(data_query)
  236. html = template.render({
  237. 'data_query': data_query,
  238. }, request)
  239. return HttpResponse(html)
  240. def user_start_novnc(request, cl_hostname):
  241. print(cl_hostname)
  242. cl_hostname = cl_hostname + ":5900"
  243. 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)
  244. return HttpResponse("http://SRV-GParc:6080/vnc.html?host=SRV-GParc&port=6080")
  245. def srv_update(request):
  246. data_query = PIS.objects.all()
  247. for item in data_query:
  248. item.save()
  249. return HttpResponse("ok")
  250. def user_update(request):
  251. data_query = PIC.objects.all()
  252. for item in data_query:
  253. # try:
  254. # tmp_service = Services.objects.get(SERVICE_Nom = item.PIC_Service)
  255. # except:
  256. # tmp_service = Services.objects.get(SERVICE_Nom = "VOID")
  257. # item.PIC_Service = tmp_service
  258. # if item.PIC_L_Chassi_Type == "CL" or item.PIC_L_Chassi_Type == "PC" :
  259. # try :
  260. # list_filter_ecran = Accessory_Link.objects.get(pic = item)
  261. # except :
  262. # item.PIC_Accessory.add(Accessory_Link.objects.get(Accessory_N_slugify = "ecran"))
  263. if item.PIC_Site.CLIN_Anag == "GEN":
  264. item.PIC_Site.CLIN_Anag = "HPN"
  265. item.save()
  266. return HttpResponse("ok")