<s> from django . contrib . auth import login as auth_login <EOL> from django . contrib . auth import logout as auth_logout <EOL> from django . contrib . auth . forms import AuthenticationForm <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . translation import ugettext_lazy <EOL> from . . core . config import config <EOL> from . . utils . rest_api import ( <EOL> ModelViewSet , <EOL> Response , <EOL> SimpleMetadata , <EOL> ValidationError , <EOL> detail_route , <EOL> status , <EOL> ) <EOL> from . . utils . views import APIView , PDFView <EOL> from . access_permissions import UserAccessPermissions <EOL> from . models import Group , User <EOL> from . pdf import users_passwords_to_pdf , users_to_pdf <EOL> from . serializers import GroupSerializer , UserFullSerializer <EOL> class UserViewSet ( ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> access_permissions = UserAccessPermissions ( ) <EOL> queryset = User . objects . all ( ) <EOL> def check_view_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action == '<STR_LIT>' : <EOL> result = self . get_access_permissions ( ) . can_retrieve ( self . request . user ) <EOL> elif self . action in ( '<STR_LIT>' , '<STR_LIT:list>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> result = self . request . user . has_perm ( '<STR_LIT>' ) <EOL> elif self . action in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> result = ( self . request . user . has_perm ( '<STR_LIT>' ) and <EOL> self . request . user . has_perm ( '<STR_LIT>' ) and <EOL> self . request . user . has_perm ( '<STR_LIT>' ) ) <EOL> else : <EOL> result = False <EOL> return result <EOL> def get_serializer_class ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> serializer_class = UserFullSerializer <EOL> else : <EOL> serializer_class = super ( ) . get_serializer_class ( ) <EOL> return serializer_class <EOL> def list ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = super ( ) . list ( request , * args , ** kwargs ) <EOL> self . extract_default_password ( response ) <EOL> return response <EOL> def retrieve ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> response = super ( ) . retrieve ( request , * args , ** kwargs ) <EOL> self . extract_default_password ( response ) <EOL> return response <EOL> def extract_default_password ( self , response ) : <EOL> """<STR_LIT>""" <EOL> if not self . request . user . has_perm ( '<STR_LIT>' ) : <EOL> if isinstance ( response . data , dict ) : <EOL> try : <EOL> del response . data [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> elif isinstance ( response . data , list ) : <EOL> for user in response . data : <EOL> try : <EOL> del user [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> def update ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if ( request . user . has_perm ( '<STR_LIT>' ) and <EOL> request . user . has_perm ( '<STR_LIT>' ) ) : <EOL> if request . data . get ( '<STR_LIT>' ) is False and self . get_object ( ) == request . user : <EOL> raise ValidationError ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> response = super ( ) . update ( request , * args , ** kwargs ) <EOL> else : <EOL> user = self . get_object ( ) <EOL> if request . user != user : <EOL> self . permission_denied ( request ) <EOL> whitelist = ( <EOL> '<STR_LIT:username>' , <EOL> '<STR_LIT:title>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , ) <EOL> keys = list ( request . data . keys ( ) ) <EOL> for key in keys : <EOL> if key not in whitelist : <EOL> del request . data [ key ] <EOL> serializer = self . get_serializer ( <EOL> user , <EOL> data = request . data , <EOL> partial = kwargs . get ( '<STR_LIT>' , False ) ) <EOL> serializer . is_valid ( raise_exception = True ) <EOL> serializer . save ( ) <EOL> response = Response ( serializer . data ) <EOL> return response <EOL> def destroy ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> instance = self . get_object ( ) <EOL> if instance == self . request . user : <EOL> raise ValidationError ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> self . perform_destroy ( instance ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> @ detail_route ( methods = [ '<STR_LIT>' ] ) <EOL> def reset_password ( self , request , pk = None ) : <EOL> """<STR_LIT>""" <EOL> user = self . get_object ( ) <EOL> if request . data . get ( '<STR_LIT:password>' ) : <EOL> user . default_password = request . data [ '<STR_LIT:password>' ] <EOL> user . set_password ( user . default_password ) <EOL> user . save ( ) <EOL> return Response ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> class GroupViewSetMetadata ( SimpleMetadata ) : <EOL> """<STR_LIT>""" <EOL> def get_field_info ( self , field ) : <EOL> """<STR_LIT>""" <EOL> field_info = super ( ) . get_field_info ( field ) <EOL> if field . field_name == '<STR_LIT>' : <EOL> for choice in field_info [ '<STR_LIT>' ] : <EOL> choice [ '<STR_LIT>' ] = choice [ '<STR_LIT>' ] . split ( '<STR_LIT>' ) [ <NUM_LIT:2> ] <EOL> return field_info <EOL> class GroupViewSet ( ModelViewSet ) : <EOL> """<STR_LIT>""" <EOL> metadata_class = GroupViewSetMetadata <EOL> queryset = Group . objects . all ( ) <EOL> serializer_class = GroupSerializer <EOL> def check_view_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . action in ( '<STR_LIT>' , '<STR_LIT:list>' , '<STR_LIT>' ) : <EOL> result = self . request . user . is_authenticated ( ) or config [ '<STR_LIT>' ] <EOL> elif self . action in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> result = ( self . request . user . has_perm ( '<STR_LIT>' ) and <EOL> self . request . user . has_perm ( '<STR_LIT>' ) and <EOL> self . request . user . has_perm ( '<STR_LIT>' ) ) <EOL> else : <EOL> result = False <EOL> return result <EOL> def destroy ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> instance = self . get_object ( ) <EOL> if instance . pk in ( <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> self . permission_denied ( request ) <EOL> self . perform_destroy ( instance ) <EOL> return Response ( status = status . HTTP_204_NO_CONTENT ) <EOL> class UserLoginView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def post ( self , * args , ** kwargs ) : <EOL> form = AuthenticationForm ( self . request , data = self . request . data ) <EOL> if not form . is_valid ( ) : <EOL> raise ValidationError ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> self . user = form . get_user ( ) <EOL> auth_login ( self . request , self . user ) <EOL> return super ( ) . post ( * args , ** kwargs ) <EOL> def get_context_data ( self , ** context ) : <EOL> """<STR_LIT>""" <EOL> if self . request . method == '<STR_LIT:GET>' : <EOL> if config [ '<STR_LIT>' ] : <EOL> context [ '<STR_LIT>' ] = config [ '<STR_LIT>' ] <EOL> else : <EOL> try : <EOL> user = User . objects . get ( username = '<STR_LIT>' ) <EOL> except User . DoesNotExist : <EOL> context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> if user . check_password ( '<STR_LIT>' ) : <EOL> context [ '<STR_LIT>' ] = _ ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) . format ( <EOL> username = '<STR_LIT>' , <EOL> password = '<STR_LIT>' ) <EOL> else : <EOL> context [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> context [ '<STR_LIT>' ] = self . user . pk <EOL> return super ( ) . get_context_data ( ** context ) <EOL> class UserLogoutView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> def post ( self , * args , ** kwargs ) : <EOL> if not self . request . user . is_authenticated ( ) : <EOL> raise ValidationError ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> auth_logout ( self . request ) <EOL> return super ( ) . post ( * args , ** kwargs ) <EOL> class WhoAmIView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> def get_context_data ( self , ** context ) : <EOL> """<STR_LIT>""" <EOL> return super ( ) . get_context_data ( <EOL> user_id = self . request . user . pk , <EOL> guest_enabled = config [ '<STR_LIT>' ] , <EOL> ** context ) <EOL> class SetPasswordView ( APIView ) : <EOL> """<STR_LIT>""" <EOL> http_method_names = [ '<STR_LIT>' ] <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> user = request . user <EOL> if user . check_password ( request . data [ '<STR_LIT>' ] ) : <EOL> user . set_password ( request . data [ '<STR_LIT>' ] ) <EOL> user . save ( ) <EOL> else : <EOL> raise ValidationError ( { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> return super ( ) . post ( request , * args , ** kwargs ) <EOL> class UsersListPDF ( PDFView ) : <EOL> """<STR_LIT>""" <EOL> required_permission = '<STR_LIT>' <EOL> filename = ugettext_lazy ( '<STR_LIT>' ) <EOL> document_title = ugettext_lazy ( '<STR_LIT>' ) <EOL> def append_to_pdf ( self , pdf ) : <EOL> """<STR_LIT>""" <EOL> users_to_pdf ( pdf ) <EOL> class UsersPasswordsPDF ( PDFView ) : <EOL> """<STR_LIT>""" <EOL> required_permission = '<STR_LIT>' <EOL> filename = ugettext_lazy ( '<STR_LIT>' ) <EOL> top_space = <NUM_LIT:0> <EOL> def build_document ( self , pdf_document , story ) : <EOL> pdf_document . build ( story ) <EOL> def append_to_pdf ( self , pdf ) : <EOL> """<STR_LIT>""" <EOL> users_passwords_to_pdf ( pdf ) </s>
