Python django.utils.six.text_type() Examples

The following are code examples for showing how to use django.utils.six.text_type(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don't like. You can also save this page to your account.

Example 1
Project: django-powerpages   Author: Open-E-WEB   File: test_sitemap_view.py    (MIT License) View Source Project 6 votes vote down vote up
def parse_sitemap(content):
    if not isinstance(content, six.text_type):
        content = content.decode('utf-8')
    urlset_match = re.search(
        r'<urlset[^>]*>(?P<urls>[\s\S]*)</urlset>', content
    )
    if urlset_match:
        results = []
        urlset_content = urlset_match.groupdict()['urls']
        for url_content in re.findall(r'<url>([\s\S]+)</url>', urlset_content):
            results.append(
                dict(
                    re.findall(r'<([^>]+)>([^<]*)</[^>]+>', url_content)
                )
            )
    else:
        results = None
    return results 
Example 2
Project: django-jchart   Author: matthisk   File: tests.py    (license) View Source Project 6 votes vote down vote up
def test_chart_view_get(self):
        ChartViewSubClass = type('ChartViewSubClass', (ChartView, ), {
                'chart_instance': LineChart()
            })
        chart_view = ChartViewSubClass()

        request_factory = RequestFactory()
        request = request_factory.get('/test-url')
        response = chart_view.get(request)

        self.assertEquals(response.status_code, 200)
        charset = getattr(response, 'charset', 'utf-8')
        content = response.content.decode(charset)
        data = json.loads(content)

        self.assertIn('data', data)
        self.assertIn('options', data)
        self.assertIn('type', data)

        self.assertTrue(isinstance(data['data'], dict))
        self.assertTrue(isinstance(data['options'], dict))
        self.assertTrue(isinstance(data['type'], (six.string_types, six.text_type)))

        self.assertIn(data['type'], ['bar', 'line', 'radar', 'polarArea', 'pie', 'bubble'])
        self.assertIn('title', data['options']) 
Example 3
Project: django-jchart   Author: matthisk   File: tests.py    (license) View Source Project 6 votes vote down vote up
def test_chart_view_get(self):
        ChartViewSubClass = type('ChartViewSubClass', (ChartView, ), {
                'chart_instance': LineChart()
            })
        chart_view = ChartViewSubClass()

        request_factory = RequestFactory()
        request = request_factory.get('/test-url')
        response = chart_view.get(request)

        self.assertEquals(response.status_code, 200)
        charset = getattr(response, 'charset', 'utf-8')
        content = response.content.decode(charset)
        data = json.loads(content)

        self.assertIn('data', data)
        self.assertIn('options', data)
        self.assertIn('type', data)

        self.assertTrue(isinstance(data['data'], dict))
        self.assertTrue(isinstance(data['options'], dict))
        self.assertTrue(isinstance(data['type'], (six.string_types, six.text_type)))

        self.assertIn(data['type'], ['bar', 'line', 'radar', 'polarArea', 'pie', 'bubble'])
        self.assertIn('title', data['options']) 
Example 4
Project: django-collectionfield   Author: escer   File: fields.py    (MIT License) View Source Project 6 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(CollectionField, self).deconstruct()
        if self.collection_type is not list:
            kwargs['collection_type'] = self.collection_type
        if self.item_type is not six.text_type:
            kwargs['item_type'] = self.item_type
        if self.sort:
            kwargs['sort'] = True
        if self.unique_items is not None:
            kwargs['unique_items'] = self.unique_items
        if self.max_items is not None:
            kwargs['max_items'] = self.max_items
        if self.delimiter != '|':
            kwargs['delimiter'] = self.delimiter
        if kwargs.get('max_length') == 1024:
            del kwargs['max_length']
        if kwargs.get('default') is self.collection_type:
            del kwargs['default']
        return name, path, args, kwargs 
Example 5
Project: django-collectionfield   Author: escer   File: forms.py    (MIT License) View Source Project 6 votes vote down vote up
def prepare_value(self, value):
        """Converts value to list of string for widget usage"""
        if not isinstance(value, choicelist):
            value = value or ()
            value = self.collection_type(
                self.item_type(item) for item in value
            )
            if isinstance(value, set) or not self.unique_items:
                collection = value
            else:
                collection = []
                # Remove the duplicates while keeping order:
                [
                    collection.append(item)
                    for item in value if item not in collection
                ]
            if self.sort:
                collection = sorted(collection)
            value = choicelist(
                six.text_type(item) for item in collection
            )
        return value 
Example 6
Project: ecs_sclm   Author: meaningful   File: filer_image_tags.py    (license) View Source Project 6 votes vote down vote up
def _resize(original_size, index, divisor=0, padding=0,
            keep_aspect_ratio=False):
    if isinstance(original_size, six.text_type):
        m = RE_SIZE.match(original_size)
        if m:
            original_size = (int(m.group(1)), int(m.group(2)))
        else:
            return original_size
    else:
        try:
            original_size = (int(original_size[0]), int(original_size[1]))
        except (TypeError, ValueError):
            return original_size
    try:
        padding = int(padding)
        divisor = int(divisor)
    except (TypeError, ValueError):
        return original_size
    # Re-calculate size
    new_x, new_y = _recalculate_size(original_size, index, divisor=divisor,
                                     padding=padding,
                                     keep_aspect_ratio=keep_aspect_ratio)
    return (new_x, new_y) 
Example 7
Project: CodingDojo   Author: ComputerSocietyUNB   File: urlresolvers.py    (license) View Source Project 6 votes vote down vote up
def regex(self):
        """
        Returns a compiled regular expression, depending upon the activated
        language-code.
        """
        language_code = get_language()
        if language_code not in self._regex_dict:
            if isinstance(self._regex, six.string_types):
                regex = self._regex
            else:
                regex = force_text(self._regex)
            try:
                compiled_regex = re.compile(regex, re.UNICODE)
            except re.error as e:
                raise ImproperlyConfigured(
                    '"%s" is not a valid regular expression: %s' %
                    (regex, six.text_type(e)))

            self._regex_dict[language_code] = compiled_regex
        return self._regex_dict[language_code] 
Example 8
Project: CodingDojo   Author: ComputerSocietyUNB   File: hstore.py    (license) View Source Project 6 votes vote down vote up
def to_python(self, value):
        if not value:
            return {}
        if not isinstance(value, dict):
            try:
                value = json.loads(value)
            except ValueError:
                raise ValidationError(
                    self.error_messages['invalid_json'],
                    code='invalid_json',
                )

        if not isinstance(value, dict):
            raise ValidationError(
                self.error_messages['invalid_format'],
                code='invalid_format',
            )

        # Cast everything to strings for ease.
        for key, val in value.items():
            value[key] = six.text_type(val)
        return value 
Example 9
Project: CodingDojo   Author: ComputerSocietyUNB   File: widgets.py    (license) View Source Project 6 votes vote down vote up
def url_params_from_lookup_dict(lookups):
    """
    Converts the type of lookups specified in a ForeignKey limit_choices_to
    attribute to a dictionary of query parameters
    """
    params = {}
    if lookups and hasattr(lookups, 'items'):
        items = []
        for k, v in lookups.items():
            if callable(v):
                v = v()
            if isinstance(v, (tuple, list)):
                v = ','.join(str(x) for x in v)
            elif isinstance(v, bool):
                # See django.db.fields.BooleanField.get_prep_lookup
                v = ('0', '1')[v]
            else:
                v = six.text_type(v)
            items.append((k, v))
        params.update(dict(items))
    return params 
Example 10
Project: CodingDojo   Author: ComputerSocietyUNB   File: response.py    (license) View Source Project 6 votes vote down vote up
def make_bytes(self, value):
        """Turn a value into a bytestring encoded in the output charset."""
        # Per PEP 3333, this response body must be bytes. To avoid returning
        # an instance of a subclass, this function returns `bytes(value)`.
        # This doesn't make a copy when `value` already contains bytes.

        # Handle string types -- we can't rely on force_bytes here because:
        # - under Python 3 it attempts str conversion first
        # - when self._charset != 'utf-8' it re-encodes the content
        if isinstance(value, bytes):
            return bytes(value)
        if isinstance(value, six.text_type):
            return bytes(value.encode(self.charset))

        # Handle non-string types (#16494)
        return force_bytes(value, self.charset)

    # These methods partially implement the file-like object interface.
    # See https://docs.python.org/3/library/io.html#io.IOBase

    # The WSGI server must call this method upon completion of the request.
    # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html 
Example 11
Project: CodingDojo   Author: ComputerSocietyUNB   File: schema.py    (license) View Source Project 6 votes vote down vote up
def effective_default(self, field):
        """
        Returns a field's effective database default value
        """
        if field.has_default():
            default = field.get_default()
        elif not field.null and field.blank and field.empty_strings_allowed:
            if field.get_internal_type() == "BinaryField":
                default = six.binary_type()
            else:
                default = six.text_type()
        else:
            default = None
        # If it's a callable, call it
        if six.callable(default):
            default = default()
        # Run it through the field's get_db_prep_save method so we can send it
        # to the database.
        default = field.get_db_prep_save(default, self.connection)
        return default 
Example 12
Project: CodingDojo   Author: ComputerSocietyUNB   File: base.py    (license) View Source Project 6 votes vote down vote up
def date_error_message(self, lookup_type, field_name, unique_for):
        opts = self._meta
        field = opts.get_field(field_name)
        return ValidationError(
            message=field.error_messages['unique_for_date'],
            code='unique_for_date',
            params={
                'model': self,
                'model_name': six.text_type(capfirst(opts.verbose_name)),
                'lookup_type': lookup_type,
                'field': field_name,
                'field_label': six.text_type(capfirst(field.verbose_name)),
                'date_field': unique_for,
                'date_field_label': six.text_type(capfirst(opts.get_field(unique_for).verbose_name)),
            }
        ) 
Example 13
Project: CodingDojo   Author: ComputerSocietyUNB   File: testcases.py    (license) View Source Project 6 votes vote down vote up
def assertHTMLEqual(self, html1, html2, msg=None):
        """
        Asserts that two HTML snippets are semantically the same.
        Whitespace in most cases is ignored, and attribute ordering is not
        significant. The passed-in arguments must be valid HTML.
        """
        dom1 = assert_and_parse_html(self, html1, msg,
            'First argument is not valid HTML:')
        dom2 = assert_and_parse_html(self, html2, msg,
            'Second argument is not valid HTML:')

        if dom1 != dom2:
            standardMsg = '%s != %s' % (
                safe_repr(dom1, True), safe_repr(dom2, True))
            diff = ('\n' + '\n'.join(difflib.ndiff(
                           six.text_type(dom1).splitlines(),
                           six.text_type(dom2).splitlines())))
            standardMsg = self._truncateMessage(standardMsg, diff)
            self.fail(self._formatMessage(msg, standardMsg)) 
Example 14
Project: CodingDojo   Author: ComputerSocietyUNB   File: testcases.py    (license) View Source Project 6 votes vote down vote up
def assertXMLEqual(self, xml1, xml2, msg=None):
        """
        Asserts that two XML snippets are semantically the same.
        Whitespace in most cases is ignored, and attribute ordering is not
        significant. The passed-in arguments must be valid XML.
        """
        try:
            result = compare_xml(xml1, xml2)
        except Exception as e:
            standardMsg = 'First or second argument is not valid XML\n%s' % e
            self.fail(self._formatMessage(msg, standardMsg))
        else:
            if not result:
                standardMsg = '%s != %s' % (safe_repr(xml1, True), safe_repr(xml2, True))
                diff = ('\n' + '\n'.join(
                    difflib.ndiff(
                        six.text_type(xml1).splitlines(),
                        six.text_type(xml2).splitlines(),
                    )
                ))
                standardMsg = self._truncateMessage(standardMsg, diff)
                self.fail(self._formatMessage(msg, standardMsg)) 
Example 15
Project: CodingDojo   Author: ComputerSocietyUNB   File: formats.py    (license) View Source Project 6 votes vote down vote up
def localize(value, use_l10n=None):
    """
    Checks if value is a localizable type (date, number...) and returns it
    formatted as a string using current locale format.

    If use_l10n is provided and is not None, that will force the value to
    be localized (or not), overriding the value of settings.USE_L10N.
    """
    if isinstance(value, bool):
        return mark_safe(six.text_type(value))
    elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
        return number_format(value, use_l10n=use_l10n)
    elif isinstance(value, datetime.datetime):
        return date_format(value, 'DATETIME_FORMAT', use_l10n=use_l10n)
    elif isinstance(value, datetime.date):
        return date_format(value, use_l10n=use_l10n)
    elif isinstance(value, datetime.time):
        return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n)
    else:
        return value 
