diff --git a/config.json b/config.json index ad54687..2b2a059 100644 --- a/config.json +++ b/config.json @@ -233,6 +233,13 @@ "format": {"type": "percent", "args": [1, 0]}, "sort": true }, + { + "id": "sharemetadata", + "title": "Share Metadata", + "type": "boolean", + "columnWidth": 144, + "sort": true + }, { "id": "random", "title": "Random", diff --git a/oml/item/models.py b/oml/item/models.py index c217a27..8fc82de 100644 --- a/oml/item/models.py +++ b/oml/item/models.py @@ -176,6 +176,8 @@ class Item(db.Model): value = ''.join(value) value = ox.get_sort_title(value) value = utils.sort_title(value).lower() + elif sort_type == 'boolean': + pass else: if isinstance(value, list): value = '\n'.join(value) @@ -203,11 +205,16 @@ class Item(db.Model): keys = [] for key in config['itemKeys']: - if key.get('find') or key.get('filter') or key.get('type') in [['string'], 'string']: + if key.get('find') or \ + key.get('filter') or key.get('type') in [['string'], 'string'] or \ + (key.get('type') == 'boolean' and key.get('sort')): value = self.json().get(key['id'], None) if key.get('filterMap') and value: value = re.compile(key.get('filterMap')).findall(value) if value: value = value[0] + if key.get('type') == 'boolean': + value = True if value else False + value = str(value).lower() if value: keys.append(key['id']) if isinstance(value, dict): @@ -528,6 +535,8 @@ for key in config['itemKeys']: col = sa.Column(sa.Float(), index=True) elif sort_type == 'date': col = sa.Column(sa.DateTime(), index=True) + elif sort_type == 'boolean': + col = sa.Column(sa.Boolean(), index=True) else: col = sa.Column(sa.String(1000), index=True) setattr(Sort, '%s' % key['id'], col) diff --git a/oml/queryparser.py b/oml/queryparser.py index ff92b43..d492c27 100644 --- a/oml/queryparser.py +++ b/oml/queryparser.py @@ -98,9 +98,13 @@ class Parser(object): q = ~q return q elif key_type == 'boolean': - q = getattr(self._model, 'find_%s' % k) == v - if exclude: - q = ~q + v = str(v).lower() + v = v == 'true' + vk = getattr(self._sort, k) + q = operators.eq(vk, v) + ids = self._model.query.join(self._sort).filter(q).options(load_only('id')) + in_op = operators.notin_op if exclude else operators.in_op + q = in_op(self._model.id, ids) return q elif key_type in ("string", "text"): if isinstance(v, str): diff --git a/oml/update.py b/oml/update.py index d6e5bd9..0bdc2b2 100644 --- a/oml/update.py +++ b/oml/update.py @@ -389,7 +389,8 @@ def migrate_4(): def migrate_5(): db.run_sql([ 'DROP INDEX IF EXISTS user_metadata_index', - 'CREATE UNIQUE INDEX user_metadata_index ON user_metadata(item_id, user_id)' + 'CREATE UNIQUE INDEX user_metadata_index ON user_metadata(item_id, user_id)', + 'UPDATE sort SET sharemetadata = 0', ]), with db.session() as session: import user.models