def postprocess_content_file(self, file_upload): # validate file size allowed_size = request.app.config['{0}.size_limit'.format(self.type)] file_size = get_file_size(file_upload.file, limit=allowed_size) if file_size > allowed_size: h_size = html.hsize(allowed_size) raise form.ValidationError('file_size', {'size': h_size}) self.processed_data['file_size'] = file_size # must seek to the beginning of file so it can be saved file_upload.file.seek(0) return file_upload
def cleanup(): forms = request.forms action = forms.get('action', 'check') if action not in ['check', 'delete']: # Translators, used as response to innvalid HTTP request abort(400, _('Invalid request')) free = zipballs.free_space()[0] cleanup = list(zipballs.cleanup_list(free)) selected = forms.getall('selection') metadata = list(cleanup) selected = [z for z in metadata if z['md5'] in selected] if action == 'check': if not selected: # Translators, used as message to user when clean-up is started # without selecting any content message = _('No content selected') else: tot = hsize(sum([s['size'] for s in selected])) message = str( # Translators, used when user is previewing clean-up, %s is # replaced by amount of content that can be freed in bytes, # KB, MB, etc _('%s can be freed by removing selected content')) % tot return {'vals': forms, 'metadata': metadata, 'message': message, 'needed': zipballs.needed_space(free)} else: conf = request.app.config archive = Archive.setup(conf['librarian.backend'], request.db.main, unpackdir=conf['content.unpackdir'], contentdir=conf['content.contentdir'], spooldir=conf['content.spooldir'], meta_filename=conf['content.metadata']) if selected: archive.remove_from_archive([z['md5'] for z in selected]) request.app.cache.invalidate(prefix='content') redirect(i18n_url('content:list')) else: # Translators, error message shown on clean-up page when there was # no deletable content message = _('Nothing to delete') return {'vals': MultiDict(), 'metadata': cleanup, 'message': message, 'needed': archive.needed_space()}
def validate(self): active_storage_id = storage.get_consolidate_status() if active_storage_id: raise form.ValidationError('already_running', {}) # Perform preflight check storages = storage.get_content_storages() storage_id = self.processed_data['storage_id'] try: dest = storages.move_preflight(storage_id) except storage.NotFoundError: raise form.ValidationError('storage_not_found', {}) except storage.NoMoveTargetError: raise form.ValidationError('no_move_target', {}) except storage.NothingToMoveError: raise form.ValidationError('nothing_to_move', {}) except storage.CapacityError as err: params = dict(size=hsize(err.capacity)) raise form.ValidationError('not_enough_space', params) else: self.processed_data['storages'] = storages self.processed_data['dest'] = dest
def test_hsize_units(): assert mod.hsize(12, 'm', 1000) == '12.00 m' assert mod.hsize(1200, 'm', 1000) == '1.20 Km'
def test_hsize_step(): assert mod.hsize(12, step=1000) == '12.00 B' assert mod.hsize(1200, step=1000) == '1.20 KB'
def test_hsize(): assert mod.hsize(12) == '12.00 B' assert mod.hsize(1030) == '1.01 KB' assert mod.hsize(2097152) == '2.00 MB' assert mod.hsize(12, sep='') == '12.00B' assert mod.hsize(12, rounding=0) == '12 B'