Skip to content
Snippets Groups Projects
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
serializers.py 3.38 KiB
from rest_framework import serializers
from .models import Status, Task, Workflow, LogEntry, Configuration



class WorkflowSerializer(serializers.ModelSerializer):

    class Meta:
        model = Workflow
        fields = "__all__"


class LogEntrySerializer(serializers.ModelSerializer):

    class Meta:
        model = LogEntry
        fields = "__all__"


class TaskWriteSerializer(serializers.ModelSerializer):

    status_history = serializers.StringRelatedField(
        many=True,
        required=False,
    )

    log_entries = serializers.StringRelatedField(
        many=True,
        required=False
    )

    successors = serializers.StringRelatedField(
        many=True,
        required=False,
    )

    new_workflow_id = serializers.SerializerMethodField()  # no corresponding model property.
    new_workflow_uri = serializers.SerializerMethodField()  # no corresponding model property.

    class Meta:
        model = Task
        fields = ('id','task_type','filter','predecessor','successors',
                  'project','sas_id','priority','purge_policy','resume',
                  'new_workflow_id','new_workflow_uri','workflow',
                  'stage_request_id',
                  'status','new_status',
                  'inputs','outputs','metrics','status_history',
                  'size_to_process','size_processed','total_processing_time',
                  'log_entries'
                  )

    def get_new_workflow_id(self, instance):
        return instance.workflow.id

    def get_new_workflow_uri(self, instance):
        return instance.workflow.workflow_uri

    def create(self, validated_data):
        task = Task.objects.create(**validated_data)

        workflow_uri = self.initial_data['new_workflow_uri']
        if workflow_uri:
            #workflow_uri = validated_data['new_workflow_uri']
            workflow = Workflow.objects.get(workflow_uri=workflow_uri)
            task.workflow = workflow
            task.save()
            return task

        workflow_id = self.initial_data['new_workflow_id']
        if workflow_id:
            workflow = Workflow.objects.get(id=workflow_id)
            task.workflow = workflow
            task.save()
            return task

class TaskReadSerializer(serializers.ModelSerializer):

    status_history = serializers.StringRelatedField(
        many=True,
        required=False,
    )

    log_entries = serializers.StringRelatedField(
        many=True,
        required=False
    )

    successors = serializers.StringRelatedField(
        many=True,
        required=False,
    )

    class Meta:
        model = Task
        fields = ('id','task_type','filter','predecessor','successors',
                  'project','sas_id','priority','purge_policy','resume',
                  'workflow',
                  'stage_request_id',
                  'status','new_status',
                  'inputs','outputs','metrics','status_history',
                  'size_to_process', 'size_processed', 'total_processing_time',
                  'log_entries'
                  )


class StatusSerializer(serializers.ModelSerializer):

    class Meta:
        model = Status
        fields = "__all__"


class WorkflowSerializer(serializers.ModelSerializer):

    class Meta:
        model = Workflow
        fields = "__all__"


class ConfigurationSerializer(serializers.ModelSerializer):

    class Meta:
        model = Configuration
        fields = "__all__"