def get_model_class(self, model_path):
     if not model_path:
         raise base.DeserializationError(
             "Model path must exist, but given '{}'".format(model_path))
     try:
         return apps.get_model(model_path)
     except (LookupError, TypeError):
         raise base.DeserializationError(
             "Model path can not be found: path: {}".format(model_path))
Example #2
0
    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 _get_model(self, model_identifier):
     """
     Look up model
     """
     if not model_identifier:
         raise base.DeserializationError(
             "Worksheet is missing the required model name")
     try:
         return apps.get_model(model_identifier)
     except (LookupError, TypeError):
         raise base.DeserializationError(
             "Worksheet has invalid model identifier: '%s'" %
             (model_identifier))
 def field_is_nonrelational(self, ignore, model_class, field):
     if (not field.remote_field):
         return True
     else:
         if (ignore):
             return False
         elif isinstance(field.remote_field, models.ManyToManyRel):
             raise base.DeserializationError(
                 "Model contains a (not parsable) Many to Many field: model:{}: field:{}"
                 .format(model_class._meta, field_name))
         elif isinstance(field.remote_field, models.ManyToOneRel):
             raise base.DeserializationError(
                 "Model contains a (not parsable) Many to One field: model:{}: field:{}"
                 .format(model_class._meta, field_name))
Example #5
0
    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 = {}
        field_names = {f.name for f in Model._meta.get_fields()}
        # Do the pk
        #obj_pk = obj.pk
        #    if obj_pk is not None:
        #        data['pk'] = str(obj_pk)
        for field_node in node.getElementsByTagName("field"):
            field_name = field_node.getAttribute("name")
            # If the field is missing the name attribute, bail
            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 or map is used.
            if self.ignore and field_name not in field_names:
                continue
            field = Model._meta.get_field(field_name)

            # Relation fields error or ignore
            if (field.remote_field and 
            (isinstance(field.remote_field, models.ManyToManyRel)
            or isinstance(field.remote_field, models.ManyToOneRel))
            ):
                if self.ignore:
                    continue
                else:
                    if field.remote_field and isinstance(field.remote_field, models.ManyToManyRel):
                        raise base.DeserializationError("<field> node is a (not parsable) Many to Many field")
                    else:
                        raise base.DeserializationError("<field> node is a (not parsable) Many to One foreign key field")
            else:
                #print('node:'  + str(field_node.childNodes))
                c = field_node.firstChild
                if (c):
                    value = field.to_python(c.data.strip())
                else:
                    value = None
                data[field.name] = value
        
        obj = Model(**data)
        return base.DeserializedObject(obj)
    def _get_model(self, sheet):
        """
        Each sheet on the workbook represents a model.
        Returns the model class from a specific sheet,
        or raises when the model has been not found.
        """

        if not sheet.title:
            raise base.DeserializationError(
                "Worksheet is missing the required model name")
        try:
            return apps.get_model(sheet.title)
        except (LookupError, TypeError):
            raise base.DeserializationError(
                'Worksheet has invalid model identifier: {}'.format(
                    sheet.title))
Example #7
0
 def _get_model(model_identifier):
     try:
         return apps.get_model(model_identifier)
     except (LookupError, TypeError):
         msg = ('Invalid model identifier: \'{}\' '
                ''.format(model_identifier))
         raise base.DeserializationError(msg)
Example #8
0
    def _get_model_from_node(self, node):
        """
        Helper to look up a model from a <type=model>.
        """

        name = ""
        #print "node.prefix", node.prefix
        #print "node.tagName", node.tagName
        #print "node.namespaceURI", node.namespaceURI
        if node.namespaceURI and node.namespaceURI.startswith(
                "urn:x-ns:hotjazz:"):
            name = node.namespaceURI[len("urn:x-ns:hotjazz:"):] + ":" + \
                node.tagName.split(":")[-1]
        else:
            name = node.tagName
        #print "name", name

        model_identifier = name.replace("_", ".").replace(":", ".")
        #print "model_identifier", model_identifier

        try:
            Model = models.get_model(*model_identifier.split("."))
        except TypeError:
            raise
            #Model = None
        if Model is None:
            raise base.DeserializationError(
                "<%s> node has invalid model identifier: '%s'" % \
                    (node.nodeName, model_identifier))
        return Model
