Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TrunkLVProjectSerializer(GenericSerializer):
- """
- Отвечает за сериализация данных модели TrunkLVProject (табличное представление),
- оправляемых в ответе на запрос.
- """
- readonly_field = ['id_field']
- mandatory_fields = ['id_field',]
- create_field = [
- 'lv_project_field',
- ]
- id_field = GenericSerializerField(
- model_field_name='id',
- )
- lv_project_name_field = GenericSerializerField(
- model_field_name='lv_project__name'
- )
- trunks_login_field = GenericSerializerField(
- model_field_name='trunks__login'
- )
- trunks_password_field = GenericSerializerField(
- model_field_name='trunks__password'
- )
- trunks_hostname_field = GenericSerializerField(
- model_field_name='trunks__hostname'
- )
- # Поля не связанные с моделью
- more_info_btn_field = serializers.SerializerMethodField()
- delete_btn_field = serializers.SerializerMethodField()
- fields_information = {
- 'id_field': GenericSerializer.get_field_information(
- model_name='id', field_verbose='ID',
- ),
- 'lv_project_name_field': GenericSerializer.get_field_information(
- model_name='lv_project__name', field_verbose='Наименование проекта',
- ),
- 'trunks_login_field': GenericSerializer.get_field_information(
- model_name='trunks__login', field_verbose='Логин',
- ),
- 'trunks_password_field': GenericSerializer.get_field_information(
- model_name='trunks__password', field_verbose='Пароль'
- ),
- 'trunks_hostname_field': GenericSerializer.get_field_information(
- model_name='trunks__hostname', field_verbose='Хост'
- ),
- # Поля не связанные с моделью
- 'delete_btn_field': GenericSerializer.get_field_information(),
- 'more_info_btn_field': GenericSerializer.get_field_information(),
- }
- def get_more_info_btn_field(self, obj: LVProject) -> dict:
- """Определяет возвращаемое значение для поля кнопки перехода к проекту LV."""
- url = reverse('lvproject_retrieve', args=[obj.id])
- verbose = 'Подробнее'
- return self.get_button_field(action=GenericSerializer.ButtonActions.REDIRECT, url=url, verbose=verbose)
- def get_delete_btn_field(self, obj: LVProject) -> dict:
- """Определяет возвращаемое значение для поля кнопки удаления."""
- url = reverse('lvproject_retrieve', args=[obj.id])
- verbose = 'Удалить'
- return self.get_button_field(action=GenericSerializer.ButtonActions.DELETE, url=url, verbose=verbose)
- class Meta:
- model = TrunkLVProject
- depth = 1
- fields = [
- 'id_field',
- 'lv_project_name_field',
- 'trunks_login_field',
- 'trunks_password_field',
- 'trunks_hostname_field',
- 'delete_btn_field',
- 'more_info_btn_field',
- ]
- class SingleTrunkLVProjectSerializer(GenericSerializer):
- """
- Отвечает за сериализация данных модели TrunkLVProject (табличное представление),
- оправляемых в ответе на запрос.
- """
- readonly_field = ['id_field']
- mandatory_fields = ['id_field',]
- create_field = [
- 'lv_project_field',
- ]
- id_field = GenericSerializerField(model_field_name='id')
- lv_project_field = GenericSerializerField(
- model_field_name='lv_project_id',
- )
- trunks_field = GenericSerializerField(
- model_field_name='trunks_id',
- )
- fields_information = {
- 'id_field': GenericSerializer.get_field_information(
- model_name='id', field_verbose='ID',
- ),
- 'lv_project_field': GenericSerializer.get_field_information(
- model_name='lv_project_id', field_verbose='ID',
- ),
- 'trunks_field': GenericSerializer.get_field_information(
- model_name='trunks_id', field_verbose='ID',
- ),
- }
- class Meta:
- model = TrunkLVProject
- depth = 1
- fields = [
- 'id_field',
- 'lv_project_field',
- 'trunks_field',
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement