jumpserver/apps/terminal/serializers.py

79 lines
2.0 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# -*- coding: utf-8 -*-
#
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
from common.utils import get_object_or_none
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):
2017-12-12 08:05:21 +00:00
try:
status = obj.status_set.latest()
except Status.DoesNotExist:
status = None
2017-12-01 13:22:32 +00:00
if not status:
return False
delta = timezone.now() - status.date_created
if delta < timezone.timedelta(seconds=600):
2016-11-13 14:34:38 +00:00
return True
2016-11-27 15:36:35 +00:00
else:
return False
2016-11-13 14:34:38 +00:00
2016-10-16 14:12:13 +00:00
class SessionSerializer(serializers.ModelSerializer):
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:
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
def get_command_amount(self, obj):
2018-01-21 09:27:27 +00:00
return self.command_store.count(session=str(obj.id))
2016-10-18 15:49:04 +00:00
class StatusSerializer(serializers.ModelSerializer):
2017-11-14 01:44:16 +00:00
class Meta:
fields = '__all__'
model = Status
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__'
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()