2019-10-24 06:36:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from common.serializers import AdaptedBulkListSerializer
|
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-12-16 08:53:29 +00:00
|
|
|
from django.db.models import Count
|
2019-10-24 06:36:01 +00:00
|
|
|
from ..models import User, UserGroup
|
|
|
|
from .. import utils
|
|
|
|
|
|
|
|
__all__ = [
|
2019-12-16 08:53:29 +00:00
|
|
|
'UserGroupSerializer',
|
2019-10-24 06:36:01 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class UserGroupSerializer(BulkOrgResourceModelSerializer):
|
|
|
|
users = serializers.PrimaryKeyRelatedField(
|
2019-12-16 08:53:29 +00:00
|
|
|
required=False, many=True, queryset=User.objects, label=_('User'),
|
|
|
|
write_only=True
|
2019-10-24 06:36:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserGroup
|
|
|
|
list_serializer_class = AdaptedBulkListSerializer
|
|
|
|
fields = [
|
2019-12-16 08:53:29 +00:00
|
|
|
'id', 'name', 'users', 'users_amount', 'comment',
|
|
|
|
'date_created', 'created_by',
|
2019-10-24 06:36:01 +00:00
|
|
|
]
|
|
|
|
extra_kwargs = {
|
|
|
|
'created_by': {'label': _('Created by'), 'read_only': True}
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_fields_queryset()
|
|
|
|
|
|
|
|
def set_fields_queryset(self):
|
|
|
|
users_field = self.fields['users']
|
|
|
|
users_field.child_relation.queryset = utils.get_current_org_members()
|
|
|
|
|
|
|
|
def validate_users(self, users):
|
|
|
|
for user in users:
|
|
|
|
if user.is_super_auditor:
|
|
|
|
msg = _('Auditors cannot be join in the user group')
|
|
|
|
raise serializers.ValidationError(msg)
|
|
|
|
return users
|
|
|
|
|
2019-12-16 08:53:29 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
|
|
|
queryset = queryset.annotate(users_amount=Count('users'))
|
|
|
|
return queryset
|