From 8db9bdb9e2f5fa5917a28b715e46e09a5c8ab00f Mon Sep 17 00:00:00 2001 From: j Date: Sun, 10 Jan 2016 14:40:38 +0530 Subject: [PATCH] make api query more accessible in shell --- oml/item/models.py | 7 ++++++- oml/item/query.py | 3 +-- oml/queryparser.py | 6 +++--- oml/user/models.py | 5 ++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/oml/item/models.py b/oml/item/models.py index cc3903c..2d98726 100644 --- a/oml/item/models.py +++ b/oml/item/models.py @@ -18,6 +18,7 @@ from db import MutableDict import json_pickler from .icons import icons from .person import get_sort_name +from queryparser import Parser from settings import config from utils import remove_empty_folders from websocket import trigger_event @@ -91,6 +92,10 @@ class Item(db.Model): state.db.session.commit() return item + @classmethod + def find(cls, data): + return Parser(cls, user_items, Find, Sort).find(data) + def add_user(self, user): if not user in self.users: self.users.append(user) @@ -514,7 +519,7 @@ class Find(db.Model): id = sa.Column(sa.Integer(), primary_key=True) item_id = sa.Column(sa.String(32), sa.ForeignKey('item.id')) - item = sa.orm.relationship('Item', backref=sa.orm.backref('find', lazy='dynamic')) + item = sa.orm.relationship('Item', backref=sa.orm.backref('find_', lazy='dynamic')) key = sa.Column(sa.String(200), index=True) value = sa.Column(sa.Text()) findvalue = sa.Column(sa.Text(), index=True) diff --git a/oml/item/query.py b/oml/item/query.py index 5dc5300..e3d45fc 100644 --- a/oml/item/query.py +++ b/oml/item/query.py @@ -5,7 +5,6 @@ #does not work in sqlite #from sqlalchemy.sql.expression import nullslast -from queryparser import Parser from . import models import settings import utils @@ -19,7 +18,7 @@ def parse(data): if key in data: query[key] = data[key] #print data - query['qs'] = Parser(models.Item, models.user_items).find(data) + query['qs'] = models.Item.find(data) if not 'group' in query: query['qs'] = order(query['qs'], query['sort']) return query diff --git a/oml/queryparser.py b/oml/queryparser.py index 461489e..ff92b43 100644 --- a/oml/queryparser.py +++ b/oml/queryparser.py @@ -38,10 +38,10 @@ def get_operator(op, type='str'): class Parser(object): - def __init__(self, model, user_items): + def __init__(self, model, user_items, find, sort): self._model = model - self._find = model.find.mapper.class_ - self._sort = model.sort.mapper.class_ + self._find = find + self._sort = sort self._user = model.users.mapper.class_ self._user_items = user_items self._list = model.lists.mapper.class_ diff --git a/oml/user/models.py b/oml/user/models.py index 5fb6abc..c87d670 100644 --- a/oml/user/models.py +++ b/oml/user/models.py @@ -7,7 +7,6 @@ import sqlalchemy as sa from changelog import Changelog from db import MutableDict -from queryparser import Parser import db import json_pickler import settings @@ -268,9 +267,9 @@ class List(db.Model): self.user.clear_list_cache() def get_items(self): - from item.models import Item, user_items + from item.models import Item if self.type == 'smart': - return Parser(Item, user_items).find({'query': self._query}) + return Item.find({'query': self._query}) else: return self.user.items.join(Item.lists, aliased=True).filter(List.id == self.id)