views.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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 django_tables2 import SingleTableView
  14. from .tables import *
  15. from django import template
  16. from django.views.generic.edit import UpdateView, CreateView
  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. {'title':'Информация о партнере', 'link':'partner_information/add/', 'sel':'person'},
  102. {'title':'Тарифы', 'link':'service', 'sel':'tikets'},
  103. ]
  104. def partner_information(request):
  105. context = get_context(request, {
  106. 'title':'Информация о партнере',
  107. })
  108. return render(request, 'SharixAdmin/partner_information.html', context)
  109. def get_context(request, page_context) -> dict:
  110. base_context = {
  111. "title":page_context['title'],
  112. 'url_path':resolve(request.path_info).url_name,
  113. 'menu':menu
  114. }
  115. context = dict(list(base_context.items()) + list(page_context.items()))
  116. return context
  117. class PartnersListView(SingleTableView):
  118. table_class = PartnersTable
  119. queryset = Company.objects.all()
  120. template_name = 'SharixAdmin/partners.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_list': context['object_list'],
  126. }))
  127. return context
  128. @login_required
  129. def change_provider_status(request):
  130. if request.method == 'POST':
  131. provider_id = request.POST.get('provider_id')
  132. new_status = request.POST.get('new_status')
  133. provider = Provider.objects.get(pk=provider_id)
  134. provider.status = new_status
  135. provider.save()
  136. return JsonResponse({'status': 'success'})
  137. else:
  138. return JsonResponse({'status': 'error'})
  139. class ServiceTariffUpdateView(UpdateView):
  140. model = Service
  141. form_class = ServiceTariffUpdateForm
  142. template_name = "SharixAdmin/service_tariff_form.html"
  143. def get_context_data(self, **kwargs):
  144. context = super().get_context_data(**kwargs)
  145. context.update(get_context(self.request, {
  146. 'title': 'Тарифы услуг',
  147. 'object': self.object,
  148. }))
  149. return context
  150. def get_success_url(self):
  151. return reverse('service_tariff')
  152. class ServiceTariffCreate(CreateView):
  153. model = Service
  154. form_class = ServiceTariffCreateForm
  155. template_name = "SharixAdmin/service_tariff_form.html"
  156. def get_context_data(self, **kwargs):
  157. context = super().get_context_data(**kwargs)
  158. context.update(get_context(self.request, {
  159. 'title': 'Тарифы услуг',
  160. 'object': self.object,
  161. }))
  162. return context
  163. def get_success_url(self):
  164. return reverse('service_tariff')
  165. class ProviderListView(SingleTableView):
  166. table_class = ProviderTable
  167. queryset = Provider.objects.all()
  168. template_name = 'SharixAdmin/provider.html'
  169. def get_context_data(self, **kwargs):
  170. context = super().get_context_data(**kwargs)
  171. context.update(get_context(self.request, {
  172. 'title': 'Исполнители',
  173. 'object_list': context['object_list'],
  174. }))
  175. return context
  176. class ServiceTariffListView(SingleTableView):
  177. table_class = ServiceTariffTable
  178. queryset = Service.objects.all()
  179. template_name = 'SharixAdmin/service_tariff.html'
  180. # paginate_by = 2
  181. def get_context_data(self, **kwargs):
  182. context = super().get_context_data(**kwargs)
  183. context.update(get_context(self.request, {
  184. 'title': 'Исполнители',
  185. 'object_list': context['object_list'],
  186. }))
  187. return context
  188. class ServiceTypeUpdateView(UpdateView):
  189. model = ServiceType
  190. form_class = ServiceTypeUpdateForm
  191. template_name = "SharixAdmin/service_type_form.html"
  192. def get_context_data(self, **kwargs):
  193. context = super().get_context_data(**kwargs)
  194. context.update(get_context(self.request, {
  195. 'title': 'Тарифы услуг',
  196. 'object_list': context['object_list'],
  197. }))
  198. return context
  199. class ResourceListView(SingleTableView):
  200. table_class = ResourceTable
  201. queryset = Resource.objects.all()
  202. template_name = 'SharixAdmin/resource.html'
  203. def get_context_data(self, **kwargs):
  204. context = super().get_context_data(**kwargs)
  205. context.update(get_context(self.request, {
  206. 'title': 'Ресурсы',
  207. 'object_list': context['object_list'],
  208. }))
  209. return context
  210. class ServiceTypeCreate(CreateView):
  211. model = ServiceType
  212. form_class = ServiceTypeCreateForm
  213. template_name = "SharixAdmin/service_type_form.html"
  214. def get_context_data(self, **kwargs):
  215. context = super().get_context_data(**kwargs)
  216. context.update(get_context(self.request, {
  217. 'title': 'Услуги сервиса',
  218. 'object': self.object,
  219. }))
  220. return context
  221. def get_success_url(self):
  222. return reverse('service_type')
  223. @login_required
  224. def change_partners_status(request):
  225. if request.method == 'POST':
  226. partners_id = request.POST.get('partners_id')
  227. new_status = request.POST.get('new_status')
  228. partners = Company.objects.get(pk=partners_id)
  229. partners.status = new_status
  230. partners.save()
  231. return JsonResponse({'status': 'success'})
  232. else:
  233. return JsonResponse({'status': 'error'})
  234. @login_required
  235. def change_resource_status(request):
  236. if request.method == 'POST':
  237. resource_id = request.POST.get('resource_id')
  238. new_status = request.POST.get('new_status')
  239. resource = Resource.objects.get(pk=resource_id)
  240. resource.status = new_status
  241. resource.save()
  242. return JsonResponse({'status': 'success'})
  243. else:
  244. return JsonResponse({'status': 'error'})
  245. class ServiceTypeDelete(DeleteView):
  246. model = ServiceType
  247. template_name = "SharixAdmin/service_type_delete.html"
  248. def get_context_data(self, **kwargs):
  249. context = super().get_context_data(**kwargs)
  250. context.update(get_context(self.request, {
  251. 'title': 'Услуги сервиса',
  252. 'object': self.object,
  253. }))
  254. return context
  255. def get_success_url(self):
  256. return reverse('service_type')
  257. class ServiceTypeListView(SingleTableView):
  258. table_class = ServiceTypeTable
  259. queryset = ServiceType.objects.all()
  260. template_name = 'SharixAdmin/service_type.html'
  261. # paginate_by = 2
  262. def get_context_data(self, **kwargs):
  263. context = super().get_context_data(**kwargs)
  264. context.update(get_context(self.request, {
  265. 'title': 'Услуги сервиса',
  266. 'object_list': context['object_list'],
  267. }))
  268. return context
  269. class ServiceInformationUpdateView(UpdateView):
  270. model = Service
  271. form_class = ServiceInformationUpdateForm
  272. template_name = "SharixAdmin/service_information_form.html"
  273. def get_context_data(self, **kwargs):
  274. context = super().get_context_data(**kwargs)
  275. context.update(get_context(self.request, {
  276. 'title': 'Информация о сервисе',
  277. 'object': self.object,
  278. }))
  279. return context
  280. def get_success_url(self):
  281. return reverse('test-page')
  282. class ServiceInformationCreate(CreateView):
  283. model = Service
  284. form_class = ServiceInformationCreateForm
  285. template_name = "SharixAdmin/service_information_form.html"
  286. def get_context_data(self, **kwargs):
  287. context = super().get_context_data(**kwargs)
  288. context.update(get_context(self.request, {
  289. 'title': 'Информация о сервисе',
  290. 'object': self.object,
  291. }))
  292. return context
  293. def get_success_url(self):
  294. return reverse('test-page')
  295. class ServiceListView(SingleTableView):
  296. table_class = ServiceTable
  297. queryset = Service.objects.all()
  298. template_name = 'SharixAdmin/service.html'
  299. def get_context_data(self, **kwargs):
  300. context = super().get_context_data(**kwargs)
  301. context.update(get_context(self.request, {
  302. 'title': 'Тарифы',
  303. 'object_list': context['object_list'],
  304. }))
  305. return context
  306. @login_required
  307. def change_service_status(request):
  308. if request.method == 'POST':
  309. service_id = request.POST.get('service_id')
  310. new_status = request.POST.get('new_status')
  311. service = Service.objects.get(pk=service_id)
  312. service.status = new_status
  313. service.save()
  314. #Shema views
  315. @login_required
  316. def schema_v3(request):
  317. return render(request, "SharixAdmin/schema.html")
  318. class PartnerInformationUpdateView(UpdateView):
  319. model = Company
  320. form_class = PartnerInformationUpdateForm
  321. template_name = "SharixAdmin/partner_information_form.html"
  322. def get_context_data(self, **kwargs):
  323. context = super().get_context_data(**kwargs)
  324. context.update(get_context(self.request, {
  325. 'title': 'Информация о партнере',
  326. 'object': self.object,
  327. }))
  328. return context
  329. def get_success_url(self):
  330. return reverse('test-page')
  331. class PartnerInformationCreate(CreateView):
  332. model = Company
  333. form_class = PartnerInformationCreateForm
  334. template_name = "SharixAdmin/partner_information_form.html"
  335. def get_context_data(self, **kwargs):
  336. context = super().get_context_data(**kwargs)
  337. context.update(get_context(self.request, {
  338. 'title': 'Информация о партнере',
  339. 'object': self.object,
  340. }))
  341. return context
  342. def get_success_url(self):
  343. return reverse('test-page')