2023-04-26 08:54:14 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2023-01-16 11:02:09 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-02-21 10:31:42 +00:00
|
|
|
from accounts.models import AutomationExecution
|
2023-01-16 11:02:09 +00:00
|
|
|
from assets.const import AutomationTypes
|
|
|
|
from assets.models import Asset, Node, BaseAutomation
|
2023-01-18 03:54:06 +00:00
|
|
|
from common.const.choices import Trigger
|
|
|
|
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
2023-02-21 10:31:42 +00:00
|
|
|
from common.utils import get_logger
|
|
|
|
from ops.mixin import PeriodTaskSerializerMixin
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
logger = get_logger(__file__)
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'BaseAutomationSerializer', 'AutomationExecutionSerializer',
|
|
|
|
'UpdateAssetSerializer', 'UpdateNodeSerializer', 'AutomationAssetsSerializer',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class BaseAutomationSerializer(PeriodTaskSerializerMixin, BulkOrgResourceModelSerializer):
|
|
|
|
assets = ObjectRelatedField(many=True, required=False, queryset=Asset.objects, label=_('Assets'))
|
|
|
|
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
read_only_fields = [
|
|
|
|
'date_created', 'date_updated', 'created_by', 'periodic_display', 'executed_amount'
|
|
|
|
]
|
|
|
|
fields = read_only_fields + [
|
|
|
|
'id', 'name', 'is_periodic', 'interval', 'crontab', 'comment',
|
|
|
|
'type', 'accounts', 'nodes', 'assets', 'is_active',
|
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'required': True},
|
|
|
|
'type': {'read_only': True},
|
|
|
|
'periodic_display': {'label': _('Periodic perform')},
|
|
|
|
'executed_amount': {'label': _('Executed amount')},
|
|
|
|
}
|
|
|
|
|
2023-02-21 10:31:42 +00:00
|
|
|
def validate_name(self, name):
|
2023-02-22 08:23:18 +00:00
|
|
|
if self.instance and self.instance.name == name:
|
2023-02-22 03:04:29 +00:00
|
|
|
return name
|
2023-02-21 10:31:42 +00:00
|
|
|
if BaseAutomation.objects.filter(name=name, type=self.model_type).exists():
|
|
|
|
raise serializers.ValidationError(_('Name already exists'))
|
|
|
|
return name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def model_type(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
class AutomationExecutionSerializer(serializers.ModelSerializer):
|
|
|
|
snapshot = serializers.SerializerMethodField(label=_('Automation snapshot'))
|
|
|
|
type = serializers.ChoiceField(choices=AutomationTypes.choices, write_only=True, label=_('Type'))
|
2023-01-18 03:54:06 +00:00
|
|
|
trigger = LabeledChoiceField(choices=Trigger.choices, read_only=True, label=_("Trigger mode"))
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AutomationExecution
|
|
|
|
read_only_fields = [
|
2023-01-18 03:54:06 +00:00
|
|
|
'trigger', 'date_start', 'date_finished', 'snapshot', 'status'
|
2023-01-16 11:02:09 +00:00
|
|
|
]
|
2023-01-18 03:54:06 +00:00
|
|
|
fields = ['id', 'automation', 'type'] + read_only_fields
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_snapshot(obj):
|
2023-07-05 02:08:01 +00:00
|
|
|
tp = obj.snapshot.get('type', '')
|
|
|
|
type_display = tp if not hasattr(AutomationTypes, tp) \
|
|
|
|
else getattr(AutomationTypes, tp).label
|
2023-01-16 11:02:09 +00:00
|
|
|
snapshot = {
|
|
|
|
'type': tp,
|
2023-07-05 02:08:01 +00:00
|
|
|
'name': obj.snapshot.get('name'),
|
|
|
|
'comment': obj.snapshot.get('comment'),
|
|
|
|
'accounts': obj.snapshot.get('accounts'),
|
|
|
|
'node_amount': len(obj.snapshot.get('nodes', [])),
|
|
|
|
'asset_amount': len(obj.snapshot.get('assets', [])),
|
|
|
|
'type_display': type_display,
|
2023-01-16 11:02:09 +00:00
|
|
|
}
|
|
|
|
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)
|