<s> import logging <EOL> from collections import OrderedDict <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import Group <EOL> from django . contrib . auth . tokens import default_token_generator <EOL> from django . contrib . sites . shortcuts import get_current_site <EOL> from django . utils . encoding import force_bytes <EOL> from django . utils . http import urlsafe_base64_encode <EOL> from rest_framework import serializers <EOL> from stackdio . core . fields import HyperlinkedField , PasswordField <EOL> from stackdio . core . serializers import StackdioHyperlinkedModelSerializer <EOL> from . import models , utils <EOL> logger = logging . getLogger ( __name__ ) <EOL> LDAP_MANAGED_FIELDS = ( <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> ) <EOL> class UserGroupSerializer ( StackdioHyperlinkedModelSerializer ) : <EOL> class Meta : <EOL> model = Group <EOL> lookup_field = '<STR_LIT:name>' <EOL> fields = ( <EOL> '<STR_LIT:url>' , <EOL> '<STR_LIT:name>' , <EOL> ) <EOL> class GroupUserSerializer ( StackdioHyperlinkedModelSerializer ) : <EOL> class Meta : <EOL> model = get_user_model ( ) <EOL> lookup_field = '<STR_LIT:username>' <EOL> fields = ( <EOL> '<STR_LIT:url>' , <EOL> '<STR_LIT:username>' , <EOL> ) <EOL> class GroupSerializer ( StackdioHyperlinkedModelSerializer ) : <EOL> users = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , lookup_field = '<STR_LIT:name>' ) <EOL> action = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , lookup_field = '<STR_LIT:name>' ) <EOL> user_permissions = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , <EOL> lookup_field = '<STR_LIT:name>' , <EOL> ) <EOL> group_permissions = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , <EOL> lookup_field = '<STR_LIT:name>' , <EOL> ) <EOL> class Meta : <EOL> model = Group <EOL> lookup_field = '<STR_LIT:name>' <EOL> fields = ( <EOL> '<STR_LIT:url>' , <EOL> '<STR_LIT:name>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:action>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class GroupActionReturnSerializer ( GroupSerializer ) : <EOL> users = GroupUserSerializer ( source = '<STR_LIT>' , many = True ) <EOL> class GroupActionSerializer ( serializers . Serializer ) : <EOL> available_actions = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> action = serializers . ChoiceField ( available_actions ) <EOL> user = serializers . SlugRelatedField ( slug_field = '<STR_LIT:username>' , queryset = models . get_user_queryset ( ) ) <EOL> def to_representation ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return GroupActionReturnSerializer ( <EOL> instance , <EOL> context = self . context <EOL> ) . to_representation ( instance ) <EOL> def save ( self , ** kwargs ) : <EOL> group = self . instance <EOL> action = self . validated_data [ '<STR_LIT:action>' ] <EOL> user = self . validated_data [ '<STR_LIT:user>' ] <EOL> if action == '<STR_LIT>' : <EOL> group . user_set . add ( user ) <EOL> elif action == '<STR_LIT>' : <EOL> group . user_set . remove ( user ) <EOL> return group <EOL> class UserSettingsSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = models . UserSettings <EOL> fields = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def update ( self , instance , validated_data ) : <EOL> previous_value = instance . advanced_view <EOL> instance = super ( UserSettingsSerializer , self ) . update ( instance , validated_data ) <EOL> new_value = instance . advanced_view <EOL> request = self . context [ '<STR_LIT>' ] . _request <EOL> if previous_value and not new_value : <EOL> messages . info ( request , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> elif not previous_value and new_value : <EOL> messages . info ( request , '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return instance <EOL> class UserSerializer ( StackdioHyperlinkedModelSerializer ) : <EOL> superuser = serializers . BooleanField ( source = '<STR_LIT>' , read_only = True ) <EOL> groups = serializers . HyperlinkedIdentityField ( <EOL> view_name = '<STR_LIT>' , <EOL> lookup_field = '<STR_LIT:username>' <EOL> ) <EOL> settings = UserSettingsSerializer ( ) <EOL> change_password = HyperlinkedField ( view_name = '<STR_LIT>' ) <EOL> class Meta : <EOL> model = get_user_model ( ) <EOL> lookup_field = '<STR_LIT:username>' <EOL> fields = ( <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> extra_kwargs = { <EOL> '<STR_LIT:email>' : { '<STR_LIT>' : True , '<STR_LIT>' : False } , <EOL> } <EOL> def validate ( self , attrs ) : <EOL> if settings . LDAP_ENABLED and self . instance : <EOL> errors = OrderedDict ( ) <EOL> for attr , value in attrs . items ( ) : <EOL> current_value = getattr ( self . instance , attr ) <EOL> if attr in LDAP_MANAGED_FIELDS and value != current_value : <EOL> errors [ attr ] = [ '<STR_LIT>' ] <EOL> if errors : <EOL> raise serializers . ValidationError ( errors ) <EOL> return attrs <EOL> def create ( self , validated_data ) : <EOL> """<STR_LIT>""" <EOL> user = super ( UserSerializer , self ) . create ( validated_data ) <EOL> request = self . context [ '<STR_LIT>' ] <EOL> from_email = None <EOL> subject_template_name = '<STR_LIT>' <EOL> email_template_name = '<STR_LIT>' <EOL> current_site = get_current_site ( request ) <EOL> site_name = current_site . name <EOL> domain = current_site . domain <EOL> context = { <EOL> '<STR_LIT:email>' : user . email , <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT>' : site_name , <EOL> '<STR_LIT>' : urlsafe_base64_encode ( force_bytes ( user . pk ) ) , <EOL> '<STR_LIT:user>' : user , <EOL> '<STR_LIT>' : default_token_generator . make_token ( user ) , <EOL> '<STR_LIT>' : '<STR_LIT>' if request . is_secure ( ) else '<STR_LIT:http>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> '<STR_LIT>' . format ( site_name ) <EOL> } <EOL> utils . send_mail ( subject_template_name , email_template_name , context , from_email , user . email ) <EOL> return user <EOL> def update ( self , instance , validated_data ) : <EOL> settings = validated_data . pop ( '<STR_LIT>' ) <EOL> if settings : <EOL> settings_serializer = self . fields [ '<STR_LIT>' ] <EOL> settings_serializer . update ( instance . settings , settings ) <EOL> instance = super ( UserSerializer , self ) . update ( instance , validated_data ) <EOL> if settings : <EOL> validated_data [ '<STR_LIT>' ] = settings <EOL> return instance <EOL> class PublicUserSerializer ( UserSerializer ) : <EOL> """<STR_LIT>""" <EOL> class Meta ( UserSerializer . Meta ) : <EOL> fields = ( <EOL> '<STR_LIT:url>' , <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:email>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class ChangePasswordSerializer ( serializers . Serializer ) : <EOL> current_password = PasswordField ( label = '<STR_LIT>' ) <EOL> new_password1 = PasswordField ( label = '<STR_LIT>' ) <EOL> new_password2 = PasswordField ( label = '<STR_LIT>' ) <EOL> def to_representation ( self , instance ) : <EOL> """<STR_LIT>""" <EOL> return UserSerializer ( instance , context = self . context ) . to_representation ( instance ) <EOL> def validate ( self , attrs ) : <EOL> if settings . LDAP_ENABLED : <EOL> raise serializers . ValidationError ( { <EOL> '<STR_LIT>' : [ '<STR_LIT>' <EOL> '<STR_LIT>' ] <EOL> } ) <EOL> user = self . instance <EOL> if not user . check_password ( attrs [ '<STR_LIT>' ] ) : <EOL> raise serializers . ValidationError ( { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } ) <EOL> if attrs [ '<STR_LIT>' ] != attrs [ '<STR_LIT>' ] : <EOL> raise serializers . ValidationError ( { <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } ) <EOL> return attrs <EOL> def save ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert hasattr ( self , '<STR_LIT>' ) , ( <EOL> '<STR_LIT>' <EOL> ) <EOL> assert not self . errors , ( <EOL> '<STR_LIT>' <EOL> ) <EOL> new_password = self . validated_data [ '<STR_LIT>' ] <EOL> self . instance . set_password ( new_password ) <EOL> self . instance . save ( ) <EOL> return self . instance </s>
