def perform_create(self, serializer): user = serializer.save() signals.user_registered.send(sender=self.__class__, user=user, request=self.request) if settings.get('SEND_ACTIVATION_EMAIL'): self.send_activation_email(user) elif settings.get('SEND_CONFIRMATION_EMAIL'): self.send_confirmation_email(user)
def get_urls_mapping(self, **kwargs): mapping = self.urls_mapping.copy() mapping.update(kwargs) if self.urls_extra_mapping: mapping.update(self.urls_extra_mapping) mapping.update(settings.get('ROOT_VIEW_URLS_MAPPING')) return mapping
def _action(self, serializer): self.request.user.set_password(serializer.data['new_password']) self.request.user.save() if settings.get('LOGOUT_ON_PASSWORD_CHANGE'): utils.logout_user(self.request) return response.Response(status=status.HTTP_204_NO_CONTENT)
def create(self, validated_data): if settings.get('SEND_ACTIVATION_EMAIL'): with transaction.atomic(): user = User.objects.create_user(**validated_data) user.is_active = False user.save(update_fields=['is_active']) else: user = User.objects.create_user(**validated_data) return user
def get(self, serializer_name): try: if isinstance(self.serializers[serializer_name], six.string_types): self.serializers[serializer_name] = self.load_serializer( self.serializers[serializer_name]) return self.serializers[serializer_name] except KeyError: raise Exception("Try to use serializer name '%s' that is not one of: %s" % ( serializer_name, tuple(settings.get('SERIALIZERS').keys()) ))
def _action(self, serializer): serializer.user.is_active = True serializer.user.save() signals.user_activated.send(sender=self.__class__, user=serializer.user, request=self.request) if settings.get('SEND_CONFIRMATION_EMAIL'): email_factory = utils.UserConfirmationEmailFactory.from_request( self.request, user=serializer.user) email = email_factory.create() email.send() return Response(status=status.HTTP_204_NO_CONTENT)
class UserRegistrationSerializer(serializers.ModelSerializer): password = serializers.CharField(style={'input_type': 'password'}, write_only=True, validators=settings.get('PASSWORD_VALIDATORS')) class Meta: model = User exclude = ('is_active', 'is_staff', ) def create(self, validated_data): if settings.get('SEND_ACTIVATION_EMAIL'): with transaction.atomic(): user = User.objects.create_user(**validated_data) user.is_active = False user.save(update_fields=['is_active']) else: user = User.objects.create_user(**validated_data) return user
def validate_email(self, value): if settings.get('PASSWORD_RESET_SHOW_EMAIL_NOT_FOUND') and \ not self.context['view'].get_users(value): raise serializers.ValidationError(self.error_messages['email_not_found']) return value
class PasswordSerializer(serializers.Serializer): new_password = serializers.CharField(style={'input_type': 'password'}, validators=settings.get('PASSWORD_VALIDATORS'))
class TokenSerializer(serializers.ModelSerializer): auth_token = serializers.CharField(source='key') class Meta: model = Token fields = ( 'auth_token', ) class SerializersManager(object): def __init__(self, serializer_confs): self.serializers = serializer_confs.copy() def get(self, serializer_name): try: if isinstance(self.serializers[serializer_name], six.string_types): self.serializers[serializer_name] = self.load_serializer( self.serializers[serializer_name]) return self.serializers[serializer_name] except KeyError: raise Exception("Try to use serializer name '%s' that is not one of: %s" % ( serializer_name, tuple(settings.get('SERIALIZERS').keys()) )) def load_serializer(self, serializer_class): return import_string(serializer_class) serializers_manager = SerializersManager(settings.get('SERIALIZERS'))
def get_context(self): context = super(UserPasswordResetEmailFactory, self).get_context() context['url'] = settings.get('PASSWORD_RESET_CONFIRM_URL').format( **context) return context
def get_context(self): context = super(UserActivationEmailFactory, self).get_context() context['url'] = settings.get('ACTIVATION_URL').format(**context) return context
def get_serializer_class(self): if settings.get('SET_USERNAME_RETYPE'): return serializers.serializers_manager.get('set_username_retype') return serializers.serializers_manager.get('set_username')
def get_serializer_class(self): if settings.get('PASSWORD_RESET_CONFIRM_RETYPE'): return serializers.serializers_manager.get( 'password_reset_confirm_retype') return serializers.serializers_manager.get('password_reset_confirm')
def get_serializer_class(self): if settings.get('SET_PASSWORD_RETYPE'): return serializers.serializers_manager.get('set_password_retype') return serializers.serializers_manager.get('set_password')