Python django.utils.encoding.smart_text() Examples
The following are 30
code examples of django.utils.encoding.smart_text().
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.encoding
, or try the search function
.

Example #1
Source File: utils.py From GTDWeb with GNU General Public License v2.0 | 7 votes |
def display_for_field(value, field): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return _boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(timezone.template_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field, models.FileField) and value: return format_html('<a href="{}">{}</a>', value.url, value) else: return smart_text(value)
Example #2
Source File: backends.py From django-oidc-rp with MIT License | 6 votes |
def get_or_create_user(username, email): username = smart_text(username) users = get_user_model().objects.filter(email=email) if len(users) == 0: user = get_user_model().objects.create_user(username, email=email) elif len(users) == 1: return users[0] else: # duplicate handling current_user = None for u in users: current_user = u if hasattr(u, 'oidc_user'): return u return current_user return user
Example #3
Source File: filters.py From StormOnline with Apache License 2.0 | 6 votes |
def choices(self): yield { 'selected': self.lookup_exact_val == '' and not self.lookup_isnull_val, 'query_string': self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]), 'display': _('All'), } for pk_val, val in self.lookup_choices: yield { 'selected': self.lookup_exact_val == smart_text(pk_val), 'query_string': self.query_string({ self.lookup_exact_name: pk_val, }, [self.lookup_isnull_name]), 'display': val, } if (is_related_field(self.field) and self.field.field.null or hasattr(self.field, 'rel') and self.field.null): yield { 'selected': bool(self.lookup_isnull_val), 'query_string': self.query_string({ self.lookup_isnull_name: 'True', }, [self.lookup_exact_name]), 'display': EMPTY_CHANGELIST_VALUE, }
Example #4
Source File: filters.py From StormOnline with Apache License 2.0 | 6 votes |
def choices(self): yield { 'selected': (self.lookup_exact_val is '' and self.lookup_isnull_val is ''), 'query_string': self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]), 'display': _('All'), } include_none = False for val in self.lookup_choices: if val is None: include_none = True continue val = smart_text(val) yield { 'selected': self.lookup_exact_val == val, 'query_string': self.query_string({self.lookup_exact_name: val}, [self.lookup_isnull_name]), 'display': val, } if include_none: yield { 'selected': bool(self.lookup_isnull_val), 'query_string': self.query_string({self.lookup_isnull_name: 'True'}, [self.lookup_exact_name]), 'display': EMPTY_CHANGELIST_VALUE, }
Example #5
Source File: xversion.py From StormOnline with Apache License 2.0 | 6 votes |
def get_related_versions(self, obj, version, formset): """Retreives all the related Version objects for the given FormSet.""" object_id = obj.pk # Get the fk name. try: fk_name = formset.fk.name + '_' + formset.fk.rel.get_related_field().name except AttributeError: # This is a GenericInlineFormset, or similar. fk_name = formset.ct_fk_field.name # Look up the revision data. revision_versions = version.revision.version_set.all() related_versions = dict([(related_version.object_id, related_version) for related_version in revision_versions if ContentType.objects.get_for_id(related_version.content_type_id).model_class() == formset.model and smart_text(related_version.field_dict[fk_name]) == smart_text(object_id)]) return related_versions
Example #6
Source File: editable.py From StormOnline with Apache License 2.0 | 6 votes |
def _get_new_field_html(self, field_name): try: f, attr, value = lookup_field(field_name, self.org_obj, self) except (AttributeError, ObjectDoesNotExist): return EMPTY_CHANGELIST_VALUE else: allow_tags = False if f is None: allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: allow_tags = True text = boolean_icon(value) else: text = smart_text(value) else: if isinstance(f.rel, models.ManyToOneRel): field_val = getattr(self.org_obj, f.name) if field_val is None: text = EMPTY_CHANGELIST_VALUE else: text = field_val else: text = display_for_field(value, f) return mark_safe(text) if allow_tags else conditional_escape(text)
Example #7
Source File: i18n.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def get_formats(): """ Returns all formats strings required for i18n to work """ FORMAT_SETTINGS = ( 'DATE_FORMAT', 'DATETIME_FORMAT', 'TIME_FORMAT', 'YEAR_MONTH_FORMAT', 'MONTH_DAY_FORMAT', 'SHORT_DATE_FORMAT', 'SHORT_DATETIME_FORMAT', 'FIRST_DAY_OF_WEEK', 'DECIMAL_SEPARATOR', 'THOUSAND_SEPARATOR', 'NUMBER_GROUPING', 'DATE_INPUT_FORMATS', 'TIME_INPUT_FORMATS', 'DATETIME_INPUT_FORMATS' ) result = {} for module in [settings] + get_format_modules(reverse=True): for attr in FORMAT_SETTINGS: result[attr] = get_format(attr) formats = {} for k, v in result.items(): if isinstance(v, (six.string_types, int)): formats[k] = smart_text(v) elif isinstance(v, (tuple, list)): formats[k] = [smart_text(value) for value in v] return formats
Example #8
Source File: fields.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def to_python(self, value): """ Validates that the input is a decimal number. Returns a Decimal instance. Returns None for empty values. Ensures that there are no more than max_digits in the number, and no more than decimal_places digits after the decimal point. """ if value in self.empty_values: return None if self.localize: value = formats.sanitize_separators(value) value = smart_text(value).strip() try: value = Decimal(value) except DecimalException: raise ValidationError(self.error_messages['invalid'], code='invalid') return value
Example #9
Source File: related.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_to_currently_related=False): """ Returns choices with a default blank choices included, for use as SelectField choices for this field. Analog of django.db.models.fields.Field.get_choices(), provided initially for utilization by RelatedFieldListFilter. """ first_choice = blank_choice if include_blank else [] queryset = self.related_model._default_manager.all() if limit_to_currently_related: queryset = queryset.complex_filter( {'%s__isnull' % self.related_model._meta.model_name: False} ) lst = [(x._get_pk_val(), smart_text(x)) for x in queryset] return first_choice + lst
Example #10
Source File: helpers.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def contents(self): from django.contrib.admin.templatetags.admin_list import _boolean_icon from django.contrib.admin.views.main import EMPTY_CHANGELIST_VALUE field, obj, model_admin = self.field['field'], self.form.instance, self.model_admin try: f, attr, value = lookup_field(field, obj, model_admin) except (AttributeError, ValueError, ObjectDoesNotExist): result_repr = EMPTY_CHANGELIST_VALUE else: if f is None: boolean = getattr(attr, "boolean", False) if boolean: result_repr = _boolean_icon(value) else: result_repr = smart_text(value) if getattr(attr, "allow_tags", False): result_repr = mark_safe(result_repr) else: result_repr = linebreaksbr(result_repr) else: if isinstance(f.rel, ManyToManyRel) and value is not None: result_repr = ", ".join(map(six.text_type, value.all())) else: result_repr = display_for_field(value, f) return conditional_escape(result_repr)
Example #11
Source File: xml_serializer.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def handle_fk_field(self, obj, field): """ Called to handle a ForeignKey (we need to treat them slightly differently from regular fields). """ self._start_relational_field(field) related_att = getattr(obj, field.get_attname()) if related_att is not None: if self.use_natural_foreign_keys and hasattr(field.rel.to, 'natural_key'): related = getattr(obj, field.name) # If related object has a natural key, use it related = related.natural_key() # Iterable natural keys are rolled out as subelements for key_value in related: self.xml.startElement("natural", {}) self.xml.characters(smart_text(key_value)) self.xml.endElement("natural") else: self.xml.characters(smart_text(related_att)) else: self.xml.addQuickElement("None") self.xml.endElement("field")
Example #12
Source File: models.py From django-actions-logger with MIT License | 6 votes |
def changes_str(self, colon=': ', arrow=smart_text(' \u2192 '), separator='; '): """ Return the changes recorded in this log entry as a string. The formatting of the string can be customized by setting alternate values for colon, arrow and separator. If the formatting is still not satisfying, please use :py:func:`LogAction.changes_dict` and format the string yourself. :param colon: The string to place between the field name and the values. :param arrow: The string to place between each old and new value. :param separator: The string to place between each field. :return: A readable string of the changes in this log entry. """ substrings = [] for field, values in iteritems(self.changes_dict): substring = smart_text('{field_name:s}{colon:s}{old:s}{arrow:s}{new:s}').format( field_name=field, colon=colon, old=values[0], arrow=arrow, new=values[1], ) substrings.append(substring) return separator.join(substrings)
Example #13
Source File: renderers.py From kobo-predict with BSD 2-Clause "Simplified" License | 6 votes |
def _to_xml(self, xml, data): if isinstance(data, (list, tuple)): for item in data: xml.startElement(self.element_node, {}) self._to_xml(xml, item) xml.endElement(self.element_node) elif isinstance(data, dict): for key, value in six.iteritems(data): xml.startElement(key, {}) self._to_xml(xml, value) xml.endElement(key) elif data is None: # Don't output any value pass else: xml.characters(smart_text(data))
Example #14
Source File: models.py From BikeMaps with MIT License | 6 votes |
def pre_save(self, instance, add): default = super(AutoSlugField, self).pre_save(instance, add) if default or not add or not self.populate_from: return default value = getattr(instance, self.populate_from) if value is None: return default slug = slugify(smart_text(value))[:self.max_length].strip('-') # Update the model’s attribute setattr(instance, self.attname, slug) return slug # def deconstruct(self): # TODO: django 1.7 requires this
Example #15
Source File: auth.py From mozilla-django-oidc with Mozilla Public License 2.0 | 6 votes |
def default_username_algo(email): """Generate username for the Django user. :arg str/unicode email: the email address to use to generate a username :returns: str/unicode """ # bluntly stolen from django-browserid # store the username as a base64 encoded sha224 of the email address # this protects against data leakage because usernames are often # treated as public identifiers (so we can't use the email address). username = base64.urlsafe_b64encode( hashlib.sha1(force_bytes(email)).digest() ).rstrip(b'=') return smart_text(username)
Example #16
Source File: test_auth.py From mozilla-django-oidc with Mozilla Public License 2.0 | 6 votes |
def test_disallowed_unsecured_valid_token(self): """Test payload data from valid secure token (unsecured disallowed).""" header = force_bytes(json.dumps({'alg': 'HS256', 'typ': 'JWT'})) payload = force_bytes(json.dumps({'foo': 'bar'})) # Compute signature key = b'mysupersecuretestkey' h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend()) msg = '{}.{}'.format(smart_text(b64encode(header)), smart_text(b64encode(payload))) h.update(force_bytes(msg)) signature = b64encode(h.finalize()) token = '{}.{}.{}'.format( smart_text(b64encode(header)), smart_text(b64encode(payload)), smart_text(signature) ) token_bytes = force_bytes(token) key_text = smart_text(key) output = self.backend.get_payload_data(token_bytes, key_text) self.assertEqual(output, payload)
Example #17
Source File: test_auth.py From mozilla-django-oidc with Mozilla Public License 2.0 | 6 votes |
def test_allowed_unsecured_invalid_token(self): """Test payload data from invalid secure token (unsecured allowed).""" header = force_bytes(json.dumps({'alg': 'HS256', 'typ': 'JWT'})) payload = force_bytes(json.dumps({'foo': 'bar'})) # Compute signature key = b'mysupersecuretestkey' fake_key = b'mysupersecurefaketestkey' h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend()) msg = '{}.{}'.format(smart_text(b64encode(header)), smart_text(b64encode(payload))) h.update(force_bytes(msg)) signature = b64encode(h.finalize()) token = '{}.{}.{}'.format( smart_text(b64encode(header)), smart_text(b64encode(payload)), smart_text(signature) ) token_bytes = force_bytes(token) key_text = smart_text(fake_key) with self.assertRaises(SuspiciousOperation) as ctx: self.backend.get_payload_data(token_bytes, key_text) self.assertEqual(ctx.exception.args[0], 'JWS token verification failed.')
Example #18
Source File: test_auth.py From mozilla-django-oidc with Mozilla Public License 2.0 | 6 votes |
def test_disallowed_unsecured_invalid_token(self): """Test payload data from invalid secure token (unsecured disallowed).""" header = force_bytes(json.dumps({'alg': 'HS256', 'typ': 'JWT'})) payload = force_bytes(json.dumps({'foo': 'bar'})) # Compute signature key = b'mysupersecuretestkey' fake_key = b'mysupersecurefaketestkey' h = hmac.HMAC(key, hashes.SHA256(), backend=default_backend()) msg = '{}.{}'.format(smart_text(b64encode(header)), smart_text(b64encode(payload))) h.update(force_bytes(msg)) signature = b64encode(h.finalize()) token = '{}.{}.{}'.format( smart_text(b64encode(header)), smart_text(b64encode(payload)), smart_text(signature) ) token_bytes = force_bytes(token) key_text = smart_text(fake_key) with self.assertRaises(SuspiciousOperation) as ctx: self.backend.get_payload_data(token_bytes, key_text) self.assertEqual(ctx.exception.args[0], 'JWS token verification failed.')
Example #19
Source File: filters.py From weibo-analysis-system with MIT License | 6 votes |
def choices(self): yield { 'selected': self.lookup_exact_val == '' and not self.lookup_isnull_val, 'query_string': self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]), 'display': _('All'), } for pk_val, val in self.lookup_choices: yield { 'selected': self.lookup_exact_val == smart_text(pk_val), 'query_string': self.query_string({ self.lookup_exact_name: pk_val, }, [self.lookup_isnull_name]), 'display': val, } if (is_related_field(self.field) and self.field.field.null or hasattr(self.field, 'remote_field') and self.field.null): yield { 'selected': bool(self.lookup_isnull_val), 'query_string': self.query_string({ self.lookup_isnull_name: 'True', }, [self.lookup_exact_name]), 'display': EMPTY_CHANGELIST_VALUE, }
Example #20
Source File: filters.py From weibo-analysis-system with MIT License | 6 votes |
def choices(self): yield { 'selected': (self.lookup_exact_val is '' and self.lookup_isnull_val is ''), 'query_string': self.query_string({}, [self.lookup_exact_name, self.lookup_isnull_name]), 'display': _('All'), } include_none = False for val in self.lookup_choices: if val is None: include_none = True continue val = smart_text(val) yield { 'selected': self.lookup_exact_val == val, 'query_string': self.query_string({self.lookup_exact_name: val}, [self.lookup_isnull_name]), 'display': val, } if include_none: yield { 'selected': bool(self.lookup_isnull_val), 'query_string': self.query_string({self.lookup_isnull_name: 'True'}, [self.lookup_exact_name]), 'display': EMPTY_CHANGELIST_VALUE, }
Example #21
Source File: fields.py From django-places with MIT License | 5 votes |
def value_to_string(self, obj): value = self._get_val_from_obj(obj) return smart_text(value)
Example #22
Source File: url_validator.py From coursys with GNU General Public License v3.0 | 5 votes |
def __call__(self, value): try: super(QuickURLValidator, self).__call__(value) except ValidationError as e: # Trivial case failed. Try for possible IDN domain if value: value = smart_text(value) scheme, netloc, path, query, fragment = urllib.parse.urlsplit(value) try: netloc = netloc.encode('idna') # IDN -> ACE except UnicodeError: # invalid domain part raise e url = urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) super(URLValidator, self).__call__(url) else: raise else: url = value if True: # self.verify_exists no longer exists, but we're doing it anyway. import urllib.request, urllib.error, urllib.parse headers = { "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept-Language": "en-us,en;q=0.5", "Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7", "Connection": "close", "User-Agent": 'CourSys', } try: req = urllib.request.Request(url, None, headers) u = urllib.request.urlopen(req, timeout=2) except ValueError: raise ValidationError('Enter a valid URL.', code='invalid') except: # urllib2.URLError, httplib.InvalidURL, etc. raise ValidationError('This URL appears to be a broken link.', code='invalid_link')
Example #23
Source File: authentication.py From django-oidc-rp with MIT License | 5 votes |
def authenticate(self, request): """ Authenticates users using a provided Bearer token. """ # First step, retrieves the Bearer token from the authorization header. auth = get_authorization_header(request).split() if not auth or smart_text(auth[0].lower()) != 'bearer': return if len(auth) == 1: raise AuthenticationFailed('Invalid authorization header; no bearer token provided') elif len(auth) > 2: raise AuthenticationFailed('Invalid authorization header; many bearer tokens provided') bearer_token = smart_text(auth[1]) # Tries to retrieve user information from the OP. try: userinfo_response = requests.get( oidc_rp_settings.PROVIDER_USERINFO_ENDPOINT, headers={'Authorization': 'Bearer {0}'.format(bearer_token)}) userinfo_response.raise_for_status() except HTTPError: raise AuthenticationFailed('Bearer token seems invalid or expired.') userinfo_response_data = userinfo_response.json() # Tries to retrieve a corresponding user in the local database and creates it if applicable. try: oidc_user = OIDCUser.objects.select_related('user').get( sub=userinfo_response_data.get('sub')) except OIDCUser.DoesNotExist: oidc_user = create_oidc_user_from_claims(userinfo_response_data) oidc_user_created.send(sender=self.__class__, request=request, oidc_user=oidc_user) else: update_oidc_user_from_claims(oidc_user, userinfo_response_data) return oidc_user.user, bearer_token
Example #24
Source File: filters.py From StormOnline with Apache License 2.0 | 5 votes |
def choices(self): yield { 'selected': self.lookup_exact_val is '', 'query_string': self.query_string({}, [self.lookup_exact_name]), 'display': _('All') } for lookup, title in self.field.flatchoices: yield { 'selected': smart_text(lookup) == self.lookup_exact_val, 'query_string': self.query_string({self.lookup_exact_name: lookup}), 'display': title, }
Example #25
Source File: filters.py From StormOnline with Apache License 2.0 | 5 votes |
def choices(self): self.lookup_in_val = (type(self.lookup_in_val) in (tuple,list)) and self.lookup_in_val or list(self.lookup_in_val) yield { 'selected': len(self.lookup_in_val) == 0, 'query_string': self.query_string({},[self.lookup_in_name]), 'display': _('All'), } for val in self.lookup_choices: yield { 'selected': smart_text(val) in self.lookup_in_val, 'query_string': self.query_string({self.lookup_in_name: ",".join([val]+self.lookup_in_val),}), 'remove_query_string': self.query_string({self.lookup_in_name: ",".join([v for v in self.lookup_in_val if v != val]),}), 'display': val, }
Example #26
Source File: dashboard.py From StormOnline with Apache License 2.0 | 5 votes |
def valid_value(self, value): value = self.prepare_value(value) for k, v in self.choices: if value == smart_text(k): return True return False
Example #27
Source File: base.py From StormOnline with Apache License 2.0 | 5 votes |
def default(self, o): if isinstance(o, datetime.datetime): return o.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(o, datetime.date): return o.strftime('%Y-%m-%d') elif isinstance(o, decimal.Decimal): return str(o) elif isinstance(o, Promise): return force_text(o) else: try: return super(JSONEncoder, self).default(o) except Exception: return smart_text(o)
Example #28
Source File: detail.py From StormOnline with Apache License 2.0 | 5 votes |
def init(self): self.label = label_for_field(self.field_name, self.obj.__class__, model_admin=self.admin_view, return_attr=False ) try: f, attr, value = lookup_field( self.field_name, self.obj, self.admin_view) except (AttributeError, ObjectDoesNotExist): self.text else: if f is None: self.allow_tags = getattr(attr, 'allow_tags', False) boolean = getattr(attr, 'boolean', False) if boolean: self.allow_tags = True self.text = boolean_icon(value) else: self.text = smart_text(value) else: if isinstance(f.rel, models.ManyToOneRel): self.text = getattr(self.obj, f.name) else: self.text = display_for_field(value, f) self.field = f self.attr = attr self.value = value
Example #29
Source File: util.py From StormOnline with Apache License 2.0 | 5 votes |
def display_for_field(value, field): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if field.flatchoices: return dict(field.flatchoices).get(value, EMPTY_CHANGELIST_VALUE) # NullBooleanField needs special-case null-handling, so it comes # before the general null test. elif isinstance(field, models.BooleanField) or isinstance(field, models.NullBooleanField): return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(field, models.DateTimeField): return formats.localize(tz_localtime(value)) elif isinstance(field, (models.DateField, models.TimeField)): return formats.localize(value) elif isinstance(field, models.DecimalField): return formats.number_format(value, field.decimal_places) elif isinstance(field, models.FloatField): return formats.number_format(value) elif isinstance(field.rel, models.ManyToManyRel): return ', '.join([smart_text(obj) for obj in value.all()]) else: return smart_text(value)
Example #30
Source File: util.py From StormOnline with Apache License 2.0 | 5 votes |
def display_for_value(value, boolean=False): from xadmin.views.list import EMPTY_CHANGELIST_VALUE if boolean: return boolean_icon(value) elif value is None: return EMPTY_CHANGELIST_VALUE elif isinstance(value, datetime.datetime): return formats.localize(tz_localtime(value)) elif isinstance(value, (datetime.date, datetime.time)): return formats.localize(value) elif isinstance(value, (decimal.Decimal, float)): return formats.number_format(value) else: return smart_text(value)