views.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. from django.shortcuts import render
  2. from django.http import HttpResponseRedirect
  3. from .models import *
  4. from butler.forms import *
  5. # Create your views here.
  6. from django.core.mail import EmailMessage, get_connection
  7. import core.config_template as settings
  8. def send_email_mess(body, sub):
  9. with get_connection(
  10. host=settings.EMAIL_HOST,
  11. port=settings.EMAIL_PORT,
  12. username=settings.EMAIL_HOST_USER,
  13. password=settings.EMAIL_HOST_PASSWORD,
  14. use_tls=settings.EMAIL_USE_TLS,
  15. use_ssl=settings.EMAIL_USE_SSL
  16. ) as connection:
  17. #print(form.cleaned_data)
  18. subject = sub
  19. email_from = settings.EMAIL_HOST_USER
  20. recipient_list = [settings.EMAIL_TO]
  21. message = body
  22. EmailMessage(subject, message, email_from, recipient_list, connection=connection).send()
  23. def index_html(request):
  24. if request.POST:
  25. form = MyForm(request.POST)
  26. if form.is_valid():
  27. body = f"""
  28. Имя: {form.cleaned_data['name']}\n
  29. Email: {form.cleaned_data['email']}\n
  30. Комментарий: {form.cleaned_data['message']}
  31. """
  32. send_email_mess(body, "Пользователь оставил отзыв")
  33. else:
  34. form = MyForm()
  35. menu = ButlerPage.objects.filter(slug__startswith='clean')
  36. context = {
  37. # "mainpage":ButlerPage.objects.get(slug="main"),
  38. "menu":menu,
  39. "form":form
  40. }
  41. return render(request, 'butler/pages/main.html', context=context)
  42. def page_html(request):
  43. if request.POST:
  44. form = MyForm(request.POST)
  45. if form.is_valid():
  46. print(form.cleaned_data)
  47. else:
  48. form = MyForm()
  49. slug = (request.path[1:-1])
  50. if not ButlerPage.objects.filter(slug=slug):
  51. menu = ButlerPage.objects.filter(slug__startswith='clean')
  52. print(menu)
  53. if not ButlerPage.objects.filter(slug__startswith='clean'):
  54. menu = ButlerPage.objects.filter(slug__startswith='clean')
  55. context = {
  56. "form":form
  57. }
  58. else:
  59. context = {
  60. "form":form,
  61. "menu":menu
  62. }
  63. return render(request, 'butler/pages/in_work.html', context=context)
  64. else:
  65. menu = ButlerPage.objects.filter(slug__startswith='clean')
  66. print(menu)
  67. context = {
  68. "mainpage":ButlerPage.objects.get(slug=slug),
  69. "form":form,
  70. "menu":menu
  71. }
  72. return render(request, 'butler/pages/simple.html', context=context)
  73. def send_email(request):
  74. if request.method == 'POST':
  75. print(request.POST['email'])
  76. return HttpResponseRedirect('/')
  77. #simple page
  78. def contact_html(request):
  79. return render(request, 'butler/pages/contact.html')
  80. def price_html(request):
  81. return render(request, 'butler/pages/price.html')
  82. def order_html(request):
  83. if request.POST:
  84. form = OrderForm(request.POST)
  85. if form.is_valid():
  86. services = ""
  87. for item in form.cleaned_data['service']:
  88. services += f"{SERVICE_CHOICES[int(item)][1]}, "
  89. body = f"""
  90. Имя: {form.cleaned_data['name']}\n
  91. Фамилия: {form.cleaned_data['surname']}\n
  92. Номер телефона: {form.cleaned_data['phone']}\n
  93. Выбранные услуги: {services}\n
  94. Желаемое время оказания услуги: {form.cleaned_data['datetime']}\n
  95. Комментарий: {form.cleaned_data['comment'] or "Комментарий отсутствует"}
  96. """
  97. send_email_mess(body, "ЗАКАЗ")
  98. else:
  99. form = OrderForm()
  100. context = {
  101. "form":form
  102. }
  103. return render(request, 'butler/pages/order.html', context)
  104. def faq_html(request):
  105. if request.POST:
  106. form = MyForm(request.POST)
  107. if form.is_valid():
  108. body = f"""
  109. Имя: {form.cleaned_data['name']}\n
  110. Email: {form.cleaned_data['email']}\n
  111. Вопрос: {form.cleaned_data['message']}
  112. """
  113. send_email_mess(body, "Пользователь задал вопрос")
  114. else:
  115. form = MyForm()
  116. context = {
  117. "form":form,
  118. "faq":FAQmodel.objects.all()
  119. }
  120. return render(request, 'butler/pages/faq.html', context)