class UserModelSerializer(ModelSerializer):
    email = serializers.CharField(required=False)

    class Meta:
        model = User
        extra_kwargs = {
            field: {
                'read_only': True
            }
            for field in ['is_staff', 'last_login', 'date_joined']
        }
        fields = [
            'email', 'full_name', 'is_active', 'is_staff', 'last_login',
            'date_joined'
        ]

    @transaction.atomic
    def update(self, instance, data: dict, **kwargs):
        user = super().update(instance, data)

        # Set all organization where user is owner inactive
        if data.get('is_active') == False and settings.MULTI_ORGANIZATIONS:
            from purplship.server.orgs import models as orgs
            user_orgs = orgs.Organization.objects.filter(
                owner__organization_user__user__id=user.id)
            user_orgs.update(is_active=False)

        return user
    def _create_model_serializer(carrier_model):
        _name = f'{carrier_model.__name__}'
        _extra_fields = {}

        if hasattr(carrier_model, 'account_country_code'):
            _extra_fields.update(account_country_code=serializers.CharField(
                required=not partial))

        class Meta:
            model = carrier_model
            exclude = [
                'created_at', 'updated_at', 'created_by', 'capabilities',
                'active_users'
            ]

        return owned_model_serializer(
            type(
                _name, (ModelSerializer, ), {
                    'Meta': Meta,
                    'carrier_id': serializers.CharField(required=not partial),
                    **_extra_fields
                }))
class AddressModelSerializer(validators.AugmentedAddressSerializer,
                             ModelSerializer):
    country_code = serializers.CharField(required=False)

    class Meta:
        model = manager.Address
        extra_kwargs = {
            **{key: {
                'required': True
            }
               for key in ['country_code']},
            **{
                key: {
                    'read_only': False
                }
                for key in ['validate_location', 'validation']
            },
        }
        exclude = ['created_at', 'updated_at', 'created_by']
class CommodityModelSerializer(ModelSerializer):
    weight_unit = serializers.CharField()

    class Meta:
        model = manager.Commodity
        exclude = ['created_at', 'updated_at', 'created_by']
class DefaultTemplateSerializer(serializers.EntitySerializer):
    label = serializers.CharField()
    is_default = serializers.BooleanField()
    address = serializers.AddressData(required=False)
    customs = serializers.CustomsData(required=False)
    parcel = serializers.ParcelData(required=False)