2019-12-05 07:09:25 +00:00
|
|
|
# ~*~ coding: utf-8 ~*~
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2023-02-20 05:31:56 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-12-05 07:09:25 +00:00
|
|
|
from django_celery_beat.models import PeriodicTask
|
2023-02-20 05:31:56 +00:00
|
|
|
from rest_framework import serializers
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2023-05-16 06:48:00 +00:00
|
|
|
from ops.celery import app
|
|
|
|
from ops.models import CeleryTask, CeleryTaskExecution
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
__all__ = [
|
2022-10-24 12:14:18 +00:00
|
|
|
'CeleryResultSerializer', 'CeleryTaskExecutionSerializer',
|
|
|
|
'CeleryPeriodTaskSerializer', 'CeleryTaskSerializer'
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class CeleryResultSerializer(serializers.Serializer):
|
|
|
|
id = serializers.UUIDField()
|
|
|
|
result = serializers.JSONField()
|
|
|
|
state = serializers.CharField(max_length=16)
|
|
|
|
|
|
|
|
|
|
|
|
class CeleryPeriodTaskSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = PeriodicTask
|
|
|
|
fields = [
|
|
|
|
'name', 'task', 'enabled', 'description',
|
|
|
|
'last_run_at', 'total_run_count'
|
|
|
|
]
|
2022-10-24 12:14:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CeleryTaskSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = CeleryTask
|
2023-01-16 11:02:09 +00:00
|
|
|
read_only_fields = ['id', 'name', 'meta', 'summary', 'state', 'date_last_publish']
|
2022-11-16 07:04:46 +00:00
|
|
|
fields = read_only_fields
|
2022-10-24 12:14:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CeleryTaskExecutionSerializer(serializers.ModelSerializer):
|
2022-12-02 09:18:11 +00:00
|
|
|
is_success = serializers.BooleanField(required=False, read_only=True, label=_('Success'))
|
2023-05-16 06:48:00 +00:00
|
|
|
task_name = serializers.SerializerMethodField()
|
2022-12-02 09:18:11 +00:00
|
|
|
|
2022-10-24 12:14:18 +00:00
|
|
|
class Meta:
|
|
|
|
model = CeleryTaskExecution
|
|
|
|
fields = [
|
2023-05-16 06:48:00 +00:00
|
|
|
"id", "name", "task_name", "args", "kwargs", "time_cost", "timedelta",
|
2023-02-20 05:31:56 +00:00
|
|
|
"is_success", "is_finished", "date_published",
|
|
|
|
"date_start", "date_finished"
|
2022-10-24 12:14:18 +00:00
|
|
|
]
|
2023-05-16 06:48:00 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_task_name(obj):
|
|
|
|
from assets.const import AutomationTypes as AssetTypes
|
|
|
|
from accounts.const import AutomationTypes as AccountTypes
|
|
|
|
tp_dict = dict(AssetTypes.choices) | dict(AccountTypes.choices)
|
|
|
|
tp = obj.kwargs.get('tp')
|
|
|
|
task = app.tasks.get(obj.name)
|
|
|
|
task_name = getattr(task, 'verbose_name', obj.name)
|
|
|
|
if tp:
|
|
|
|
task_name = f'{task_name}({tp_dict.get(tp, tp)})'
|
|
|
|
return task_name
|