Louis Mazin 32750a0cd7 nice
2026-09-19 14:38:51 +02:00

49 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from rest_framework import serializers
from django.contrib.auth.models import User
from .models import TimeEntry
class TimeEntrySerializer(serializers.ModelSerializer):
class Meta:
model = TimeEntry
fields = '__all__'
read_only_fields = ('user',)
def create(self, validated_data):
# Set the user to the current request user
validated_data['user'] = self.context['request'].user
return super().create(validated_data)
def update(self, instance, validated_data):
# Prevent updating the user field
validated_data.pop('user', None)
return super().update(instance, validated_data)
class TimeEntryCreateSerializer(serializers.ModelSerializer):
class Meta:
model = TimeEntry
fields = ['date', 'start_time', 'end_time']
def validate(self, attrs):
if not attrs.get('start_time') and not attrs.get('end_time'):
raise serializers.ValidationError('Une heure d’entrée ou de sortie est requise.')
if attrs.get('start_time') and attrs.get('end_time') and attrs['end_time'] <= attrs['start_time']:
raise serializers.ValidationError('L’heure de sortie doit être après l’heure d’entrée.')
return attrs
class TimeEntryUpdateSerializer(serializers.ModelSerializer):
class Meta:
model = TimeEntry
fields = ['start_time', 'end_time']
def validate(self, attrs):
if not attrs.get('start_time') and not attrs.get('end_time'):
raise serializers.ValidationError('Une heure d’entrée ou de sortie est requise.')
if attrs.get('start_time') and attrs.get('end_time') and attrs['end_time'] <= attrs['start_time']:
raise serializers.ValidationError('L’heure de sortie doit être après l’heure d’entrée.')
return attrs
class WeeklySummarySerializer(serializers.Serializer):
user = serializers.CharField()
username = serializers.CharField()
total_hours = serializers.FloatField()
entries = serializers.ListField(child=serializers.DictField())