Example 16
Project: CodingDojo   Author: ComputerSocietyUNB   File: dateformat.py    (license) View Source Project 6 votes vote down vote up
def T(self):
        """
        Time zone of this machine; e.g. 'EST' or 'MDT'.

        If timezone information is not available, this method returns
        an empty string.
        """
        if not self.timezone:
            return ""

        name = None
        try:
            name = self.timezone.tzname(self.data)
        except Exception:
            # pytz raises AmbiguousTimeError during the autumn DST change.
            # This happens mainly when __init__ receives a naive datetime
            # and sets self.timezone = get_default_timezone().
            pass
        if name is None:
            name = self.format('O')
        return six.text_type(name) 
Example 17
Project: NarshaTech   Author: KimJangHyeon   File: widgets.py    (license) View Source Project 6 votes vote down vote up
def url_params_from_lookup_dict(lookups):
    """
    Converts the type of lookups specified in a ForeignKey limit_choices_to
    attribute to a dictionary of query parameters
    """
    params = {}
    if lookups and hasattr(lookups, 'items'):
        items = []
        for k, v in lookups.items():
            if callable(v):
                v = v()
            if isinstance(v, (tuple, list)):
                v = ','.join(str(x) for x in v)
            elif isinstance(v, bool):
                v = ('0', '1')[v]
            else:
                v = six.text_type(v)
            items.append((k, v))
        params.update(dict(items))
    return params 
