197 lines
5.7 KiB
Python
197 lines
5.7 KiB
Python
from rest_framework import serializers
|
|
from .models import ContactSubmission
|
|
|
|
|
|
class ContactSubmissionSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for ContactSubmission model.
|
|
Handles both creation and retrieval of contact form submissions.
|
|
"""
|
|
|
|
# Computed fields
|
|
full_name = serializers.ReadOnlyField()
|
|
is_high_priority = serializers.ReadOnlyField()
|
|
is_enterprise_client = serializers.ReadOnlyField()
|
|
industry_display = serializers.SerializerMethodField()
|
|
project_type_display = serializers.SerializerMethodField()
|
|
budget_display = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = ContactSubmission
|
|
fields = [
|
|
'id',
|
|
'first_name',
|
|
'last_name',
|
|
'full_name',
|
|
'email',
|
|
'phone',
|
|
'company',
|
|
'job_title',
|
|
'industry',
|
|
'industry_display',
|
|
'company_size',
|
|
'project_type',
|
|
'project_type_display',
|
|
'timeline',
|
|
'budget',
|
|
'budget_display',
|
|
'message',
|
|
'newsletter_subscription',
|
|
'privacy_consent',
|
|
'status',
|
|
'priority',
|
|
'is_high_priority',
|
|
'is_enterprise_client',
|
|
'created_at',
|
|
'updated_at',
|
|
'admin_notes',
|
|
'assigned_to',
|
|
]
|
|
read_only_fields = [
|
|
'id',
|
|
'status',
|
|
'priority',
|
|
'created_at',
|
|
'updated_at',
|
|
'admin_notes',
|
|
'assigned_to',
|
|
]
|
|
|
|
def get_industry_display(self, obj):
|
|
return obj.get_industry_display()
|
|
|
|
def get_project_type_display(self, obj):
|
|
return obj.get_project_type_display()
|
|
|
|
def get_budget_display(self, obj):
|
|
return obj.get_budget_display()
|
|
|
|
def validate_email(self, value):
|
|
"""
|
|
Custom email validation to ensure it's a business email.
|
|
"""
|
|
if value and not any(domain in value.lower() for domain in ['@gmail.com', '@yahoo.com', '@hotmail.com']):
|
|
return value
|
|
# Allow personal emails but log them
|
|
return value
|
|
|
|
def validate_privacy_consent(self, value):
|
|
"""
|
|
Ensure privacy consent is given.
|
|
"""
|
|
if not value:
|
|
raise serializers.ValidationError("Privacy consent is required to submit the form.")
|
|
return value
|
|
|
|
def validate(self, attrs):
|
|
"""
|
|
Cross-field validation.
|
|
"""
|
|
# Ensure required fields are present
|
|
required_fields = ['first_name', 'last_name', 'email', 'company', 'job_title', 'message']
|
|
for field in required_fields:
|
|
if not attrs.get(field):
|
|
raise serializers.ValidationError(f"{field.replace('_', ' ').title()} is required.")
|
|
|
|
# Validate enterprise client indicators
|
|
if attrs.get('company_size') in ['201-1000', '1000+'] and attrs.get('budget') in ['under-50k', '50k-100k']:
|
|
# This might be a mismatch, but we'll allow it and flag for review
|
|
pass
|
|
|
|
return attrs
|
|
|
|
|
|
class ContactSubmissionCreateSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Simplified serializer for creating contact submissions.
|
|
Only includes fields that should be provided by the frontend.
|
|
"""
|
|
|
|
class Meta:
|
|
model = ContactSubmission
|
|
fields = [
|
|
'first_name',
|
|
'last_name',
|
|
'email',
|
|
'phone',
|
|
'company',
|
|
'job_title',
|
|
'industry',
|
|
'company_size',
|
|
'project_type',
|
|
'timeline',
|
|
'budget',
|
|
'message',
|
|
'newsletter_subscription',
|
|
'privacy_consent',
|
|
]
|
|
|
|
def validate_privacy_consent(self, value):
|
|
"""
|
|
Ensure privacy consent is given.
|
|
"""
|
|
if not value:
|
|
raise serializers.ValidationError("Privacy consent is required to submit the form.")
|
|
return value
|
|
|
|
def validate(self, attrs):
|
|
"""
|
|
Cross-field validation for creation.
|
|
"""
|
|
# Ensure required fields are present
|
|
required_fields = ['first_name', 'last_name', 'email', 'company', 'job_title', 'message']
|
|
for field in required_fields:
|
|
if not attrs.get(field):
|
|
raise serializers.ValidationError(f"{field.replace('_', ' ').title()} is required.")
|
|
|
|
return attrs
|
|
|
|
|
|
class ContactSubmissionListSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Simplified serializer for listing contact submissions.
|
|
Used in admin views and API listings.
|
|
"""
|
|
|
|
full_name = serializers.ReadOnlyField()
|
|
is_high_priority = serializers.ReadOnlyField()
|
|
is_enterprise_client = serializers.ReadOnlyField()
|
|
|
|
class Meta:
|
|
model = ContactSubmission
|
|
fields = [
|
|
'id',
|
|
'full_name',
|
|
'email',
|
|
'company',
|
|
'job_title',
|
|
'project_type',
|
|
'status',
|
|
'priority',
|
|
'is_high_priority',
|
|
'is_enterprise_client',
|
|
'created_at',
|
|
]
|
|
|
|
|
|
class ContactSubmissionUpdateSerializer(serializers.ModelSerializer):
|
|
"""
|
|
Serializer for updating contact submissions (admin use).
|
|
"""
|
|
|
|
class Meta:
|
|
model = ContactSubmission
|
|
fields = [
|
|
'status',
|
|
'priority',
|
|
'admin_notes',
|
|
'assigned_to',
|
|
]
|
|
|
|
def validate_status(self, value):
|
|
"""
|
|
Validate status transitions.
|
|
"""
|
|
# Add business logic for status transitions if needed
|
|
return value
|