views.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 metaservicesynced.models import *
  9. from .forms import *
  10. from SharixAdmin.models import *
  11. from django.contrib.auth import logout
  12. from django.db.models import Q
  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':'partner_information/add/', 'sel':'person'},
  84. {'title':'Тарифы', 'link':'service', 'sel':'tikets'},
  85. ]
  86. def partner_information(request):
  87. context = get_context(request, {
  88. 'title':'Информация о партнере',
  89. })
  90. return render(request, 'SharixAdmin/partner_information.html', context)
  91. def get_context(request, page_context) -> dict:
  92. base_context = {
  93. "title":page_context['title'],
  94. 'url_path':resolve(request.path_info).url_name,
  95. 'menu':menu
  96. }
  97. context = dict(list(base_context.items()) + list(page_context.items()))
  98. return context
  99. class ServiceListView(SingleTableView):
  100. table_class = ServiceTable
  101. queryset = Service.objects.all()
  102. template_name = 'SharixAdmin/service.html'
  103. def get_context_data(self, **kwargs):
  104. context = super().get_context_data(**kwargs)
  105. context.update(get_context(self.request, {
  106. 'title': 'Тарифы',
  107. 'object_list': context['object_list'],
  108. }))
  109. return context
  110. @login_required
  111. def change_service_status(request):
  112. if request.method == 'POST':
  113. service_id = request.POST.get('service_id')
  114. new_status = request.POST.get('new_status')
  115. service = Service.objects.get(pk=service_id)
  116. service.status = new_status
  117. service.save()
  118. return JsonResponse({'status': 'success'})
  119. else:
  120. return JsonResponse({'status': 'error'})
  121. #Shema views
  122. @login_required
  123. def schema_v3(request):
  124. return render(request, "SharixAdmin/schema.html")
  125. class PartnerInformationUpdateView(UpdateView):
  126. model = Company
  127. form_class = PartnerInformationUpdateForm
  128. template_name = "SharixAdmin/partner_information_form.html"
  129. def get_context_data(self, **kwargs):
  130. context = super().get_context_data(**kwargs)
  131. context.update(get_context(self.request, {
  132. 'title': 'Информация о партнере',
  133. 'object': self.object,
  134. }))
  135. return context
  136. def get_success_url(self):
  137. return reverse('test-page')
  138. class PartnerInformationCreate(CreateView):
  139. model = Company
  140. form_class = PartnerInformationCreateForm
  141. template_name = "SharixAdmin/partner_information_form.html"
  142. def get_context_data(self, **kwargs):
  143. context = super().get_context_data(**kwargs)
  144. context.update(get_context(self.request, {
  145. 'title': 'Информация о партнере',
  146. 'object': self.object,
  147. }))
  148. return context
  149. def get_success_url(self):
  150. return reverse('test-page')