update
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
0
emails/management/__init__.py
Normal file
0
emails/management/__init__.py
Normal file
BIN
emails/management/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
emails/management/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
0
emails/management/commands/__init__.py
Normal file
0
emails/management/commands/__init__.py
Normal file
BIN
emails/management/commands/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
emails/management/commands/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
Binary file not shown.
49
emails/management/commands/create_default_folders.py
Normal file
49
emails/management/commands/create_default_folders.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from emails.models import EmailFolder
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Create default email folders for all users'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
users = User.objects.all()
|
||||
created_count = 0
|
||||
|
||||
for user in users:
|
||||
# Check if user already has folders
|
||||
if EmailFolder.objects.filter(user=user).exists():
|
||||
self.stdout.write(
|
||||
self.style.WARNING(f'User {user.email} already has folders, skipping...')
|
||||
)
|
||||
continue
|
||||
|
||||
# Create default folders
|
||||
default_folders = [
|
||||
{'name': 'Inbox', 'folder_type': 'inbox', 'color': '#007bff'},
|
||||
{'name': 'Sent', 'folder_type': 'sent', 'color': '#28a745'},
|
||||
{'name': 'Drafts', 'folder_type': 'drafts', 'color': '#ffc107'},
|
||||
{'name': 'Trash', 'folder_type': 'trash', 'color': '#dc3545'},
|
||||
{'name': 'Spam', 'folder_type': 'spam', 'color': '#6c757d'},
|
||||
{'name': 'Archive', 'folder_type': 'archive', 'color': '#17a2b8'},
|
||||
]
|
||||
|
||||
for folder_data in default_folders:
|
||||
EmailFolder.objects.create(
|
||||
user=user,
|
||||
name=folder_data['name'],
|
||||
folder_type=folder_data['folder_type'],
|
||||
color=folder_data['color'],
|
||||
is_system=True
|
||||
)
|
||||
created_count += 1
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'Created default folders for user {user.email}')
|
||||
)
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f'Successfully created {created_count} default folders')
|
||||
)
|
||||
Binary file not shown.
Binary file not shown.
@@ -64,6 +64,23 @@ class EmailSerializer(serializers.ModelSerializer):
|
||||
return obj.threads.count()
|
||||
|
||||
|
||||
class JSONListField(serializers.Field):
|
||||
"""Custom field to handle JSON strings for email lists."""
|
||||
|
||||
def to_internal_value(self, data):
|
||||
import json
|
||||
if isinstance(data, str):
|
||||
try:
|
||||
return json.loads(data)
|
||||
except json.JSONDecodeError:
|
||||
# If it's not valid JSON, treat as a single email
|
||||
return [data] if data else []
|
||||
return data or []
|
||||
|
||||
def to_representation(self, value):
|
||||
return value
|
||||
|
||||
|
||||
class EmailCreateSerializer(serializers.ModelSerializer):
|
||||
"""Serializer for creating emails."""
|
||||
|
||||
@@ -73,11 +90,16 @@ class EmailCreateSerializer(serializers.ModelSerializer):
|
||||
write_only=True
|
||||
)
|
||||
|
||||
# Override email fields to use custom JSON field
|
||||
to_emails = JSONListField()
|
||||
cc_emails = JSONListField(required=False)
|
||||
bcc_emails = JSONListField(required=False)
|
||||
|
||||
class Meta:
|
||||
model = Email
|
||||
fields = (
|
||||
'subject', 'to_emails', 'cc_emails', 'bcc_emails', 'reply_to',
|
||||
'body_text', 'body_html', 'priority', 'folder', 'attachments'
|
||||
'body_text', 'body_html', 'priority', 'attachments'
|
||||
)
|
||||
|
||||
def validate_to_emails(self, value):
|
||||
@@ -85,6 +107,12 @@ class EmailCreateSerializer(serializers.ModelSerializer):
|
||||
raise serializers.ValidationError("At least one recipient is required.")
|
||||
return value
|
||||
|
||||
def validate_cc_emails(self, value):
|
||||
return value or []
|
||||
|
||||
def validate_bcc_emails(self, value):
|
||||
return value or []
|
||||
|
||||
def create(self, validated_data):
|
||||
attachments_data = validated_data.pop('attachments', [])
|
||||
user = self.context['request'].user
|
||||
@@ -92,6 +120,17 @@ class EmailCreateSerializer(serializers.ModelSerializer):
|
||||
# Set from_email to user's email
|
||||
validated_data['from_email'] = user.email
|
||||
|
||||
# Set default folder to "Sent" if not specified
|
||||
if 'folder' not in validated_data:
|
||||
try:
|
||||
sent_folder = EmailFolder.objects.get(user=user, folder_type='sent')
|
||||
validated_data['folder'] = sent_folder
|
||||
except EmailFolder.DoesNotExist:
|
||||
# Fallback to first available folder
|
||||
folder = EmailFolder.objects.filter(user=user).first()
|
||||
if folder:
|
||||
validated_data['folder'] = folder
|
||||
|
||||
# Create email
|
||||
email = Email.objects.create(user=user, **validated_data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user