serializers.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # @Author : Rocky
  2. # @File : serializers.py
  3. # @Time : 2023/7/24 13:35
  4. from rest_framework import serializers
  5. from rest_framework.serializers import ModelSerializer, SerializerMethodField
  6. from background.Object import AmazonS3Util
  7. from background.models import ProductInfo, VideoInfo, QuickStartInfo, UpgradeFirmwareInfo
  8. bucket_name = 'ansjerfilemanager'
  9. class ReadWriteSerializerMethodField(SerializerMethodField):
  10. """
  11. 支持可读写的SerializerMethodField
  12. 可实现Model字段和Serializer字段更加灵活地解绑
  13. 通过实现get_xxxfield方法,实现从Model的某个字段读值映射到Serializer对应字段
  14. 通过实现set_xxxfield方法,实现从Serializer字段回填值到Model对应字段
  15. """
  16. def __init__(self, method_name=None, write_method_name=None, **kwargs):
  17. self.method_name = method_name
  18. self.write_method_name = write_method_name
  19. kwargs["source"] = "*"
  20. super(SerializerMethodField, self).__init__(**kwargs)
  21. def bind(self, field_name, parent):
  22. # 绑定读函数 get_{field_name} 和写函数 set_{field_name}
  23. default_method_name = f"get_{field_name}"
  24. default_write_method_name = f"set_{field_name}"
  25. if self.method_name is None:
  26. self.method_name = default_method_name
  27. if self.write_method_name is None:
  28. self.write_method_name = default_write_method_name
  29. super(SerializerMethodField, self).bind(field_name, parent)
  30. def to_representation(self, value):
  31. # 读取过程hook
  32. method = getattr(self.parent, self.method_name)
  33. return method(value)
  34. def to_internal_value(self, data):
  35. # 写入过程hook
  36. method = getattr(self.parent, self.write_method_name)
  37. return method(data)
  38. class ProductInfoSerializer(ModelSerializer):
  39. class Meta:
  40. model = ProductInfo
  41. fields = '__all__'
  42. class VideoInSerializer(ModelSerializer):
  43. link = ReadWriteSerializerMethodField()
  44. class Meta:
  45. model = VideoInfo
  46. fields = '__all__'
  47. @staticmethod
  48. def get_link(obj):
  49. file_key = 'vsees/video_file/{}'.format(obj.link)
  50. s3_obj = AmazonS3Util()
  51. link = s3_obj.generate_file_obj_url(bucket_name, file_key)
  52. return link
  53. @staticmethod
  54. def set_link(obj):
  55. return {'link': obj}
  56. class QuickStartInfoSerializer(ModelSerializer):
  57. link = ReadWriteSerializerMethodField()
  58. class Meta:
  59. model = QuickStartInfo
  60. fields = '__all__'
  61. @staticmethod
  62. def get_link(obj):
  63. file_key = 'vsees/quick_start_file/{}'.format(obj.link)
  64. s3_obj = AmazonS3Util()
  65. link = s3_obj.generate_file_obj_url(bucket_name, file_key)
  66. return link
  67. @staticmethod
  68. def set_link(obj):
  69. return {'link': obj}
  70. class UpgradeFirmwareInfoSerializer(ModelSerializer):
  71. link = ReadWriteSerializerMethodField()
  72. class Meta:
  73. model = UpgradeFirmwareInfo
  74. fields = '__all__'
  75. @staticmethod
  76. def get_link(obj):
  77. file_key = 'vsees/upgrade_firmware_file/{}'.format(obj.link)
  78. s3_obj = AmazonS3Util()
  79. link = s3_obj.generate_file_obj_url(bucket_name, file_key)
  80. return link
  81. @staticmethod
  82. def set_link(obj):
  83. return {'link': obj}