Example #9
0
 def _get_model_from_node(self, node, attr):
     """
     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 #10
0
    def __init__(self, *args, **kwargs):
        r"""
        Initialize XLIFF 2.0 deserializer

        :param \*args: The supplied arguments
        :type \*args: list

        :param \**kwargs: The supplied keyword arguments
        :type \**kwargs: dict

        :raises ~django.core.serializers.base.DeserializationError: If the deserialization fails
        """
        # Initialize base deserializer
        super().__init__(*args, **kwargs)
        # Get language objects from <xliff>-node
        for event, node in self.event_stream:
            if event == "START_ELEMENT" and node.nodeName == "xliff":
                # Get source language stored in the xliff node
                self.source_language = Language.objects.get(
                    slug=self.require_attribute(node, "srcLang"))
                # Get target language stored in the xliff node
                self.target_language = Language.objects.get(
                    slug=self.require_attribute(node, "trgLang"))
                logger.debug(
                    "Starting XLIFF 2.0 deserialization for translation from %r to %r",
                    self.source_language,
                    self.target_language,
                )
                return
        raise base.DeserializationError(
            "The XLIFF file does not contain an <xliff>-block,")
Example #11
0
    def _handle_object(self, node):
        """
        Convert a model node to a DeserializedObject.
        """
        # Look up the model using the model loading mechanism. If this fails,
        # bail.
        Model = self._get_model_from_node(node)

        # Start building a data dictionary from the object.
        # If the node is missing the pk set it to None
        if node.hasAttribute("hotjazz:pk"):
            pk = node.getAttribute("hotjazz: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 child_node in node.childNodes:
            # ignore anything but elements
            if child_node.nodeType != Node.ELEMENT_NODE:
                continue

            # if the object has non-field contents, bail
            if not child_node.hasAttribute("hotjazz:type") \
                    or not child_node.getAttribute("hotjazz:type").endswith("Field"):
                raise base.DeserializationError("Unrecognized node (%s)" %
                                                smart_unicode(child_node))

            field_node = child_node
            field_name = field_node.tagName.split(":")[-1]

            # 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:
                value = None
                try:
                    value = field.to_python(getInnerText(field_node).strip())
                except ValidationError:
                    # assuming this is a NULL value for a non-char column
                    pass
                data[field.name] = value

        # Return a DeserializedObject so that the m2m data has a place to live.
        result = base.DeserializedObject(Model(**data), m2m_data)
        return result
Example #12
0
def _get_model(model_identifier):
    """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 #13
