2022-12-16 09:16:14 +00:00
|
|
|
from django.db import models
|
2020-06-02 12:02:22 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-12-07 10:58:57 +00:00
|
|
|
from rest_framework import serializers
|
2022-08-24 08:14:32 +00:00
|
|
|
|
|
|
|
from assets.const import Protocol
|
2022-12-16 09:16:14 +00:00
|
|
|
from common.drf.fields import LabeledChoiceField
|
2022-12-07 10:58:57 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-12-05 07:09:25 +00:00
|
|
|
from ..models import Session
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
__all__ = [
|
|
|
|
'SessionSerializer', 'SessionDisplaySerializer',
|
2020-04-07 04:26:47 +00:00
|
|
|
'ReplaySerializer', 'SessionJoinValidateSerializer',
|
2020-03-12 08:24:38 +00:00
|
|
|
]
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
|
2022-12-16 09:16:14 +00:00
|
|
|
class SessionType(models.TextChoices):
|
|
|
|
normal = 'normal', _('Normal')
|
|
|
|
tunnel = 'tunnel', _('Tunnel')
|
|
|
|
command = 'command', _('Command')
|
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
class SessionSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
org_id = serializers.CharField(allow_blank=True)
|
2022-08-24 08:14:32 +00:00
|
|
|
protocol = serializers.ChoiceField(choices=Protocol.choices, label=_("Protocol"))
|
2022-12-20 09:52:08 +00:00
|
|
|
type = LabeledChoiceField(choices=SessionType.choices, label=_("Type"), default=SessionType.normal)
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Session
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ["id"]
|
|
|
|
fields_small = fields_mini + [
|
2022-12-16 09:16:14 +00:00
|
|
|
"user", "asset", "user_id", "asset_id", 'account',
|
|
|
|
"protocol", 'type', "login_from", "remote_addr",
|
|
|
|
"is_success", "is_finished", "has_replay", "has_command",
|
|
|
|
"date_start", "date_end", "comment"
|
2019-12-05 07:09:25 +00:00
|
|
|
]
|
2022-07-01 11:07:42 +00:00
|
|
|
fields_fk = ["terminal", ]
|
2022-12-07 10:58:57 +00:00
|
|
|
fields_custom = ["can_replay", "can_join", "can_terminate"]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields = fields_small + fields_fk + fields_custom
|
2020-06-02 12:02:22 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
"protocol": {'label': _('Protocol')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'user_id': {'label': _('User ID')},
|
|
|
|
'asset_id': {'label': _('Asset ID')},
|
2021-07-15 10:09:29 +00:00
|
|
|
'login_from_display': {'label': _('Login from display')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'is_success': {'label': _('Is success')},
|
|
|
|
'can_replay': {'label': _('Can replay')},
|
|
|
|
'can_join': {'label': _('Can join')},
|
2022-07-20 05:53:01 +00:00
|
|
|
'terminal': {'label': _('Terminal ID')},
|
2020-11-11 02:27:18 +00:00
|
|
|
'is_finished': {'label': _('Is finished')},
|
2021-09-15 12:51:59 +00:00
|
|
|
'can_terminate': {'label': _('Can terminate')},
|
2022-07-04 07:14:59 +00:00
|
|
|
'terminal_display': {'label': _('Terminal display')},
|
2020-06-02 12:02:22 +00:00
|
|
|
}
|
2019-12-05 07:09:25 +00:00
|
|
|
|
|
|
|
|
2020-03-12 08:24:38 +00:00
|
|
|
class SessionDisplaySerializer(SessionSerializer):
|
2021-10-21 03:33:06 +00:00
|
|
|
command_amount = serializers.IntegerField(read_only=True, label=_('Command amount'))
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
class Meta(SessionSerializer.Meta):
|
2022-07-01 11:07:42 +00:00
|
|
|
fields = SessionSerializer.Meta.fields + ['command_amount', ]
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2019-12-05 07:09:25 +00:00
|
|
|
class ReplaySerializer(serializers.Serializer):
|
|
|
|
file = serializers.FileField(allow_empty_file=True)
|
2021-12-08 07:35:22 +00:00
|
|
|
version = serializers.IntegerField(write_only=True, required=False, min_value=2, max_value=3)
|
2020-04-07 04:26:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SessionJoinValidateSerializer(serializers.Serializer):
|
|
|
|
user_id = serializers.UUIDField()
|
|
|
|
session_id = serializers.UUIDField()
|