Esempio n. 1
0
    def set(self, instance, value, **kwargs):
        """
        The passed in object should be a records object, or a sequence of dictionaries
        About link data:
          * interpretations:
            * if data not starts with standard protocol names (http://, ftp://) than
              *uid* field data will be used as reference
          * record removed if:
            * no data;
            * data contains UID of not existent object
        About title:
          * if there is UID of existent object and record has same title to the original
            object - title will not be saved.
        """
        catalog = getToolByName(instance, "uid_catalog")

        if value is None:
            value = ()

        if not isinstance(value, (ListType, TupleType)):
            value = value,

        result = []
        for row in value:
            data = {}
            for key in [
                    key for key in set(row)
                    if not (key.startswith('_') or key.endswith('_'))
            ]:
                data[key] = str(row[key]).strip()
            uid = str(row.get("uid", "")).strip()
            link = str(row.get("link", "")).strip()
            title = str(row.get("title", ""))

            if not title == "":
                data["title"] = title

            if link == "":
                continue
            elif self.isRemoteURL(link):
                data["link"] = urlparse.urlunparse(urlparse.urlparse(link))
            else:
                if uid == '':
                    continue

                brains = catalog(UID=uid)
                if len(brains) == 0:
                    continue
                # Found objects with pointed UID
                brain = brains[0]
                data["uid"] = uid
                # Fix title for uid
                if data.get("title", "") == getattr(brain, "Title", ""):
                    data['title'] = ""
            result.append(data)

        DataGridField.set(self, instance, result, **kwargs)

        uids = [r["uid"] for r in result if r.get("uid")]
        ReferenceField.set(self, instance, uids, **kwargs)
    def set(self, instance, value, **kwargs):
        """The passed in object should be a records object, or a sequence of
        dictionaries

        About link data:
          * interpretations:
            * if data not starts with standard protocol names (http://, ftp://)
              than *uid* field data will be used as reference
          * record removed if:
            * no data;
            * data contains UID of not existent object

        About title:
          * if there is UID of existent object and record has same title to the
            original object - title will not be saved.
        """
        catalog = getToolByName(instance, "uid_catalog")

        if value is None:
            value = ()

        if not isinstance(value, (ListType, TupleType)):
            value = value,

        result = []
        for row in value:
            data = {}
            for key in [key for key in set(row)
                        if not(key.startswith('_') or key.endswith('_'))]:
                data[key] = str(encode(row[key], self)).strip()
            uid = str(row.get("uid", "")).strip()
            link = str(row.get("link", "")).strip()
            title = str(encode(row.get("title", ""), self))

            if not title == "":
                data["title"] = title

            if link == "":
                continue
            elif self.isRemoteURL(link):
                data["link"] = urlparse.urlunparse(urlparse.urlparse(link))
            else:
                if uid == '':
                    continue

                brains = catalog(UID=uid)
                if len(brains) == 0:
                    continue
                # Found objects with pointed UID
                brain = brains[0]
                data["uid"] = uid
                # Fix title for uid
                if data.get("title", "") == getattr(brain, "Title", ""):
                    data['title'] = ""
            result.append(data)

        DataGridField.set(self, instance, result, **kwargs)

        uids = [r["uid"] for r in result if r.get("uid")]
        ReferenceField.set(self, instance, uids, **kwargs)