2023-07-24 03:52:25 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-04-03 05:55:27 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from common.serializers.fields import LabeledChoiceField
|
2022-07-07 07:41:26 +00:00
|
|
|
from common.utils.common import pretty_string
|
2023-04-03 05:55:27 +00:00
|
|
|
from common.utils.random import random_string
|
|
|
|
from orgs.mixins.serializers import OrgResourceModelSerializerMixin
|
|
|
|
from ..const import ActionPermission
|
2021-09-07 10:16:27 +00:00
|
|
|
from ..models import SessionSharing, SessionJoinRecord
|
|
|
|
|
|
|
|
__all__ = ['SessionSharingSerializer', 'SessionJoinRecordSerializer']
|
|
|
|
|
|
|
|
|
|
|
|
class SessionSharingSerializer(OrgResourceModelSerializerMixin):
|
2022-06-30 03:21:26 +00:00
|
|
|
users = serializers.ListSerializer(
|
2022-06-30 04:34:23 +00:00
|
|
|
child=serializers.CharField(max_length=36), allow_null=True, write_only=True
|
2022-06-30 03:21:26 +00:00
|
|
|
)
|
2023-04-03 05:55:27 +00:00
|
|
|
action_permission = LabeledChoiceField(
|
2023-04-03 07:37:32 +00:00
|
|
|
default=ActionPermission.writable, choices=ActionPermission.choices,
|
|
|
|
write_only=True, label=_('Action permission')
|
2023-04-03 05:55:27 +00:00
|
|
|
)
|
2022-06-30 03:21:26 +00:00
|
|
|
|
2021-09-07 10:16:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = SessionSharing
|
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'verify_code', 'is_active', 'expired_time', 'created_by',
|
2023-04-03 05:55:27 +00:00
|
|
|
'date_created', 'date_updated', 'users', 'users_display',
|
2023-07-27 04:52:33 +00:00
|
|
|
'action_permission', 'origin', 'url',
|
2021-09-07 10:16:27 +00:00
|
|
|
]
|
|
|
|
fields_fk = ['session', 'creator']
|
|
|
|
fields = fields_small + fields_fk
|
|
|
|
read_only_fields = ['verify_code']
|
|
|
|
|
2022-06-30 03:21:26 +00:00
|
|
|
def save(self, **kwargs):
|
|
|
|
users = self.validated_data.get('users', [])
|
|
|
|
self.validated_data['users'] = ','.join(users)
|
|
|
|
return super().save(**kwargs)
|
|
|
|
|
2021-09-07 10:16:27 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
validated_data['verify_code'] = random_string(4)
|
|
|
|
session = validated_data.get('session')
|
|
|
|
if session:
|
|
|
|
validated_data['creator_id'] = session.user_id
|
2022-07-07 07:41:26 +00:00
|
|
|
validated_data['created_by'] = pretty_string(str(session.user), max_length=32)
|
2021-09-07 10:16:27 +00:00
|
|
|
validated_data['org_id'] = session.org_id
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
|
|
|
|
class SessionJoinRecordSerializer(OrgResourceModelSerializerMixin):
|
2023-04-03 05:55:27 +00:00
|
|
|
action_permission = LabeledChoiceField(
|
|
|
|
choices=ActionPermission.choices, read_only=True, label=_('Action permission')
|
|
|
|
)
|
|
|
|
|
2021-09-07 10:16:27 +00:00
|
|
|
class Meta:
|
|
|
|
model = SessionJoinRecord
|
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'joiner_display', 'verify_code', 'date_joined', 'date_left',
|
|
|
|
'remote_addr', 'login_from', 'is_success', 'reason', 'is_finished',
|
2023-04-03 05:55:27 +00:00
|
|
|
'created_by', 'date_created', 'date_updated', 'action_permission'
|
2021-09-07 10:16:27 +00:00
|
|
|
]
|
|
|
|
fields_fk = ['session', 'sharing', 'joiner']
|
|
|
|
fields = fields_small + fields_fk
|
|
|
|
extra_kwargs = {
|
|
|
|
'session': {'required': False},
|
|
|
|
'joiner': {'required': True},
|
|
|
|
'sharing': {'required': True},
|
|
|
|
'remote_addr': {'required': True},
|
|
|
|
'verify_code': {'required': True},
|
|
|
|
'joiner_display': {'label': _('Joiner')},
|
|
|
|
}
|
|
|
|
|
|
|
|
def create(self, validate_data):
|
|
|
|
sharing = validate_data.get('sharing')
|
|
|
|
if sharing:
|
|
|
|
validate_data['session'] = sharing.session
|
|
|
|
validate_data['org_id'] = sharing.org_id
|
|
|
|
return super().create(validate_data)
|