Ejemplo n.º 1
0
class ActivitySerializer(ModelSerializer):
    """Activity serializer."""

    duration = DurationField(read_only=True)
    user = ResourceRelatedField(read_only=True, default=CurrentUserDefault())
    task = ResourceRelatedField(queryset=Task.objects.all(),
                                allow_null=True,
                                required=False)
    blocks = ResourceRelatedField(read_only=True, many=True)

    included_serializers = {
        'blocks': 'timed.tracking.serializers.ActivityBlockSerializer',
        'task': 'timed.projects.serializers.TaskSerializer',
        'user': '******',
    }

    class Meta:
        """Meta information for the activity serializer."""

        model = models.Activity
        fields = [
            'comment',
            'start_datetime',
            'duration',
            'user',
            'task',
            'blocks',
        ]
Ejemplo n.º 2
0
class MonthStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField()
    year = IntegerField()
    month = IntegerField()

    class Meta:
        resource_name = "month-statistics"
Ejemplo n.º 3
0
class TaskStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField(read_only=True)
    task = relations.ResourceRelatedField(model=Task, read_only=True)

    included_serializers = {
        "task": "timed.projects.serializers.TaskSerializer"
    }

    class Meta:
        resource_name = "task-statistics"
Ejemplo n.º 4
0
class UserStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField(read_only=True)
    user = relations.ResourceRelatedField(model=get_user_model(),
                                          read_only=True)

    included_serializers = {
        "user": "******"
    }

    class Meta:
        resource_name = "user-statistics"
Ejemplo n.º 5
0
class ProjectStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField()
    project = relations.ResourceRelatedField(source="task__project",
                                             model=Project,
                                             read_only=True)

    included_serializers = {
        "project": "timed.projects.serializers.ProjectSerializer"
    }

    class Meta:
        resource_name = "project-statistics"
Ejemplo n.º 6
0
class CustomerStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField()
    customer = relations.ResourceRelatedField(source="task__project__customer",
                                              model=Customer,
                                              read_only=True)

    included_serializers = {
        "customer": "timed.projects.serializers.CustomerSerializer"
    }

    class Meta:
        resource_name = "customer-statistics"
Ejemplo n.º 7
0
class UserAbsenceTypeSerializer(ModelSerializer):
    """Absence type serializer for a user.

    This is only a simulated relation to the user to show the absence credits
    and balances.
    """

    credit = IntegerField()
    used_days = IntegerField()
    used_duration = DurationField()
    balance = IntegerField()

    user = relations.SerializerMethodResourceRelatedField(
        source='get_user', model=get_user_model(), read_only=True)

    absence_credits = relations.SerializerMethodResourceRelatedField(
        source='get_absence_credits',
        model=models.AbsenceCredit,
        many=True,
        read_only=True)

    def get_user(self, instance):
        return get_user_model().objects.get(pk=instance.user_id)

    def get_absence_credits(self, instance):
        """Get the absence credits for the user and type."""
        return models.AbsenceCredit.objects.filter(
            absence_type=instance,
            user__id=instance.user_id,
            date__gte=instance.start_date,
            date__lte=instance.end_date)

    included_serializers = {
        'absence_credits':
        'timed.employment.serializers.AbsenceCreditSerializer',
    }

    class Meta:
        """Meta information for the absence type serializer."""

        model = models.UserAbsenceType
        fields = [
            'name',
            'fill_worktime',
            'credit',
            'used_duration',
            'used_days',
            'balance',
            'absence_credits',
            'user',
        ]
Ejemplo n.º 8
0
class AbsenceSerializer(ModelSerializer):
    """Absence serializer."""

    duration = DurationField(read_only=True)
    type     = ResourceRelatedField(queryset=AbsenceType.objects.all())
    user     = ResourceRelatedField(read_only=True,
                                    default=CurrentUserDefault())

    included_serializers = {
        'user': '******',
    }

    def validate(self, data):
        """Validate the absence data.

        An absence should not be created on a public holiday or a weekend.

        :returns: The validated data
        :rtype:   dict
        """
        location = Employment.objects.for_user(
            data.get('user'),
            data.get('date')
        ).location

        if PublicHoliday.objects.filter(
            location_id=location.id,
            date=data.get('date')
        ).exists():
            raise ValidationError(
                'You can\'t create an absence on a public holiday'
            )

        workdays = [int(day) for day in location.workdays]
        if data.get('date').isoweekday() not in workdays:
            raise ValidationError('You can\'t create an absence on a weekend')

        return data

    class Meta:
        """Meta information for the absence serializer."""

        model  = models.Absence
        fields = [
            'comment',
            'date',
            'duration',
            'type',
            'user',
        ]
Ejemplo n.º 9
0
class ActivityBlockSerializer(ModelSerializer):
    """Activity block serializer."""

    duration = DurationField(read_only=True)
    activity = ResourceRelatedField(queryset=models.Activity.objects.all())

    included_serializers = {
        'activity': 'timed.tracking.serializers.ActivitySerializer',
    }

    def validate(self, data):
        """Validate the activity block.

        Ensure that a user can only have one activity with an active block.
        """
        if self.instance:
            user = self.instance.activity.user
            to_datetime = data.get('to_datetime', self.instance.to_datetime)
        else:
            user = data.get('activity').user
            to_datetime = data.get('to_datetime', None)

        blocks = models.ActivityBlock.objects.filter(activity__user=user)

        if (
            blocks.filter(to_datetime__isnull=True) and
            to_datetime is None
        ):
            raise ValidationError('A user can only have one active activity')

        return data

    class Meta:
        """Meta information for the activity block serializer."""

        model  = models.ActivityBlock
        fields = [
            'activity',
            'duration',
            'from_datetime',
            'to_datetime',
        ]
Ejemplo n.º 10
0
class YearStatisticSerializer(TotalTimeRootMetaMixin, Serializer):
    duration = DurationField()
    year = IntegerField()

    class Meta:
        resource_name = 'year-statistics'