1
0

api.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from django.shortcuts import render
  2. from django.template import loader
  3. from django.urls import reverse
  4. from django.utils import timezone
  5. from django.http import HttpResponse, HttpResponseRedirect
  6. from django.contrib import messages
  7. from django.db.models import Q
  8. from django.contrib.auth import authenticate, login, logout
  9. from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
  10. from django.contrib.auth.models import User
  11. from core.views import gen_page_base, gen_page_sys, get_get_value
  12. from .models import *
  13. def genloginapp(user_id, app_id):
  14. item = GES_User.objects.get( id = user_id)
  15. app = GES_APP.objects.get( id = app_id)
  16. app_login =""
  17. if app.GESAP_ltype == "pnom":
  18. app_login = str(item.GES_Prenom[:1]) + str(item.GES_Nom)
  19. app_login = app_login.lower()
  20. if app.GESAP_ltype == "pnomMGAG":
  21. app_login = str(item.GES_Prenom[:1]) + str(item.GES_Nom)
  22. app_login = app_login.upper()
  23. elif app.GESAP_ltype == "pre.nom":
  24. app_login = str(item.GES_Prenom) + "." + str(item.GES_Nom)
  25. app_login = app_login.lower()
  26. elif app.GESAP_ltype == "preMAG.nomMAG":
  27. app_login = str(item.GES_Prenom) + "." + str(item.GES_Nom)
  28. app_login = app_login.upper()
  29. elif app.GESAP_ltype == "pre.nom@el":
  30. app_login = str(item.GES_Prenom) + "." + str(item.GES_Nom) + "@elsan.care"
  31. app_login = app_login.lower()
  32. return app_login
  33. def link_user_to_app(user_id, action):
  34. item = GES_User.objects.get( id = user_id)
  35. for apps in item.GES_APPs.all():
  36. app_login = genloginapp(item.id, apps.id)
  37. print(app_login)
  38. obj, created = GES_APPLink.objects.update_or_create(GES_APPLink_User= item, GES_APPLink_APP= apps)
  39. print(created)
  40. if created == True or action == "check":
  41. obj.GES_APPLink_Login = app_login
  42. obj.GES_APPLink_MDP=item.GES_Motdepasse
  43. obj.GES_OK = False
  44. obj.save()
  45. return
  46. def user_get_list(request):
  47. template = loader.get_template('gest_user_api_get_list.html')
  48. get_search = request.GET.get('get_search', '')
  49. if get_search :
  50. #print(get_search)
  51. data_query = GES_User.objects.filter(Q(GES_Nom__icontains=get_search)
  52. |Q(GES_Prenom__icontains=get_search)
  53. |Q(GES_Service__SERVICE_Nom__icontains=get_search)
  54. |Q(GES_Fonction__GESFC_Nom__icontains=get_search)
  55. ).order_by('GES_L_Site','GES_Service','GES_Fonction','GES_Nom','GES_Prenom')
  56. else :
  57. #print("not get_search")
  58. data_query = GES_User.objects.filter(GES_OK = False).order_by('GES_L_Site','GES_Service','GES_Fonction','GES_Nom','GES_Prenom')
  59. html = template.render({
  60. 'data_query': data_query,
  61. }, request)
  62. return HttpResponse(html)
  63. def user_get(request, id):
  64. template = loader.get_template('gest_user_api_get.html')
  65. item = GES_User.objects.get(id = id)
  66. link_user_to_app(item.id, 'update')
  67. item.apps = GES_APPLink.objects.filter(GES_APPLink_User = item.id)
  68. html = template.render({
  69. 'item': item,
  70. }, request)
  71. return HttpResponse(html)
  72. def user_get_for_send(request, id):
  73. template = loader.get_template('gest_user_api_get_for_send.html')
  74. item = GES_User.objects.get(id = id)
  75. link_user_to_app(item.id, 'update')
  76. item.apps = GES_APPLink.objects.filter(GES_APPLink_User = item.id)
  77. html = template.render({
  78. 'item': item,
  79. }, request)
  80. return HttpResponse(html)
  81. def user_edit(request, id):
  82. get_search = request.GET.get('get_search', '')
  83. print(get_search)
  84. template = loader.get_template('gest_user_api_edit.html')
  85. if id == None or id == '0':
  86. item = GES_User()
  87. item.id = 0
  88. else :
  89. item = GES_User.objects.get(id = id)
  90. form = GES_User_form(instance=item)
  91. form.url = "gest_user_edit"
  92. html = template.render({
  93. 'get_search':get_search,
  94. 'form': form,
  95. 'item': item,
  96. }, request)
  97. return HttpResponse(html)
  98. def app_edit(request, id):
  99. get_search = request.GET.get('get_search', '')
  100. print(get_search)
  101. template = loader.get_template('gest_user_api_edit.html')
  102. if id == None or id == '0':
  103. item = GES_APPLink()
  104. item.id = 0
  105. else :
  106. item = GES_APPLink.objects.get(id = id)
  107. form = GES_APPLink_form(instance=item)
  108. form.url = "gest_app_edit"
  109. html = template.render({
  110. 'get_search':get_search,
  111. 'form': form,
  112. 'item': item,
  113. }, request)
  114. return HttpResponse(html)
  115. def switch_app_status(request, id):
  116. try:
  117. item = GES_APPLink.objects.get(id = id)
  118. except:
  119. return HttpResponse('Error')
  120. if item.GES_APPLink_Create == False :
  121. item.GES_APPLink_Create = True
  122. else :
  123. item.GES_APPLink_Create = False
  124. item.save()
  125. return HttpResponse(item.GES_APPLink_User.id)