Example 18
Project: planet-b-saleor   Author: planet-b   File: base.py    (license) View Source Project 6 votes vote down vote up
def _process_filter(self, field_attname, lookup, value):
        # Get the field
        field = self._get_filterable_field(field_attname)

        if field is None:
            raise FieldError(
                'Cannot filter search results with field "' + field_attname + '". Please add index.FilterField(\'' +
                field_attname + '\') to ' + self.queryset.model.__name__ + '.search_fields.'
            )

        # Process the lookup
        result = self._process_lookup(field, lookup, value)

        if result is None:
            raise FilterError(
                'Could not apply filter on search results: "' + field_attname + '__' +
                lookup + ' = ' + text_type(value) + '". Lookup "' + lookup + '"" not recognised.'
            )

        return result 
Example 19
Project: django-oscar-wagtail   Author: LabD   File: views.py    (license) View Source Project 6 votes vote down vote up
def product_chosen(request, pk):
    product = get_object_or_404(Product, pk=pk)

    product_json = json.dumps({
        'id': product.pk,
        'string': text_type(product),
        'edit_link': reverse(
            'dashboard:catalogue-product', kwargs={'pk': product.pk})
    })

    return render_modal_workflow(
        request,
        None, 'oscar_wagtail/chooser/product_chosen.js',
        {
            'product_json': product_json,
        }
    ) 
