diff --git a/apps/orgs/serializers.py b/apps/orgs/serializers.py index ca5bdddba..ae98420e0 100644 --- a/apps/orgs/serializers.py +++ b/apps/orgs/serializers.py @@ -14,57 +14,22 @@ class OrgSerializer(ModelSerializer): class Meta: model = Organization list_serializer_class = AdaptedBulkListSerializer - fields = '__all__' + fields_mini = ['id', 'name'] + fields_small = fields_mini + [ + 'created_by', 'date_created', 'comment' + ] + fields_m2m = ['users', 'admins', 'auditors'] + fields = fields_small + fields_m2m read_only_fields = ['created_by', 'date_created'] + extra_kwargs = { + 'admins': {'write_only': True}, + 'users': {'write_only': True}, + 'auditors': {'write_only': True}, + } -class OrgReadSerializer(ModelSerializer): - admins = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) - auditors = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) - users = serializers.SlugRelatedField(slug_field='name', many=True, read_only=True) - user_groups = serializers.SerializerMethodField() - assets = serializers.SerializerMethodField() - domains = serializers.SerializerMethodField() - admin_users = serializers.SerializerMethodField() - system_users = serializers.SerializerMethodField() - labels = serializers.SerializerMethodField() - perms = serializers.SerializerMethodField() - - class Meta: - model = Organization - fields = '__all__' - - @staticmethod - def get_data_from_model(obj, model): - current_org = get_current_org() - set_current_org(Organization.root()) - if model == Asset: - data = [o.hostname for o in model.objects.filter(org_id=obj.id)] - else: - data = [o.name for o in model.objects.filter(org_id=obj.id)] - set_current_org(current_org) - return data - - def get_user_groups(self, obj): - return self.get_data_from_model(obj, UserGroup) - - def get_assets(self, obj): - return self.get_data_from_model(obj, Asset) - - def get_domains(self, obj): - return self.get_data_from_model(obj, Domain) - - def get_admin_users(self, obj): - return self.get_data_from_model(obj, AdminUser) - - def get_system_users(self, obj): - return self.get_data_from_model(obj, SystemUser) - - def get_labels(self, obj): - return self.get_data_from_model(obj, Label) - - def get_perms(self, obj): - return self.get_data_from_model(obj, AssetPermission) +class OrgReadSerializer(OrgSerializer): + pass class OrgMembershipAdminSerializer(OrgMembershipSerializerMixin, ModelSerializer):