booleancharfield.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #coding=utf-8
  2. from rest_framework import serializers
  3. from libs.utils import strftime,strfdate
  4. from apps.base import Formater
  5. def getAttributeValue(instance, obj):
  6. if '.' in instance.source:
  7. k = instance.source.split('.')
  8. for i in range(len(k)):
  9. if hasattr(obj, k[i]):
  10. obj = getattr(obj, k[i])
  11. val = obj
  12. else:
  13. val = getattr(obj, instance.source)
  14. return val
  15. class BooleanCharField(serializers.BooleanField):
  16. def get_attribute(self, obj):
  17. return obj
  18. def to_representation(self, obj):
  19. val = getAttributeValue(self, obj)
  20. return u'是' if val else u'否'
  21. class TimeCharField(serializers.CharField):
  22. def get_attribute(self, obj):
  23. return obj
  24. def to_representation(self, obj):
  25. val = getAttributeValue(self, obj)
  26. return strftime(val)
  27. class DateCharField(serializers.CharField):
  28. def get_attribute(self, obj):
  29. return obj
  30. def to_representation(self, obj):
  31. val = getAttributeValue(self, obj)
  32. return strfdate(val)
  33. class AmountShowCharField(serializers.CharField):
  34. def get_attribute(self, obj):
  35. return obj
  36. def to_representation(self, obj):
  37. val = getAttributeValue(self, obj)
  38. return Formater.formatAmountShow(val)
  39. class AmountShowCharFieldWithTwoDecimalPlaces(serializers.CharField):
  40. def get_attribute(self, obj):
  41. return obj
  42. def to_representation(self, obj):
  43. val = getAttributeValue(self, obj)
  44. return Formater.formatAmountShowWithTwoDecimalPlaces(val)
  45. class EmptyAmountShowCharField(serializers.CharField):
  46. def get_attribute(self, obj):
  47. return obj
  48. def to_representation(self, obj):
  49. val = getAttributeValue(self, obj)
  50. return Formater.formatEmptyAmountShow(val)
  51. class PriceShowCharField(serializers.CharField):
  52. def get_attribute(self, obj):
  53. return obj
  54. def to_representation(self, obj):
  55. val = getAttributeValue(self, obj)
  56. return Formater.formatPriceShow(val)
  57. class EmptyPriceShowCharField(serializers.CharField):
  58. def get_attribute(self, obj):
  59. return obj
  60. def to_representation(self, obj):
  61. val = getAttributeValue(self, obj)
  62. return Formater.formatEmptyPriceShow(val)
  63. class CountShowCharField(serializers.CharField):
  64. def get_attribute(self, obj):
  65. return obj
  66. def to_representation(self, obj):
  67. val = getAttributeValue(self, obj)
  68. return Formater.formatCountShow(val)