Example 20
Project: Scrum   Author: prakharchoudhary   File: jsonb.py    (license) View Source Project 6 votes vote down vote up
def to_python(self, value):
        if self.disabled:
            return value
        if value in self.empty_values:
            return None
        elif isinstance(value, (list, dict, int, float, JSONString)):
            return value
        try:
            converted = json.loads(value)
        except ValueError:
            raise forms.ValidationError(
                self.error_messages['invalid'],
                code='invalid',
                params={'value': value},
            )
        if isinstance(converted, six.text_type):
            return JSONString(converted)
        else:
            return converted 
Example 21
Project: Scrum   Author: prakharchoudhary   File: widgets.py    (license) View Source Project 6 votes vote down vote up
def url_params_from_lookup_dict(lookups):
    """
    Converts the type of lookups specified in a ForeignKey limit_choices_to
    attribute to a dictionary of query parameters
    """
    params = {}
    if lookups and hasattr(lookups, 'items'):
        items = []
        for k, v in lookups.items():
            if callable(v):
                v = v()
            if isinstance(v, (tuple, list)):
                v = ','.join(str(x) for x in v)
            elif isinstance(v, bool):
                v = ('0', '1')[v]
            else:
                v = six.text_type(v)
            items.append((k, v))
        params.update(dict(items))
    return params 
Example 22
Project: django   Author: alexsukhrin   File: jsonb.py    (license) View Source Project 6 votes vote down vote up
def to_python(self, value):
        if self.disabled:
            return value
        if value in self.empty_values:
            return None
        elif isinstance(value, (list, dict, int, float, JSONString)):
            return value
        try:
            converted = json.loads(value)
        except ValueError:
            raise forms.ValidationError(
                self.error_messages['invalid'],
                code='invalid',
                params={'value': value},
            )
        if isinstance(converted, six.text_type):
            return JSONString(converted)
        else:
            return converted 
Example 23
Project: habilitacion   Author: GabrielBD   File: utils.py    (license) View Source Project 6 votes vote down vote up
def http_quote(string):
    """
    Given a unicode string, will do its dandiest to give you back a
    valid ascii charset string you can use in, say, http headers and the
    like.
    """
    if isinstance(string, six.text_type):
        try:
            import unidecode
        except ImportError:
            pass
        else:
            string = unidecode.unidecode(string)
        string = string.encode('ascii', 'replace')
    # Wrap in double-quotes for ; , and the like
    string = string.replace(b'\\', b'\\\\').replace(b'"', b'\\"')
    return '"{0!s}"'.format(string.decode()) 
