Advertisement
Mochinov

Untitled

May 23rd, 2022
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. from typing import Optional, Union
  2.  
  3. from django.contrib.auth.decorators import login_required
  4. from django.contrib.auth.models import User
  5. from django.db.models import QuerySet
  6. from django.http import HttpRequest
  7. from django.utils.decorators import method_decorator
  8. from rest_framework import permissions
  9. from rest_framework.decorators import permission_classes
  10. from rest_framework.request import Request
  11. from rest_framework.response import Response
  12.  
  13.  
  14. from misc.to_int import safe_to_int
  15. from misc.views.generic_api_single_view import GenericApiSingleView
  16. from misc.views.generic_api_view import GenericApiView
  17. from misc.views.generic_filters import GenericFilters
  18. from misc.views.generic_responces import get_status_response
  19. from misc.views.table_style import TableStyle
  20. from crm_softphone.models import Trunk, TrunkLVProject
  21. from partners.models import LVProject
  22.  
  23.  
  24. @permission_classes((permissions.AllowAny,))
  25. class TrunkLVProjectTableView(GenericApiView):
  26. """Определяет структуру и содержание ответа на запросы к данным таблицы LVProject."""
  27.  
  28. table_name = 'TrunkLVProject'
  29. table_style = TableStyle.get_table_style(hide_multiple_table_add=True)
  30.  
  31. class Filters(GenericFilters):
  32. pass
  33.  
  34. FIELDS_FILTERS = [
  35. GenericApiView.get_filter(
  36. filter_name='id_field', field_name='id',
  37. filer_method=Filters.generic_int_equal_filter
  38. ),
  39. GenericApiView.get_filter(
  40. filter_name='trunks_field', field_name='trunks_id',
  41. filer_method=Filters.generic_int_equal_filter
  42. ),
  43. ]
  44.  
  45. PERMISSIONS = {
  46. 'create': 'partners.crm_partners_lvproject_add',
  47. 'view': 'partners.crm_partners_lvproject_view',
  48. # 'edit': 'partners.crm_partners_lvproject_edit',
  49. 'delete': 'partners.crm_partners_lvproject_delete',
  50. }
  51.  
  52. def get_empty_item_prefilled_fields(self, context: dict) -> dict:
  53. """Возвращает список предзаполненных полей для пустого элемента."""
  54.  
  55. trunks_field = context.get('trunks_field')
  56. prefilled_fields = {
  57. 'trunks_field': trunks_field,
  58. }
  59.  
  60. return prefilled_fields
  61.  
  62. @classmethod
  63. def get_field_dict(cls, context: dict, user: User) -> dict:
  64. """Распаковывает ответ и возвращает словарь со значениями полей"""
  65.  
  66. print(context)
  67.  
  68. print(
  69. context.get('trunks_field'),
  70. context.get('lv_project_field'),
  71.  
  72. TrunkLVProjectTableView.get_unpack_value(context.get('trunks_field')),
  73. TrunkLVProjectTableView.get_unpack_value(context.get('lv_project_field')),
  74. )
  75.  
  76. trunks_id_field = TrunkLVProjectTableView.get_unpack_value(context.get('trunks_field'))
  77. trunks_id_field = safe_to_int(trunks_id_field, -1)
  78. trunks = Trunk.objects.filter(id=trunks_id_field).first()
  79.  
  80. lv_project_id_field = TrunkLVProjectTableView.get_unpack_value(context.get('lv_project_field'))
  81. lv_project_id_field = safe_to_int(lv_project_id_field, -1)
  82. lv_project = LVProject.objects.filter(id=lv_project_id_field).first()
  83.  
  84.  
  85. data = {
  86. 'lv_project': lv_project,
  87. 'trunks': trunks
  88. }
  89.  
  90. print(data)
  91.  
  92. return data
  93.  
  94. def get_queryset(self, context: dict, user: User) -> QuerySet:
  95. """Возвращает базовый (до фильтрации и пагинации) QuerySet для таблицы."""
  96.  
  97. return TrunkLVProject.objects.all().select_related('lv_project')
  98.  
  99.  
  100. @permission_classes((permissions.AllowAny,))
  101. class TrunkLVProjectElementView(GenericApiSingleView):
  102. """Определяет структуру и содержание ответа на запросы к данным элемента LVProject."""
  103.  
  104. table_name = 'TrunkLVProject'
  105. element_verbose = 'Проект LV'
  106. table_view_model = TrunkLVProjectTableView
  107.  
  108. PERMISSIONS = {
  109. 'create': 'partners.crm_partners_lvproject_add',
  110. 'view': 'partners.crm_partners_lvproject_view',
  111. 'edit': 'partners.crm_partners_lvproject_edit',
  112. 'delete': 'partners.crm_partners_lvproject_delete',
  113. }
  114.  
  115. def get_queryset(self, context: dict, user: User) -> QuerySet:
  116. """Возвращает базовый (до фильтрации и пагинации) QuerySet."""
  117.  
  118. return TrunkLVProject.objects.all()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement