Advertisement
Mochinov

Untitled

May 23rd, 2022
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1.  
  2. class TrunkLVProjectSerializer(GenericSerializer):
  3. """
  4. Отвечает за сериализация данных модели TrunkLVProject (табличное представление),
  5. оправляемых в ответе на запрос.
  6. """
  7.  
  8. readonly_field = ['id_field']
  9. mandatory_fields = ['id_field',]
  10. create_field = [
  11. 'lv_project_field',
  12. ]
  13.  
  14. id_field = GenericSerializerField(
  15. model_field_name='id',
  16. )
  17. lv_project_name_field = GenericSerializerField(
  18. model_field_name='lv_project__name'
  19. )
  20. trunks_login_field = GenericSerializerField(
  21. model_field_name='trunks__login'
  22. )
  23. trunks_password_field = GenericSerializerField(
  24. model_field_name='trunks__password'
  25. )
  26. trunks_hostname_field = GenericSerializerField(
  27. model_field_name='trunks__hostname'
  28. )
  29.  
  30. # Поля не связанные с моделью
  31. more_info_btn_field = serializers.SerializerMethodField()
  32. delete_btn_field = serializers.SerializerMethodField()
  33.  
  34. fields_information = {
  35. 'id_field': GenericSerializer.get_field_information(
  36. model_name='id', field_verbose='ID',
  37. ),
  38. 'lv_project_name_field': GenericSerializer.get_field_information(
  39. model_name='lv_project__name', field_verbose='Наименование проекта',
  40. ),
  41. 'trunks_login_field': GenericSerializer.get_field_information(
  42. model_name='trunks__login', field_verbose='Логин',
  43. ),
  44. 'trunks_password_field': GenericSerializer.get_field_information(
  45. model_name='trunks__password', field_verbose='Пароль'
  46. ),
  47. 'trunks_hostname_field': GenericSerializer.get_field_information(
  48. model_name='trunks__hostname', field_verbose='Хост'
  49. ),
  50.  
  51. # Поля не связанные с моделью
  52. 'delete_btn_field': GenericSerializer.get_field_information(),
  53. 'more_info_btn_field': GenericSerializer.get_field_information(),
  54. }
  55.  
  56.  
  57. def get_more_info_btn_field(self, obj: LVProject) -> dict:
  58. """Определяет возвращаемое значение для поля кнопки перехода к проекту LV."""
  59.  
  60. url = reverse('lvproject_retrieve', args=[obj.id])
  61. verbose = 'Подробнее'
  62. return self.get_button_field(action=GenericSerializer.ButtonActions.REDIRECT, url=url, verbose=verbose)
  63.  
  64.  
  65. def get_delete_btn_field(self, obj: LVProject) -> dict:
  66. """Определяет возвращаемое значение для поля кнопки удаления."""
  67.  
  68. url = reverse('lvproject_retrieve', args=[obj.id])
  69. verbose = 'Удалить'
  70. return self.get_button_field(action=GenericSerializer.ButtonActions.DELETE, url=url, verbose=verbose)
  71.  
  72. class Meta:
  73. model = TrunkLVProject
  74. depth = 1
  75. fields = [
  76. 'id_field',
  77. 'lv_project_name_field',
  78. 'trunks_login_field',
  79. 'trunks_password_field',
  80. 'trunks_hostname_field',
  81. 'delete_btn_field',
  82. 'more_info_btn_field',
  83. ]
  84.  
  85.  
  86. class SingleTrunkLVProjectSerializer(GenericSerializer):
  87. """
  88. Отвечает за сериализация данных модели TrunkLVProject (табличное представление),
  89. оправляемых в ответе на запрос.
  90. """
  91.  
  92. readonly_field = ['id_field']
  93. mandatory_fields = ['id_field',]
  94. create_field = [
  95. 'lv_project_field',
  96. ]
  97.  
  98. id_field = GenericSerializerField(model_field_name='id')
  99. lv_project_field = GenericSerializerField(
  100. model_field_name='lv_project_id',
  101. )
  102. trunks_field = GenericSerializerField(
  103. model_field_name='trunks_id',
  104. )
  105.  
  106. fields_information = {
  107. 'id_field': GenericSerializer.get_field_information(
  108. model_name='id', field_verbose='ID',
  109. ),
  110. 'lv_project_field': GenericSerializer.get_field_information(
  111. model_name='lv_project_id', field_verbose='ID',
  112. ),
  113. 'trunks_field': GenericSerializer.get_field_information(
  114. model_name='trunks_id', field_verbose='ID',
  115. ),
  116. }
  117.  
  118. class Meta:
  119. model = TrunkLVProject
  120. depth = 1
  121. fields = [
  122. 'id_field',
  123. 'lv_project_field',
  124. 'trunks_field',
  125. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement