2016-10-16 14:12:13 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2018-09-29 02:44:52 +00:00
|
|
|
from django.core.cache import cache
|
2016-11-13 14:34:38 +00:00
|
|
|
from django.utils import timezone
|
2016-10-16 14:12:13 +00:00
|
|
|
from rest_framework import serializers
|
2018-03-07 03:28:42 +00:00
|
|
|
from rest_framework_bulk.serializers import BulkListSerializer
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2018-03-07 03:28:42 +00:00
|
|
|
from common.mixins import BulkSerializerMixin
|
2017-12-01 09:28:47 +00:00
|
|
|
from .models import Terminal, Status, Session, Task
|
2018-05-22 10:22:06 +00:00
|
|
|
from .backends import get_multi_command_storage
|
2016-10-16 14:12:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TerminalSerializer(serializers.ModelSerializer):
|
2017-12-01 09:55:19 +00:00
|
|
|
session_online = serializers.SerializerMethodField()
|
2016-11-13 14:34:38 +00:00
|
|
|
is_alive = serializers.SerializerMethodField()
|
2016-10-19 10:33:14 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
class Meta:
|
|
|
|
model = Terminal
|
2017-12-25 04:22:49 +00:00
|
|
|
fields = [
|
|
|
|
'id', 'name', 'remote_addr', 'http_port', 'ssh_port',
|
|
|
|
'comment', 'is_accepted', "is_active", 'session_online',
|
|
|
|
'is_alive'
|
|
|
|
]
|
2016-10-19 10:33:14 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2017-12-01 09:55:19 +00:00
|
|
|
def get_session_online(obj):
|
|
|
|
return Session.objects.filter(terminal=obj.id, is_finished=False).count()
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2016-11-13 14:34:38 +00:00
|
|
|
@staticmethod
|
|
|
|
def get_is_alive(obj):
|
2018-09-29 02:44:52 +00:00
|
|
|
key = StatusSerializer.CACHE_KEY_PREFIX + str(obj.id)
|
|
|
|
return cache.get(key)
|
2016-11-13 14:34:38 +00:00
|
|
|
|
2016-10-16 14:12:13 +00:00
|
|
|
|
2018-09-07 04:40:26 +00:00
|
|
|
class SessionSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
2017-12-03 16:21:26 +00:00
|
|
|
command_amount = serializers.SerializerMethodField()
|
2018-05-22 10:22:06 +00:00
|
|
|
command_store = get_multi_command_storage()
|
2017-10-31 03:34:20 +00:00
|
|
|
|
2016-10-18 15:49:04 +00:00
|
|
|
class Meta:
|
2017-12-01 09:28:47 +00:00
|
|
|
model = Session
|
2018-03-07 03:28:42 +00:00
|
|
|
list_serializer_class = BulkListSerializer
|
2017-11-14 01:44:16 +00:00
|
|
|
fields = '__all__'
|
2016-10-18 15:49:04 +00:00
|
|
|
|
2017-12-03 16:21:26 +00:00
|
|
|
def get_command_amount(self, obj):
|
2018-01-21 09:27:27 +00:00
|
|
|
return self.command_store.count(session=str(obj.id))
|
2017-12-03 16:21:26 +00:00
|
|
|
|
2016-10-18 15:49:04 +00:00
|
|
|
|
2017-12-03 12:42:36 +00:00
|
|
|
class StatusSerializer(serializers.ModelSerializer):
|
2018-09-29 02:44:52 +00:00
|
|
|
CACHE_KEY_PREFIX = 'terminal_status_'
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = '__all__'
|
2017-12-01 09:28:47 +00:00
|
|
|
model = Status
|
2017-10-31 03:34:20 +00:00
|
|
|
|
2018-09-29 02:44:52 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
terminal_id = str(validated_data['terminal'].id)
|
|
|
|
key = self.CACHE_KEY_PREFIX + terminal_id
|
|
|
|
cache.set(key, 1, 60)
|
|
|
|
return validated_data
|
|
|
|
|
2017-10-31 03:34:20 +00:00
|
|
|
|
2018-03-07 03:28:42 +00:00
|
|
|
class TaskSerializer(BulkSerializerMixin, serializers.ModelSerializer):
|
2017-11-14 01:44:16 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
fields = '__all__'
|
2017-12-01 09:28:47 +00:00
|
|
|
model = Task
|
2018-03-07 03:28:42 +00:00
|
|
|
list_serializer_class = BulkListSerializer
|
2017-12-18 10:38:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReplaySerializer(serializers.Serializer):
|
|
|
|
file = serializers.FileField()
|
|
|
|
|