Internal
Public Access
Features: - Django-based REST API with web interface - Task management with tags, priorities, and due dates - Time tracking with start/stop timers - Subtasks support - Task filtering (all, today, upcoming, overdue, completed) - Tag-based organization with color coding - Sorting by due date and priority - Auto-assign tags when filtering - Responsive 3-pane layout (sidebar, task list, detail panel) - Task sharing between users - Mobile-responsive design with dark mode support 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
96 lines
2.9 KiB
Python
96 lines
2.9 KiB
Python
"""
|
|
User serializers for KeepItGoing API.
|
|
"""
|
|
|
|
from django.contrib.auth import get_user_model
|
|
from django.contrib.auth.password_validation import validate_password
|
|
from rest_framework import serializers
|
|
from .models import DeviceToken
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class UserSerializer(serializers.ModelSerializer):
|
|
"""Serializer for user details."""
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = [
|
|
'id', 'email', 'username', 'first_name', 'last_name',
|
|
'timezone', 'avatar', 'default_reminder_minutes',
|
|
'email_notifications', 'push_notifications',
|
|
'created_at', 'updated_at',
|
|
]
|
|
read_only_fields = ['id', 'email', 'created_at', 'updated_at']
|
|
|
|
|
|
class UserRegistrationSerializer(serializers.ModelSerializer):
|
|
"""Serializer for user registration."""
|
|
|
|
password = serializers.CharField(
|
|
write_only=True,
|
|
required=True,
|
|
validators=[validate_password],
|
|
style={'input_type': 'password'}
|
|
)
|
|
password_confirm = serializers.CharField(
|
|
write_only=True,
|
|
required=True,
|
|
style={'input_type': 'password'}
|
|
)
|
|
|
|
class Meta:
|
|
model = User
|
|
fields = ['email', 'username', 'password', 'password_confirm', 'first_name', 'last_name']
|
|
|
|
def validate(self, attrs):
|
|
if attrs['password'] != attrs['password_confirm']:
|
|
raise serializers.ValidationError({
|
|
'password_confirm': "Passwords do not match."
|
|
})
|
|
return attrs
|
|
|
|
def create(self, validated_data):
|
|
validated_data.pop('password_confirm')
|
|
user = User.objects.create_user(**validated_data)
|
|
return user
|
|
|
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
|
"""Serializer for password change."""
|
|
|
|
old_password = serializers.CharField(
|
|
required=True,
|
|
style={'input_type': 'password'}
|
|
)
|
|
new_password = serializers.CharField(
|
|
required=True,
|
|
validators=[validate_password],
|
|
style={'input_type': 'password'}
|
|
)
|
|
|
|
def validate_old_password(self, value):
|
|
user = self.context['request'].user
|
|
if not user.check_password(value):
|
|
raise serializers.ValidationError("Current password is incorrect.")
|
|
return value
|
|
|
|
|
|
class DeviceTokenSerializer(serializers.ModelSerializer):
|
|
"""Serializer for device tokens."""
|
|
|
|
class Meta:
|
|
model = DeviceToken
|
|
fields = ['id', 'platform', 'token', 'device_name', 'is_active', 'created_at']
|
|
read_only_fields = ['id', 'created_at']
|
|
|
|
def create(self, validated_data):
|
|
validated_data['user'] = self.context['request'].user
|
|
# Update if token already exists, otherwise create
|
|
device_token, created = DeviceToken.objects.update_or_create(
|
|
user=validated_data['user'],
|
|
token=validated_data['token'],
|
|
defaults=validated_data
|
|
)
|
|
return device_token
|