Example 24
Project: drf-batch-requests   Author: roman-karpovich   File: request.py    (license) View Source Project 6 votes vote down vote up
def _process_attr(self, attr):
        params = re.findall(
            r'({result=(?P<name>\w+):\$\.(?P<value>[a-zA-Z0-9.*]+)})', attr
        )
        if not params:
            return attr

        for url_param in params:
            if url_param[1] not in self.named_responses:
                raise ValidationError('Named request {} is missing'.format(url_param[1]))

            result = get_attribute(
                self.named_responses[url_param[1]]['_data'],
                url_param[2].split('.')
            )
            if isinstance(result, list):
                result = ','.join(map(six.text_type, result))

            if attr == url_param[0]:
                attr = result
            else:
                attr = attr.replace(url_param[0], str(result))

        return attr 
Example 25
Project: sdining   Author: Lurance   File: fields.py    (license) View Source Project 6 votes vote down vote up
def __init__(self, choices, **kwargs):
        self.grouped_choices = to_choices_dict(choices)
        self.choices = flatten_choices_dict(self.grouped_choices)
        self.html_cutoff = kwargs.pop('html_cutoff', self.html_cutoff)
        self.html_cutoff_text = kwargs.pop('html_cutoff_text', self.html_cutoff_text)

        # Map the string representation of choices to the underlying value.
        # Allows us to deal with eg. integer choices while supporting either
        # integer or string input, but still get the correct datatype out.
        self.choice_strings_to_values = {
            six.text_type(key): key for key in self.choices.keys()
        }

        self.allow_blank = kwargs.pop('allow_blank', False)

        super(ChoiceField, self).__init__(**kwargs) 
Example 26
Project: sdining   Author: Lurance   File: renderers.py    (license) View Source Project 6 votes vote down vote up
def render_field(self, field, parent_style):
        if isinstance(field._field, serializers.HiddenField):
            return ''

        style = dict(self.default_style[field])
        style.update(field.style)
        if 'template_pack' not in style:
            style['template_pack'] = parent_style.get('template_pack', self.template_pack)
        style['renderer'] = self

        # Get a clone of the field with text-only value representation.
        field = field.as_form_field()

        if style.get('input_type') == 'datetime-local' and isinstance(field.value, six.text_type):
            field.value = field.value.rstrip('Z')

        if 'template' in style:
            template_name = style['template']
        else:
            template_name = style['template_pack'].strip('/') + '/' + style['base_template']

        template = loader.get_template(template_name)
        context = {'field': field, 'style': style}
        return template_render(template, context) 
Example 27
Project: sdining   Author: Lurance   File: parsers.py    (license) View Source Project 6 votes vote down vote up
def parse(self, stream, media_type=None, parser_context=None):
        """
        Parses the incoming bytestream as a multipart encoded form,
        and returns a DataAndFiles object.

        `.data` will be a `QueryDict` containing all the form parameters.
        `.files` will be a `QueryDict` containing all the form files.
        """
        parser_context = parser_context or {}
        request = parser_context['request']
        encoding = parser_context.get('encoding', settings.DEFAULT_CHARSET)
        meta = request.META.copy()
        meta['CONTENT_TYPE'] = media_type
        upload_handlers = request.upload_handlers

        try:
            parser = DjangoMultiPartParser(meta, stream, upload_handlers, encoding)
            data, files = parser.parse()
            return DataAndFiles(data, files)
        except MultiPartParserError as exc:
            raise ParseError('Multipart form parse error - %s' % six.text_type(exc)) 
Example 28
Project: django-electron-pdf   Author: namespace-ee   File: utils.py    (license) View Source Project 6 votes vote down vote up
def http_quote(string):
    """
    Given a unicode string, will do its dandiest to give you back a
    valid ascii charset string you can use in, say, http headers and the
    like.
    """
    if isinstance(string, six.text_type):
        try:
            import unidecode
        except ImportError:
            pass
        else:
            string = unidecode.unidecode(string)
        string = string.encode('ascii', 'replace')
    # Wrap in double-quotes for ; , and the like
    string = string.replace(b'\\', b'\\\\').replace(b'"', b'\\"')
    return '"{0!s}"'.format(string.decode()) 
Example 29
Project: django-echarts   Author: kinegratii   File: echarts.py    (license) View Source Project 6 votes vote down vote up
def echarts_js_dependencies(context, *args):
    dependencies = []

    def _add(_x):
        if _x not in dependencies:
            dependencies.append(_x)

    for a in args:
        if hasattr(a, 'js_dependencies'):
            for d in a.js_dependencies:
                _add(d)
        elif isinstance(a, six.text_type):
            _add(a)
    if len(dependencies) > 1:
        dependencies.remove('echarts')
        dependencies = ['echarts'] + list(dependencies)
    links = map(DJANGO_ECHARTS_SETTINGS.host_store.generate_js_link, dependencies)

    return template.Template(
        '<br/>'.join(['<script src="{link}"></script>'.format(link=l) for l in links])
    ).render(context) 
