示例#1
0
    def resolve_json_id(self, json_id, allow_no_match=False):
        """
            Given an id found in scraped JSON, return a DB id for the object.

            params:
                json_id:        id from json
                allow_no_match: just return None if id can't be resolved

            returns:
                database id

            raises:
                ValueError if id couldn't be resolved
        """
        if not json_id:
            return None

        if json_id.startswith('~'):
            # keep caches of all the pseudo-ids to avoid doing 1000s of lookups during import
            if json_id not in self.pseudo_id_cache:
                spec = get_pseudo_id(json_id)
                spec = self.limit_spec(spec)

                if isinstance(spec, Q):
                    objects = self.model_class.objects.filter(spec)
                else:
                    objects = self.model_class.objects.filter(**spec)
                ids = {each.id for each in objects}
                if len(ids) == 1:
                    self.pseudo_id_cache[json_id] = ids.pop()
                    errmsg = None
                elif not ids:
                    errmsg = 'cannot resolve pseudo id to {}: {}'.format(
                        self.model_class.__name__, json_id)
                else:
                    errmsg = 'multiple objects returned for {} pseudo id {}: {}'.format(
                        self.model_class.__name__, json_id, ids)

                # either raise or log error
                if errmsg:
                    if not allow_no_match:
                        raise UnresolvedIdError(errmsg)
                    else:
                        self.error(errmsg)
                        self.pseudo_id_cache[json_id] = None

            # return the cached object
            return self.pseudo_id_cache[json_id]

        # get the id that the duplicate points to, or use self
        json_id = self.duplicates.get(json_id, json_id)

        try:
            return self.json_to_db_id[json_id]
        except KeyError:
            raise UnresolvedIdError('cannot resolve id: {}'.format(json_id))
示例#2
0
文件: base.py 项目: rshorey/pupa
    def resolve_json_id(self, json_id):
        """
            Given an id found in scraped JSON, return a DB id for the object.

            params:
                json_id:    id from json

            returns:
                database id

            raises:
                ValueError if id couldn't be resolved
        """
        if not json_id:
            return None

        if json_id.startswith('~'):
            # keep caches of all the pseudo-ids to avoid doing 1000s of lookups during import
            if json_id not in self.pseudo_id_cache:
                spec = get_pseudo_id(json_id)
                spec = self.limit_spec(spec)
                try:
                    self.pseudo_id_cache[
                        json_id] = self.model_class.objects.get(**spec).id
                except self.model_class.DoesNotExist:
                    raise UnresolvedIdError(
                        'cannot resolve pseudo id to {}: {}'.format(
                            self.model_class.__name__, json_id))
                except self.model_class.MultipleObjectsReturned:
                    raise UnresolvedIdError(
                        'multiple objects returned for pseudo id to {}: {}'.
                        format(self.model_class.__name__, json_id))

            # return the cached object
            return self.pseudo_id_cache[json_id]

        # get the id that the duplicate points to, or use self
        json_id = self.duplicates.get(json_id, json_id)

        try:
            return self.json_to_db_id[json_id]
        except KeyError:
            raise UnresolvedIdError('cannot resolve id: {}'.format(json_id))