def validate(self, attrs): if (attrs['provider'] == ROLE_FB and (attrs['fb_id'] is None or not attrs['fb_id'])): raise validation_error(ERROR_CODE['4006']) if (attrs['provider'] == ROLE_FB and (attrs['fb_access_token'] is None or not attrs['fb_access_token'])): raise validation_error(ERROR_CODE['4004']) if (attrs['provider'] == ROLE_TWITTER and (attrs['twitter_access_token'] is None or attrs['twitter_access_token_secret'] is None or not attrs['twitter_access_token'] or not attrs['twitter_access_token_secret'])): raise validation_error(ERROR_CODE['4005']) return attrs
def validate(self, attrs): username = attrs.get('username', None) password = attrs.get('password', None) user_obj = User.objects.filter( Q(email=username) | Q(username=username)).first() if user_obj is not None: credentials = {'username': user_obj.username, 'password': password} if all(credentials.values()): user = authenticate(**credentials) if user: super().update_device_id(self.initial_data, user) return LoginResponseSerializer(user_obj).data raise validation_error(ERROR_CODE['4008']) raise validation_error(ERROR_CODE['4008']) raise validation_error(ERROR_CODE['4007'])
def create(self, request, *args, **kwargs): serializer = self.serializer_class(data=request.data) if serializer.is_valid(raise_exception=True): # user_obj = serializer.save() # user = LoginResponseSerializer(user_obj).data return success_response(serializer.validated_data) return validation_error(serializer.errors)
def validate_password(self, current_password): """ check if password is true """ user = self.context['request'] if not user.check_password(current_password): raise validation_error(ERROR_CODE["4009"]) return current_password
def update_social_media_sync(self, request): data_serializer = self.serializer_class(request.user, data=request.data, context={'request': request}, partial=True) if data_serializer.is_valid(raise_exception=True): data_serializer.update(request.user, validated_data=request.data) return success_response(MESSAGES['1003']) return validation_error(data_serializer.errors)
def validate_password(self, password): """ validating password :param password: string :return: boolean value based on success or failure of validation . """ find_numeric = re.findall(r'\d', password) find_lowercase = re.findall('[a-z]', password) find_uppercase = re.findall('[A-Z]', password) count = 0 if find_numeric: count = count + 1 if find_lowercase: count = count + 1 if find_uppercase: count = count + 1 if count < 3: raise validation_error(ERROR_CODE["4002"]) if not len(password) >= PASSWORD_LENGTH: raise validation_error(ERROR_CODE["4001"]) return password
def update(self, instance, validated_data): user = self.context['request'].user provider = validated_data.get('provider', None) is_synced = validated_data.get('is_synced', None) is_synced, is_converted = self.strtobool(is_synced) if is_converted: obj_user = User.objects.get(id=user.id) if provider == ROLE_FB: obj_user.fb_sync = is_synced elif provider == ROLE_TWITTER: obj_user.twitter_sync = is_synced obj_user.save() else: raise validation_error(ERROR_CODE['4010']) return instance
def update(self, request, *args, **kwargs): """ This method is used to update the user profile. :param request: :return: """ instance = User.objects.filter(id=request.user.id).first() data_serializer = self.serializer_class(instance, data=request.data, context={'request': request}, partial=True) if data_serializer.is_valid(raise_exception=True): data_serializer.update( instance, validated_data=data_serializer.validated_data) return success_response(MESSAGES['1003']) return validation_error(data_serializer.errors)
def validate(self, attrs): """ validate if both the password match""" if attrs.get('password') != attrs.get('confirm_password'): raise validation_error(ERROR_CODE["4003"]) return attrs
def create(self, request, *args, **kwargs): data = self.serializer_class(data=request.data, context={'request': request.user}) if data.is_valid(raise_exception=True): return success_response(MESSAGES['1004']) return validation_error(data.errors)
def create(self, validated_data): # instance = UserContestsModel.objects.filter(user=validated_data['user']) # if instance: # fb_feed_id = (validated_data['fb_feed_id'] # if not instance[0].fb_feed_id # else instance[0].fb_feed_id) # twitter_feed_id = (validated_data['twitter_feed_id'] # if not instance[0].twitter_feed_id # else instance[0].twitter_feed_id) # fb_access_token = (validated_data['fb_access_token'] # if not instance[0].user.fb_access_token # else instance[0].user.fb_access_token) # twitter_access_token = (validated_data['twitter_access_token'] # if not instance[0].user.twitter_access_token # else instance[0].user.twitter_access_token) # twitter_access_token_secret = (validated_data['twitter_access_token_secret'] # if not instance[0].user.twitter_access_token_secret # else instance[0].user.twitter_access_token_secret) # instance.update(fb_feed_id=fb_feed_id, # twitter_feed_id=twitter_feed_id, # provider=provider) # User.objects.filter( # id=validated_data['user']).update( # fb_access_token=fb_access_token, # twitter_access_token=twitter_access_token, # twitter_access_token_secret=twitter_access_token_secret) # else: provider = validated_data['provider'] user_id = validated_data.pop('user', None) fb_access_token = validated_data.pop('fb_access_token', None) twitter_access_token = validated_data.pop('twitter_access_token', None) twitter_access_token_secret = validated_data.pop( 'twitter_access_token_secret', None) user_obj = User.objects.filter(id=user_id) if user_obj: user_obj.update( fb_access_token=fb_access_token, twitter_access_token=twitter_access_token, twitter_access_token_secret=twitter_access_token_secret) instance = UserContestsModel.objects.create(user=user_obj[0], **validated_data) if instance: instance.user = user_obj[0] instance.save() if provider == ROLE_FB: fb = c_facebook() feed_id = fb.get_latest_post( facebook_id=validated_data['fb_id'], access_token=fb_access_token) likes_count = fb.get_likes_count(feed_id, fb_access_token) # comments_count = fb.get_comments_count(feed_id, validated_data['fb_access_token']) instance.fb_feed_id = feed_id instance.fb_likes_count = likes_count instance.save() elif provider == ROLE_TWITTER: twitter = c_twitter() obj_post = twitter.get_post_details( validated_data['twitter_feed_id']) instance.twitter_likes_count = obj_post.favorite_count instance.save() return instance raise validation_error(ERROR_CODE['4007'])
def create(self, request, *args, **kwargs): user_serializer = self.serializer_class(data=request.data) if user_serializer.is_valid(raise_exception=True): user_serializer.save() return success_response(MESSAGES['1001']) return validation_error(user_serializer.errors)