0
    def _handle_fk_field_node(self, node, field):
        """
        Handle a field node for a ForeignKey
        """
        if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
            keys = node.getElementsByTagName('hotjazz:natural_ref')
            # If there are 'natural_ref' subelements, it must be a natural key
            if keys:
                obj_pk = self._resolve_natural_key_fk(node, field)
                return obj_pk
        # Otherwise, treat like a normal PK
        key = node.getElementsByTagName("hotjazz:pk_ref")
        if not key:
            return None
            # assuming for a moment this is actually a nullable field...
            # raise base.DeserializationError(
            #         "Expecting hotjazz:pk_ref to deserialize process field %s" % (
            #                 smart_unicode(field.name),))
        if key.length != 1:
            raise base.DeserializationError(
                "Expecting one hotjazz:pk_ref to deserialize process field %s, got %d"
                % (smart_unicode(field.name), key.length))

        field_value = getInnerText(key.item(0)).strip()
        obj_pk = field.rel.to._meta.get_field(
            field.rel.field_name).to_python(field_value)
        return obj_pk
    def _get_value(self, cell, field):
        """
        Returns the value of the cell casted to the proper field type
        :param cell:
        :param field:
        :return:
        """

        # Empty cell:
        if cell.value is None:
            return None

        # Cell with excel formula:
        if str(cell.value).startswith('='):
            raise base.DeserializationError(
                "Formulas are not supported at this time. Cell %s%s" %
                (cell.row, cell.column))

        # Process each field type:
        elif isinstance(field, models.ForeignKey):
            return field.related_model.objects.get(pk=cell.value)

        elif isinstance(field, models.AutoField):
            return cell.value

        elif isinstance(field, models.BooleanField):
            return cell.value if type(cell.value) is bool else bool(cell.value)

        elif isinstance(field, models.DateTimeField):
            # Order matters, DateTime needs to come before Date because datetime subclasses date
            # isinstance(aDateTimeInstance, DateField) is True, isinstance(aDateInstance, DateTimeField) is False
            if type(cell.value) is datetime:
                if settings.USE_TZ and timezone.is_naive(cell.value):
                    return timezone.make_aware(cell.value, pytz.UTC)
                else:
                    return cell.value
            else:
                # Handle a couple different timestamp formats -- first one is how *we* save 'em
                for pattern, format in DATETIME_FORMATS.items():
                    if re.match(pattern, str(cell.value)):
                        return datetime.strptime(str(cell.value), format)
                return str(cell.value)  # hope for the best

        elif isinstance(field, models.DateField):
            if type(cell.value) is datetime:
                return cell.value.date()
            else:
                return cell.value if type(
                    cell.value) is date else datetime.strptime(
                        cell.value, Y_M_D_FORMAT).date()

        elif isinstance(field, models.DecimalField):
            try:
                return decimal.Decimal(cell.value)
            except Exception as ex:
                print(cell.value)

        else:
            return str(cell.value)
Example #15
0
    def get_object(self, node):
        """
        Retrieve an object from the serialized unit node.

        :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

        :return: The original page translation
        :rtype: ~integreat_cms.cms.models.pages.page_translation.PageTranslation

        """
        # Get original page
        page_id = self.require_attribute(node, "original")
        try:
            page = Page.objects.get(id=page_id)
        except (ValueError, Page.DoesNotExist) as e:
            # If the id isn't a number or if no page with this id is found, check if the external file reference is given
            external_file = node.getElementsByTagName("external-file")
            if not external_file:
                # If no such reference is given, just raise the initial error
                raise e
            # Get href of external file and parse url
            page_link = (urlparse(
                self.require_attribute(external_file[0],
                                       "href")).path.strip("/").split("/"))
            logger.debug(
                "<external-file>-node found, parsed page link: %r",
                page_link,
            )
            # Expect the link to be in the format /<region_slug>/<language_slug>/[<parent_page_slug>]/<page_slug>/
            if len(page_link) < 3:
                raise base.DeserializationError(
                    "The page link of the <external-file> reference needs at least 3 segments"
                ) from e
            page_translation_slug = page_link.pop()
            region_slug, language_slug = page_link[:2]
            page = Page.objects.get(
                region__slug=region_slug,
                translations__slug=page_translation_slug,
                translations__language__slug=language_slug,
            )

        logger.debug(
            "Referenced original page: %r",
            page,
        )

        # Get target language of this file
        target_language_slug = self.require_attribute(node, "target-language")

        # Get existing target translation or create a new one
        page_translation = page.get_translation(
            target_language_slug) or PageTranslation(
                page=page,
                language=Language.objects.get(slug=target_language_slug),
            )
        return page_translation
Example #16
0
def _get_model(model_identifier):
    """
    Helper to look up a model from an "app_label.module_name" string.
    """
    registry = Base._decl_class_registry
    Model = registry.get(model_identifier, None)
    if Model is None:
        raise base.DeserializationError(u"Invalid model identifier: '%s'" \
            % model_identifier)
    return Model
