def get_view_count(item_type: enums.ItemType = enums.ItemType.Gallery, search_query: str = "", filter_id: int = None, view_filter: enums.ViewType = enums.ViewType.Library): """ Get count of items in view Args: item_type: possible items are :py:attr:`.ItemType.Gallery`, :py:attr:`.ItemType.Collection`, :py:attr:`.ItemType.Grouping` search_query: filter item by search terms filter_id: current filter list id view_filter: ... Returns: ``` { 'count': int } ``` """ view_filter = enums.ViewType.get(view_filter) item_type = enums.ItemType.get(item_type) db_msg, db_model = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Collection, enums.ItemType.Grouping)) model_ids = search_cmd.ModelFilter().run(db_model, search_query) return message.Identity('count', {'count': len(model_ids)})
def search_tags( search_query: str = "", search_options: dict = {}, only_namespace: bool = False, only_tag: bool = False, sort_by: enums.ItemSort = None, sort_desc: bool = False, limit: int = 100, offset: int = None, ): """ Search for tags Args: search_query: search string search_options: options to apply when filtering, see :ref:`Settings` for available search options only_namespace: only search for matching namespace <not implemented yet> only_tag: only search for matching tag <not implemented yet> sort_by: either a :py:class:`.ItemSort` or a sort index sort_desc: order descending (default is ascending) limit: limit the amount of items returned offset: offset the results by n items Returns: .. code-block:: guess { namespace : [ tag message object, ...], ... } """ if search_options: search_option_names = [ x.name for x in search_cmd._get_search_options() ] for n in search_options: if n not in search_option_names: raise exceptions.APIError( utils.this_function(), "Invalid search option name '{}'".format(n)) db_model = db.NamespaceTags model_ids = search_cmd.ModelFilter().run(db_model, search_query, search_options) order_exp, group_exp, join_exp = helpers._sort_helper( sort_by, sort_desc, db_model) items = database_cmd.GetModelItems().run(db_model, model_ids, limit=limit, offset=offset, join=join_exp, order_by=order_exp, group_by=group_exp) msg = _contruct_tags_msg(items) return message.Identity('tags', msg)
def library_view(item_type: enums.ItemType = enums.ItemType.Gallery, page: int = 0, limit: int = 100, sort_by: str = "", search_query: str = "", filter_id: int = None, view_filter: enums.ViewType = enums.ViewType.Library, ctx=None): """ Fetch items from the database. Provides pagination. Args: item_type: possible items are :py:attr:`.ItemType.Gallery`, :py:attr:`.ItemType.Collection`, :py:attr:`.ItemType.Grouping` page: current page (zero-indexed) sort_by: name of column to order by ... limit: amount of items per page search_query: filter item by search terms filter_id: current filter list id view_filter: ... Returns: list of item message objects """ utils.require_context(ctx) view_filter = enums.ViewType.get(view_filter) item_type = enums.ItemType.get(item_type) db_msg, db_model = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Collection, enums.ItemType.Grouping)) items = message.List(db_model.__name__.lower(), db_msg) model_ids = search_cmd.ModelFilter().run(db_model, search_query) [ items.append(db_msg(x)) for x in database_cmd.GetModelItemByID().run( db_model, model_ids, limit=limit, offset=page * limit) ] return items
def _view_helper(item_type: enums.ItemType=enums.ItemType.Gallery, search_query: str = "", filter_id: int = None, view_filter: enums.ViewType = enums.ViewType.Library, item_id: int = None, related_type: enums.ItemType = None, search_options: dict = {}, ): if view_filter is not None: view_filter = enums.ViewType.get(view_filter) if related_type is not None: related_type = enums.ItemType.get(related_type) item_type = enums.ItemType.get(item_type) if search_options: search_option_names = [x.name for x in search_cmd._get_search_options()] for n in search_options: if n not in search_option_names: raise exceptions.APIError(utils.this_function(), "Invalid search option name '{}'".format(n)) filter_op = [] join_exp = [] parent_model = None db_msg, db_model = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Collection, enums.ItemType.Grouping)) if related_type: parent_model = db_model db_msg, db_model = related_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Page)) col = db.relationship_column(parent_model, db_model) if not col: raise exceptions.APIError( utils.this_function(), "{} has no relationship with {}".format( related_type, item_type)) if item_id is None: raise exceptions.APIError(utils.this_function(), "Missing id of parent item") if filter_id: if db_model != db.Gallery: g_col = db.relationship_column(db_model, db.Gallery) if not g_col: raise exceptions.APIError( utils.this_function(), "Cannot use {} because {} has no relationship with {}".format( enums.ItemType.GalleryFilter, related_type if related_type else item_type, enums.ItemType.Gallery)) join_exp.append(g_col) join_exp.append(db.relationship_column(db.Gallery, db.GalleryFilter)) filter_op.append(db.GalleryFilter.id == filter_id) model_ids = None if not db_model == db.Page: model_ids = search_cmd.ModelFilter().run(db_model, search_query, search_options) metatag_name = None if view_filter == enums.ViewType.Favorite: metatag_name = db.MetaTag.names.favorite elif view_filter == enums.ViewType.Inbox: metatag_name = db.MetaTag.names.inbox elif view_filter == enums.ViewType.Trash: metatag_name = db.MetaTag.names.trash if metatag_name: if hasattr(db_model, "metatags"): filter_op.append(db.MetaTag.name == metatag_name) join_exp.append(db_model.metatags) elif view_filter == enums.ViewType.Library: if hasattr(db_model, "metatags"): filter_op.append(~db_model.metatags.any(db.MetaTag.name == db.MetaTag.names.inbox)) filter_op.append(~db_model.metatags.any(db.MetaTag.name == db.MetaTag.names.trash)) elif view_filter == enums.ViewType.All: if hasattr(db_model, "metatags"): filter_op.append(~db_model.metatags.any(db.MetaTag.name == db.MetaTag.names.trash)) if related_type: filter_op.append(parent_model.id == item_id) join_exp.append(col) if len(filter_op) > 1: filter_op = db.and_op(*filter_op) elif filter_op: filter_op = filter_op[0] else: filter_op = None return view_filter, item_type, db_msg, db_model, model_ids, filter_op, join_exp, metatag_name
def search_item( item_type: enums.ItemType = enums.ItemType.Gallery, search_query: str = "", search_options: dict = {}, sort_by: enums.ItemSort = None, sort_desc: bool = False, full_search: bool = True, limit: int = 100, offset: int = None, ): """ Search for item Args: item_type: all of :py:attr:`.ItemType` except :py:attr:`.ItemType.Page` and :py:attr:`.ItemType.GalleryFilter` search_query: filter item by search terms search_options: options to apply when filtering, see :ref:`Settings` for available search options sort_by: either a :py:class:`.ItemSort` or a sort index sort_desc: order descending (default is ascending) limit: amount of items offset: offset the results by n items Returns: .. code-block:: guess [ item message object, ... ] .. seealso:: :func:`.get_sort_indexes` """ item_type = enums.ItemType.get(item_type) if search_options: search_option_names = [ x.name for x in search_cmd._get_search_options() ] for n in search_options: if n not in search_option_names: raise exceptions.APIError( utils.this_function(), "Invalid search option name '{}'".format(n)) if item_type in (enums.ItemType.Page, enums.ItemType.GalleryFilter): raise exceptions.APIError(utils.this_function(), "Unsupported itemtype {}".format(item_type)) db_msg, db_model = item_type._msg_and_model() model_ids = set() if full_search: model_ids = search_cmd.ModelFilter().run(db_model, search_query, search_options) items = message.List("items", db_msg) order_exp, group_exp, join_exp = helpers._sort_helper( sort_by, sort_desc, db_model) [ items.append(db_msg(x)) for x in database_cmd.GetModelItems().run(db_model, model_ids, limit=limit, offset=offset, join=join_exp, order_by=order_exp, group_by=group_exp) ] return items