Example 30
Project: my-first-blog   Author: AnkurBegining   File: tokens.py    (license) View Source Project 5 votes vote down vote up
def _make_hash_value(self, user, timestamp):
        return (
            six.text_type(user.pk) + six.text_type(timestamp) +
            six.text_type(user.profile.email_confirmed)
        ) 
Example 31
Project: django-powerpages   Author: Open-E-WEB   File: test_template_tags.py    (MIT License) View Source Project 5 votes vote down vote up
def test_current_page_info_edit_mode_enabled(self):
        Page.objects.create(
            url='/', alias='home', title="Home Page",
            template='{% current_page_info %}'
        )
        session = self.client.session
        session['WEBSITE_EDIT_MODE'] = True
        session.save()
        response = self.client.get('/')
        content = response.content
        if not isinstance(content, six.text_type):
            content = content.decode('utf-8')
        self.assertEqual(
            content,
            '''
<link rel="stylesheet" href="/static/powerpages/css/current_page_info.css">
<div class="current-page-info">
<h5>Page Information:</h5>
<ul>
<li><span>Name: </span><strong>Home Page</strong></li>
<li><span>Alias: </span><strong>home</strong></li>
<li><span>ID: </span><strong>1</strong></li>
<li><a href="/admin/powerpages/page/1/change/">edit in Admin &raquo;</a></li>
<li>
<a href="/powerpages-admin/switch-edit-mode/">disable Edit Mode &raquo;</a>
</li>
</ul>
</div>
            '''.strip().replace('\n', '')
        ) 
Example 32
Project: django-powerpages   Author: Open-E-WEB   File: test_template_tags.py    (MIT License) View Source Project 5 votes vote down vote up
def test_current_page_info_edit_mode_disabled(self):
        Page.objects.create(
            url='/', alias='home', title="Home Page",
            template='{% current_page_info %}'
        )
        # NO EDIT MODE ENABLED
        response = self.client.get('/')
        content = response.content
        if not isinstance(content, six.text_type):
            content = content.decode('utf-8')
        self.assertEqual(content, '') 
Example 33
Project: django-powerpages   Author: Open-E-WEB   File: test_models.py    (MIT License) View Source Project 5 votes vote down vote up
def test_unicode(self):
        page = Page.objects.create(
            url='/test/',
        )
        self.assertEqual(six.text_type(page), '/test/')

    # def get_absolute_url(self): 
Example 34
Project: django-powerpages   Author: Open-E-WEB   File: cachekeys.py    (MIT License) View Source Project 5 votes vote down vote up
def get_cache_name(prefix, name):
    """
    Cache name constructor. Uses the same methods as django cache system
    Examples:
    *) prefix=profile.cache, name=<requestuser.id>
    *) prefix=template.cache.sidebar, name=<requestuser.id>
    """
    return '{0}.{1}'.format(
        prefix, hashlib.md5(six.text_type(name).encode('utf-8')).hexdigest()
    ) 
Example 35
Project: django-tmpl   Author: jarrekk   File: tokens.py    (license) View Source Project 5 votes vote down vote up
def _make_hash_value(self, user, timestamp):
        return (six.text_type(user.pk) + six.text_type(timestamp)) + six.text_type(user.is_active) 
Example 36
Project: django-jchart   Author: matthisk   File: tests.py    (license) View Source Project 5 votes vote down vote up
def test_chart_config(self):
        for response in self.responses:
            charset = getattr(response, 'charset', 'utf-8')
            content = response.content.decode(charset)
            data = json.loads(content)
            self.assertIn('data', data)
            self.assertIn('options', data)
            self.assertIn('type', data)

            self.assertTrue(isinstance(data['data'], dict))
            self.assertTrue(isinstance(data['options'], dict))
            self.assertTrue(isinstance(data['type'], (six.string_types, six.text_type)))

            self.assertIn(data['type'], ['bar', 'line', 'radar', 'polarArea', 'pie', 'bubble'])
            self.assertIn('title', data['options']) 
Example 37
Project: django-jchart   Author: matthisk   File: mixins.py    (license) View Source Project 5 votes vote down vote up
def get_content_type(self):
        if (self.content_type is not None and
            not isinstance(self.content_type,
                           (six.string_types, six.text_type))):
            raise ImproperlyConfigured(
                '{0} is missing content type. Define {0}.content_type, '
                'or override {0}.get_content_type().'.format(
                    self.__class__.__name__))
        return self.content_type or "application/json" 
