views.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 gen_speeddial():
  25. try:
  26. speeddial = Speed_Dial.objects.order_by('-sd_poid')
  27. except:
  28. speeddial = Speed_Dial.objects.none()
  29. return speeddial
  30. def get_data_value(name):
  31. try:
  32. data = Data.objects.get(d_titre_slugify = name)
  33. except:
  34. data = Data()
  35. if name == "site-name":
  36. data.d_titre = name
  37. data.d_type = "txt"
  38. data.d_variable = "Duhaz Core"
  39. data.save()
  40. elif name == "site-logo":
  41. data.d_titre = name
  42. data.d_type = "txt"
  43. data.d_variable = "far fa-clone"
  44. data.save()
  45. elif name == "background-color":
  46. data.d_titre = name
  47. data.d_type = "txt"
  48. data.d_variable = "#999"
  49. data.save()
  50. elif name == "background":
  51. data.d_titre = name
  52. data.d_type = "txt"
  53. data.d_variable = "background.jpeg"
  54. data.save()
  55. elif name == "background-logo":
  56. data.d_titre = name
  57. data.d_type = "txt"
  58. data.d_variable = "logo-txt-Mrduhaz.png"
  59. data.save()
  60. elif name == "login-menu":
  61. data.d_titre = name
  62. data.d_type = "txt"
  63. data.d_variable = "True"
  64. data.save()
  65. elif name == "includ-right-panel":
  66. data.d_titre = name
  67. data.d_type = "txt"
  68. data.d_variable = "None"
  69. data.save()
  70. else :
  71. data.d_variable = "Blop"
  72. return data.d_variable
  73. def update_data_value(name, value):
  74. try:
  75. data = Data.objects.get(d_titre_slugify = name)
  76. data.d_variable = value
  77. data.save()
  78. except:
  79. data = Data.objects.none()
  80. data.d_variable = "Blop"
  81. return data.d_variable
  82. def gen_page_base():
  83. page = Page.objects.none()
  84. page.p_menu_haut = gen_menu('haut')
  85. page.p_menu_pied = gen_menu('pied')
  86. page.p_see_title_and_des_in_templates = True
  87. page.c_sitename = get_data_value('site-name')
  88. page.c_sitelogo = get_data_value('site-logo')
  89. page.c_bgcolor = get_data_value('background-color')
  90. page.c_bgimage = get_data_value('background')
  91. page.c_bgimagelogo = get_data_value('background-logo')
  92. page.c_menulogin = get_data_value('login-menu')
  93. page.c_includ_rp = get_data_value('includ-right-panel')
  94. return page
  95. def gen_page_sys(p_titre_slugify):
  96. #print(p_titre_slugify)
  97. try :
  98. page = Page.objects.get(p_titre_slugify = p_titre_slugify)
  99. except:
  100. page = gen_page_base()
  101. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  102. page.p_titre = "404 ! Erreur sur la page demmandé"
  103. page.p_icone = "fas fa-bug"
  104. page.p_menu_haut = gen_menu('haut')
  105. page.p_menu_pied = gen_menu('pied')
  106. page.p_meta_title = page.p_titre
  107. page.c_sitename = get_data_value('site-name')
  108. page.c_sitelogo = get_data_value('site-logo')
  109. page.c_bgcolor = get_data_value('background-color')
  110. page.c_bgimage = get_data_value('background')
  111. page.c_bgimagelogo = get_data_value('background-logo')
  112. page.c_menulogin = get_data_value('login-menu')
  113. page.c_includ_rp = get_data_value('includ-right-panel')
  114. return page
  115. def index(request):
  116. page = gen_page_sys('bienvenus')
  117. page.speeddial = gen_speeddial()
  118. template = loader.get_template('page.html')
  119. context = {
  120. 'page' : page,
  121. }
  122. return HttpResponse(template.render(context, request))
  123. def page(request, p_url):
  124. #print(p_url)
  125. p_url = "/"+p_url
  126. template = loader.get_template('page.html')
  127. try:
  128. page = Page.objects.get(p_adresse = p_url)
  129. page.p_menu_haut = gen_menu('haut')
  130. page.p_menu_pied = gen_menu('pied')
  131. page.p_meta_title = page.p_titre
  132. page.c_sitename = get_data_value('site-name')
  133. page.c_sitelogo = get_data_value('site-logo')
  134. page.c_bgcolor = get_data_value('background-color')
  135. page.c_bgimage = get_data_value('background')
  136. page.c_bgimagelogo = get_data_value('background-logo')
  137. page.c_menulogin = get_data_value('login-menu')
  138. except:
  139. page = gen_page_base()
  140. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  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))