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
|
|
|
|
|
|
|
|
from users.models import User, UserGroup
|
|
|
|
from assets.models import Asset, Domain, AdminUser, SystemUser, Label
|
|
|
|
from perms.models import AssetPermission
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2018-11-02 06:38:44 +00:00
|
|
|
from .utils import set_current_org, get_current_org
|
2018-07-25 03:21:12 +00:00
|
|
|
from .models import Organization
|
2018-11-02 06:38:44 +00:00
|
|
|
from .mixins import OrgMembershipSerializerMixin
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgSerializer(ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Organization
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-07-25 03:21:12 +00:00
|
|
|
fields = '__all__'
|
2019-03-19 11:09:09 +00:00
|
|
|
read_only_fields = ['created_by', 'date_created']
|
2018-11-02 06:38:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgReadSerializer(ModelSerializer):
|
|
|
|
admins = 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 OrgMembershipAdminSerializer(OrgMembershipSerializerMixin, ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Organization.admins.through
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-11-02 06:38:44 +00:00
|
|
|
fields = '__all__'
|
|
|
|
|
|
|
|
|
|
|
|
class OrgMembershipUserSerializer(OrgMembershipSerializerMixin, ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Organization.users.through
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-11-02 06:38:44 +00:00
|
|
|
fields = '__all__'
|