views.py 5.8 KB

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