127 lines
4.4 KiB
Python
127 lines
4.4 KiB
Python
from rest_framework import serializers
|
|
from django.contrib.auth import get_user_model
|
|
from .models import Contact, ContactGroup, ContactInteraction, ContactImport
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class ContactGroupSerializer(serializers.ModelSerializer):
|
|
"""Serializer for contact groups."""
|
|
|
|
contact_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = ContactGroup
|
|
fields = '__all__'
|
|
read_only_fields = ('user', 'created_at', 'updated_at')
|
|
|
|
def get_contact_count(self, obj):
|
|
return obj.contacts.count()
|
|
|
|
|
|
class ContactSerializer(serializers.ModelSerializer):
|
|
"""Serializer for contacts."""
|
|
|
|
full_name = serializers.CharField(source='get_full_name', read_only=True)
|
|
address = serializers.CharField(source='get_address', read_only=True)
|
|
group_name = serializers.CharField(source='group.name', read_only=True)
|
|
interaction_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Contact
|
|
fields = '__all__'
|
|
read_only_fields = ('user', 'created_at', 'updated_at')
|
|
|
|
def get_interaction_count(self, obj):
|
|
return obj.interactions.count()
|
|
|
|
|
|
class ContactCreateSerializer(serializers.ModelSerializer):
|
|
"""Serializer for creating contacts."""
|
|
|
|
class Meta:
|
|
model = Contact
|
|
fields = (
|
|
'group', 'first_name', 'last_name', 'email', 'phone', 'company',
|
|
'job_title', 'notes', 'website', 'birthday', 'address_line1',
|
|
'address_line2', 'city', 'state', 'postal_code', 'country',
|
|
'linkedin', 'twitter', 'facebook', 'is_favorite', 'is_blocked'
|
|
)
|
|
|
|
def validate_email(self, value):
|
|
user = self.context['request'].user
|
|
if Contact.objects.filter(user=user, email=value).exists():
|
|
raise serializers.ValidationError("A contact with this email already exists.")
|
|
return value
|
|
|
|
|
|
class ContactInteractionSerializer(serializers.ModelSerializer):
|
|
"""Serializer for contact interactions."""
|
|
|
|
contact_name = serializers.CharField(source='contact.get_full_name', read_only=True)
|
|
created_by_name = serializers.CharField(source='created_by.get_full_name', read_only=True)
|
|
|
|
class Meta:
|
|
model = ContactInteraction
|
|
fields = '__all__'
|
|
read_only_fields = ('created_by', 'date')
|
|
|
|
|
|
class ContactImportSerializer(serializers.ModelSerializer):
|
|
"""Serializer for contact imports."""
|
|
|
|
class Meta:
|
|
model = ContactImport
|
|
fields = '__all__'
|
|
read_only_fields = ('user', 'status', 'total_contacts', 'imported_contacts', 'failed_contacts', 'error_log', 'created_at', 'completed_at')
|
|
|
|
|
|
class ContactBulkActionSerializer(serializers.Serializer):
|
|
"""Serializer for bulk contact actions."""
|
|
|
|
ACTION_CHOICES = [
|
|
('add_to_group', 'Add to group'),
|
|
('remove_from_group', 'Remove from group'),
|
|
('mark_favorite', 'Mark as favorite'),
|
|
('mark_unfavorite', 'Mark as unfavorite'),
|
|
('block', 'Block'),
|
|
('unblock', 'Unblock'),
|
|
('delete', 'Delete'),
|
|
]
|
|
|
|
contact_ids = serializers.ListField(
|
|
child=serializers.IntegerField(),
|
|
min_length=1
|
|
)
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES)
|
|
group_id = serializers.IntegerField(required=False)
|
|
|
|
def validate_contact_ids(self, value):
|
|
user = self.context['request'].user
|
|
# Verify all contacts belong to the user
|
|
contact_count = Contact.objects.filter(id__in=value, user=user).count()
|
|
if contact_count != len(value):
|
|
raise serializers.ValidationError("Some contacts don't exist or don't belong to you.")
|
|
return value
|
|
|
|
def validate(self, attrs):
|
|
action = attrs.get('action')
|
|
group_id = attrs.get('group_id')
|
|
|
|
if action in ['add_to_group', 'remove_from_group'] and not group_id:
|
|
raise serializers.ValidationError("group_id is required for group actions.")
|
|
|
|
return attrs
|
|
|
|
|
|
class ContactSearchSerializer(serializers.Serializer):
|
|
"""Serializer for contact search."""
|
|
|
|
query = serializers.CharField(required=False)
|
|
group = serializers.IntegerField(required=False)
|
|
is_favorite = serializers.BooleanField(required=False)
|
|
is_blocked = serializers.BooleanField(required=False)
|
|
company = serializers.CharField(required=False)
|
|
city = serializers.CharField(required=False)
|
|
country = serializers.CharField(required=False)
|