You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jumpserver/apps/terminal/serializers.py

65 lines
1.6 KiB

# -*- coding: utf-8 -*-
#
8 years ago
from django.utils import timezone
from rest_framework import serializers
from .models import Terminal, Status, Session, Task
from .backends import get_command_store
class TerminalSerializer(serializers.ModelSerializer):
session_online = serializers.SerializerMethodField()
8 years ago
is_alive = serializers.SerializerMethodField()
class Meta:
model = Terminal
fields = ['id', 'name', 'remote_addr', 'http_port', 'ssh_port',
'comment', 'is_accepted', 'session_online', 'is_alive']
@staticmethod
def get_session_online(obj):
return Session.objects.filter(terminal=obj.id, is_finished=False).count()
8 years ago
@staticmethod
def get_is_alive(obj):
try:
status = obj.status_set.latest()
except Status.DoesNotExist:
status = None
if not status:
return False
delta = timezone.now() - status.date_created
if delta < timezone.timedelta(seconds=600):
8 years ago
return True
8 years ago
else:
return False
8 years ago
class SessionSerializer(serializers.ModelSerializer):
command_amount = serializers.SerializerMethodField()
command_store = get_command_store()
8 years ago
class Meta:
model = Session
fields = '__all__'
8 years ago
def get_command_amount(self, obj):
return len(self.command_store.filter(session=obj.session))
8 years ago
class StatusSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Status
class TaskSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = Task