def create_token(self, session_id, session_key, end_date=None, token_expire_seconds=None): session_key_256 = make_sha256(session_key) # Delete all active tokens self.delete_session_key_tokens(session_key_256) token = '%s-%s' % (session_id, make_unique_hash(length=70)) token_256 = make_sha256(token) data = { 'lock_key': make_token_lock(self.request, token, session_id), 'session_id': session_id, 'session_key': session_key_256} if end_date: data['end_date'] = date_to_timestamp(end_date) if token_expire_seconds: data['token_expire_seconds'] = int(token_expire_seconds) info = compact_dump(data) # Save token file_path = self.get_token_file_path(token_256) put_binary_on_file(file_path, info) # Save reference reference_path = self.get_reference_file_path(session_key_256) put_binary_on_file(reference_path, token_256 + NEW_LINE, mode='ab') return token
def create_file_path(self, file_date=None): file_date = maybe_date(file_date or TODAY_DATE()) base_folder_path = file_date.strftime('%Y%m/%d') last_folder = 0 full_base_folder_path = join_paths(self.storage_path, base_folder_path) folders = sorted(int(i) for i in get_dir_filenames(full_base_folder_path) if i.isdigit()) if folders: last_folder = folders[-1] folder_path = join_paths(base_folder_path, last_folder) full_folder_path = join_paths(self.storage_path, folder_path) if len(get_dir_filenames(full_folder_path)) >= self.max_blocks_per_folder: folder_path = join_paths(base_folder_path, last_folder + 1) while True: filename = make_unique_hash(length=80) path = join_paths(folder_path, filename) full_path = join_paths(self.storage_path, path) if not isfile(full_path): return full_path, path
def save_file( self, binary, application_code, code_key=None, type_key=None, data=None, filename=None, title=None, parent_id=None): file_path = self.save_file_path(binary, filename) session_id = session_type = None if self.request.authenticated: session_id = self.request.authenticated.session_id session_type = self.request.authenticated.session_type # Add applications file relation new = File( file_id=file_path.id, parent_id=parent_id, key=make_unique_hash(70), application_code=to_unicode(application_code), code_key=maybe_unicode(code_key), type_key=maybe_unicode(type_key), data=maybe_unicode(data), filename=maybe_unicode(filename), title=maybe_unicode(title), session_type=session_type, session_id=session_id) self.session.add(new) self.session.flush() # Add some attributes new.size = file_path.size new.mimetype = file_path.mimetype return new
def create_temporary_file(mode='wb'): temporary_path = join_paths(FILES_TEMPORARY_DIR, make_unique_hash(64)) open_file = get_open_file(temporary_path, mode=mode) return temporary_path, open_file
def create_message( self, subject, recipients, body=None, html=None, sender=None, cc=None, bcc=None, reply_to=None, content_charset='utf-8', attachments=None, message_id=None): force_development_email = None if not self.request.is_production_environ: force_development_email = self.settings.get('force_development_email') or None if body: body = to_string(body, encoding=content_charset) if not html: html = body.replace(NEW_LINE, HTML_NEW_LINE) if html: html = to_string(html, encoding=content_charset) if not html.lower().startswith('<html'): html = '<html><body>%s</body></html>' % html options = {} # FROM sender if sender: options['sender'] = format_email( sender, encoding=content_charset, force_development_email=force_development_email) # Envelope CC if cc: if isinstance(cc, dict): cc = [cc] options['cc'] = [ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(cc)] # Envelope BCC if bcc: if isinstance(bcc, dict): bcc = [bcc] options['bcc'] = [ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(bcc)] if not message_id: domain = self.settings.get('message_domain') or self.api_session_manager.default_domain message_id = make_msgid(make_unique_hash(10), domain) extra_headers = { 'Date': formatdate(localtime=True), 'Message-ID': to_string(message_id)} # Force reply to another email if reply_to: extra_headers['Reply-To'] = '<%s>' % to_string(reply_to) mime_attachments = [] if attachments: if isinstance(attachments, dict): attachments = [attachments] for attachment in maybe_list(attachments): f = attachment.get('file') or attachment['fp'] filename = to_string(attachment.get('filename') or basename(f.name)).replace(' ', '') mimetype = to_string(attachment.get('content_type') or find_mimetype(filename, f)) f.seek(0) mime_attachments.append(self.api_session_manager.attachment_cls( data=f, filename=filename, content_type=mimetype)) return self.api_session_manager.message_cls( subject=to_string(subject, encoding=content_charset), html=html, body=body, recipients=[ format_email(e, content_charset, force_development_email=force_development_email) for e in maybe_list(recipients)], attachments=mime_attachments, extra_headers=extra_headers, **options)
def create_authorization(self, session_id, token_expire_seconds=None): session_key = make_unique_hash(length=70) token = self.create_token(session_id, session_key, token_expire_seconds=token_expire_seconds) return session_key, token