2020-10-19 12:13:01 +00:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
|
|
|
|
2020-10-28 04:44:19 +00:00
|
|
|
from rest_framework import serializers
|
2020-10-28 06:45:30 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-08-17 12:05:07 +00:00
|
|
|
|
2020-10-19 12:13:01 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2021-09-09 08:04:54 +00:00
|
|
|
from assets.serializers.base import AuthSerializerMixin
|
2021-01-06 04:44:12 +00:00
|
|
|
from common.drf.serializers import MethodSerializer
|
2021-09-09 08:04:54 +00:00
|
|
|
from .attrs import (
|
|
|
|
category_serializer_classes_mapping,
|
|
|
|
type_serializer_classes_mapping
|
|
|
|
)
|
2020-10-19 12:13:01 +00:00
|
|
|
from .. import models
|
2021-07-27 08:06:00 +00:00
|
|
|
from .. import const
|
2020-10-19 12:13:01 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2021-09-12 13:00:51 +00:00
|
|
|
'AppSerializer', 'MiniAppSerializer', 'AppSerializerMixin',
|
2021-09-09 08:04:54 +00:00
|
|
|
'AppAccountSerializer', 'AppAccountSecretSerializer'
|
2020-10-19 12:13:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2021-09-09 08:04:54 +00:00
|
|
|
class AppSerializerMixin(serializers.Serializer):
|
2021-01-06 04:44:12 +00:00
|
|
|
attrs = MethodSerializer()
|
2021-01-05 15:39:38 +00:00
|
|
|
|
2021-01-06 04:44:12 +00:00
|
|
|
def get_attrs_serializer(self):
|
2021-01-19 07:44:19 +00:00
|
|
|
default_serializer = serializers.Serializer(read_only=True)
|
2021-01-12 10:06:42 +00:00
|
|
|
if isinstance(self.instance, models.Application):
|
2021-01-19 07:44:19 +00:00
|
|
|
_type = self.instance.type
|
|
|
|
_category = self.instance.category
|
2021-01-05 15:39:38 +00:00
|
|
|
else:
|
2021-01-19 07:44:19 +00:00
|
|
|
_type = self.context['request'].query_params.get('type')
|
|
|
|
_category = self.context['request'].query_params.get('category')
|
|
|
|
|
|
|
|
if _type:
|
|
|
|
serializer_class = type_serializer_classes_mapping.get(_type)
|
|
|
|
elif _category:
|
|
|
|
serializer_class = category_serializer_classes_mapping.get(_category)
|
|
|
|
else:
|
|
|
|
serializer_class = default_serializer
|
|
|
|
|
|
|
|
if not serializer_class:
|
|
|
|
serializer_class = default_serializer
|
|
|
|
|
|
|
|
if isinstance(serializer_class, type):
|
|
|
|
serializer = serializer_class()
|
|
|
|
else:
|
|
|
|
serializer = serializer_class
|
2021-01-06 04:44:12 +00:00
|
|
|
return serializer
|
2021-01-05 15:39:38 +00:00
|
|
|
|
2021-09-09 08:04:54 +00:00
|
|
|
def create(self, validated_data):
|
|
|
|
return super().create(validated_data)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
2021-01-05 15:39:38 +00:00
|
|
|
|
2021-09-09 08:04:54 +00:00
|
|
|
class AppSerializer(AppSerializerMixin, BulkOrgResourceModelSerializer):
|
2021-07-30 11:13:47 +00:00
|
|
|
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category display'))
|
|
|
|
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display'))
|
2020-10-28 04:44:19 +00:00
|
|
|
|
2020-10-19 12:13:01 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Application
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
2021-07-27 08:06:00 +00:00
|
|
|
'category', 'category_display', 'type', 'type_display',
|
|
|
|
'attrs', 'date_created', 'date_updated', 'created_by', 'comment'
|
2020-10-19 12:13:01 +00:00
|
|
|
]
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_fk = ['domain']
|
|
|
|
fields = fields_small + fields_fk
|
2020-10-19 12:13:01 +00:00
|
|
|
read_only_fields = [
|
|
|
|
'created_by', 'date_created', 'date_updated', 'get_type_display',
|
|
|
|
]
|
|
|
|
|
2021-01-03 21:27:03 +00:00
|
|
|
def validate_attrs(self, attrs):
|
|
|
|
_attrs = self.instance.attrs if self.instance else {}
|
|
|
|
_attrs.update(attrs)
|
|
|
|
return _attrs
|
2021-01-05 15:39:38 +00:00
|
|
|
|
2021-06-11 03:05:40 +00:00
|
|
|
|
2021-09-12 13:00:51 +00:00
|
|
|
class MiniAppSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = models.Application
|
|
|
|
fields = AppSerializer.Meta.fields_mini
|
|
|
|
|
|
|
|
|
2021-09-09 08:04:54 +00:00
|
|
|
class AppAccountSerializer(AuthSerializerMixin, BulkOrgResourceModelSerializer):
|
2021-07-30 09:19:07 +00:00
|
|
|
category = serializers.ChoiceField(label=_('Category'), choices=const.AppCategory.choices, read_only=True)
|
2021-07-30 11:18:36 +00:00
|
|
|
category_display = serializers.SerializerMethodField(label=_('Category display'))
|
2021-07-30 09:19:07 +00:00
|
|
|
type = serializers.ChoiceField(label=_('Type'), choices=const.AppType.choices, read_only=True)
|
2021-07-30 11:18:36 +00:00
|
|
|
type_display = serializers.SerializerMethodField(label=_('Type display'))
|
2021-06-11 03:05:40 +00:00
|
|
|
|
2021-07-27 08:06:00 +00:00
|
|
|
category_mapper = dict(const.AppCategory.choices)
|
|
|
|
type_mapper = dict(const.AppType.choices)
|
2021-06-11 03:05:40 +00:00
|
|
|
|
2021-09-09 08:04:54 +00:00
|
|
|
class Meta:
|
|
|
|
model = models.Account
|
|
|
|
fields_mini = ['id', 'username', 'version']
|
|
|
|
fields_write_only = ['password', 'private_key']
|
|
|
|
fields_fk = ['systemuser', 'systemuser_display', 'app', 'app_display']
|
|
|
|
fields = fields_mini + fields_fk + fields_write_only + [
|
|
|
|
'type', 'type_display', 'category', 'category_display',
|
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'username': {'default': '', 'required': False},
|
|
|
|
'password': {'write_only': True},
|
|
|
|
'app_display': {'label': _('Application display')}
|
|
|
|
}
|
|
|
|
use_model_bulk_create = True
|
|
|
|
model_bulk_create_kwargs = {
|
|
|
|
'ignore_conflicts': True
|
|
|
|
}
|
2021-06-11 03:05:40 +00:00
|
|
|
|
2021-07-30 09:19:07 +00:00
|
|
|
def get_category_display(self, obj):
|
2021-09-09 08:04:54 +00:00
|
|
|
return self.category_mapper.get(obj.category)
|
2021-06-11 03:05:40 +00:00
|
|
|
|
2021-07-30 09:19:07 +00:00
|
|
|
def get_type_display(self, obj):
|
2021-09-09 08:04:54 +00:00
|
|
|
return self.type_mapper.get(obj.type)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
queryset = queryset.prefetch_related('systemuser', 'app')
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
instance.load_auth()
|
|
|
|
return super().to_representation(instance)
|
|
|
|
|
|
|
|
|
|
|
|
class AppAccountSecretSerializer(AppAccountSerializer):
|
|
|
|
class Meta(AppAccountSerializer.Meta):
|
|
|
|
extra_kwargs = {
|
|
|
|
'password': {'write_only': False},
|
|
|
|
'private_key': {'write_only': False},
|
|
|
|
'public_key': {'write_only': False},
|
|
|
|
}
|