2020-08-03 08:17:46 +00:00
|
|
|
from django.db.models import F
|
2018-07-25 03:21:12 +00:00
|
|
|
from rest_framework.serializers import ModelSerializer
|
2018-11-02 06:38:44 +00:00
|
|
|
from rest_framework import serializers
|
2020-11-06 02:46:04 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
from users.models.user import User
|
2020-08-03 08:17:46 +00:00
|
|
|
from common.drf.serializers import BulkModelSerializer
|
|
|
|
from common.db.models import concated_display as display
|
2020-11-06 02:46:04 +00:00
|
|
|
from .models import Organization, OrganizationMember, ROLE
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
|
|
|
2021-01-17 04:08:21 +00:00
|
|
|
class ResourceStatisticsSerializer(serializers.Serializer):
|
|
|
|
users_amount = serializers.IntegerField(required=False)
|
|
|
|
groups_amount = serializers.IntegerField(required=False)
|
|
|
|
|
|
|
|
assets_amount = serializers.IntegerField(required=False)
|
|
|
|
nodes_amount = serializers.IntegerField(required=False)
|
|
|
|
admin_users_amount = serializers.IntegerField(required=False)
|
|
|
|
system_users_amount = serializers.IntegerField(required=False)
|
|
|
|
domains_amount = serializers.IntegerField(required=False)
|
|
|
|
gateways_amount = serializers.IntegerField(required=False)
|
|
|
|
|
|
|
|
applications_amount = serializers.IntegerField(required=False)
|
|
|
|
asset_perms_amount = serializers.IntegerField(required=False)
|
|
|
|
app_perms_amount = serializers.IntegerField(required=False)
|
|
|
|
|
|
|
|
|
2021-09-09 12:46:37 +00:00
|
|
|
class OrgSerializer(BulkModelSerializer):
|
2020-08-19 13:44:20 +00:00
|
|
|
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True, required=False)
|
|
|
|
admins = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True, required=False)
|
|
|
|
auditors = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True, required=False)
|
2020-07-20 02:42:22 +00:00
|
|
|
|
2021-01-17 11:10:11 +00:00
|
|
|
resource_statistics = ResourceStatisticsSerializer(source='resource_statistics_cache', read_only=True)
|
2021-01-17 04:08:21 +00:00
|
|
|
|
2018-07-25 03:21:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = Organization
|
2020-06-11 10:05:18 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
2021-04-29 11:10:45 +00:00
|
|
|
'resource_statistics',
|
|
|
|
'is_default', 'is_root',
|
|
|
|
'date_created',
|
|
|
|
'comment', 'created_by',
|
2020-06-11 10:05:18 +00:00
|
|
|
]
|
2021-01-17 04:08:21 +00:00
|
|
|
|
2020-06-11 10:05:18 +00:00
|
|
|
fields_m2m = ['users', 'admins', 'auditors']
|
|
|
|
fields = fields_small + fields_m2m
|
2019-03-19 11:09:09 +00:00
|
|
|
read_only_fields = ['created_by', 'date_created']
|
2020-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
2020-11-03 13:48:05 +00:00
|
|
|
members = self._pop_members(validated_data)
|
2020-07-20 02:42:22 +00:00
|
|
|
instance = Organization.objects.create(**validated_data)
|
|
|
|
OrganizationMember.objects.add_users_by_role(instance, *members)
|
|
|
|
return instance
|
|
|
|
|
2020-11-03 13:48:05 +00:00
|
|
|
def _pop_members(self, validated_data):
|
2020-07-20 02:42:22 +00:00
|
|
|
return (
|
|
|
|
validated_data.pop('users', None),
|
|
|
|
validated_data.pop('admins', None),
|
|
|
|
validated_data.pop('auditors', None)
|
|
|
|
)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
2020-11-03 13:48:05 +00:00
|
|
|
members = self._pop_members(validated_data)
|
2020-07-20 02:42:22 +00:00
|
|
|
for attr, value in validated_data.items():
|
|
|
|
setattr(instance, attr, value)
|
|
|
|
instance.save()
|
|
|
|
OrganizationMember.objects.set_users_by_role(instance, *members)
|
|
|
|
return instance
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
|
2020-06-11 10:05:18 +00:00
|
|
|
class OrgReadSerializer(OrgSerializer):
|
|
|
|
pass
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
class OrgMemberSerializer(BulkModelSerializer):
|
2020-09-14 02:39:33 +00:00
|
|
|
org_display = serializers.CharField(read_only=True)
|
|
|
|
user_display = serializers.CharField(read_only=True)
|
|
|
|
role_display = serializers.CharField(source='get_role_display', read_only=True)
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
class Meta:
|
2020-08-05 10:26:07 +00:00
|
|
|
model = OrganizationMember
|
2021-04-29 11:10:45 +00:00
|
|
|
fields_mini = ['id']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'role', 'role_display'
|
|
|
|
]
|
|
|
|
fields_fk = ['org', 'user', 'org_display', 'user_display',]
|
|
|
|
fields = fields_small + fields_fk
|
2021-01-17 04:08:21 +00:00
|
|
|
use_model_bulk_create = True
|
|
|
|
model_bulk_create_kwargs = {'ignore_conflicts': True}
|
2020-08-03 08:17:46 +00:00
|
|
|
|
2020-10-10 10:33:43 +00:00
|
|
|
def get_unique_together_validators(self):
|
|
|
|
if self.parent:
|
|
|
|
return []
|
|
|
|
return super().get_unique_together_validators()
|
|
|
|
|
2020-08-03 08:17:46 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
return queryset.annotate(
|
|
|
|
org_display=F('org__name'),
|
|
|
|
user_display=display('user__name', 'user__username')
|
|
|
|
).distinct()
|
2020-05-21 08:09:42 +00:00
|
|
|
|
|
|
|
|
2020-11-19 06:49:50 +00:00
|
|
|
class OrgMemberOldBaseSerializer(BulkModelSerializer):
|
2020-11-06 02:46:04 +00:00
|
|
|
organization = serializers.PrimaryKeyRelatedField(
|
|
|
|
label=_('Organization'), queryset=Organization.objects.all(), required=True, source='org'
|
|
|
|
)
|
|
|
|
|
2020-11-19 06:49:50 +00:00
|
|
|
def to_internal_value(self, data):
|
|
|
|
view = self.context['view']
|
|
|
|
org_id = view.kwargs.get('org_id')
|
|
|
|
if org_id:
|
|
|
|
data['organization'] = org_id
|
|
|
|
return super().to_internal_value(data)
|
|
|
|
|
2020-11-06 02:46:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = OrganizationMember
|
|
|
|
fields = ('id', 'organization', 'user', 'role')
|
|
|
|
|
|
|
|
|
2020-11-19 06:49:50 +00:00
|
|
|
class OrgMemberAdminSerializer(OrgMemberOldBaseSerializer):
|
|
|
|
role = serializers.HiddenField(default=ROLE.ADMIN)
|
2020-11-06 02:46:04 +00:00
|
|
|
|
2020-11-19 06:49:50 +00:00
|
|
|
|
|
|
|
class OrgMemberUserSerializer(OrgMemberOldBaseSerializer):
|
|
|
|
role = serializers.HiddenField(default=ROLE.USER)
|
2020-11-06 02:46:04 +00:00
|
|
|
|
|
|
|
|
2020-05-21 08:09:42 +00:00
|
|
|
class OrgRetrieveSerializer(OrgReadSerializer):
|
|
|
|
admins = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
|
|
|
auditors = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
|
|
|
users = serializers.PrimaryKeyRelatedField(many=True, read_only=True)
|
|
|
|
|
|
|
|
class Meta(OrgReadSerializer.Meta):
|
|
|
|
pass
|
2021-03-02 06:57:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CurrentOrgSerializer(ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Organization
|
|
|
|
fields = ['id', 'name', 'is_default', 'is_root', 'comment']
|