Example 38
Project: django-jchart   Author: matthisk   File: tests.py    (license) View Source Project 5 votes vote down vote up
def test_chart_config(self):
        for response in self.responses:
            charset = getattr(response, 'charset', 'utf-8')
            content = response.content.decode(charset)
            data = json.loads(content)
            self.assertIn('data', data)
            self.assertIn('options', data)
            self.assertIn('type', data)

            self.assertTrue(isinstance(data['data'], dict))
            self.assertTrue(isinstance(data['options'], dict))
            self.assertTrue(isinstance(data['type'], (six.string_types, six.text_type)))

            self.assertIn(data['type'], ['bar', 'line', 'radar', 'polarArea', 'pie', 'bubble'])
            self.assertIn('title', data['options']) 
Example 39
Project: django-csv-export-view   Author: benkonrath   File: views.py    (BSD 3-Clause "New" or "Revised" License) View Source Project 5 votes vote down vote up
def get_field_value(self, obj, field_name):
        """ Override if a custom value or behaviour is required for specific fields. """
        if '__' not in field_name:
            if hasattr(obj, 'all') and hasattr(obj, 'iterator'):
                return ','.join([getattr(ro, field_name) for ro in obj.all()])

            try:
                field = obj._meta.get_field(field_name)
            except FieldDoesNotExist as e:
                if not hasattr(obj, field_name):
                    raise e
                # field_name is a property.
                return getattr(obj, field_name)

            value = field.value_from_object(obj)
            if field.many_to_many:
                return ','.join([six.text_type(ro) for ro in value])
            elif field.choices:
                if value is None or six.text_type(value).strip() == '':
                    return ''
                return dict(field.choices)[value]
            return field.value_from_object(obj)
        else:
            related_field_names = field_name.split('__')
            related_obj = getattr(obj, related_field_names[0])
            related_field_name = '__'.join(related_field_names[1:])
            return self.get_field_value(related_obj, related_field_name) 
Example 40
Project: django-in-request-cache   Author: mojeto   File: test_cache_a_cache.py    (MIT License) View Source Project 5 votes vote down vote up
def test_wrong_cache_property():
    cache = get_cache(fast_cache='wrong_cache', cache_to_cache='bad_cache')
    with pytest.raises(InvalidCacheBackendError) as exc_info:
        cache_to_cache = cache.cache
    assert 'bad_cache' in text_type(exc_info.value)

    with pytest.raises(InvalidCacheBackendError) as exc_info:
        fast_cache = cache.fast_cache
    assert 'wrong_cache' in text_type(exc_info.value) 
Example 41
Project: django-collectionfield   Author: escer   File: fields.py    (MIT License) View Source Project 5 votes vote down vote up
def __init__(self, verbose_name=None, name=None, collection_type=list,
                 item_type=six.text_type, sort=False, unique_items=None,
                 max_items=None, delimiter='|', **kwargs):
        """
        :param collection_type: type (class) of this collection
        :param item_type: type (class) of this collection's items
        :param sort: should items be sorted automatically?
        :param unique_items: should duplicate items be dropped?
        :param max_items: maximum number of items (enforced on validation)
        :param delimiter: separates items in string representation stored in db
        :param max_length: maximum length string representation
                           of this collection
        """
        self.collection_type = collection_type
        self.item_type = item_type
        self.sort = sort
        self.unique_items = unique_items
        self.max_items = max_items
        self.delimiter = delimiter
        kwargs['max_length'] = kwargs.get('max_length', 1024)
        kwargs['default'] = kwargs.get('default', self.collection_type)
        empty_collection = collection_type()
        if empty_collection not in self.empty_values:
            self.empty_values = self.empty_values[:] + [empty_collection]
        super(CollectionField, self).__init__(
            verbose_name=verbose_name, name=name, **kwargs
        )
        if self.max_items:
            self.validators.append(MaxItemsValidator(self.max_items))
        self.validators.append(
            ConvertedMaxLengthValidator(
                limit_value=self.max_length,
                collection_type=self.collection_type,
                item_type=self.item_type,
                sort=self.sort,
                unique_items=self._has_unique_items(),
                delimiter=self.delimiter
            )
        ) 
