Python django.utils.functional.lazy() Examples

The following are 30 code examples of django.utils.functional.lazy(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.utils.functional , or try the search function .
Example #1
Source File: context_processors.py    From GTDWeb with GNU General Public License v2.0 6 votes vote down vote up
def csrf(request):
    """
    Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
    it has not been provided by either a view decorator or the middleware
    """
    def _get_val():
        token = get_token(request)
        if token is None:
            # In order to be able to provide debugging info in the
            # case of misconfiguration, we use a sentinel value
            # instead of returning an empty dict.
            return 'NOTPROVIDED'
        else:
            return smart_text(token)
    _get_val = lazy(_get_val, six.text_type)

    return {'csrf_token': _get_val()} 
Example #2
Source File: context_processors.py    From luscan-devel with GNU General Public License v2.0 6 votes vote down vote up
def csrf(request):
    """
    Context processor that provides a CSRF token, or the string 'NOTPROVIDED' if
    it has not been provided by either a view decorator or the middleware
    """
    def _get_val():
        token = get_token(request)
        if token is None:
            # In order to be able to provide debugging info in the
            # case of misconfiguration, we use a sentinel value
            # instead of returning an empty dict.
            return 'NOTPROVIDED'
        else:
            return smart_text(token)
    _get_val = lazy(_get_val, six.text_type)

    return {'csrf_token': _get_val() } 
Example #3
Source File: fields.py    From Dailyfresh-B2C with Apache License 2.0 6 votes vote down vote up
def __init__(self, **kwargs):
        self.allow_blank = kwargs.pop('allow_blank', False)
        self.trim_whitespace = kwargs.pop('trim_whitespace', True)
        self.max_length = kwargs.pop('max_length', None)
        self.min_length = kwargs.pop('min_length', None)
        super(CharField, self).__init__(**kwargs)
        if self.max_length is not None:
            message = lazy(
                self.error_messages['max_length'].format,
                six.text_type)(max_length=self.max_length)
            self.validators.append(
                MaxLengthValidator(self.max_length, message=message))
        if self.min_length is not None:
            message = lazy(
                self.error_messages['min_length'].format,
                six.text_type)(min_length=self.min_length)
            self.validators.append(
                MinLengthValidator(self.min_length, message=message)) 
Example #4
Source File: fields.py    From Dailyfresh-B2C with Apache License 2.0 6 votes vote down vote up
def __init__(self, **kwargs):
        self.max_value = kwargs.pop('max_value', None)
        self.min_value = kwargs.pop('min_value', None)
        super(IntegerField, self).__init__(**kwargs)
        if self.max_value is not None:
            message = lazy(
                self.error_messages['max_value'].format,
                six.text_type)(max_value=self.max_value)
            self.validators.append(
                MaxValueValidator(self.max_value, message=message))
        if self.min_value is not None:
            message = lazy(
                self.error_messages['min_value'].format,
                six.text_type)(min_value=self.min_value)
            self.validators.append(
                MinValueValidator(self.min_value, message=message)) 
Example #5
Source File: fields.py    From Dailyfresh-B2C with Apache License 2.0 6 votes vote down vote up
def __init__(self, **kwargs):
        self.max_value = kwargs.pop('max_value', None)
        self.min_value = kwargs.pop('min_value', None)
        super(FloatField, self).__init__(**kwargs)
        if self.max_value is not None:
            message = lazy(
                self.error_messages['max_value'].format,
                six.text_type)(max_value=self.max_value)
            self.validators.append(
                MaxValueValidator(self.max_value, message=message))
        if self.min_value is not None:
            message = lazy(
                self.error_messages['min_value'].format,
                six.text_type)(min_value=self.min_value)
            self.validators.append(
                MinValueValidator(self.min_value, message=message)) 
Example #6
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def lazy_number(func, resultclass, number=None, **kwargs):
    if isinstance(number, six.integer_types):
        kwargs['number'] = number
        proxy = lazy(func, resultclass)(**kwargs)
    else:
        class NumberAwareString(resultclass):
            def __mod__(self, rhs):
                if isinstance(rhs, dict) and number:
                    try:
                        number_value = rhs[number]
                    except KeyError:
                        raise KeyError('Your dictionary lacks key \'%s\'. '
                            'Please provide it, because it is required to '
                            'determine whether string is singular or plural.'
                            % number)
                else:
                    number_value = rhs
                kwargs['number'] = number_value
                translated = func(**kwargs)
                try:
                    translated = translated % rhs
                except TypeError:
                    # String doesn't contain a placeholder for the number
                    pass
                return translated

        proxy = lazy(lambda **kwargs: NumberAwareString(), NumberAwareString)(**kwargs)
    return proxy 
Example #7
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_CharField(self):
        lazy_func = lazy(lambda: '', str)
        self.assertIsInstance(CharField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(CharField().get_prep_value(lazy_func()), str) 
Example #8
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_DateField(self):
        lazy_func = lazy(lambda: datetime.date.today(), datetime.date)
        self.assertIsInstance(DateField().get_prep_value(lazy_func()), datetime.date) 
Example #9
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_AutoField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(AutoField(primary_key=True).get_prep_value(lazy_func()), int) 
Example #10
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_lazy_strings_not_evaluated(self):
        lazy_func = lazy(lambda x: 0 / 0, int)  # raises ZeroDivisionError if evaluated.
        f = models.CharField(choices=[(lazy_func('group'), (('a', 'A'), ('b', 'B')))])
        self.assertEqual(f.get_choices(include_blank=True)[0], ('', '---------')) 
Example #11
Source File: test_functional.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_lazy_repr_bytes(self):
        original_object = b'J\xc3\xbcst a str\xc3\xadng'
        lazy_obj = lazy(lambda: original_object, bytes)
        self.assertEqual(repr(original_object), repr(lazy_obj())) 
Example #12
Source File: test_functional.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_lazy_repr_int(self):
        original_object = 15
        lazy_obj = lazy(lambda: original_object, int)
        self.assertEqual(repr(original_object), repr(lazy_obj())) 
Example #13
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_BooleanField(self):
        lazy_func = lazy(lambda: True, bool)
        self.assertIsInstance(BooleanField().get_prep_value(lazy_func()), bool) 
Example #14
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_URLField(self):
        lazy_func = lazy(lambda: 'http://domain.com', str)
        self.assertIsInstance(URLField().get_prep_value(lazy_func()), str) 
Example #15
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_TimeField(self):
        lazy_func = lazy(lambda: datetime.datetime.now().time(), datetime.time)
        self.assertIsInstance(TimeField().get_prep_value(lazy_func()), datetime.time) 
Example #16
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_TextField(self):
        lazy_func = lazy(lambda: 'Abc', str)
        self.assertIsInstance(TextField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(TextField().get_prep_value(lazy_func()), str) 
Example #17
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_SmallIntegerField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(SmallIntegerField().get_prep_value(lazy_func()), int) 
Example #18
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_SlugField(self):
        lazy_func = lazy(lambda: 'slug', str)
        self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(SlugField().get_prep_value(lazy_func()), str) 
Example #19
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_PositiveIntegerField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(PositiveIntegerField().get_prep_value(lazy_func()), int) 
Example #20
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_NullBooleanField(self):
        lazy_func = lazy(lambda: True, bool)
        self.assertIsInstance(NullBooleanField().get_prep_value(lazy_func()), bool) 
Example #21
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_GenericIPAddressField(self):
        lazy_func = lazy(lambda: '127.0.0.1', str)
        self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(GenericIPAddressField().get_prep_value(lazy_func()), str) 
Example #22
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_IPAddressField(self):
        lazy_func = lazy(lambda: '127.0.0.1', str)
        self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(IPAddressField().get_prep_value(lazy_func()), str) 
Example #23
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_IntegerField(self):
        lazy_func = lazy(lambda: 1, int)
        self.assertIsInstance(IntegerField().get_prep_value(lazy_func()), int) 
Example #24
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_FloatField(self):
        lazy_func = lazy(lambda: 1.2, float)
        self.assertIsInstance(FloatField().get_prep_value(lazy_func()), float) 
Example #25
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_FilePathField(self):
        lazy_func = lazy(lambda: 'tests.py', str)
        self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(FilePathField().get_prep_value(lazy_func()), str) 
Example #26
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_FileField(self):
        lazy_func = lazy(lambda: 'filename.ext', str)
        self.assertIsInstance(FileField().get_prep_value(lazy_func()), str)
        lazy_func = lazy(lambda: 0, int)
        self.assertIsInstance(FileField().get_prep_value(lazy_func()), str) 
Example #27
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_EmailField(self):
        lazy_func = lazy(lambda: 'mailbox@domain.com', str)
        self.assertIsInstance(EmailField().get_prep_value(lazy_func()), str) 
Example #28
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_DecimalField(self):
        lazy_func = lazy(lambda: Decimal('1.2'), Decimal)
        self.assertIsInstance(DecimalField().get_prep_value(lazy_func()), Decimal) 
Example #29
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_DateField(self):
        lazy_func = lazy(lambda: datetime.date.today(), datetime.date)
        self.assertIsInstance(DateField().get_prep_value(lazy_func()), datetime.date) 
Example #30
Source File: test_promises.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_DecimalField(self):
        lazy_func = lazy(lambda: Decimal('1.2'), Decimal)
        self.assertIsInstance(DecimalField().get_prep_value(lazy_func()), Decimal)