views.py 6.5 KB

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