32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
|
from datetime import datetime, timedelta, time
|
||
|
|
||
|
from rest_framework import serializers
|
||
|
from rest_framework.serializers import ModelSerializer
|
||
|
|
||
|
from dispatcher.models import Clocking
|
||
|
|
||
|
|
||
|
class ClockingSerializer(ModelSerializer):
|
||
|
total = serializers.SerializerMethodField()
|
||
|
|
||
|
class Meta:
|
||
|
model = Clocking
|
||
|
fields = "__all__"
|
||
|
|
||
|
def get_total(self, obj: Clocking):
|
||
|
total = timedelta()
|
||
|
if obj.in_am is not None and obj.out_am is not None:
|
||
|
in_am = datetime.combine(obj.date, obj.in_am)
|
||
|
out_am = datetime.combine(obj.date, obj.out_am)
|
||
|
total += out_am - in_am
|
||
|
if obj.in_pm is not None and obj.out_pm is not None:
|
||
|
in_pm = datetime.combine(obj.date, obj.in_pm)
|
||
|
out_pm = datetime.combine(obj.date, obj.out_pm)
|
||
|
total += out_pm - in_pm
|
||
|
if obj.remote is not None:
|
||
|
total += timedelta(hours=obj.remote.hour, minutes=obj.remote.minute)
|
||
|
|
||
|
seconds = total.seconds
|
||
|
minutes = seconds // 60
|
||
|
return minutes
|