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-07-20 02:42:22 +00:00
|
|
|
|
|
|
|
from users.models.user import User
|
2019-04-25 02:11:50 +00:00
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
2020-07-20 02:42:22 +00:00
|
|
|
from .models import Organization, OrganizationMember
|
2019-08-21 12:27:21 +00:00
|
|
|
from .mixins.serializers import OrgMembershipSerializerMixin
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgSerializer(ModelSerializer):
|
2020-07-20 02:42:22 +00:00
|
|
|
users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True)
|
|
|
|
admins = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True)
|
|
|
|
auditors = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects.all(), write_only=True)
|
|
|
|
|
2018-07-25 03:21:12 +00:00
|
|
|
class Meta:
|
|
|
|
model = Organization
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2020-06-11 10:05:18 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'created_by', 'date_created', 'comment'
|
|
|
|
]
|
|
|
|
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):
|
|
|
|
members = self._pop_memebers(validated_data)
|
|
|
|
instance = Organization.objects.create(**validated_data)
|
|
|
|
OrganizationMember.objects.add_users_by_role(instance, *members)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
def _pop_memebers(self, validated_data):
|
|
|
|
return (
|
|
|
|
validated_data.pop('users', None),
|
|
|
|
validated_data.pop('admins', None),
|
|
|
|
validated_data.pop('auditors', None)
|
|
|
|
)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
members = self._pop_memebers(validated_data)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class OrgMembershipAdminSerializer(OrgMembershipSerializerMixin, ModelSerializer):
|
|
|
|
class Meta:
|
2020-07-20 02:42:22 +00:00
|
|
|
model = Organization.members.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:
|
2020-07-20 02:42:22 +00:00
|
|
|
model = Organization.members.through
|
2019-04-25 02:11:50 +00:00
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
2018-11-02 06:38:44 +00:00
|
|
|
fields = '__all__'
|
2020-05-06 07:31:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OrgAllUserSerializer(serializers.Serializer):
|
|
|
|
user = serializers.UUIDField(read_only=True, source='id')
|
|
|
|
user_display = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
only_fields = ['id', 'username', 'name']
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_user_display(obj):
|
|
|
|
return str(obj)
|
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
|