91 lines
3.4 KiB
Python
91 lines
3.4 KiB
Python
from rest_framework import serializers
|
|
from .models import Account, Contact, Lead, Opportunity, Activity
|
|
from custom_objects.schema_engine import validate_entity_custom_fields
|
|
|
|
|
|
class CustomFieldsValidationMixin:
|
|
entity_name = None
|
|
|
|
def validate_custom_fields(self, value):
|
|
if value:
|
|
validate_entity_custom_fields(self.entity_name, value, partial=self.partial)
|
|
return value
|
|
|
|
def validate(self, attrs):
|
|
attrs = super().validate(attrs)
|
|
custom_fields = attrs.get('custom_fields')
|
|
if custom_fields is not None:
|
|
validate_entity_custom_fields(self.entity_name, custom_fields, partial=self.partial)
|
|
return attrs
|
|
|
|
|
|
class OwnerScopedRelationMixin:
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
request = self.context.get('request')
|
|
if request and hasattr(request, 'user') and request.user.is_authenticated:
|
|
user = request.user
|
|
if not (user.is_superuser or user.is_staff):
|
|
for field_name, field in self.fields.items():
|
|
if isinstance(field, serializers.PrimaryKeyRelatedField):
|
|
model = getattr(field.queryset, 'model', None)
|
|
if model:
|
|
if hasattr(model, 'owner'):
|
|
field.queryset = field.queryset.filter(owner=user)
|
|
elif hasattr(model, 'created_by'):
|
|
field.queryset = field.queryset.filter(created_by=user)
|
|
|
|
|
|
class AccountSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
|
entity_name = 'Account'
|
|
|
|
class Meta:
|
|
model = Account
|
|
fields = '__all__'
|
|
read_only_fields = ['id', 'created_at', 'updated_at']
|
|
|
|
|
|
class ContactSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
|
entity_name = 'Contact'
|
|
|
|
class Meta:
|
|
model = Contact
|
|
fields = '__all__'
|
|
read_only_fields = ['id', 'created_at', 'updated_at']
|
|
|
|
|
|
class LeadSerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
|
entity_name = 'Lead'
|
|
|
|
class Meta:
|
|
model = Lead
|
|
fields = '__all__'
|
|
read_only_fields = [
|
|
'id', 'is_converted', 'converted_date',
|
|
'converted_account', 'converted_contact',
|
|
'converted_opportunity', 'created_at', 'updated_at'
|
|
]
|
|
|
|
|
|
class OpportunitySerializer(OwnerScopedRelationMixin, CustomFieldsValidationMixin, serializers.ModelSerializer):
|
|
entity_name = 'Opportunity'
|
|
|
|
class Meta:
|
|
model = Opportunity
|
|
fields = '__all__'
|
|
read_only_fields = ['id', 'expected_revenue', 'created_at', 'updated_at']
|
|
|
|
|
|
class ActivitySerializer(OwnerScopedRelationMixin, serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Activity
|
|
fields = '__all__'
|
|
read_only_fields = ['id', 'completed_at', 'created_at', 'updated_at']
|
|
|
|
|
|
class LeadConvertSerializer(serializers.Serializer):
|
|
create_opportunity = serializers.BooleanField(required=False, default=False)
|
|
account_name = serializers.CharField(required=False, allow_blank=True)
|
|
opportunity_name = serializers.CharField(required=False, allow_blank=True)
|
|
opportunity_amount = serializers.DecimalField(required=False, max_digits=15, decimal_places=2, allow_null=True)
|
|
opportunity_close_date = serializers.DateField(required=False, allow_null=True)
|