def lookup(cls, dataset, data, account, match_value=True, readonly=False): data = LinkLookupSchema().to_python(data) if match_value: value = Value.by_value(dataset, data["key"]) if value is not None: return value else: value = None link = cls.by_key(dataset, data["key"]) if link is not None: return link choices = match_op(data["key"], dataset) choices = filter(lambda (c, v, s): s > 99.9, choices) if len(choices) == 1: c, value, s = choices.pop() value = Value.by_id(dataset, value) if readonly: return value link = cls() link.creator = account link.dataset = dataset link.value = value link.is_matched = value is not None link.key = data["key"] link.data = data["data"] db.session.add(link) db.session.flush() return link