views.py 8.4 KB

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