def map_files_to_node(user, node, data): """ Generate files that reference the content node. """ if settings.DEBUG: # assert that our parameters match expected values assert isinstance(user, User) assert isinstance(node, ContentNode) assert isinstance(data, list) # filter out file that are empty valid_data = filter_out_nones(data) for file_data in valid_data: filename = file_data["filename"] checksum, ext1 = os.path.splitext(filename) ext = ext1.lstrip(".") # Determine a preset if none is given kind_preset = FormatPreset.get_preset(file_data["preset"]) or FormatPreset.guess_format_preset(filename) file_path = generate_object_storage_name(checksum, filename) storage = default_storage if not storage.exists(file_path): raise IOError('{} not found'.format(file_path)) try: if file_data.get('language'): # TODO: Remove DB call per file? file_data['language'] = Language.objects.get(pk=file_data['language']) except ObjectDoesNotExist: invalid_lang = file_data.get('language') logging.warning("file_data with language {} does not exist.".format(invalid_lang)) return ValidationError("file_data given was invalid; expected string, got {}".format(invalid_lang)) resource_obj = File( checksum=checksum, contentnode=node, file_format_id=ext, original_filename=file_data.get('original_filename') or 'file', source_url=file_data.get('source_url'), file_size=file_data['size'], preset=kind_preset, language_id=file_data.get('language'), uploaded_by=user, ) resource_obj.file_on_disk.name = file_path resource_obj.save() # Handle thumbnail if resource_obj.preset and resource_obj.preset.thumbnail: node.thumbnail_encoding = json.dumps({ 'base64': get_thumbnail_encoding(str(resource_obj)), 'points': [], 'zoom': 0 }) node.save()
def test_returns_none_if_unknown_extension(self): """ Check that we return a None for an unknown format. """ filename = "blah.hat" model = FormatPreset.guess_format_preset(filename) assert isinstance(model, type(None))
def test_returns_model_if_correct_preset_name(self): """ Check that we return a FormatPreset model instance. """ filename = "blah.pdf" model = FormatPreset.guess_format_preset(filename) assert isinstance(model, FormatPreset)
def test_returns_model_if_correct_preset_name(self): """ Check that we get a Django model if we pass a correct preset name, like PDF. """ preset = "document" model = FormatPreset.get_preset(preset) assert isinstance(model, FormatPreset)
def test_returns_none_if_unknown_extension(self): """ Check that we return a None for an unknown format. """ filename = "blah.hat" model = FormatPreset.guess_format_preset(filename) assert isinstance(model, types.NoneType)
def test_returns_none_if_called_with_nonexistent_preset(self): """ Make sure we return None when we pass in a formatpreset that doesn't exist. """ preset = "hats" model = FormatPreset.get_preset(preset) assert isinstance(model, type(None))
def test_returns_none_if_called_with_nonexistent_preset(self): """ Make sure we return None when we pass in a formatpreset that doesn't exist. """ preset = "hats" model = FormatPreset.get_preset(preset) assert isinstance(model, types.NoneType)
def test_accepts_string(self): """ Check that if we pass in a string, we won't error out. """ FormatPreset.get_preset("a")
def test_accepts_string(self): """ Make sure we don't raise an error if we pass a string. """ FormatPreset.guess_format_preset("a")