views.py 8.2 KB

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