views.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. from django.shortcuts import render
  2. from django.template import loader
  3. from django.urls import reverse
  4. from django.http import HttpResponse, HttpResponseRedirect
  5. from django.contrib import messages
  6. from django.contrib.auth import authenticate, login, logout
  7. from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
  8. from django.contrib.auth.models import User
  9. from core.models import *
  10. def get_get_value(request):
  11. get_value = {}
  12. if request.method == 'GET':
  13. get = request.GET
  14. for element in get:
  15. get_value[element] = get[element]
  16. print(get_value)
  17. return get_value
  18. def gen_menu(position):
  19. try:
  20. menu = Page.objects.all().exclude(p_publier = 0).filter(p_menu_position = position).order_by('p_menu_parent')
  21. except:
  22. menu = Page.objects.none()
  23. return menu
  24. def get_groupe():
  25. try:
  26. goupes = Groupe.objects.all()
  27. except:
  28. goupes = Groupe.objects.none()
  29. return goupes
  30. def gen_speeddial(grp):
  31. print(grp)
  32. if grp != None:
  33. try:
  34. speeddial = Speed_Dial.objects.filter(sd_groupe__g_nom_slugify = grp).order_by('-sd_poid')
  35. except:
  36. speeddial = Speed_Dial.objects.none()
  37. else:
  38. try:
  39. speeddial = Speed_Dial.objects.order_by('-sd_poid')
  40. except:
  41. speeddial = Speed_Dial.objects.none()
  42. return speeddial
  43. def get_data_value(name):
  44. try:
  45. data = Data.objects.get(d_titre_slugify = name)
  46. except:
  47. data = Data()
  48. data.d_variable = "Blop"
  49. return data.d_variable
  50. def update_data_value(name, value):
  51. try:
  52. data = Data.objects.get(d_titre_slugify = name)
  53. data.d_variable = value
  54. data.save()
  55. except:
  56. data = Data.objects.none()
  57. data.d_variable = "Blop"
  58. return data.d_variable
  59. def gen_page_base():
  60. page = Page.objects.none()
  61. page.p_menu_haut = gen_menu('haut')
  62. page.p_menu_pied = gen_menu('pied')
  63. page.p_see_title_and_des_in_templates = True
  64. page.c_sitename = get_data_value('site-name')
  65. page.c_siteversion = get_data_value('site-version')
  66. page.c_sitelogo = get_data_value('site-logo')
  67. page.c_bgcolor = get_data_value('background-color')
  68. page.c_bgimage = get_data_value('background')
  69. page.c_bgimagelogo = get_data_value('background-logo')
  70. page.c_menulogin = get_data_value('login-menu')
  71. page.c_includ_rp = get_data_value('includ-right-panel')
  72. page.c_card_mp = get_data_value('card-main-panel')
  73. page.c_card_rp = get_data_value('card-right-panel')
  74. return page
  75. def gen_page_sys(p_titre_slugify):
  76. #print(p_titre_slugify)
  77. try :
  78. page = Page.objects.get(p_titre_slugify = p_titre_slugify)
  79. except:
  80. page = gen_page_base()
  81. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  82. page.p_titre = "404 ! Erreur sur la page demmandé"
  83. page.p_icone = "fas fa-bug"
  84. if page.p_groupe == True:
  85. page.groupe = get_groupe()
  86. page.p_menu_haut = gen_menu('haut')
  87. page.p_menu_pied = gen_menu('pied')
  88. page.p_meta_title = page.p_titre
  89. page.c_sitename = get_data_value('site-name')
  90. page.c_sitelogo = get_data_value('site-logo')
  91. page.c_bgcolor = get_data_value('background-color')
  92. page.c_bgimage = get_data_value('background')
  93. page.c_bgimagelogo = get_data_value('background-logo')
  94. page.c_menulogin = get_data_value('login-menu')
  95. page.c_includ_rp = get_data_value('includ-right-panel')
  96. if page.c_card_mp == "non":
  97. page.c_card_mp = False
  98. elif page.c_card_mp == "oui":
  99. page.c_card_mp = True
  100. else :
  101. page.c_card_mp = get_data_value('card-main-panel')
  102. if page.c_card_rp == "non":
  103. page.c_card_rp = "False"
  104. elif page.c_card_rp == "oui":
  105. page.c_card_rp = "True"
  106. else :
  107. page.c_card_rp = get_data_value('card-right-panel')
  108. return page
  109. def index(request):
  110. page = gen_page_sys('bienvenus')
  111. page.p_get_groupe = request.GET.get('grp')
  112. if page.p_speedial == True:
  113. try:
  114. page.speeddial = gen_speeddial(page.p_get_groupe)
  115. except:
  116. page.speeddial = gen_speeddial(None)
  117. template = loader.get_template('page.html')
  118. context = {
  119. 'page' : page,
  120. }
  121. return HttpResponse(template.render(context, request))
  122. def page(request, p_url):
  123. #print(p_url)
  124. p_url = "/"+p_url
  125. template = loader.get_template('page.html')
  126. try:
  127. page = Page.objects.get(p_adresse = p_url)
  128. page.p_menu_haut = gen_menu('haut')
  129. page.p_menu_pied = gen_menu('pied')
  130. page.p_meta_title = page.p_titre
  131. page.c_sitename = get_data_value('site-name')
  132. page.c_sitelogo = get_data_value('site-logo')
  133. page.c_bgcolor = get_data_value('background-color')
  134. page.c_bgimage = get_data_value('background')
  135. page.c_bgimagelogo = get_data_value('background-logo')
  136. page.c_menulogin = get_data_value('login-menu')
  137. except:
  138. page = gen_page_base()
  139. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  140. page.p_get_groupe = request.GET.get('grp')
  141. context = {
  142. 'page' : page,
  143. }
  144. return HttpResponse(template.render(context, request))
  145. def contact(request,):
  146. page = gen_page_base()
  147. template = loader.get_template('page.html')
  148. page.p_titre = "Nous Contacter"
  149. page.p_description = "Formulaire de prise de contact"
  150. page.p_contenu = "<p>Merci de remplir le formulaire pour nous contacter.</p>"
  151. page.p_adresse = "/contact"
  152. if request.method == "POST":
  153. form = ContactForm(request.POST)
  154. if form.is_valid():
  155. n_contact = form.save()
  156. page.p_contenu = "<p>Merci. Nous vous répondrons au vite.</p>"
  157. else :
  158. form = ContactForm()
  159. context = {
  160. 'page' : page,
  161. 'form' : form,
  162. }
  163. return HttpResponse(template.render(context, request))
  164. def p_login(request):
  165. next = request.GET.get('next','')
  166. page = gen_page_base()
  167. template = loader.get_template('login.html')
  168. if request.method == "POST":
  169. form = AuthenticationForm(request, data=request.POST)
  170. if form.is_valid():
  171. username = request.POST['username']
  172. password = request.POST['password']
  173. user = authenticate(request, username=username, password=password)
  174. if user is not None:
  175. login(request, user)
  176. messages.add_message(request, messages.INFO, 'Bonjour, vous êtes maintenant connecté')
  177. if next :
  178. return HttpResponseRedirect(next)
  179. else :
  180. return HttpResponseRedirect(reverse('core_index'))
  181. else :
  182. pass
  183. else :
  184. form = AuthenticationForm(request)
  185. context = {
  186. 'page' : page,
  187. 'form' : form,
  188. }
  189. return HttpResponse(template.render(context, request))
  190. def p_logout(request):
  191. logout(request)
  192. messages.add_message(request, messages.INFO, 'A bientôt')
  193. return HttpResponseRedirect(reverse('core_index'))
  194. def p_registration(request):
  195. page = gen_page_sys("inscription-sur-le-site")
  196. template = loader.get_template('page.html')
  197. page.p_f_titre = "Inscription"
  198. if request.method == "POST":
  199. form = UserCreationForm(request.POST)
  200. if form.is_valid():
  201. username = form.cleaned_data['username']
  202. password = form.cleaned_data['password1']
  203. user=User.objects.create_user(username=username, password=password)
  204. user.save()
  205. user = authenticate(username=username, password=password)
  206. login(request, user)
  207. messages.add_message(request, messages.INFO, 'Merci pour votre inscription')
  208. return HttpResponseRedirect(reverse('flux_control_panel'))
  209. else :
  210. form = UserCreationForm()
  211. context = {
  212. 'page' : page,
  213. 'form' : form,
  214. }
  215. return HttpResponse(template.render(context, request))