Python django.utils.encoding.smart_bytes() Examples
The following are 25
code examples of django.utils.encoding.smart_bytes().
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: attachments.py From modoboa-webmail with MIT License | 6 votes |
def save_attachment(f): """Save a new attachment to the filesystem. The attachment is not saved using its own name to the filesystem. To avoid conflicts, a random name is generated and used instead. :param f: an uploaded file object (see Django's documentation) or bytes :return: the new random name """ from tempfile import NamedTemporaryFile dstdir = os.path.join(settings.MEDIA_ROOT, "webmail") try: fp = NamedTemporaryFile(dir=dstdir, delete=False) except Exception as e: raise InternalError(str(e)) if isinstance(f, (six.binary_type, six.text_type)): fp.write(smart_bytes(f)) else: for chunk in f.chunks(): fp.write(chunk) fp.close() return fp.name
Example #2
Source File: test_mailgun.py From django-inbound-email with MIT License | 6 votes |
def test_attachments(self): """Test inbound email with attachments.""" data = mailgun_payload attachment_1 = open(self.test_upload_txt, 'r').read() attachment_2 = open(self.test_upload_png, 'rb').read() data['attachment-1'] = open(self.test_upload_txt, 'r') data['attachment-2'] = open(self.test_upload_png, 'rb') request = self.factory.post(self.url, data=data) email = self.parser.parse(request) self._assertEmailParsedCorrectly(email, data) # for each attachmen, check the contents match the input self.assertEqual(len(email.attachments), 2) # convert list of 3-tuples into dict so we can lookup by filename attachments = {k[0]: (k[1], k[2]) for k in email.attachments} self.assertEqual(smart_bytes(attachments['attachment-1'][0]), smart_bytes(attachment_1)) self.assertEqual(attachments['attachment-1'][1], 'text/plain') self.assertEqual(attachments['attachment-2'][0], attachment_2) self.assertEqual(attachments['attachment-2'][1], 'image/jpeg')
Example #3
Source File: test_sendgrid.py From django-inbound-email with MIT License | 6 votes |
def test_attachments(self): """Test inbound email with attachments.""" data = sendgrid_payload attachment_1 = open(self.test_upload_txt, 'r').read() attachment_2 = open(self.test_upload_png, 'rb').read() data['attachment1'] = open(self.test_upload_txt, 'r') data['attachment2'] = open(self.test_upload_png, 'rb') request = self.factory.post(self.url, data=data) email = self.parser.parse(request) self._assertEmailParsedCorrectly(email, data) # for each attachmen, check the contents match the input self.assertEqual(len(email.attachments), 2) # convert list of 3-tuples into dict so we can lookup by filename attachments = {k[0]: (k[1], k[2]) for k in email.attachments} self.assertEqual(smart_bytes(attachments['test_upload_file.txt'][0]), smart_bytes(attachment_1)) self.assertEqual(attachments['test_upload_file.txt'][1], 'text/plain') self.assertEqual(attachments['test_upload_file.jpg'][0], attachment_2) self.assertEqual(attachments['test_upload_file.jpg'][1], 'image/jpeg')
Example #4
Source File: views.py From 2buntu-blog with Apache License 2.0 | 6 votes |
def _encode_article(self, article): """ Encode an article. """ return { 'id': article.id, 'title': smart_text(article), 'author': { 'id': article.author.id, 'name': smart_text(article.author.profile), 'email_hash': md5(smart_bytes(article.author.email)).hexdigest(), }, 'category': { 'id': article.category.id, 'name': article.category.name, }, 'body': article.render(), 'cc_license': article.cc_license, 'date': timegm(article.date.utctimetuple()), 'url': self._request.build_absolute_uri(article.get_absolute_url()), }
Example #5
Source File: imaputils.py From modoboa-webmail with MIT License | 5 votes |
def login(self, user, passwd): """Custom login method We connect to the server, issue a LOGIN command. If successfull, we try to record a eventuel CAPABILITY untagged response. Otherwise, we issue the command. :param user: username :param passwd: password """ try: if self.conf["imap_secured"]: self.m = imaplib.IMAP4_SSL(self.address, self.port) else: self.m = imaplib.IMAP4(self.address, self.port) except (socket.error, imaplib.IMAP4.error, ssl.SSLError) as error: raise ImapError(_("Connection to IMAP server failed: %s" % error)) passwd = self.m._quote(passwd) data = self._cmd("LOGIN", smart_bytes(user), smart_bytes(passwd)) self.m.state = "AUTH" if "CAPABILITY" in self.m.untagged_responses: self.capabilities = ( self.m.untagged_responses.pop('CAPABILITY')[0] .decode().split()) else: data = self._cmd("CAPABILITY") self.capabilities = data[0].decode().split()
Example #6
Source File: utils.py From django-oidc-rp with MIT License | 5 votes |
def _get_jwks_keys(shared_key): """ Returns JWKS keys used to decrypt id_token values. """ # The OpenID Connect Provider (OP) uses RSA keys to sign/enrypt ID tokens and generate public # keys allowing to decrypt them. These public keys are exposed through the 'jwks_uri' and should # be used to decrypt the JWS - JSON Web Signature. jwks_keys = KEYS() jwks_keys.load_from_url(oidc_rp_settings.PROVIDER_JWKS_ENDPOINT) # Adds the shared key (which can correspond to the client_secret) as an oct key so it can be # used for HMAC signatures. jwks_keys.add({'key': smart_bytes(shared_key), 'kty': 'oct'}) return jwks_keys
Example #7
Source File: django_orm.py From data with GNU General Public License v3.0 | 5 votes |
def to_python(self, value): if value is None: return None if isinstance(value, oauth2client.client.Credentials): return value return pickle.loads(base64.b64decode(smart_bytes(value)))
Example #8
Source File: django_orm.py From data with GNU General Public License v3.0 | 5 votes |
def to_python(self, value): if value is None: return None if isinstance(value, oauth2client.client.Credentials): return value return pickle.loads(base64.b64decode(smart_bytes(value)))
Example #9
Source File: views.py From autocompeter with Mozilla Public License 2.0 | 5 votes |
def default_username(email): # Store the username as a base64 encoded sha1 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). return base64.urlsafe_b64encode( hashlib.sha1(smart_bytes(email)).digest() ).rstrip(b'=')
Example #10
Source File: models.py From jarvis with GNU General Public License v2.0 | 5 votes |
def to_python(self, value): """Overrides ``models.Field`` method. This is used to convert bytes (from serialization etc) to an instance of this class""" if value is None: return None elif isinstance(value, oauth2client.client.Credentials): return value else: return pickle.loads(base64.b64decode(encoding.smart_bytes(value)))
Example #11
Source File: managers.py From Inboxen with GNU Affero General Public License v3.0 | 5 votes |
def hash_it(self, data): hashed = hashlib.new(settings.COLUMN_HASHER) hashed.update(smart_bytes(data)) hashed = "{0}:{1}".format(hashed.name, hashed.hexdigest()) return hashed
Example #12
Source File: fields.py From django-generic-confirmation with BSD 3-Clause "New" or "Revised" License | 5 votes |
def from_db_value(self, value, expression, connection, context): if isinstance(value, PickledObject): # If the value is a definite pickle; and an error is raised in de-pickling # it should be allowed to propogate. return pickle.loads(smart_bytes(base64.b64decode(value))) else: try: return pickle.loads(smart_bytes(base64.b64decode(value))) except: # If an error was raised, just return the plain value return value
Example #13
Source File: test_sql_email.py From modoboa-amavis with MIT License | 5 votes |
def _fetch_message(self): message_path = os.path.join(SAMPLES_DIR, "%s-input.txt" % self.mailid) assert os.path.isfile(message_path), "%s does not exist." % message_path with open(message_path, "rb") as fp: mail_text = smart_bytes(fp.read()) return mail_text
Example #14
Source File: fields.py From 2buntu-blog with Apache License 2.0 | 5 votes |
def validate(self, value): super(CaptchaField, self).validate(value) params = urlencode({ 'secret': settings.RECAPTCHA_SECRET_KEY, 'response': smart_bytes(value), 'remoteip': self.get_remoteip(), }) url = 'https://www.google.com/recaptcha/api/siteverify?%s' % params if not loads(smart_text(urlopen(url).read())).get('success', False): raise ValidationError("Incorrect captcha solution provided.") return value
Example #15
Source File: tests.py From 2buntu-blog with Apache License 2.0 | 5 votes |
def setUp(self): # Create a dummy user, category, and article user = User.objects.create_user('user', 'user@example.com', 'user') category = Category(name='Test') category.save() self.article = Article(author=user, category=category, title='Test', body='Test', status=Article.PUBLISHED, date=now()) self.article.save() # Create a dummy image o = BytesIO(smart_bytes('x\x9cb`\x01\x00\x00\x00\xff\xff\x03\x00\x00\x06\x00\x05')) self.image = Image(caption='Test', image=File(o, name='test.png')) self.image.save()
Example #16
Source File: views.py From 2buntu-blog with Apache License 2.0 | 5 votes |
def _encode_profile(self, profile): """ Encode a profile. """ return { 'id': profile.user.id, 'name': smart_text(profile), 'email_hash': md5(smart_bytes(profile.user.email)).hexdigest(), 'age': profile.age(), 'location': profile.location, 'website': profile.website, 'bio': profile.bio, 'last_seen': timegm(profile.user.last_login.utctimetuple()) if profile.user.last_login else 0, 'url': self._request.build_absolute_uri(profile.get_absolute_url()), }
Example #17
Source File: gravatar.py From 2buntu-blog with Apache License 2.0 | 5 votes |
def gravatar(email, size=64): """ Return the Gravatar URL for the provided email. :param email: the email address to hash :param size: the size of the Gravatar to generate :returns: the URL of the image """ return '//gravatar.com/avatar/%s?d=identicon&size=%s' % ( md5(smart_bytes(email)).hexdigest(), size, )
Example #18
Source File: test_mandrill.py From django-inbound-email with MIT License | 5 votes |
def _assertEmailParsedCorrectly(self, emails, mandrill_payload, has_html=True): def _parse_emails(to): ret = [] for address, name in to: if not name: ret.append(address) else: ret.append("\"%s\" <%s>" % (name, address)) return ret def _parse_from(name, email): if not name: return email return "\"%s\" <%s>" % (name, email) for i, e in enumerate(emails): msg = json.loads(mandrill_payload['mandrill_events'])[i]['msg'] self.assertEqual(e.subject, msg['subject']) self.assertEqual(e.to, _parse_emails(msg['to'])) self.assertEqual(e.cc, _parse_emails(msg['cc'])) self.assertEqual(e.bcc, _parse_emails(msg['bcc'])) self.assertEqual( e.from_email, _parse_from(msg.get('from_name'), msg.get('from_email')) ) if has_html: self.assertEqual(e.alternatives[0][0], msg['html']) for name, contents, mimetype in e.attachments: # Check that base64 contents are decoded is_base64 = msg['attachments'][name].get('base64') req_contents = msg['attachments'][name]['content'] if is_base64: req_contents = base64.b64decode(req_contents) self.assertEqual(smart_bytes(req_contents), smart_bytes(contents)) self.assertEqual(msg['attachments'][name]['type'], mimetype) self.assertEqual(e.body, msg['text'])
Example #19
Source File: mandrill.py From django-inbound-email with MIT License | 5 votes |
def _process_attachments(self, email, attachments): for key, attachment in list(attachments.items()): is_base64 = attachment.get('base64') name = attachment.get('name') mimetype = attachment.get('type') content = attachment.get('content', "") if is_base64: content = base64.b64decode(content) # watchout: sometimes attachment contents are base64'd but mandrill doesn't set the flag elif _detect_base64(content): content = base64.b64decode(content) content = smart_bytes(content, strings_only=True) if len(content) > self.max_file_size: logger.debug( "File attachment %s is too large to process (%sB)", name, len(content) ) raise AttachmentTooLargeError( email=email, filename=name, size=len(content) ) if name and mimetype and content: email.attach(name, content, mimetype) return email
Example #20
Source File: models.py From alfred-gmail with MIT License | 5 votes |
def to_python(self, value): """Overrides ``models.Field`` method. This is used to convert bytes (from serialization etc) to an instance of this class""" if value is None: return None elif isinstance(value, oauth2client.client.Credentials): return value else: return pickle.loads(base64.b64decode(encoding.smart_bytes(value)))
Example #21
Source File: middleware.py From django-responsive2 with BSD 3-Clause "New" or "Revised" License | 5 votes |
def process_response(self, request, response): html_types = ('text/html', 'application/xhtml+xml') content_type = response.get('Content-Type', '').split(';')[0] content_encoding = response.get('Content-Encoding', '') if any((getattr(response, 'streaming', False), 'gzip' in content_encoding, content_type not in html_types)): return response if settings.RESPONSIVE_COOKIE_NAME not in request.COOKIES \ or getattr(request, 'INVALID_RESPONSIVE_COOKIE', False): expires = datetime.datetime.utcnow() + \ datetime.timedelta(days=settings.RESPONSIVE_COOKIE_AGE) snippet = render_to_string('responsive/snippet.html', { 'cookie_name': settings.RESPONSIVE_COOKIE_NAME, 'cookie_age': 60 * 60 * 24 * settings.RESPONSIVE_COOKIE_AGE, # convert to secs 'cookie_expires': expires.strftime('%a, %d %b %Y %H:%M:%S GMT') }) pattern = re.compile(b'<head()>|<head (.*?)>', re.IGNORECASE) response.content = pattern.sub(b'<head\g<1>>' + smart_bytes(snippet), response.content) if response.get('Content-Length', None): response['Content-Length'] = len(response.content) patch_vary_headers(response, ('Cookie', )) return response
Example #22
Source File: django_orm.py From aqua-monitor with GNU Lesser General Public License v3.0 | 5 votes |
def to_python(self, value): if value is None: return None if isinstance(value, oauth2client.client.Credentials): return value return pickle.loads(base64.b64decode(smart_bytes(value)))
Example #23
Source File: models.py From aqua-monitor with GNU Lesser General Public License v3.0 | 5 votes |
def to_python(self, value): """Overrides ``models.Field`` method. This is used to convert bytes (from serialization etc) to an instance of this class""" if value is None: return None elif isinstance(value, oauth2client.client.Credentials): return value else: try: return jsonpickle.decode( base64.b64decode(encoding.smart_bytes(value)).decode()) except ValueError: return pickle.loads( base64.b64decode(encoding.smart_bytes(value)))
Example #24
Source File: auth.py From mozilla-django-oidc with Mozilla Public License 2.0 | 5 votes |
def _verify_jws(self, payload, key): """Verify the given JWS payload with the given key and return the payload""" jws = JWS.from_compact(payload) try: alg = jws.signature.combined.alg.name except KeyError: msg = 'No alg value found in header' raise SuspiciousOperation(msg) if alg != self.OIDC_RP_SIGN_ALGO: msg = "The provider algorithm {!r} does not match the client's " \ "OIDC_RP_SIGN_ALGO.".format(alg) raise SuspiciousOperation(msg) if isinstance(key, six.string_types): # Use smart_bytes here since the key string comes from settings. jwk = JWK.load(smart_bytes(key)) else: # The key is a json returned from the IDP JWKS endpoint. jwk = JWK.from_json(key) if not jws.verify(jwk): msg = 'JWS token verification failed.' raise SuspiciousOperation(msg) return jws.payload
Example #25
Source File: __init__.py From templated-docs with MIT License | 4 votes |
def fill_template(template_name, context, output_format='odt'): """ Fill a document with data and convert it to the requested format. Returns an absolute path to the generated file. """ if not isinstance(context, Context): context = Context(context) context['output_format'] = output_format source_file = find_template_file(template_name) source_extension = os.path.splitext(source_file)[1] source = zipfile.ZipFile(source_file, 'r') dest_file = NamedTemporaryFile(delete=False, suffix=source_extension) dest = zipfile.ZipFile(dest_file, 'w') manifest_data = '' for name in source.namelist(): data = source.read(name) if name.endswith('.xml'): data = smart_str(data) if any(name.endswith(file) for file in ('content.xml', 'styles.xml')): template = Template(fix_inline_tags(data)) data = template.render(context) elif name == 'META-INF/manifest.xml': manifest_data = data[:-20] # Cut off the closing </manifest> tag continue # We will append it at the very end dest.writestr(name, smart_bytes(data)) for _, image in context.dicts[0].get(IMAGES_CONTEXT_KEY, {}).items(): filename = os.path.basename(image.name) ext = os.path.splitext(filename)[1][1:] manifest_data += ('<manifest:file-entry ' 'manifest:media-type="image/%(ext)s" ' 'manifest:full-path="Pictures/%(filename)s"/>\n' ) % locals() image.open() dest.writestr('Pictures/%s' % filename, image.read()) image.close() manifest_data += '</manifest:manifest>' dest.writestr('META-INF/manifest.xml', manifest_data) source.close() dest.close() if source_extension[1:] != output_format: results = Queue() convertor = Process(target=_convert_subprocess, args=(str(dest_file.name), output_format, results)) convertor.start() return results.get() else: return dest_file.name