HimoolERP/extensions/serializers.py

67 lines
2.3 KiB
Python
Raw Normal View History

2021-11-04 23:09:35 +08:00
from rest_framework.fields import SerializerMethodField, ImageField, FileField, JSONField
2021-12-12 15:44:39 +08:00
from rest_framework.fields import IntegerField, FloatField, DecimalField, BooleanField
2021-11-04 23:09:35 +08:00
from rest_framework.fields import CharField, DateField, DateTimeField
from rest_framework.serializers import Serializer, ModelSerializer
from extensions.exceptions import ValidationError
class BaseSerializer(ModelSerializer):
@property
def request(self):
return self.context['request']
@property
def team(self):
return self.context['request'].user.team
@property
def user(self):
return self.context['request'].user
def validate_foreign_key(self, model, instance, message):
if instance:
if not (instance := model.objects.filter(id=instance.id, team=self.team).first()):
raise ValidationError(message)
return instance
def validate_foreign_key_set(self, model, instances, message):
if instances:
instance_ids = [instance.id for instance in instances]
instances = model.objects.filter(id__in=instance_ids, team=self.team)
if len(instance_ids) != len(instances):
raise ValidationError(message)
return instances
def validate_unique(self, fields, message):
queryset = self.Meta.model.objects.filter(team=self.team, **fields)
if self.instance:
queryset = queryset.exclude(id=self.instance.id)
if queryset.exists():
raise ValidationError(message)
def create(self, validated_data):
validated_data['team'] = self.team
return super().create(validated_data)
class AmountField(DecimalField):
"""金额字段"""
def __init__(self, coerce_to_string=None, max_value=None, min_value=None, localize=False,
rounding=None, **kwargs):
2021-12-12 15:44:39 +08:00
2021-11-04 23:09:35 +08:00
kwargs['max_digits'], kwargs['decimal_places'] = 16, 2
2021-12-12 15:44:39 +08:00
super().__init__(coerce_to_string=coerce_to_string, max_value=max_value, min_value=min_value,
localize=localize, rounding=rounding, **kwargs)
2021-11-04 23:09:35 +08:00
__all__ = [
'Serializer', 'ModelSerializer', 'BaseSerializer',
'SerializerMethodField', 'ImageField', 'FileField', 'JSONField',
2021-12-12 15:44:39 +08:00
'BooleanField', 'IntegerField', 'FloatField', 'AmountField',
2021-11-04 23:09:35 +08:00
'CharField', 'DateField', 'DateTimeField',
]