Example 42
Project: django-collectionfield   Author: escer   File: forms.py    (MIT License) View Source Project 5 votes vote down vote up
def __init__(self, collection_type=list,
                 item_type=six.text_type, sort=False, unique_items=False,
                 max_items=None, max_length=None, delimiter='|',
                 *args, **kwargs):
        self.collection_type = collection_type
        self.item_type = item_type
        self.sort = sort
        self.unique_items = unique_items
        self.max_items = max_items
        self.max_length = max_length
        self.delimiter = delimiter
        super(CollectionFieldMixin, self).__init__(*args, **kwargs)
        empty_collection = collection_type()
        if empty_collection not in self.empty_values:
            self.empty_values = self.empty_values[:] + [empty_collection]
        if max_items:
            self.validators.append(MaxItemsValidator(self.max_items))
        if max_length:
            self.validators.append(
                ConvertedMaxLengthValidator(
                    limit_value=self.max_length,
                    collection_type=self.collection_type,
                    item_type=self.item_type,
                    sort=self.sort,
                    unique_items=self.unique_items,
                    delimiter=self.delimiter
                )
            ) 
Example 43
Project: ecs_sclm   Author: meaningful   File: compatibility.py    (license) View Source Project 5 votes vote down vote up
def upath(path):
    """
    Always return a unicode path.
    """
    if six.PY2 and not isinstance(path, six.text_type):
        return path.decode(fs_encoding)
    return path


# copied from django-cms (for compatibility with Django 1.4) 
Example 44
Project: ecs_sclm   Author: meaningful   File: multistorage_file.py    (license) View Source Project 5 votes vote down vote up
def to_python(self, value):
        if isinstance(value, list) and len(value) == 2 and isinstance(value[0], six.text_type):
            filename, payload = value
            try:
                payload = base64.b64decode(payload)
            except TypeError:
                pass
            else:
                if self.storage.exists(filename):
                    self.storage.delete(filename)
                self.storage.save(filename, ContentFile(payload))
                return filename
        return value 
Example 45
Project: CodingDojo   Author: ComputerSocietyUNB   File: fields.py    (license) View Source Project 5 votes vote down vote up
def to_python(self, value):
        # Try to coerce the value to unicode.
        unicode_value = force_text(value, strings_only=True)
        if isinstance(unicode_value, six.text_type):
            value = unicode_value.strip()
        # If unicode, try to strptime against each input format.
        if isinstance(value, six.text_type):
            for format in self.input_formats:
                try:
                    return self.strptime(value, format)
                except (ValueError, TypeError):
                    continue
        raise ValidationError(self.error_messages['invalid'], code='invalid') 
Example 46
Project: CodingDojo   Author: ComputerSocietyUNB   File: formsets.py    (license) View Source Project 5 votes vote down vote up
def as_table(self):
        "Returns this formset rendered as HTML <tr>s -- excluding the <table></table>."
        # XXX: there is no semantic division between forms here, there
        # probably should be. It might make sense to render each form as a
        # table row with each field as a td.
        forms = ' '.join(form.as_table() for form in self)
        return mark_safe('\n'.join([six.text_type(self.management_form), forms])) 
Example 47
Project: CodingDojo   Author: ComputerSocietyUNB   File: formsets.py    (license) View Source Project 5 votes vote down vote up
def as_p(self):
        "Returns this formset rendered as HTML <p>s."
        forms = ' '.join(form.as_p() for form in self)
        return mark_safe('\n'.join([six.text_type(self.management_form), forms])) 
Example 48
Project: CodingDojo   Author: ComputerSocietyUNB   File: formsets.py    (license) View Source Project 5 votes vote down vote up
def as_ul(self):
        "Returns this formset rendered as HTML <li>s."
        forms = ' '.join(form.as_ul() for form in self)
        return mark_safe('\n'.join([six.text_type(self.management_form), forms])) 
Example 49
Project: CodingDojo   Author: ComputerSocietyUNB   File: models.py    (license) View Source Project 5 votes vote down vote up
def get_unique_error_message(self, unique_check):
        if len(unique_check) == 1:
            return ugettext("Please correct the duplicate data for %(field)s.") % {
                "field": unique_check[0],
            }
        else:
            return ugettext("Please correct the duplicate data for %(field)s, "
                "which must be unique.") % {
                "field": get_text_list(unique_check, six.text_type(_("and"))),
            } 
Example 50
Project: CodingDojo   Author: ComputerSocietyUNB   File: models.py    (license) View Source Project 5 votes vote down vote up
def prepare_value(self, value):
        if (hasattr(value, '__iter__') and
                not isinstance(value, six.text_type) and
                not hasattr(value, '_meta')):
            return [super(ModelMultipleChoiceField, self).prepare_value(v) for v in value]
        return super(ModelMultipleChoiceField, self).prepare_value(value)