views.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import json
  2. from django.shortcuts import render
  3. from django.http import HttpResponseRedirect, HttpResponse, JsonResponse
  4. from django.contrib.auth.views import LoginView
  5. from django.urls import reverse_lazy, resolve, reverse
  6. from django.contrib.auth.decorators import login_required
  7. from django_tables2 import SingleTableView
  8. from .forms import *
  9. from SharixAdmin.models import *
  10. from django.contrib.auth import logout
  11. from django.db.models import Q
  12. from .tables import *
  13. from django import template
  14. from django.views.generic.edit import UpdateView, CreateView
  15. # Create your views here.
  16. @login_required
  17. def index(request):
  18. ph_num = str(request.user.phone_number)
  19. convert_ph_num = f"+{ph_num[:1]} ({ph_num[1:4]}) {ph_num[4:7]}-{ph_num[7:9]}-{ph_num[9:11]}"
  20. #print(convert_ph_num)
  21. context = get_context(request, {
  22. 'title':'Главная/баланс',
  23. 'phone':convert_ph_num
  24. })
  25. return render(request, 'SharixAdmin/main.html', context)
  26. @login_required
  27. def transactions(request):
  28. context = get_context(request, {
  29. 'title':'История платежей',
  30. })
  31. return render(request, 'SharixAdmin/transactions.html', context)
  32. @login_required
  33. def trans_id(request, trans_id):
  34. context = get_context(request, {
  35. 'title':'Услуга'
  36. })
  37. return render(request, 'SharixAdmin/trans_carried.html', context)
  38. @login_required
  39. def balance(request):
  40. context = get_context(request, {
  41. 'title':'Пополнить баланс'
  42. })
  43. if request.method == 'POST':
  44. if float(request.POST['price']) > 0:
  45. context = get_context(request, {
  46. 'title':'Пополнить баланс',
  47. 'msg':'Оплата прошла успешно ;)'
  48. })
  49. return render(request, "SharixAdmin/balance_success.html", context)
  50. else:
  51. context = get_context(request, {
  52. 'title':'Пополнить баланс',
  53. 'msg':'Оплата не прошла ;('
  54. })
  55. return render(request, "SharixAdmin/balance_success.html", context)
  56. return render(request, "SharixAdmin/balance.html", context)
  57. def logout_view(request):
  58. logout(request)
  59. return HttpResponseRedirect(reverse('authweb'))
  60. class LoginSharix(LoginView):
  61. form_class = LoginUserForm
  62. template_name = 'SharixAdmin/auth.html'
  63. def get_success_url(self):
  64. print(self.request.GET['next'])
  65. return reverse_lazy('home')
  66. def testPage(request):
  67. context = get_context(request, {
  68. 'title':'Страница в разработке'
  69. })
  70. return render(request, "SharixAdmin/test.html", context)
  71. menu = [
  72. {'title':'Главная/баланс', 'link':'home', 'sel':'house'},
  73. {'title':'Платежная информация', 'link':'test-page', 'sel':'credit-card'},
  74. {'title':'История платежей', 'link':'trans','sel':'clock-history'},
  75. {'title':'Курсы', 'link':'course', 'sel':'education'},
  76. {'title':'Личная информация', 'link':'test-page', 'sel':'person'},
  77. {'title':'Управление сервисами', 'link':'test-page', 'sel':'hdd-network'},
  78. {'title':'Мои связи', 'link':'test-page', 'sel':'people'},
  79. {'title':'Сотрудничество', 'link':'test-page', 'sel':'sotrud'},
  80. {'title':'Техподдержка', 'link':'test-page', 'sel':'gear'},
  81. {'title':'Мои заявки', 'link':'tickets', 'sel':'tikets'},
  82. {'title':'Исполнители', 'link':'provider', 'sel':'people'},
  83. {'title':'Тарифы услуг', 'link':'service_tariff', 'sel':'person'},
  84. ]
  85. def get_context(request, page_context) -> dict:
  86. base_context = {
  87. "title":page_context['title'],
  88. 'url_path':resolve(request.path_info).url_name,
  89. 'menu':menu
  90. }
  91. context = dict(list(base_context.items()) + list(page_context.items()))
  92. return context
  93. @login_required
  94. def change_provider_status(request):
  95. if request.method == 'POST':
  96. provider_id = request.POST.get('provider_id')
  97. new_status = request.POST.get('new_status')
  98. provider = Provider.objects.get(pk=provider_id)
  99. provider.status = new_status
  100. provider.save()
  101. return JsonResponse({'status': 'success'})
  102. else:
  103. return JsonResponse({'status': 'error'})
  104. class ServiceTariffUpdateView(UpdateView):
  105. model = Service
  106. form_class = ServiceTariffUpdateForm
  107. template_name = "SharixAdmin/service_tariff_form.html"
  108. def get_context_data(self, **kwargs):
  109. context = super().get_context_data(**kwargs)
  110. context.update(get_context(self.request, {
  111. 'title': 'Тарифы услуг',
  112. 'object': self.object,
  113. }))
  114. return context
  115. def get_success_url(self):
  116. return reverse('service_tariff')
  117. class ServiceTariffCreate(CreateView):
  118. model = Service
  119. form_class = ServiceTariffCreateForm
  120. template_name = "SharixAdmin/service_tariff_form.html"
  121. def get_context_data(self, **kwargs):
  122. context = super().get_context_data(**kwargs)
  123. context.update(get_context(self.request, {
  124. 'title': 'Тарифы услуг',
  125. 'object': self.object,
  126. }))
  127. return context
  128. def get_success_url(self):
  129. return reverse('service_tariff')
  130. class ProviderListView(SingleTableView):
  131. table_class = ProviderTable
  132. queryset = Provider.objects.all()
  133. template_name = 'SharixAdmin/provider.html'
  134. def get_context_data(self, **kwargs):
  135. context = super().get_context_data(**kwargs)
  136. context.update(get_context(self.request, {
  137. 'title': 'Исполнители',
  138. 'object_list': context['object_list'],
  139. }))
  140. return context
  141. class ServiceTariffListView(SingleTableView):
  142. table_class = ServiceTariffTable
  143. queryset = Service.objects.all()
  144. template_name = 'SharixAdmin/service_tariff.html'
  145. # paginate_by = 2
  146. def get_context_data(self, **kwargs):
  147. context = super().get_context_data(**kwargs)
  148. context.update(get_context(self.request, {
  149. 'title': 'Тарифы услуг',
  150. 'object_list': context['object_list'],
  151. }))
  152. return context
  153. #Shema views
  154. @login_required
  155. def schema_v3(request):
  156. return render(request, "SharixAdmin/schema.html")