Example #1
0
class IPAddressConditionSet(RequestConditionSet):
    percent = Percent()
    ip_address = IPAddress(label='IP Address')
    internal_ip = Boolean(label='Internal IPs')

    def get_namespace(self):
        return 'ip'

    def get_field_value(self, instance, field_name):
        # XXX: can we come up w/ a better API?
        # Ensure we map ``percent`` to the ``id`` column
        if field_name == 'percent':
            return self._ip_to_int(instance.META['REMOTE_ADDR'])
        elif field_name == 'ip_address':
            return instance.META['REMOTE_ADDR']
        elif field_name == 'internal_ip':
            return instance.META['REMOTE_ADDR'] in settings.INTERNAL_IPS
        return super(IPAddressConditionSet,
                     self).get_field_value(instance, field_name)

    def _ip_to_int(self, ip):
        if '.' in ip:
            # IPv4
            return sum([int(x) for x in ip.split('.')])
        if ':' in ip:
            # IPv6
            hi, lo = struct.unpack('!QQ',
                                   socket.inet_pton(socket.AF_INET6, ip))
            return (hi << 64) | lo
        raise ValueError('Invalid IP Address %r' % ip)

    def get_group_label(self):
        return 'IP Address'
Example #2
0
class IPAddressConditionSet(RequestConditionSet):
    percent = Percent()
    ip_address = IPAddress(label='IP Address')

    def get_namespace(self):
        return 'ip'

    def get_field_value(self, instance, field_name):
        # XXX: can we come up w/ a better API?
        # Ensure we map ``percent`` to the ``id`` column
        if field_name == 'percent':
            return sum(
                [int(x) for x in instance.META['REMOTE_ADDR'].split('.')])
        elif field_name == 'ip_address':
            return instance.META['REMOTE_ADDR']
        return super(IPAddressConditionSet,
                     self).get_field_value(instance, field_name)

    def get_group_label(self):
        return 'IP Address'
Example #3
0
class IPAddressConditionSet(RequestConditionSet):
    percent = Percent()
    ip_address = IPAddress(label='IP Address')
    internal_ip = Boolean(label='Internal IPs')

    def get_namespace(self):
        return 'ip'

    def get_field_value(self, instance, field_name):
        # XXX: can we come up w/ a better API?
        # Ensure we map ``percent`` to the ``id`` column
        if field_name == 'percent':
            return sum([int(x) for x in instance.META['REMOTE_ADDR'].split('.')])
        elif field_name == 'ip_address':
            # use our better internalized ip middleware
            return getattr(instance, 'ip', instance.META['REMOTE_ADDR'])
        elif field_name == 'internal_ip':
            return instance.META['REMOTE_ADDR'] in settings.INTERNAL_IPS
        return super(IPAddressConditionSet, self).get_field_value(instance, field_name)

    def get_group_label(self):
        return 'IP Address'
Example #4
0
class UserConditionSet(ModelConditionSet):
    percent = Percent()
    username = String()
    is_anonymous = Boolean(label='Anonymous')
    is_staff = Boolean(label='Staff')
    is_superuser = Boolean(label='Superuser')

    def can_execute(self, instance):
        return isinstance(instance, (User, AnonymousUser))

    def is_active(self, instance, conditions):
        """
        value is the current value of the switch
        instance is the instance of our type
        """
        if isinstance(instance, User):
            return super(UserConditionSet,
                         self).is_active(instance, conditions)

        # HACK: allow is_authenticated to work on AnonymousUser
        condition = conditions.get(self.get_namespace(),
                                   {}).get('is_anonymous')
        return bool(condition)
Example #5
0
 def test_clean_fail_empty(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('')
Example #6
0
 def test_clean_fail_no_numbers(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('-')
Example #7
0
 def test_clean_success(self):
     condition = Percent()
     assert condition.clean('0-50') == '0-50'
Example #8
0
 def test_clean_first_greater_than_second(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('80-20')
Example #9
0
 def test_clean_fail_out_of_range(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('10-160')
Example #10
0
 def test_clean_fail_empty(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('')
Example #11
0
 def test_clean_fail_no_numbers(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('-')
Example #12
0
 def test_clean_success(self):
     condition = Percent()
     assert condition.clean('0-50') == '0-50'
Example #13
0
 def test_clean_first_greater_than_second(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('80-20')
Example #14
0
 def test_clean_fail_out_of_range(self):
     condition = Percent()
     with pytest.raises(ValidationError):
         condition.clean('10-160')