views.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 == "site-version":
  46. data.d_titre = name
  47. data.d_type = "txt"
  48. data.d_variable = "Jan. 2024"
  49. data.save()
  50. elif name == "background-color":
  51. data.d_titre = name
  52. data.d_type = "txt"
  53. data.d_variable = "#999"
  54. data.save()
  55. elif name == "background":
  56. data.d_titre = name
  57. data.d_type = "txt"
  58. data.d_variable = "background.jpeg"
  59. data.save()
  60. elif name == "background-logo":
  61. data.d_titre = name
  62. data.d_type = "txt"
  63. data.d_variable = "logo-txt-Mrduhaz.png"
  64. data.save()
  65. elif name == "login-menu":
  66. data.d_titre = name
  67. data.d_type = "txt"
  68. data.d_variable = "True"
  69. data.save()
  70. elif name == "includ-right-panel":
  71. data.d_titre = name
  72. data.d_type = "txt"
  73. data.d_variable = "None"
  74. data.save()
  75. elif name == "card-main-panel":
  76. data.d_titre = name
  77. data.d_type = "txt"
  78. data.d_variable = "True"
  79. data.save()
  80. elif name == "card-right-panel":
  81. data.d_titre = name
  82. data.d_type = "txt"
  83. data.d_variable = "True"
  84. data.save()
  85. else :
  86. data.d_variable = "Blop"
  87. return data.d_variable
  88. def update_data_value(name, value):
  89. try:
  90. data = Data.objects.get(d_titre_slugify = name)
  91. data.d_variable = value
  92. data.save()
  93. except:
  94. data = Data.objects.none()
  95. data.d_variable = "Blop"
  96. return data.d_variable
  97. def gen_page_base():
  98. page = Page.objects.none()
  99. page.p_menu_haut = gen_menu('haut')
  100. page.p_menu_pied = gen_menu('pied')
  101. page.p_see_title_and_des_in_templates = True
  102. page.c_sitename = get_data_value('site-name')
  103. page.c_siteversion = get_data_value('site-version')
  104. page.c_sitelogo = get_data_value('site-logo')
  105. page.c_bgcolor = get_data_value('background-color')
  106. page.c_bgimage = get_data_value('background')
  107. page.c_bgimagelogo = get_data_value('background-logo')
  108. page.c_menulogin = get_data_value('login-menu')
  109. page.c_includ_rp = get_data_value('includ-right-panel')
  110. page.c_card_mp = get_data_value('card-main-panel')
  111. page.c_card_rp = get_data_value('card-right-panel')
  112. return page
  113. def gen_page_sys(p_titre_slugify):
  114. #print(p_titre_slugify)
  115. try :
  116. page = Page.objects.get(p_titre_slugify = p_titre_slugify)
  117. except:
  118. page = gen_page_base()
  119. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  120. page.p_titre = "404 ! Erreur sur la page demmandé"
  121. page.p_icone = "fas fa-bug"
  122. page.p_menu_haut = gen_menu('haut')
  123. page.p_menu_pied = gen_menu('pied')
  124. page.p_meta_title = page.p_titre
  125. page.c_sitename = get_data_value('site-name')
  126. page.c_sitelogo = get_data_value('site-logo')
  127. page.c_bgcolor = get_data_value('background-color')
  128. page.c_bgimage = get_data_value('background')
  129. page.c_bgimagelogo = get_data_value('background-logo')
  130. page.c_menulogin = get_data_value('login-menu')
  131. page.c_includ_rp = get_data_value('includ-right-panel')
  132. page.c_card_mp = get_data_value('card-main-panel')
  133. page.c_card_rp = get_data_value('card-right-panel')
  134. return page
  135. def index(request):
  136. page = gen_page_sys('bienvenus')
  137. page.speeddial = gen_speeddial()
  138. template = loader.get_template('page.html')
  139. context = {
  140. 'page' : page,
  141. }
  142. return HttpResponse(template.render(context, request))
  143. def page(request, p_url):
  144. #print(p_url)
  145. p_url = "/"+p_url
  146. template = loader.get_template('page.html')
  147. try:
  148. page = Page.objects.get(p_adresse = p_url)
  149. page.p_menu_haut = gen_menu('haut')
  150. page.p_menu_pied = gen_menu('pied')
  151. page.p_meta_title = page.p_titre
  152. page.c_sitename = get_data_value('site-name')
  153. page.c_sitelogo = get_data_value('site-logo')
  154. page.c_bgcolor = get_data_value('background-color')
  155. page.c_bgimage = get_data_value('background')
  156. page.c_bgimagelogo = get_data_value('background-logo')
  157. page.c_menulogin = get_data_value('login-menu')
  158. except:
  159. page = gen_page_base()
  160. page.p_contenu = "<h1>Erreur la page demandé n'existe pas </h1>"
  161. context = {
  162. 'page' : page,
  163. }
  164. return HttpResponse(template.render(context, request))
  165. def contact(request,):
  166. page = gen_page_base()
  167. template = loader.get_template('page.html')
  168. page.p_titre = "Nous Contacter"
  169. page.p_description = "Formulaire de prise de contact"
  170. page.p_contenu = "<p>Merci de remplir le formulaire pour nous contacter.</p>"
  171. page.p_adresse = "/contact"
  172. if request.method == "POST":
  173. form = ContactForm(request.POST)
  174. if form.is_valid():
  175. n_contact = form.save()
  176. page.p_contenu = "<p>Merci. Nous vous répondrons au vite.</p>"
  177. else :
  178. form = ContactForm()
  179. context = {
  180. 'page' : page,
  181. 'form' : form,
  182. }
  183. return HttpResponse(template.render(context, request))
  184. def p_login(request):
  185. next = request.GET.get('next','')
  186. page = gen_page_base()
  187. template = loader.get_template('login.html')
  188. if request.method == "POST":
  189. form = AuthenticationForm(request, data=request.POST)
  190. if form.is_valid():
  191. username = request.POST['username']
  192. password = request.POST['password']
  193. user = authenticate(request, username=username, password=password)
  194. if user is not None:
  195. login(request, user)
  196. messages.add_message(request, messages.INFO, 'Bonjour, vous êtes maintenant connecté')
  197. if next :
  198. return HttpResponseRedirect(next)
  199. else :
  200. return HttpResponseRedirect(reverse('core_index'))
  201. else :
  202. pass
  203. else :
  204. form = AuthenticationForm(request)
  205. context = {
  206. 'page' : page,
  207. 'form' : form,
  208. }
  209. return HttpResponse(template.render(context, request))
  210. def p_logout(request):
  211. logout(request)
  212. messages.add_message(request, messages.INFO, 'A bientôt')
  213. return HttpResponseRedirect(reverse('core_index'))
  214. def p_registration(request):
  215. page = gen_page_sys("inscription-sur-le-site")
  216. template = loader.get_template('page.html')
  217. page.p_f_titre = "Inscription"
  218. if request.method == "POST":
  219. form = UserCreationForm(request.POST)
  220. if form.is_valid():
  221. username = form.cleaned_data['username']
  222. password = form.cleaned_data['password1']
  223. user=User.objects.create_user(username=username, password=password)
  224. user.save()
  225. user = authenticate(username=username, password=password)
  226. login(request, user)
  227. messages.add_message(request, messages.INFO, 'Merci pour votre inscription')
  228. return HttpResponseRedirect(reverse('flux_control_panel'))
  229. else :
  230. form = UserCreationForm()
  231. context = {
  232. 'page' : page,
  233. 'form' : form,
  234. }
  235. return HttpResponse(template.render(context, request))