def get_subject(self, key, details=False, offset=0, limit=12, sort='first_publish_year desc', **filters): meta = self.get_meta(key) q = self.make_query(key, filters) subject_type = meta.name name = meta.path.replace("_", " ") if details: kw = self.query_optons_for_details() else: kw = {} from search import work_search result = work_search(q, offset=offset, limit=limit, sort=sort, **kw) for w in result.docs: w.ia = w.ia and w.ia[0] or None if not w.get('public_scan') and w.ia and w.get('lending_edition'): doc = web.ctx.site.store.get("ebooks/books/" + w['lending_edition']) or {} w['checked_out'] = doc.get("borrowed") == "true" # XXX-Anand: Oct 2013 # Somewhere something is broken, work keys are coming as OL1234W/works/ # Quick fix it solve that issue. if w.key.endswith("/works/"): w.key = "/works/" + w.key.replace("/works/", "") subject = Subject( key=key, name=name, subject_type=subject_type, work_count = result['num_found'], works=result['docs'] ) if details: subject.ebook_count = dict(result.facets["has_fulltext"]).get("true", 0) #subject.ebook_count = self.get_ebook_count(meta.name, q[meta.facet_key], q.get('publish_year')) subject.subjects = result.facets["subject_facet"] subject.places = result.facets["place_facet"] subject.people = result.facets["person_facet"] subject.times = result.facets["time_facet"] subject.authors = result.facets["author_facet"] subject.publishers = result.facets["publisher_facet"] subject.languages = result.facets['language'] # Ignore bad dates when computing publishing_history # year < 1000 or year > current_year+1 are considered bad dates current_year = datetime.datetime.utcnow().year subject.publishing_history = [[year, count] for year, count in result.facets["publish_year"] if 1000 < year <= current_year+1] # strip self from subjects and use that to find exact name for i, s in enumerate(subject[meta.key]): if "key" in s and s.key.lower() == key.lower(): subject.name = s.name; subject[meta.key].pop(i) break return subject
def get_subject(self, key, details=False, offset=0, limit=12, sort="first_publish_year desc", **filters): meta = self.get_meta(key) q = self.make_query(key, filters) subject_type = meta.name name = meta.path.replace("_", " ") if details: kw = self.query_optons_for_details() else: kw = {} from search import work_search result = work_search(q, offset=offset, limit=limit, sort=sort, **kw) for w in result.docs: w.ia = w.ia and w.ia[0] or None if not w.get("public_scan") and w.ia and w.get("lending_edition"): doc = web.ctx.site.store.get("ebooks/books/" + w["lending_edition"]) or {} w["checked_out"] = doc.get("borrowed") == "true" # XXX-Anand: Oct 2013 # Somewhere something is broken, work keys are coming as OL1234W/works/ # Quick fix it solve that issue. if w.key.endswith("/works/"): w.key = "/works/" + w.key.replace("/works/", "") subject = Subject( key=key, name=name, subject_type=subject_type, work_count=result["num_found"], works=result["docs"] ) if details: subject.ebook_count = dict(result.facets["has_fulltext"]).get("true", 0) # subject.ebook_count = self.get_ebook_count(meta.name, q[meta.facet_key], q.get('publish_year')) subject.subjects = result.facets["subject_facet"] subject.places = result.facets["place_facet"] subject.people = result.facets["person_facet"] subject.times = result.facets["time_facet"] subject.authors = result.facets["author_facet"] subject.publishers = result.facets["publisher_facet"] subject.languages = result.facets["language"] # Ignore bad dates when computing publishing_history # year < 1000 or year > current_year+1 are considered bad dates current_year = datetime.datetime.utcnow().year subject.publishing_history = [ [year, count] for year, count in result.facets["publish_year"] if 1000 < year <= current_year + 1 ] # strip self from subjects and use that to find exact name for i, s in enumerate(subject[meta.key]): if "key" in s and s.key.lower() == key.lower(): subject.name = s.name subject[meta.key].pop(i) break return subject
def get_subject(self, key, details=False, offset=0, limit=12, sort='first_publish_year desc', **filters): meta = self.get_meta(key) q = self.make_query(key, filters) subject_type = meta.name name = meta.path.replace("_", " ") if details: kw = self.query_optons_for_details() else: kw = {} from search import work_search result = work_search(q, offset=offset, limit=limit, sort=sort, **kw) for w in result.docs: w.ia = w.ia and w.ia[0] or None subject = Subject(key=key, name=name, subject_type=subject_type, work_count=result['num_found'], works=result['docs']) if details: #subject.ebook_count = dict(result.facets["has_fulltext"]).get("true", 0) subject.ebook_count = self.get_ebook_count(meta.name, q[meta.facet_key], q.get('publish_year')) subject.subjects = result.facets["subject_facet"] subject.places = result.facets["place_facet"] subject.people = result.facets["person_facet"] subject.times = result.facets["time_facet"] subject.authors = result.facets["author_facet"] subject.publishers = result.facets["publisher_facet"] subject.publishing_history = [[ year, count ] for year, count in result.facets["publish_year"] if year > 1000] # strip self from subjects and use that to find exact name for i, s in enumerate(subject[meta.key]): if s.key.lower() == key.lower(): subject.name = s.name subject[meta.key].pop(i) break return subject
def get_subject(self, key, details=False, offset=0, limit=12, sort='first_publish_year desc', **filters): meta = self.get_meta(key) q = self.make_query(key, filters) subject_type = meta.name name = meta.path.replace("_", " ") if details: kw = self.query_optons_for_details() else: kw = {} from search import work_search result = work_search(q, offset=offset, limit=limit, sort=sort, **kw) for w in result.docs: w.ia = w.ia and w.ia[0] or None if not w.get('public_scan') and w.ia and w.get('lending_edition'): doc = web.ctx.site.store.get("ebooks/books/" + w['lending_edition']) or {} w['checked_out'] = doc.get("borrowed") == "true" subject = Subject( key=key, name=name, subject_type=subject_type, work_count = result['num_found'], works=result['docs'] ) if details: subject.ebook_count = dict(result.facets["has_fulltext"]).get("true", 0) #subject.ebook_count = self.get_ebook_count(meta.name, q[meta.facet_key], q.get('publish_year')) subject.subjects = result.facets["subject_facet"] subject.places = result.facets["place_facet"] subject.people = result.facets["person_facet"] subject.times = result.facets["time_facet"] subject.authors = result.facets["author_facet"] subject.publishers = result.facets["publisher_facet"] subject.languages = result.facets['language'] subject.publishing_history = [[year, count] for year, count in result.facets["publish_year"] if year > 1000] # strip self from subjects and use that to find exact name for i, s in enumerate(subject[meta.key]): if "key" in s and s.key.lower() == key.lower(): subject.name = s.name; subject[meta.key].pop(i) break return subject
def get_subject(key, details=False, offset=0, limit=12, **filters): """Returns data related to a subject. By default, it returns a storage object with key, name, work_count and works. The offset and limit arguments are used to get the works. >>> get_subject("/subjects/Love") #doctest: +SKIP { "key": "/subjects/Love", "name": "Love", "work_count": 5129, "works": [...] } When details=True, facets and ebook_count are additionally added to the result. >>> get_subject("/subjects/Love", details=True) #doctest: +SKIP { "key": "/subjects/Love", "name": "Love", "work_count": 5129, "works": [...], "ebook_count": 94, "authors": [ { "count": 11, "name": "Plato.", "key": "/authors/OL12823A" }, ... ], "subjects": [ { "count": 1168, "name": "Religious aspects", "key": "/subjects/religious aspects" }, ... ], "times": [...], "places": [...], "people": [...], "publishing_history": [[1492, 1], [1516, 1], ...], "publishers": [ { "count": 57, "name": "Sine nomine" }, ... ] } Optional arguments limit and offset can be passed to limit the number of works returned and starting offset. Optional arguments has_fulltext and published_in can be passed to filter the results. """ m = web.re_compile(r'/subjects/(place:|time:|person:|)(.+)').match(key) if m: prefix = "/subjects/" + m.group(1) path = m.group(2) else: return None meta = finddict(SUBJECTS, prefix=prefix) q = {meta.facet_key: str_to_key(path).lower()} subject_type = meta.name name = path.replace("_", " ") def facet_wrapper(facet, value, count): if facet == "publish_year": return [int(value), count] elif facet == "publisher_facet": return web.storage(name=value, count=count) elif facet == "author_facet": author = read_author_facet(value) # FIXME return web.storage(name=author[1], key="/authors/" + author[0], count=count) elif facet in ["subject_facet", "person_facet", "place_facet", "time_facet"]: return web.storage(key=finddict(SUBJECTS, facet=facet).prefix + str_to_key(value).replace(" ", "_"), name=value, count=count) elif facet == "has_fulltext": return [value, count] else: return web.storage(name=value, count=count) # search for works from search import work_search kw = {} if details: kw['facets'] = [ {"name": "author_facet", "sort": "count"}, "language", "publisher_facet", {"name": "publish_year", "limit": -1}, "subject_facet", "person_facet", "place_facet", "time_facet", "has_fulltext"] kw['facet.mincount'] = 1 kw['facet.limit'] = 25 kw['facet_wrapper'] = facet_wrapper if filters: if filters.get("has_fulltext") == "true": q['has_fulltext'] = "true" if filters.get("publish_year"): q['publish_year'] = filters['publish_year'] result = work_search(q, offset=offset, limit=limit, sort="edition_count desc", **kw) for w in result.docs: w.ia = w.ia and w.ia[0] or None subject = web.storage( key=key, name=name, subject_type=subject_type, work_count = result['num_found'], works=result['docs'] ) if details: subject.ebook_count = dict(result.facets["has_fulltext"]).get("true", 0) subject.subjects = result.facets["subject_facet"] subject.places = result.facets["place_facet"] subject.people = result.facets["person_facet"] subject.times = result.facets["time_facet"] subject.authors = result.facets["author_facet"] subject.publishers = result.facets["publisher_facet"] subject.publishing_history = [[year, count] for year, count in result.facets["publish_year"] if year > 1000] # strip self from subjects and use that to find exact name for i, s in enumerate(subject[meta.key]): if s.key.lower() == key.lower(): subject.name = s.name; subject[meta.key].pop(i) break return subject