def get_item(item_type: enums.ItemType = enums.ItemType.Gallery, item_id: int = 0): """ Get item Args: item_type: type of item to get item_id: id of item Returns: item message object """ if not item_id: raise exceptions.APIError( utils.this_function(), f"A valid item id is required, not {item_id}") item_type = enums.ItemType.get(item_type) db_msg, db_model = item_type._msg_and_model() item = database_cmd.GetModelItems().run(db_model, {item_id})[0] if not item: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format(item_type.name, item_id)) return db_msg(item)
def add_to_filter(gallery_id: int = 0, item_id: int = 0, item: dict = {}): """ Add a gallery to a galleryfilter Args: gallery_id: id of gallery item_id: id of existing galleryfilter, mutually exclusive with ``item`` parameter item: filter message object, mutually exclusive with ``item_id`` parameter Returns: bool whether gallery was added to filter or not """ if not gallery_id: raise exceptions.APIError(utils.this_function(), "gallery_id must be a valid gallery id") g = database_cmd.GetModelItems().run(db.Gallery, {gallery_id}) if not g: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format( enums.ItemType.Gallery.name, gallery_id)) g = g[0] if item_id: p = database_cmd.GetModelItems().run(db.GalleryFilter, {item_id}) if not p: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format( enums.ItemType.GalleryFilter.name, item_id)) p = p[0] elif item: p = message.GalleryFilter.from_json(item) g.filters.append(p) s = constants.db_session() s.add(g) s.commit() return message.Identity("status", True)
def remove_from_filter(gallery_id: int = 0, item_id: int = 0): """ Remove a gallery from a galleryfilter Args: gallery_id: id of gallery item_id: id of existing galleryfilter Returns: bool whether gallery was removed from filter or not """ if not gallery_id: raise exceptions.APIError(utils.this_function(), "gallery_id must be a valid gallery id") g = database_cmd.GetModelItems().run(db.Gallery, {gallery_id}) if not g: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format( enums.ItemType.Gallery.name, gallery_id)) g = g[0] p = database_cmd.GetModelItems().run(db.GalleryFilter, {item_id}) if not p: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format( enums.ItemType.GalleryFilter.name, item_id)) p = p[0] g.filters.remove(p) s = constants.db_session() s.add(g) s.commit() return message.Identity("status", True)
def update_metatags(item_type: enums.ItemType = enums.ItemType.Gallery, item_id: int = 0, metatags: dict = {}): """ Update metatags for an item Args: item_type: possible items are :py:attr:`.ItemType.Gallery`, :py:attr:`.ItemType.Page`, :py:attr:`.ItemType.Artist`, :py:attr:`.ItemType.Collection` item_id: id of item metatag: a dict of ``{ metatag_name : bool }`` Returns: bool indicating whether metatags were updated """ if not item_id: raise exceptions.APIError(utils.this_function(), "item_id must be a valid item id") item_type = enums.ItemType.get(item_type) _, db_item = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Collection, enums.ItemType.Page, enums.ItemType.Artist)) t = database_cmd.GetModelItems().run(db_item, {item_id}) if not t: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "{} with item id '{}' not found".format(item_type, item_id)) t = t[0] mtags = {} anames = db.MetaTag.all_names() for m, v in metatags.items(): if m not in anames: raise exceptions.APIError(utils.this_function(), f"Metatag name '{m}' does not exist") mtags[m] = v st = True if t: t.update("metatags", mtags) db.object_session(t).commit() else: st = False return message.Identity('status', st)
def open_gallery(item_id: int = 0, item_type: enums.ItemType = enums.ItemType.Gallery, viewer_args: str = None): """ Open a gallery or page in an external viewer Args: item_id: id of item item_type: possible items are :py:attr:`.ItemType.Gallery`, :py:attr:`.ItemType.Page` viewer_args: commandline arguments to supply the viewer, overriding the default viewer arguments specified in settings Returns: bool indicating if item was successfully opened """ item_type = enums.ItemType.get(item_type) _, db_model = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Page)) kwargs = {} if item_type == enums.ItemType.Page: p = database_cmd.GetModelItems().run(db_model, {item_id}, columns=(db_model.gallery_id, db_model.number)) else: p = database_cmd.GetModelItems().run(db_model, {item_id}) if not p: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "{} with item id '{}' not found".format(item_type, item_id)) p = p[0] if item_type == enums.ItemType.Page: kwargs['gallery_or_id'] = p[0] kwargs['number'] = p[1] else: kwargs["gallery_or_id"] = p if viewer_args: kwargs['args'] = tuple(x.strip() for x in viewer_args.split()) opened = gallery_cmd.OpenGallery().run(**kwargs) return message.Identity("status", opened)
def update_item_tags(item_type: enums.ItemType = enums.ItemType.Gallery, item_id: int=0, tags: dict={}): """ Update tags on an item Args: item_type: possible items are :attr:`.ItemType.Gallery`, :attr:`.ItemType.Page`, :attr:`.ItemType.Grouping`, :attr:`.ItemType.Collection` item_id: id of item to update tags for tags: tags Returns: bool whether tags were updated or not """ if not item_id: raise exceptions.APIError(utils.this_function(), "item_id must be a valid item id") item_type = enums.ItemType.get(item_type) _, db_item = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Collection, enums.ItemType.Grouping, enums.ItemType.Page)) db_obj = database_cmd.GetModelItems().run(db_item, {item_id}) if not db_obj: raise exceptions.DatabaseItemNotFoundError(utils.this_function(), "'{}' with id '{}' was not found".format(item_type.name, item_id)) db_obj = db_obj[0] tags = _decontruct_tags_msg(tags) s = constants.db_session() with db.no_autoflush(s): s.add(db_obj) db_obj.tags = tags s.commit() return message.Identity("status", True)
def source_exists(item_type: enums.ItemType = enums.ItemType.Gallery, item_id: int = 0, check_all: bool = False): """ Check if gallery/page source exists on disk Args: item_type: possible items are :py:attr:`.ItemType.Gallery`, :py:attr:`.ItemType.Page` item_id: id of item check_all: goes through all pages and checks them, default behaviour is to only check parent files/folders. Only relevant for :py:attr:`.ItemType.Gallery` Returns: .. code-block:: guess { 'exists' : bool 'missing' : [ {'id': int, 'item_type': item_type}, ... ] } """ item_type = enums.ItemType.get(item_type) _, db_model = item_type._msg_and_model( (enums.ItemType.Gallery, enums.ItemType.Page)) if item_type == enums.ItemType.Page: item = database_cmd.GetModelItems().run(db_model, {item_id}, columns=(db.Page.path, )) elif item_type == enums.ItemType.Gallery: item = database_cmd.GetModelItems().run( db_model, {item_id}, columns=(db.Gallery.single_source, )) if not item: raise exceptions.DatabaseItemNotFoundError( utils.this_function(), "'{}' with id '{}' was not found".format(item_type.name, item_id)) else: item = item[0] paths = {} not_empty = True if item_type == enums.ItemType.Page: paths[item_id] = (item[0], item_type.value) elif item_type == enums.ItemType.Gallery: s = constants.db_session() if item and not check_all: p = s.query(db.Page.path).filter(db.Gallery.id == item_id).first() if p: paths[item_id] = (os.path.split(p[0])[0], item_type.value) else: not_empty = True else: ps = s.query( db.Page.id, db.Page.path).filter(db.Page.gallery_id == item_id).all() for p in ps: paths[p[0]] = (p[1], enums.ItemType.Page.value) not_empty = bool(ps) missing = [] for t_id in paths: src, t_type = paths[t_id] try: e = io_cmd.CoreFS(src).exists except exceptions.ArchiveExistError: e = False if not e: missing.append({'id': t_id, 'item_type': t_type}) return message.Identity("exists", { 'exists': not missing and not_empty, 'missing': missing })