<s> from django . contrib . auth . models import User <EOL> from rest_framework import serializers <EOL> from onadata . libs . serializers . fields . hyperlinked_multi_identity_field import HyperlinkedMultiIdentityField <EOL> from onadata . libs . serializers . user_serializer import UserSerializer <EOL> from onadata . apps . api . models import OrganizationProfile , Team <EOL> class TeamSerializer ( serializers . Serializer ) : <EOL> url = HyperlinkedMultiIdentityField ( <EOL> view_name = '<STR_LIT>' ) <EOL> name = serializers . CharField ( max_length = <NUM_LIT:100> , source = '<STR_LIT>' , <EOL> required = True ) <EOL> organization = serializers . SlugRelatedField ( <EOL> slug_field = '<STR_LIT:username>' , <EOL> queryset = User . objects . filter ( <EOL> pk__in = OrganizationProfile . objects . values ( '<STR_LIT:user>' ) ) ) <EOL> projects = serializers . HyperlinkedRelatedField ( view_name = '<STR_LIT>' , <EOL> many = True , <EOL> read_only = True ) <EOL> users = serializers . SerializerMethodField ( '<STR_LIT>' ) <EOL> def get_team_users ( self , obj ) : <EOL> users = [ ] <EOL> if obj : <EOL> for user in obj . user_set . all ( ) : <EOL> user_data = UserSerializer ( instance = user , <EOL> context = self . context ) . data <EOL> user_data . pop ( '<STR_LIT:url>' , None ) <EOL> users . append ( user_data ) <EOL> return users <EOL> def create ( self , validated_data ) : <EOL> org = validated_data . get ( '<STR_LIT>' , None ) <EOL> team_name = validated_data . get ( '<STR_LIT>' , None ) <EOL> request = self . context . get ( '<STR_LIT>' ) <EOL> created_by = request . user <EOL> return Team . objects . create ( organization = org , name = team_name , <EOL> created_by = created_by ) <EOL> def update ( self , attrs , instance = None ) : <EOL> org = attrs . get ( '<STR_LIT>' , None ) <EOL> projects = attrs . get ( '<STR_LIT>' , [ ] ) <EOL> instance . organization = org if org else instance . organization <EOL> instance . name = attrs . get ( '<STR_LIT>' , instance . name ) <EOL> instance . projects . clear ( ) <EOL> for project in projects : <EOL> instance . projects . add ( project ) <EOL> return instance </s>
