Python django.core.serializers.base.DeserializationError() Examples
The following are 23
code examples of django.core.serializers.base.DeserializationError().
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.core.serializers.base
, or try the search function
.
Example #1
Source File: json.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def Deserializer(stream_or_string, **options): """ Deserialize a stream or string of JSON data. """ if not isinstance(stream_or_string, (bytes, six.string_types)): stream_or_string = stream_or_string.read() if isinstance(stream_or_string, bytes): stream_or_string = stream_or_string.decode('utf-8') try: objects = json.loads(stream_or_string) for obj in PythonDeserializer(objects, **options): yield obj except GeneratorExit: raise except Exception as e: # Map to deserializer error six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
Example #2
Source File: models.py From oldp with MIT License | 6 votes |
def from_json_file(file_path): with open(file_path) as f: out = serializers.deserialize("json", f.read()) # , ignorenonexistent=True) # print(len(out)) try: for o in out: return o.object except DeserializationError: pass raise ProcessingError('Cannot deserialize: %s' % file_path) # MySQL utf8mb4 bugfix # if instance.raw is not None: # instance.raw = ''.join([char if ord(char) < 128 else '' for char in instance.raw]) # # if instance.text is not None: # instance.text = ''.join([char if ord(char) < 128 else '' for char in instance.text]) # # return instance
Example #3
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_helpful_error_message_invalid_field(self): """ If there is an invalid field value, the error message should contain the model associated with it. """ test_string = """[{ "pk": "1", "model": "serializers.player", "fields": { "name": "Bob", "rank": "invalidint", "team": "Team" } }]""" expected = "(serializers.player:pk=1) field_value was 'invalidint'" with self.assertRaisesMessage(DeserializationError, expected): list(serializers.deserialize('json', test_string))
Example #4
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 6 votes |
def test_helpful_error_message_invalid_pk(self): """ If there is an invalid primary key, the error message should contain the model associated with it. """ test_string = """[{ "pk": "badpk", "model": "serializers.player", "fields": { "name": "Bob", "rank": 1, "team": "Team" } }]""" with self.assertRaisesMessage(DeserializationError, "(serializers.player:pk=badpk)"): list(serializers.deserialize('json', test_string))
Example #5
Source File: test_serialize.py From sfm-ui with MIT License | 6 votes |
def test_deserialize_wrong_collection_set(self): # This is testing that deserialization is not permitted when collection is placed # in incorrect collection set directory. serializer = serialize.RecordSerializer(data_dir=self.data_dir) serializer.serialize_collection_set(self.collection_set) # Partially clean the database in preparation for deserializing self.collection1.delete() self.collection_set.delete() self.assertTrue(os.path.exists(self.collection1_records_path)) new_collection_set_path = "{}x".format(self.collection_set_path) shutil.move(self.collection_set_path, new_collection_set_path) self.assertFalse(os.path.exists(self.collection1_records_path)) deserializer = serialize.RecordDeserializer(data_dir=self.data_dir) caught_error = False try: deserializer.deserialize_collection_set(new_collection_set_path) except DeserializationError: caught_error = True self.assertTrue(caught_error)
Example #6
Source File: xml_serializer.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def _get_model_from_node(self, node, attr): """ Helper to look up a model from a <object model=...> or a <field rel=... to=...> node. """ model_identifier = node.getAttribute(attr) if not model_identifier: raise base.DeserializationError( "<%s> node is missing the required '%s' attribute" \ % (node.nodeName, attr)) try: Model = models.get_model(*model_identifier.split(".")) except TypeError: Model = None if Model is None: raise base.DeserializationError( "<%s> node has invalid model identifier: '%s'" % \ (node.nodeName, model_identifier)) return Model
Example #7
Source File: json.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def Deserializer(stream_or_string, **options): """ Deserialize a stream or string of JSON data. """ if not isinstance(stream_or_string, (bytes, six.string_types)): stream_or_string = stream_or_string.read() if isinstance(stream_or_string, bytes): stream_or_string = stream_or_string.decode('utf-8') try: objects = json.loads(stream_or_string) for obj in PythonDeserializer(objects, **options): yield obj except GeneratorExit: raise except Exception as e: # Map to deserializer error raise DeserializationError(e)
Example #8
Source File: pyyaml.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def Deserializer(stream_or_string, **options): """ Deserialize a stream or string of YAML data. """ if isinstance(stream_or_string, bytes): stream_or_string = stream_or_string.decode('utf-8') if isinstance(stream_or_string, six.string_types): stream = StringIO(stream_or_string) else: stream = stream_or_string try: for obj in PythonDeserializer(yaml.safe_load(stream), **options): yield obj except GeneratorExit: raise except Exception as e: # Map to deserializer error raise DeserializationError(e)
Example #9
Source File: 0002_initial_data.py From djangoSIGE with MIT License | 6 votes |
def load_fixture(apps, schema_editor): # Save the old _get_model() function old_get_model = python._get_model # Define new _get_model() function here, which utilizes the apps argument to # get the historical version of a model. This piece of code is directly stolen # from django.core.serializers.python._get_model, unchanged. def _get_model(model_identifier): try: return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError( "Invalid model identifier: '%s'" % model_identifier) # Replace the _get_model() function on the module, so loaddata can utilize it. python._get_model = _get_model try: # Call loaddata command call_command('loaddata', 'estoque_initial_data.json') finally: # Restore old _get_model() function python._get_model = old_get_model
Example #10
Source File: xml_serializer.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def _get_model_from_node(self, node, attr): """ Helper to look up a model from a <object model=...> or a <field rel=... to=...> node. """ model_identifier = node.getAttribute(attr) if not model_identifier: raise base.DeserializationError( "<%s> node is missing the required '%s' attribute" % (node.nodeName, attr)) try: return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError( "<%s> node has invalid model identifier: '%s'" % (node.nodeName, model_identifier))
Example #11
Source File: pyyaml.py From GTDWeb with GNU General Public License v2.0 | 6 votes |
def Deserializer(stream_or_string, **options): """ Deserialize a stream or string of YAML data. """ if isinstance(stream_or_string, bytes): stream_or_string = stream_or_string.decode('utf-8') if isinstance(stream_or_string, six.string_types): stream = StringIO(stream_or_string) else: stream = stream_or_string try: for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options): yield obj except GeneratorExit: raise except Exception as e: # Map to deserializer error six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
Example #12
Source File: helper.py From byro with Apache License 2.0 | 5 votes |
def load_fixtures(self, fixtures, apps): # Via https://stackoverflow.com/questions/25960850/loading-initial-data-with-django-1-7-and-data-migrations/39743581#39743581 # we need to monkeypatch a Django internal in order to provide it with # the correct (old) view of the model from django.core.serializers import base, python # Define new _get_model() function here, which utilizes the apps argument to # get the historical version of a model. This piece of code is directly stolen # from django.core.serializers.python._get_model, unchanged. def _get_model(model_identifier): try: return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError( "Invalid model identifier: '%s'" % model_identifier ) # Save the old _get_model() function old_get_model = python._get_model # Replace the _get_model() function on the module, so loaddata can utilize it. python._get_model = _get_model try: # From django/test/testcases.py for db_name in self._databases_names(include_mirrors=False): try: call_command( "loaddata", *fixtures, **{"verbosity": 0, "commit": False, "database": db_name} ) except Exception: self._rollback_atomics(self.cls_atomics) raise finally: # Restore old _get_model() function python._get_model = old_get_model
Example #13
Source File: python.py From luscan-devel with GNU General Public License v2.0 | 5 votes |
def _get_model(model_identifier): """ Helper to look up a model from an "app_label.module_name" string. """ try: Model = models.get_model(*model_identifier.split(".")) except TypeError: Model = None if Model is None: raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier) return Model
Example #14
Source File: python.py From GTDWeb with GNU General Public License v2.0 | 5 votes |
def _get_model(model_identifier): """ Helper to look up a model from an "app_label.model_name" string. """ try: return apps.get_model(model_identifier) except (LookupError, TypeError): raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
Example #15
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_loaddata_not_found_fields_not_ignore(self): """ Test for ticket #9279 -- Error is raised for entries in the serialized data for fields that have been removed from the database when not ignored. """ with self.assertRaises(DeserializationError): management.call_command( 'loaddata', 'sequence_extra', verbosity=0, )
Example #16
Source File: test_yaml.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_yaml_deserializer_exception(self): with self.assertRaises(DeserializationError): for obj in serializers.deserialize("yaml", "{"): pass
Example #17
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_json_deserializer_exception(self): with self.assertRaises(DeserializationError): for obj in serializers.deserialize("json", """[{"pk":1}"""): pass
Example #18
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_helpful_error_message_for_many2many_non_natural(self): """ Invalid many-to-many keys should throw a helpful error message. """ test_string = """[{ "pk": 1, "model": "serializers.article", "fields": { "author": 1, "headline": "Unknown many to many", "pub_date": "2014-09-15T10:35:00", "categories": [1, "doesnotexist"] } }, { "pk": 1, "model": "serializers.author", "fields": { "name": "Agnes" } }, { "pk": 1, "model": "serializers.category", "fields": { "name": "Reference" } }]""" expected = "(serializers.article:pk=1) field_value was 'doesnotexist'" with self.assertRaisesMessage(DeserializationError, expected): list(serializers.deserialize('json', test_string))
Example #19
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_helpful_error_message_for_many2many_natural1(self): """ Invalid many-to-many keys should throw a helpful error message. This tests the code path where one of a list of natural keys is invalid. """ test_string = """[{ "pk": 1, "model": "serializers.categorymetadata", "fields": { "kind": "author", "name": "meta1", "value": "Agnes" } }, { "pk": 1, "model": "serializers.article", "fields": { "author": 1, "headline": "Unknown many to many", "pub_date": "2014-09-15T10:35:00", "meta_data": [ ["author", "meta1"], ["doesnotexist", "meta1"], ["author", "meta1"] ] } }, { "pk": 1, "model": "serializers.author", "fields": { "name": "Agnes" } }]""" key = ["doesnotexist", "meta1"] expected = "(serializers.article:pk=1) field_value was '%r'" % key with self.assertRaisesMessage(DeserializationError, expected): for obj in serializers.deserialize('json', test_string): obj.save()
Example #20
Source File: test_json.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_helpful_error_message_for_many2many_natural2(self): """ Invalid many-to-many keys should throw a helpful error message. This tests the code path where a natural many-to-many key has only a single value. """ test_string = """[{ "pk": 1, "model": "serializers.article", "fields": { "author": 1, "headline": "Unknown many to many", "pub_date": "2014-09-15T10:35:00", "meta_data": [1, "doesnotexist"] } }, { "pk": 1, "model": "serializers.categorymetadata", "fields": { "kind": "author", "name": "meta1", "value": "Agnes" } }, { "pk": 1, "model": "serializers.author", "fields": { "name": "Agnes" } }]""" expected = "(serializers.article:pk=1) field_value was 'doesnotexist'" with self.assertRaisesMessage(DeserializationError, expected): for obj in serializers.deserialize('json', test_string, ignore=False): obj.save()
Example #21
Source File: tests.py From djongo with GNU Affero General Public License v3.0 | 5 votes |
def test_loaddata_not_found_fields_not_ignore(self): """ Test for ticket #9279 -- Error is raised for entries in the serialized data for fields that have been removed from the database when not ignored. """ with self.assertRaises(DeserializationError): management.call_command( 'loaddata', 'sequence_extra', verbosity=0, )
Example #22
Source File: xml_serializer.py From luscan-devel with GNU General Public License v2.0 | 4 votes |
def _handle_object(self, node): """ Convert an <object> node to a DeserializedObject. """ # Look up the model using the model loading mechanism. If this fails, # bail. Model = self._get_model_from_node(node, "model") # Start building a data dictionary from the object. # If the node is missing the pk set it to None if node.hasAttribute("pk"): pk = node.getAttribute("pk") else: pk = None data = {Model._meta.pk.attname : Model._meta.pk.to_python(pk)} # Also start building a dict of m2m data (this is saved as # {m2m_accessor_attribute : [list_of_related_objects]}) m2m_data = {} # Deseralize each field. for field_node in node.getElementsByTagName("field"): # If the field is missing the name attribute, bail (are you # sensing a pattern here?) field_name = field_node.getAttribute("name") if not field_name: raise base.DeserializationError("<field> node is missing the 'name' attribute") # Get the field from the Model. This will raise a # FieldDoesNotExist if, well, the field doesn't exist, which will # be propagated correctly. field = Model._meta.get_field(field_name) # As is usually the case, relation fields get the special treatment. if field.rel and isinstance(field.rel, models.ManyToManyRel): m2m_data[field.name] = self._handle_m2m_field_node(field_node, field) elif field.rel and isinstance(field.rel, models.ManyToOneRel): data[field.attname] = self._handle_fk_field_node(field_node, field) else: if field_node.getElementsByTagName('None'): value = None else: value = field.to_python(getInnerText(field_node).strip()) data[field.name] = value # Return a DeserializedObject so that the m2m data has a place to live. return base.DeserializedObject(Model(**data), m2m_data)
Example #23
Source File: xml_serializer.py From GTDWeb with GNU General Public License v2.0 | 4 votes |
def _handle_object(self, node): """ Convert an <object> node to a DeserializedObject. """ # Look up the model using the model loading mechanism. If this fails, # bail. Model = self._get_model_from_node(node, "model") # Start building a data dictionary from the object. data = {} if node.hasAttribute('pk'): data[Model._meta.pk.attname] = Model._meta.pk.to_python( node.getAttribute('pk')) # Also start building a dict of m2m data (this is saved as # {m2m_accessor_attribute : [list_of_related_objects]}) m2m_data = {} field_names = {f.name for f in Model._meta.get_fields()} # Deserialize each field. for field_node in node.getElementsByTagName("field"): # If the field is missing the name attribute, bail (are you # sensing a pattern here?) field_name = field_node.getAttribute("name") if not field_name: raise base.DeserializationError("<field> node is missing the 'name' attribute") # Get the field from the Model. This will raise a # FieldDoesNotExist if, well, the field doesn't exist, which will # be propagated correctly unless ignorenonexistent=True is used. if self.ignore and field_name not in field_names: continue field = Model._meta.get_field(field_name) # As is usually the case, relation fields get the special treatment. if field.rel and isinstance(field.rel, models.ManyToManyRel): m2m_data[field.name] = self._handle_m2m_field_node(field_node, field) elif field.rel and isinstance(field.rel, models.ManyToOneRel): data[field.attname] = self._handle_fk_field_node(field_node, field) else: if field_node.getElementsByTagName('None'): value = None else: value = field.to_python(getInnerText(field_node).strip()) data[field.name] = value obj = base.build_instance(Model, data, self.db) # Return a DeserializedObject so that the m2m data has a place to live. return base.DeserializedObject(obj, m2m_data)