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 attribute, bail. pk = node.getAttribute("pk") if not pk: raise base.DeserializationError( "<object> node is missing the 'pk' attribute") 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 elif isinstance(field, TransField): value = field.to_python( xml_serializer.getInnerText( field_node).strip()).raw_data else: value = field.to_python( xml_serializer.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)
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 attribute, bail. pk = node.getAttribute("pk") if not pk: raise base.DeserializationError("<object> node is missing the 'pk' attribute") 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 elif isinstance(field, TransField): value = field.to_python(xml_serializer.getInnerText(field_node).strip()).raw_data else: value = field.to_python(xml_serializer.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)
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 attribute, fail. pk = node.getAttribute("pk") if not pk: raise base.DeserializationError("<object> node is missing the 'pk' attribute") 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. try: field = Model._meta.get_field(field_name) except FieldDoesNotExist, e: try: language = get_field_language(field_name) except: raise e else: lang_codes = [l[0] for l in settings.LANGUAGES] if language not in lang_codes: # fails silently because the LANGUAGES in these settings # are not same as fixtures data continue else: raise e # 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(xml_serializer.getInnerText(field_node).strip()) data[field.name] = value
def get_inner_text(node): """ Get all the inner text of a DOM node (recursively). """ # inspired by http://mail.python.org/pipermail/xml-sig/2005-March/011022.html inner_text = [] for child in node.childNodes: if child.nodeType == child.TEXT_NODE: inner_text.append(child.data) elif child.nodeType == child.ELEMENT_NODE: inner_text.extend(getInnerText(child)) else: pass return u"".join(inner_text)
def process_element(self, node, parent=None): #logger.debug('InboundFaxProcessor::process_element(<%s>, <%s>)', node, parent) model = self._object_mappings[node.nodeName] instance = model() for event, child_node in self.event_stream: if event == 'START_ELEMENT' and child_node.nodeName in model._fieldmap: self.event_stream.expandNode(child_node) field = instance._meta.get_field(model._fieldmap[child_node.nodeName]) setattr(instance, field.name, xml_serializer.getInnerText(child_node)) elif event == 'START_ELEMENT' and child_node.nodeName in self._object_mappings: child = self.process_element(child_node, node) self._children.append(child) #logger.debug('InboundFaxProcessor::_handle_object stashing child <%s>', child) elif event == 'END_ELEMENT' and child_node.nodeName == node.nodeName: break return instance
def _handle_m2m_field_node(self, node, field): """ Handle a <field> node for a GM2MField """ if not isinstance(field, GM2MField): return super(Deserializer, self)._handle_m2m_field_node(node, field) objs = [] for obj_node in node.getElementsByTagName('object'): natural = obj_node.getElementsByTagName('natural') # extract contenttype ct_node = obj_node.getElementsByTagName('contenttype')[0] model = ct.ContentType.objects.get_by_natural_key( ct_node.getAttribute('app'), ct_node.getAttribute('model')).model_class() mngr = model._default_manager.db_manager(self.db) if natural: # extract natural keys key = [xml_serializer.getInnerText(k).strip() for k in natural] else: # normal value key = obj_node.getAttribute('pk') if hasattr(model._default_manager, 'get_by_natural_key'): if hasattr(key, '__iter__') \ and not isinstance(key, six.text_type): obj = mngr.get_by_natural_key(*key) else: obj = mngr.get_by_natural_key(key) else: obj = mngr.get(pk=key) objs.append(obj) return objs
def _handle_m2m_field_node(self, node, field): """ Handle a <field> node for a GM2MField """ if not isinstance(field, GM2MField): return super(Deserializer, self)._handle_m2m_field_node(node, field) objs = [] for obj_node in node.getElementsByTagName('object'): natural = obj_node.getElementsByTagName('natural') # extract contenttype ct_node = obj_node.getElementsByTagName('contenttype')[0] model = ct.ContentType.objects.get_by_natural_key( ct_node.getAttribute('app'), ct_node.getAttribute('model') ).model_class() mngr = model._default_manager.db_manager(self.db) if natural: # extract natural keys key = [xml_serializer.getInnerText(k).strip() for k in natural] else: # normal value key = obj_node.getAttribute('pk') if hasattr(model._default_manager, 'get_by_natural_key'): if hasattr(key, '__iter__') \ and not isinstance(key, six.text_type): obj = mngr.get_by_natural_key(*key) else: obj = mngr.get_by_natural_key(key) else: obj = mngr.get(pk=key) objs.append(obj) return objs
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_path = node.getAttribute("model") model_class = self.get_model_class(model_path) # Start building a data dictionary from the object. data = {} if node.hasAttribute('pk'): data[model_class._meta.pk.attname] = self.pk_to_python(model_class, node.getAttribute('pk')) field_names = self.field_names(model_class) # Deserialize each field. for field_node in node.getElementsByTagName("field"): # If the field is missing the name attribute, bail 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_class._meta.get_field(field_name) # Do not handle relation fields. if(self.field_is_nonrelational(self.ignore, model_class, field)): 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_class, data, self.db) return base.DeserializedObject(obj)
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 attribute, fail. pk = node.getAttribute("pk") if not pk: raise base.DeserializationError( "<object> node is missing the 'pk' attribute") 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. try: field = Model._meta.get_field(field_name) except FieldDoesNotExist, e: try: language = get_field_language(field_name) except: raise e else: lang_codes = [l[0] for l in settings.LANGUAGES] if language not in lang_codes: # fails silently because the LANGUAGES in these settings # are not same as fixtures data continue else: raise e # 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( xml_serializer.getInnerText(field_node).strip()) data[field.name] = value
def handle_object(self, node): """ Convert a ``<file>``-node to a ``DeserializedObject``. :param node: The current xml node of the object :type node: xml.dom.minidom.Element :raises ~django.core.serializers.base.DeserializationError: If the deserialization fails :raises ~django.core.exceptions.FieldDoesNotExist: If the XLIFF file contains a field which doesn't exist on the :return: The deserialized page translation :rtype: django.core.serializers.base.DeserializedObject PageTranslation model """ # Get page translation (process varies for the different xliff versions) page_translation = self.get_object(node) logger.debug( "Existing page translation: %r", page_translation, ) # Increment the version number page_translation.version = page_translation.version + 1 # Make sure object is not in translation anymore if it was before page_translation.currently_in_translation = False # Set the id to None to make sure a new object is stored in the database when save() is called page_translation.id = None # Deserialize each field. for field_node in node.getElementsByTagName(self.unit_node): # Check to which attribute this resource belongs to field_name = self.require_attribute(field_node, "resname") # Get the field from the PageTranslation model try: field = page_translation._meta.get_field(field_name) except FieldDoesNotExist as e: # If the field doesn't exist, check if a legacy field is supported field_name = settings.XLIFF_LEGACY_FIELDS.get(field_name) try: field = page_translation._meta.get_field(field_name) except FieldDoesNotExist: # If the legacy field doesn't exist as well, just raise the initial exception # pylint: disable=raise-missing-from raise e # Now get the actual target value of the field target = field_node.getElementsByTagName("target") if not target: raise DeserializationError( f"Field {field_name} does not contain a <target> node." ) # Set the field attribute of the page translation to the new target value setattr( page_translation, field_name, field.to_python(xml_serializer.getInnerText(target[0]).strip()), ) logger.debug("Deserialized page translation: %r", page_translation) # Return a DeserializedObject return DeserializedObject(page_translation)