Example #17
0
 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 #18
0
def _get_model(model_identifier):
    """
    Helper to look up a model from an "app_label.model_name" string.
    """
    try:
        Model = apps.get_model(*model_identifier.split("."))
    except (LookupError, TypeError):
        raise base.DeserializationError("Invalid model identifier: '%s'" %
                                        model_identifier)
    return Model
Example #19
0
    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.remote_field and isinstance(field.remote_field,
                                                 models.ManyToManyRel):
                m2m_data[field.name] = self._handle_m2m_field_node(
                    field_node, field)
            elif field.remote_field and isinstance(field.remote_field,
                                                   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)
Example #20
0
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(u"Invalid model identifier: '%s'" % model_identifier)
    return Model
Example #21
0
def _get_model(model_identifier, state):
    """
    Helper to look up a model from an "app_label.model_name" string.
    """
    try:
        if is_django_1_7:
            apps = state.render()
        else:
            apps = state.apps
        return apps.get_model(model_identifier)
    except (LookupError, TypeError):
        raise base.DeserializationError("Invalid model identifier: '%s'" %
                                        model_identifier)
Example #22
0
def Deserializer(object_list, **options):
    """Deserialize simple Python objects back into Model instances.

  It's expected that you pass the Python objects themselves (instead of a
  stream or a string) to the constructor
  """
    models.get_apps()
    for d in object_list:
        # Look up the model and starting build a dict of data for it.
        Model = python._get_model(d["model"])
        data = {}
        key = resolve_key(Model._meta.module_name, d["pk"])
        if key.name():
            data["key_name"] = key.name()
        parent = None
        if key.parent():
            parent = FakeParent(key.parent())
        m2m_data = {}

        # Handle each field
        for (field_name, field_value) in d["fields"].iteritems():
            if isinstance(field_value, str):
                field_value = smart_unicode(field_value,
                                            options.get(
                                                "encoding",
                                                settings.DEFAULT_CHARSET),
                                            strings_only=True)
            field = Model.properties()[field_name]

            if isinstance(field, db.Reference):
                # Resolve foreign key references.
                data[field.name] = resolve_key(Model._meta.module_name,
                                               field_value)
                if not data[field.name].name():
                    raise base.DeserializationError(
                        u"Cannot load Reference with "
                        "unnamed key: '%s'" % field_value)
            else:
                data[field.name] = field.validate(field_value)
        # Create the new model instance with all it's data, but no parent.
        object = Model(**data)
        # Now add the parent into the hidden attribute, bypassing the type checks
        # in the Model's __init__ routine.
        object._parent = parent
        # When the deserialized object is saved our replacement DeserializedObject
        # class will set object._parent to force the real parent model to be loaded
        # the first time it is referenced.
        yield base.DeserializedObject(object, m2m_data)
Example #23
0
    def __next__(self):
        d = self.data_it.__next__()
        
        # Look up the model using the model loading mechanism.
        model_path = None
        try:
            model_path = self.model_path_from_data(d)
            model_class = self.get_model_class(model_path)
        except base.DeserializationError:
            if self.ignore:
                return self.__next__()
            else:
                raise

        # Start building a data dictionary from the object.
        data = {}
        pk = self.get_pk_from_data(d)
        if (pk):
            try:
                data[model_class._meta.pk.attname] = self.pk_to_python(model_class, pk)
            except Exception as e:
                raise base.DeserializationError.WithData(e, model_path, pk, None)
        if model_class not in self.field_names_cache:
            self.field_names_cache[model_class] = self.field_names(model_class)
        field_names = self.field_names_cache[model_class]

        # Handle each field
        for (field_name, field_value) in self.fields_from_data(d).items():
            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)):
                try:
                    data[field.name] = field.to_python(field_value)
                except Exception as e:
                    raise base.DeserializationError("{}: ({}:pk={}) field:'{}': field_value:'{}'".format(
                        e, 
                        model_path, 
                        pk, 
                        field_name,
                        field_value
                    ))

        obj = base.build_instance(model_class, data, self.db)
        return base.DeserializedObject(obj)
    def _get_model(model_identifier):
        try:
            model = apps.get_model(model_identifier)
        except (LookupError, TypeError):
            raise base.DeserializationError(
                "Invalid model identifier: '{}'".format(model_identifier))
        else:
            for manager in managers:
                model_name = manager['model_name']
                manager_class = manager['manager_class']
                manager_instance = manager_class()

                Model = apps.get_model(model_name)
                manager_instance.model = Model
                Model._meta.default_manager = manager_instance

            return model
