1
0

api.py 4.5 KB

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