views.py 7.2 KB

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