def save_tag_relationships(object_id, model, tags=None, new=False): if isinstance(tags, (unicode, str)): tags = tags.split(',') if not isinstance(tags, list): return False tags = [tag.strip() for tag in tags if tag] tags = web.uniq(tags) if not tags: return False t = db.transaction() try: new_tag_ids = new_tags(tags) if new: add_tag_ids = new_tag_ids else: old_tag_relationships = db.select('object_tag_relationships', what='tag_id', where='object_id=$object_id AND model=$model', vars={'object_id': object_id, 'model': model}) old_tag_ids = [tag['tag_id'] for tag in old_tag_relationships] del_tag_ids = set(old_tag_ids) - set(new_tag_ids) add_tag_ids = set(new_tag_ids) - set(old_tag_ids) if del_tag_ids: db.delete('object_tag_relationships', where='object_id=$object_id AND model=$model AND tag_id IN $del_tag_ids', vars={'object_id': object_id, 'model': model, 'del_tag_ids': list(del_tag_ids)}) if add_tag_ids: db.multiple_insert('object_tag_relationships', [{'tag_id': tag_id, 'object_id': object_id, 'model': model} for tag_id in add_tag_ids]) except: t.rollback() return False else: t.commit() return True
def get_book_provider( ed_or_solr: Union[Edition, dict]) -> Optional[AbstractBookProvider]: # On search results, we want to display IA copies first. # Issue is that an edition can be provided by multiple providers; we can easily # choose the correct copy when on an edition, but on a solr record, with all copies # of all editions aggregated, it's more difficult. # So we do some ugly ocaid sniffing to try to guess :/ Idea being that we ignore # OCAIDs that look like they're from other providers. prefer_ia = not isinstance(ed_or_solr, Edition) if prefer_ia: ia_ocaids = [ ocaid # Subjects/publisher pages have ia set to a specific value :/ for ocaid in uniq(ia_provider.get_identifiers(ed_or_solr) or []) if not is_non_ia_ocaid(ocaid) ] prefer_ia = bool(ia_ocaids) provider_order = get_provider_order(prefer_ia) for provider in provider_order: if provider.get_identifiers(ed_or_solr): return provider # No luck return None
def save_category_relationships(object_id, category_ids, new=False): if not isinstance(category_ids, list): return False category_ids = web.uniq(category_ids) if not category_ids: return False t = db.transaction() try: if new: add_category_ids = category_ids else: old_category_relationships = db.select('object_category_relationships', what='category_id', where='object_id=$object_id', vars={'object_id': object_id}) old_category_ids = [category['category_id'] for category in old_category_relationships] del_category_ids = set(old_category_ids) - set(category_ids) add_category_ids = set(category_ids) - set(old_category_ids) if del_category_ids: db.delete('object_category_relationships', where='object_id=$object_id AND category_id IN $del_category_ids', vars={'object_id': object_id, 'del_category_ids': list(del_category_ids)}) if add_category_ids: db.multiple_insert('object_category_relationships', [{'category_id': category_id, 'object_id': object_id} for category_id in add_category_ids]) except: t.rollback() return False else: t.commit() return True
def get_provider_order(prefer_ia=False) -> list[AbstractBookProvider]: default_order = prefer_ia_provider_order if prefer_ia else PROVIDER_ORDER provider_order = default_order provider_overrides = None # Need this to work in test environments if 'env' in web.ctx: provider_overrides = web.input(providerPref=None, _method='GET').providerPref if provider_overrides: new_order: list[AbstractBookProvider] = [] for name in provider_overrides.split(','): if name == '*': new_order += default_order else: provider = get_book_provider_by_name(name) if not provider: # TODO: Show the user a warning somehow continue new_order.append(provider) new_order = uniq(new_order + default_order) if new_order: provider_order = new_order return provider_order
""" Check if the ocaid "looks like" it's from another provider """ providers = (provider for provider in PROVIDER_ORDER if provider.short_name != 'ia') return any(provider.is_own_ocaid(ocaid) for provider in providers) def get_book_provider_by_name( short_name: str) -> Optional[AbstractBookProvider]: return next((p for p in PROVIDER_ORDER if p.short_name == short_name), None) ia_provider = cast(InternetArchiveProvider, get_book_provider_by_name('ia')) prefer_ia_provider_order = uniq([ia_provider, *PROVIDER_ORDER]) def get_provider_order(prefer_ia=False) -> list[AbstractBookProvider]: default_order = prefer_ia_provider_order if prefer_ia else PROVIDER_ORDER provider_order = default_order provider_overrides = web.input(providerPref=None).providerPref if provider_overrides: new_order: list[AbstractBookProvider] = [] for name in provider_overrides.split(','): if name == '*': new_order += default_order else: provider = get_book_provider_by_name(name) if not provider: