2022-11-08 09:54:51 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from ops.mixin import PeriodTaskSerializerMixin
|
|
|
|
from assets.const import AutomationTypes
|
2022-11-09 07:42:21 +00:00
|
|
|
from assets.models import Asset, Node, BaseAutomation, AutomationExecution
|
2022-11-08 09:54:51 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
|
|
|
from common.utils import get_logger
|
2022-11-09 07:42:21 +00:00
|
|
|
from common.drf.fields import ObjectRelatedField
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'BaseAutomationSerializer', 'AutomationExecutionSerializer',
|
|
|
|
'UpdateAssetSerializer', 'UpdateNodeSerializer', 'AutomationAssetsSerializer',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSerializer):
|
2022-11-09 07:42:21 +00:00
|
|
|
assets = ObjectRelatedField(many=True, required=False, queryset=Asset.objects, label=_('Assets'))
|
|
|
|
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
|
|
|
|
2022-11-08 09:54:51 +00:00
|
|
|
class Meta:
|
|
|
|
read_only_fields = [
|
|
|
|
'date_created', 'date_updated', 'created_by', 'periodic_display'
|
|
|
|
]
|
|
|
|
fields = read_only_fields + [
|
|
|
|
'id', 'name', 'is_periodic', 'interval', 'crontab', 'comment',
|
|
|
|
'type', 'accounts', 'nodes', 'assets', 'is_active'
|
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'required': True},
|
2022-11-09 07:42:21 +00:00
|
|
|
'type': {'read_only': True},
|
2022-11-08 09:54:51 +00:00
|
|
|
'periodic_display': {'label': _('Periodic perform')},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationExecutionSerializer(serializers.ModelSerializer):
|
|
|
|
snapshot = serializers.SerializerMethodField(label=_('Automation snapshot'))
|
|
|
|
type = serializers.ChoiceField(choices=AutomationTypes.choices, write_only=True, label=_('Type'))
|
|
|
|
trigger_display = serializers.ReadOnlyField(source='get_trigger_display', label=_('Trigger mode'))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AutomationExecution
|
2022-11-09 07:42:21 +00:00
|
|
|
read_only_fields = [
|
|
|
|
'trigger_display', 'date_start', 'date_finished', 'snapshot', 'status'
|
2022-11-08 09:54:51 +00:00
|
|
|
]
|
2022-11-09 07:42:21 +00:00
|
|
|
fields = ['id', 'automation', 'trigger', 'type'] + read_only_fields
|
2022-11-08 09:54:51 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_snapshot(obj):
|
|
|
|
tp = obj.snapshot['type']
|
|
|
|
snapshot = {
|
|
|
|
'type': tp,
|
|
|
|
'name': obj.snapshot['name'],
|
|
|
|
'comment': obj.snapshot['comment'],
|
|
|
|
'accounts': obj.snapshot['accounts'],
|
|
|
|
'node_amount': len(obj.snapshot['nodes']),
|
|
|
|
'asset_amount': len(obj.snapshot['assets']),
|
|
|
|
'type_display': getattr(AutomationTypes, tp).label,
|
|
|
|
}
|
|
|
|
return snapshot
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BaseAutomation
|
|
|
|
fields = ['id', 'assets']
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = BaseAutomation
|
|
|
|
fields = ['id', 'nodes']
|
|
|
|
|
|
|
|
|
|
|
|
class AutomationAssetsSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
only_fields = ['id', 'name', 'address']
|
|
|
|
fields = tuple(only_fields)
|