views.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. from metaservicesynced.models import ServiceType
  17. from django.core import serializers
  18. from django_tables2 import SingleTableView
  19. from django.views.generic.edit import UpdateView, CreateView, DeleteView
  20. # Create your views here.
  21. @login_required
  22. def index(request):
  23. ph_num = str(request.user.phone_number)
  24. convert_ph_num = f"+{ph_num[:1]} ({ph_num[1:4]}) {ph_num[4:7]}-{ph_num[7:9]}-{ph_num[9:11]}"
  25. #print(convert_ph_num)
  26. context = get_context(request, {
  27. 'title':'Главная/баланс',
  28. 'phone':convert_ph_num
  29. })
  30. return render(request, 'SharixAdmin/main.html', context)
  31. @login_required
  32. def transactions(request):
  33. context = get_context(request, {
  34. 'title':'История платежей',
  35. })
  36. return render(request, 'SharixAdmin/transactions.html', context)
  37. @login_required
  38. def servicetype(request):
  39. service_types = ServiceType.objects.all()
  40. context = get_context(request, {
  41. 'title':'Услуги сервиса',
  42. 'service_types':service_types,
  43. })
  44. return render(request, 'SharixAdmin/servicetype.html', context)
  45. @login_required
  46. def trans_id(request, trans_id):
  47. context = get_context(request, {
  48. 'title':'Услуга'
  49. })
  50. return render(request, 'SharixAdmin/trans_carried.html', context)
  51. @login_required
  52. def balance(request):
  53. context = get_context(request, {
  54. 'title':'Пополнить баланс'
  55. })
  56. if request.method == 'POST':
  57. if float(request.POST['price']) > 0:
  58. context = get_context(request, {
  59. 'title':'Пополнить баланс',
  60. 'msg':'Оплата прошла успешно ;)'
  61. })
  62. return render(request, "SharixAdmin/balance_success.html", context)
  63. else:
  64. context = get_context(request, {
  65. 'title':'Пополнить баланс',
  66. 'msg':'Оплата не прошла ;('
  67. })
  68. return render(request, "SharixAdmin/balance_success.html", context)
  69. return render(request, "SharixAdmin/balance.html", context)
  70. def logout_view(request):
  71. logout(request)
  72. return HttpResponseRedirect(reverse('authweb'))
  73. class LoginSharix(LoginView):
  74. form_class = LoginUserForm
  75. template_name = 'SharixAdmin/auth.html'
  76. def get_success_url(self):
  77. print(self.request.GET['next'])
  78. return reverse_lazy('home')
  79. def testPage(request):
  80. context = get_context(request, {
  81. 'title':'Страница в разработке'
  82. })
  83. return render(request, "SharixAdmin/test.html", context)
  84. menu = [
  85. {'title':'Главная/баланс', 'link':'home', 'sel':'house'},
  86. {'title':'Платежная информация', 'link':'test-page', 'sel':'credit-card'},
  87. {'title':'История платежей', 'link':'trans','sel':'clock-history'},
  88. {'title':'Курсы', 'link':'course', 'sel':'education'},
  89. {'title':'Личная информация', 'link':'test-page', 'sel':'person'},
  90. {'title':'Управление сервисами', 'link':'test-page', 'sel':'hdd-network'},
  91. {'title':'Мои связи', 'link':'test-page', 'sel':'people'},
  92. {'title':'Сотрудничество', 'link':'test-page', 'sel':'sotrud'},
  93. {'title':'Техподдержка', 'link':'test-page', 'sel':'gear'},
  94. {'title':'Мои заявки', 'link':'tickets', 'sel':'tikets'},
  95. {'title':'Исполнители', 'link':'provider', 'sel':'people'},
  96. {'title':'Тарифы услуг', 'link':'service_tariff', 'sel':'person'},
  97. {'title':'Партнеры', 'link':'partners', 'sel':'people'},
  98. {'title':'Ресурсы', 'link':'resource', 'sel':'sotrud'},
  99. {'title':'Услуги сервиса', 'link':'service_type', 'sel':'hdd-network'},
  100. {'title':'Информация о сервисе', 'link':'partner_information/add/', 'sel':'hdd-network'},
  101. ]
  102. def get_context(request, page_context) -> dict:
  103. base_context = {
  104. "title":page_context['title'],
  105. 'url_path':resolve(request.path_info).url_name,
  106. 'menu':menu
  107. }
  108. context = dict(list(base_context.items()) + list(page_context.items()))
  109. return context
  110. class PartnersListView(SingleTableView):
  111. table_class = PartnersTable
  112. queryset = Company.objects.all()
  113. template_name = 'SharixAdmin/partners.html'
  114. def get_context_data(self, **kwargs):
  115. context = super().get_context_data(**kwargs)
  116. context.update(get_context(self.request, {
  117. 'title': 'Партнеры',
  118. 'object_list': context['object_list'],
  119. }))
  120. return context
  121. @login_required
  122. def change_provider_status(request):
  123. if request.method == 'POST':
  124. provider_id = request.POST.get('provider_id')
  125. new_status = request.POST.get('new_status')
  126. provider = Provider.objects.get(pk=provider_id)
  127. provider.status = new_status
  128. provider.save()
  129. return JsonResponse({'status': 'success'})
  130. else:
  131. return JsonResponse({'status': 'error'})
  132. class ServiceTariffUpdateView(UpdateView):
  133. model = Service
  134. form_class = ServiceTariffUpdateForm
  135. template_name = "SharixAdmin/service_tariff_form.html"
  136. def get_context_data(self, **kwargs):
  137. context = super().get_context_data(**kwargs)
  138. context.update(get_context(self.request, {
  139. 'title': 'Тарифы услуг',
  140. 'object': self.object,
  141. }))
  142. return context
  143. def get_success_url(self):
  144. return reverse('service_tariff')
  145. class ServiceTariffCreate(CreateView):
  146. model = Service
  147. form_class = ServiceTariffCreateForm
  148. template_name = "SharixAdmin/service_tariff_form.html"
  149. def get_context_data(self, **kwargs):
  150. context = super().get_context_data(**kwargs)
  151. context.update(get_context(self.request, {
  152. 'title': 'Тарифы услуг',
  153. 'object': self.object,
  154. }))
  155. return context
  156. def get_success_url(self):
  157. return reverse('service_tariff')
  158. class ProviderListView(SingleTableView):
  159. table_class = ProviderTable
  160. queryset = Provider.objects.all()
  161. template_name = 'SharixAdmin/provider.html'
  162. def get_context_data(self, **kwargs):
  163. context = super().get_context_data(**kwargs)
  164. context.update(get_context(self.request, {
  165. 'title': 'Исполнители',
  166. 'object_list': context['object_list'],
  167. }))
  168. return context
  169. class ServiceTariffListView(SingleTableView):
  170. table_class = ServiceTariffTable
  171. queryset = Service.objects.all()
  172. template_name = 'SharixAdmin/service_tariff.html'
  173. # paginate_by = 2
  174. def get_context_data(self, **kwargs):
  175. context = super().get_context_data(**kwargs)
  176. context.update(get_context(self.request, {
  177. 'title': 'Исполнители',
  178. 'object_list': context['object_list'],
  179. }))
  180. return context
  181. class ServiceTypeUpdateView(UpdateView):
  182. model = ServiceType
  183. form_class = ServiceTypeUpdateForm
  184. template_name = "SharixAdmin/service_type_form.html"
  185. def get_context_data(self, **kwargs):
  186. context = super().get_context_data(**kwargs)
  187. context.update(get_context(self.request, {
  188. 'title': 'Тарифы услуг',
  189. 'object_list': context['object_list'],
  190. }))
  191. return context
  192. class ResourceListView(SingleTableView):
  193. table_class = ResourceTable
  194. queryset = Resource.objects.all()
  195. template_name = 'SharixAdmin/resource.html'
  196. def get_context_data(self, **kwargs):
  197. context = super().get_context_data(**kwargs)
  198. context.update(get_context(self.request, {
  199. 'title': 'Ресурсы',
  200. 'object_list': context['object_list'],
  201. }))
  202. return context
  203. class ServiceTypeCreate(CreateView):
  204. model = ServiceType
  205. form_class = ServiceTypeCreateForm
  206. template_name = "SharixAdmin/service_type_form.html"
  207. def get_context_data(self, **kwargs):
  208. context = super().get_context_data(**kwargs)
  209. context.update(get_context(self.request, {
  210. 'title': 'Услуги сервиса',
  211. 'object': self.object,
  212. }))
  213. return context
  214. def get_success_url(self):
  215. return reverse('service_type')
  216. @login_required
  217. def change_partners_status(request):
  218. if request.method == 'POST':
  219. partners_id = request.POST.get('partners_id')
  220. new_status = request.POST.get('new_status')
  221. partners = Company.objects.get(pk=partners_id)
  222. partners.status = new_status
  223. partners.save()
  224. return JsonResponse({'status': 'success'})
  225. else:
  226. return JsonResponse({'status': 'error'})
  227. @login_required
  228. def change_resource_status(request):
  229. if request.method == 'POST':
  230. resource_id = request.POST.get('resource_id')
  231. new_status = request.POST.get('new_status')
  232. resource = Resource.objects.get(pk=resource_id)
  233. resource.status = new_status
  234. resource.save()
  235. return JsonResponse({'status': 'success'})
  236. else:
  237. return JsonResponse({'status': 'error'})
  238. class ServiceTypeDelete(DeleteView):
  239. model = ServiceType
  240. template_name = "SharixAdmin/service_type_delete.html"
  241. def get_context_data(self, **kwargs):
  242. context = super().get_context_data(**kwargs)
  243. context.update(get_context(self.request, {
  244. 'title': 'Услуги сервиса',
  245. 'object': self.object,
  246. }))
  247. return context
  248. def get_success_url(self):
  249. return reverse('service_type')
  250. class ServiceTypeListView(SingleTableView):
  251. table_class = ServiceTypeTable
  252. queryset = ServiceType.objects.all()
  253. template_name = 'SharixAdmin/service_type.html'
  254. # paginate_by = 2
  255. def get_context_data(self, **kwargs):
  256. context = super().get_context_data(**kwargs)
  257. context.update(get_context(self.request, {
  258. 'title': 'Услуги сервиса',
  259. 'object_list': context['object_list'],
  260. }))
  261. return context
  262. class ServiceInformationUpdateView(UpdateView):
  263. model = Service
  264. form_class = ServiceInformationUpdateForm
  265. template_name = "SharixAdmin/service_information_form.html"
  266. def get_context_data(self, **kwargs):
  267. context = super().get_context_data(**kwargs)
  268. context.update(get_context(self.request, {
  269. 'title': 'Информация о сервисе',
  270. 'object': self.object,
  271. }))
  272. return context
  273. def get_success_url(self):
  274. return reverse('test-page')
  275. class ServiceInformationCreate(CreateView):
  276. model = Service
  277. form_class = ServiceInformationCreateForm
  278. template_name = "SharixAdmin/service_information_form.html"
  279. def get_context_data(self, **kwargs):
  280. context = super().get_context_data(**kwargs)
  281. context.update(get_context(self.request, {
  282. 'title': 'Информация о сервисе',
  283. 'object': self.object,
  284. }))
  285. return context
  286. def get_success_url(self):
  287. return reverse('test-page')
  288. #Shema views
  289. @login_required
  290. def schema_v3(request):
  291. return render(request, "SharixAdmin/schema.html")