Example #25
0
    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)
Example #26
0
 def next(self):
     for event, node in self.event_stream:
         if event == "START_ELEMENT" and node.nodeName == "object":
             if node.hasAttribute("overwrite") and \
                node.getAttribute("overwrite") == 'no':
                 Model = self._get_model_from_node(node, "model")
                 pk = node.getAttribute("pk")
                 if not pk:
                     raise base.DeserializationError(
                         "<object> node is missing the 'pk' attribute")
                 if not node.hasAttribute("compare-by"):
                     filters = {'pk': Model._meta.pk.to_python(pk)}
                 else:  # comparing by custom field defined in the fixture
                     field = str(node.getAttribute("compare-by"))
                     value = node.getAttribute(field)
                     filters = {field: value}
                 if Model._base_manager.filter(**filters):
                     # if object is found we will not overwrite it
                     # because is marked as non overridable
                     continue
             self.event_stream.expandNode(node)
             return self._handle_object(node)
     raise StopIteration
Example #27
0
 def _get_model(model_identifier):
     try:
         return apps.get_model(model_identifier)
     except (LookupError, TypeError):
         raise base.DeserializationError(
             "Invalid model identifier: '%s'" % model_identifier)
Example #28
0
                else:
                    data[field.attname] = None

            # Handle all other fields
            else:
                try:
                    data[field.name] = field.to_python(field_value)
                except Exception as e:
                    raise base.DeserializationError.WithData(e, d['model'], d.get('pk'), field_value)

<<<<<<< HEAD
        obj = base.build_instance(Model, data, db)
=======
        obj = base.build_instance(Model, data, using)
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
        yield base.DeserializedObject(obj, m2m_data)


def _get_model(model_identifier):
<<<<<<< HEAD
    """
    Helper to look up a model from an "app_label.model_name" string.
    """
=======
    """Look up a model from an "app_label.model_name" string."""
>>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435
    try:
        return apps.get_model(model_identifier)
    except (LookupError, TypeError):
        raise base.DeserializationError("Invalid model identifier: '%s'" % model_identifier)
    model: The name of the model this key is being resolved for. Only used in
      the fourth case below (a plain key_name string).
    key_data: The data to create a key instance from. May be in four formats:
      * The str() output of a key instance. Eg. A base64 encoded string.
      * The repr() output of a key instance. Eg. A string for eval().
      * A list of arguments to pass to db.Key.from_path.
      * A single string value, being the key_name of the instance. When this
        format is used the resulting key has no parent, and is for the model
        named in the model parameter.

  Returns:
    An instance of db.Key. If the data cannot be used to create a Key instance
    an error will be raised.
  """
    if isinstance(key_data, list):
        # The key_data is a from_path sequence.
        return db.Key.from_path(*key_data)
    elif isinstance(key_data, basestring):
        if key_data.find("from_path") != -1:
            # key_data is encoded in repr(key) format
            return eval(key_data)
        else:
            try:
                # key_data encoded a str(key) format
                return db.Key(key_data)
            except datastore_types.datastore_errors.BadKeyError, e:
                # Final try, assume it's a plain key name for the model.
                return db.Key.from_path(model, key_data)
    else:
        raise base.DeserializationError(u"Invalid key data: '%s'" % key_data)
Example #30
0
    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