def POST(self): if not support_db: return "Couldn't initialise connection to support database" form = web.input() email = form.get("email", "") topic = form.get("topic", "") description = form.get("question", "") url = form.get("url", "") user = accounts.get_current_user() useragent = web.ctx.env.get("HTTP_USER_AGENT","") if not all([email, topic, description]): return "" c = support_db.create_case(creator_name = user and user.get_name() or "", creator_email = email, creator_useragent = useragent, creator_username = user and user.get_username() or "", subject = topic, description = description, url = url, assignee = config.get("support_case_default_address","*****@*****.**")) # Send an email to the creator of the case subject = "Case #%s: %s"%(c.caseno, topic) message = render_template("email/support_case", c) web.sendmail(config.get("support_case_control_address","*****@*****.**"), email, subject, message) return render_template("email/case_created", c)
def POST(self, key): i = web.input() if "_delete" in i: doc = web.ctx.site.store.get(key) if doc: doc['current_status'] = "deleted" web.ctx.site.store[doc['_key']] = doc add_flash_message("info", "The requested library has been deleted.") raise web.seeother("/libraries/dashboard") i._key = web.rstrips(i.key, "/").replace(" ", "_") page = libraries_dashboard()._create_pending_library(i) if web.ctx.site.get(page.key): add_flash_message("error", "URL %s is already used. Please choose a different one." % page.key) return render_template("type/library/edit", page) elif not i.key.startswith("/libraries/"): add_flash_message("error", "The key must start with /libraries/.") return render_template("type/library/edit", page) page._save() doc = web.ctx.site.store.get(key) if doc: doc['current_status'] = "approved" web.ctx.site.store[doc['_key']] = doc raise web.seeother(page.key)
def GET(self, key): page = subjects.get_subject(key, details=True) page.name = get_language_name(key.split("/")[-1]) if page.work_count == 0: return render_template("languages/notfound.tmpl", key) return render_template("languages/view", page)
def GET(self, key): key = key.replace("_", " ") page = subjects.get_subject(key, details=True) if page.work_count == 0: return render_template("publishers/notfound.tmpl", key) return render_template("publishers/view", page)
def GET(self, key): page = subjects.get_subject(key, details=True) page.name = get_language_name(key.split("/")[-1]) if page.work_count == 0: web.ctx.status = "404 Not Found" return render_template('languages/notfound.tmpl', key) return render_template("languages/view", page)
def test_returncart_template(self, render_template, mock_site, olconfig): html = unicode(render_template("home/returncart")) assert html.strip() == "" mock_site.quicksave("/people/foo/lists/OL1L", "/type/list") olconfig.setdefault("home", {})['returncart_list'] = "/people/foo/lists/OL1L" html = unicode(render_template("home/returncart", "/people/foo/lists/OL1L")) assert "Return Cart" in html
def test_lending_template(self, render_template, mock_site, olconfig): html = unicode(render_template("home/lendinglibrary")) assert html.strip() == "" mock_site.quicksave("/people/foo/lists/OL1L", "/type/list") olconfig.setdefault("home", {})['lending_list'] = "/people/foo/lists/OL1L" html = unicode(render_template("home/lendinglibrary", "/people/foo/lists/OL1L")) assert "Lending Library" in html
def GET(self, key): key = key.replace("_", " ") page = subjects.get_subject(key, details=True) if page.work_count == 0: web.ctx.status = "404 Not Found" return render_template('publishers/notfound.tmpl', key) return render_template("publishers/view", page)
def GET(self, key): nkey = self.normalize_key(key) if nkey != key: raise web.redirect(nkey) page = get_subject(key, details=True) if page.work_count == 0: return render_template("subjects/notfound.tmpl", key) return render_template("subjects", page)
def GET(self, key): nkey = self.normalize_key(key) if nkey != key: raise web.redirect(nkey) page = get_subject(key, details=True) if not page or page.work_count == 0: web.ctx.status = "404 Not Found" return render_template('subjects/notfound.tmpl', key) return render_template("subjects", page)
def POST_old(self): i = web.input(email='', url='', question='') fields = web.storage({ 'email': i.email, 'irl': i.url, 'comment': i.question, 'sent': datetime.datetime.utcnow(), 'browser': web.ctx.env.get('HTTP_USER_AGENT', '') }) msg = render_template('email/spam_report', fields) web.sendmail(i.email, config.report_spam_address, msg.subject, str(msg)) print config.report_spam_address return render_template("support", done = True)
def POST(self): i = web.input() doc = dict(i) errors = {} if not doc.get('name'): errors['name'] = 'name is a required field' addresses = doc.get('addresses', '').strip() if addresses: for line in addresses.splitlines(): tokens = line.split('|') if len(tokens) != 9: errors['addresses'] = 'address field is invalid' break latlong = tokens[8] if ',' not in latlong or len(latlong.split(',')) != 2: errors['addresses'] = 'Lat, Long is invalid' break else: errors['addresses'] = 'addresses is a required field' ip_ranges = doc.get('ip_ranges', '').strip() if ip_ranges: bad = find_bad_ip_ranges(ip_ranges) if bad: errors['ip_ranges'] = 'Invalid IP range(s): ' + '; '.join(bad) else: errors['ip_ranges'] = 'IP ranges is a required field' if errors: return render_template("libraries/add", errors) seq = web.ctx.site.seq.next_value("libraries") doc.update({ "_key": "libraries/pending-%d" % seq, "type": "library", "current_status": "pending", "registered_on": datetime.datetime.utcnow().isoformat() }) web.ctx.site.store[doc['_key']] = doc self.sendmail(i.contact_email, render_template("libraries/email_confirmation")) if config.get("libraries_admin_email"): self.sendmail(config.libraries_admin_email, render_template("libraries/email_notification", i)) return render_template("libraries/postadd")
def GET(self, caseid): if not support_db: return render_template("admin/cases", None, None, True, False) case = support_db.get_case(caseid) date_pretty_printer = lambda x: x.strftime("%B %d, %Y") if len(case.history) == 1: last_email = case.description else: last_email = case.history[-1]['text'] try: last_email = "\n".join(" > %s"%x for x in last_email.split("\n")) + "\n\n" except Exception: last_email = "" admins = ((x.get_email(), x.get_name(), x.get_email() == case.assignee) for x in accounts.get_group("admin").members) return render_template("admin/case", case, last_email, admins, date_pretty_printer)
def POST(self, caseid): if not support_db: return render_template("admin/cases", None, None, True, False) case = support_db.get_case(caseid) form = web.input() action = form.get("button","") {"SEND REPLY" : self.POST_sendreply, "UPDATE" : self.POST_update, "CLOSE CASE" : self.POST_closecase, "REOPEN CASE": self.POST_reopencase}[action](form,case) date_pretty_printer = lambda x: x.strftime("%B %d, %Y") last_email = case.history[-1]['text'] last_email = "\n".join("> %s"%x for x in textwrap.wrap(last_email)) admins = ((x.get_email(), x.get_name(), x.get_email() == case.assignee) for x in accounts.get_group("admin").members) return render_template("admin/case", case, last_email, admins, date_pretty_printer)
def GET(self): db = connect_to_taskdb() filters = web.input(command = None, finishedat_start = None, finishedat_end = None, limit = 20, offset = 0) command = filters["command"] limit = int(filters["limit"]) offset = int(filters["offset"]) if not command: command = None # To make the view parameters work properly. otherwise, command becomes '' if filters["finishedat_start"]: finishedat_start = datetime.datetime.strptime(filters['finishedat_start'],"%Y-%m-%d %H:%M") else: finishedat_start = datetime.datetime(year = 2000, day = 1, month = 1) if filters["finishedat_end"]: finishedat_end = datetime.datetime.strptime(filters['finishedat_end'],"%Y-%m-%d %H:%M") else: finishedat_end = datetime.datetime.utcnow() finishedat_start = calendar.timegm(finishedat_start.timetuple()) finishedat_end = calendar.timegm(finishedat_end.timetuple()) completed_tasks = (process_task_row(x.doc) for x in db.view("history/tasks", startkey = [command,finishedat_end], endkey = [command,finishedat_start], limit = limit, skip = offset, include_docs = True, descending = True, stale = "ok")) return render_template("admin/tasks/index", completed_tasks)
def GET(self, key): nkey = self.normalize_key(key) if nkey != key: raise web.redirect(nkey) subj = get_subject(key, details=True) subj.v2 = True delegate.context.setdefault('bodyid', 'subject') if not subj or subj.work_count == 0: web.ctx.status = "404 Not Found" page = render_template('subjects/notfound.tmpl', key) else: page = render_template("subjects", page=subj) page.v2 = True return page
def POST(self): form = web.input() email = form.get("email", "") topic = form.get("topic", "") description = form.get("question", "") url = form.get("url", "") user = accounts.get_current_user() useragent = web.ctx.env.get("HTTP_USER_AGENT","") if not all([email, topic, description]): return "" default_assignees = config.get("support_default_assignees",{}) topic_key = str(topic.replace(" ","_").lower()) if topic_key in default_assignees: assignee = default_assignees.get(topic_key) else: assignee = default_assignees.get("default", "*****@*****.**") stats.increment("ol.support.all") subject = "Support case *%s*"%topic url = web.ctx.home + url displayname = user and user.get_name() or "" username = user and user.get_username() or "" message = SUPPORT_EMAIL_TEMPLATE % locals() sendmail(email, assignee, subject, message) return render_template("email/case_created", assignee)
def GET(self): def get_results(q, offset=0, limit=100): q = escape_bracket(q) solr_select = solr_author_select_url + "?q.op=AND&q=%s&fq=&start=%d&rows=%d&fl=*&qt=standard&wt=json" % (web.urlquote(q), offset, limit) solr_select += '&sort=work_count+desc' return json.loads(urllib.urlopen(solr_select).read()) return render_template('search/authors.tmpl', get_results)
def GET(self): def get_results(q, offset=0, limit=100): q = escape_bracket(q) solr_select = solr_select_url + "?fq=type:edition&q.op=AND&q=%s&start=%d&rows=%d&fl=*&qt=standard&wt=json" % (web.urlquote(q), offset, limit) return run_solr_search(solr_select) return render_template('search/editions.tmpl', get_results)
def GET(self): from . import search result = search.get_works_solr().select('*:*', rows=0, facets=['language'], facet_limit=500) languages = [web.storage(name=get_language_name(row.value), key='/languages/' + row.value, count=row.count) for row in result['facets']['language']] print >> web.debug, languages[:10] return render_template("languages/index", languages)
def GET(self, id): id = int(id) change = web.ctx.site.get_change(id) if not change: web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path) path = self.get_change_url(change) if path != web.ctx.path: raise web.redirect(path) else: tname = "recentchanges/" + change.kind + "/view" if tname in render: return render_template(tname, change) else: return render_template("recentchanges/default/view", change)
def GET(self): try: if 'counts_db' in config.admin: stats = admin.get_stats() else: stats = None except Exception: logger.error("Error in getting stats", exc_info=True) stats = None blog_posts = get_blog_feeds() lending_list = config.get("home", {}).get("lending_list") returncart_list = config.get("home", {}).get("returncart_list") user = accounts.get_current_user() loans = borrow.get_loans(user) if user else None popular_available, popular_waitlist = popular_carousel() return render_template( "home/index", stats=stats, blog_posts=blog_posts, lending_list=lending_list, returncart_list=returncart_list, user=user, loans=loans, popular_books=popular_available, waitlisted_books=popular_waitlist )
def GET(self): if not support_db: return "The Openlibrary support system is currently offline. Please try again later." i = web.input(path=None) user = accounts.get_current_user() email = user and user.email return render_template("support", email=email, url=i.path)
def get_thing_info(thing): global tombstone_db try: tasks = get_tasks_info(thing, tombstone_db) except couchdb.http.ResourceNotFound: return("No such thing to inspect") return render_template("admin/inspect/thing", thing, tasks)
def GET(self): i = web.input(key=[]) keys = uniq(i.key) # filter bad keys keys = self.filter_authors(keys) return render_template('merge/authors', keys, top_books_from_author=top_books_from_author)
def POST_login(self, i): # make sure the username is valid if not forms.vlogin.valid(i.username): return self.error("account_user_notfound", i) # Try to find account with exact username, failing which try for case variations. account = accounts.find(username=i.username) or accounts.find(lusername=i.username) if not account: return self.error("account_user_notfound", i) if i.redirect == "/account/login" or i.redirect == "": i.redirect = "/" status = account.login(i.password) if status == 'ok': expires = (i.remember and 3600*24*7) or "" web.setcookie(config.login_cookie_name, web.ctx.conn.get_auth_token(), expires=expires) raise web.seeother(i.redirect) elif status == "account_not_verified": return render_template("account/not_verified", username=account.username, password=i.password, email=account.email) elif status == "account_not_found": return self.error("account_user_notfound", i) elif status == "account_blocked": return self.error("account_blocked", i) else: return self.error("account_incorrect_password", i)
def render(name, *a, **kw): as_string = kw.pop("as_string", True) d = render_template(name, *a, **kw) if as_string: return unicode(d) else: return d
def test_home_template(self, render_template, mock_site, olconfig, monkeypatch): docs = [MockDoc(_id = datetime.datetime.now().strftime("counts-%Y-%m-%d"), human_edits = 1, bot_edits = 1, lists = 1, visitors = 1, loans = 1, members = 1, works = 1, editions = 1, ebooks = 1, covers = 1, authors = 1, subjects = 1)]* 100 stats = dict(human_edits = Stats(docs, "human_edits", "human_edits"), bot_edits = Stats(docs, "bot_edits", "bot_edits"), lists = Stats(docs, "lists", "total_lists"), visitors = Stats(docs, "visitors", "visitors"), loans = Stats(docs, "loans", "loans"), members = Stats(docs, "members", "total_members"), works = Stats(docs, "works", "total_works"), editions = Stats(docs, "editions", "total_editions"), ebooks = Stats(docs, "ebooks", "total_ebooks"), covers = Stats(docs, "covers", "total_covers"), authors = Stats(docs, "authors", "total_authors"), subjects = Stats(docs, "subjects", "total_subjects")) mock_site.quicksave("/people/foo/lists/OL1L", "/type/list") olconfig.setdefault("home", {})['lending_list'] = "/people/foo/lists/OL1L" monkeypatch.setattr(home, "get_returncart", lambda limit: []) html = unicode(render_template("home/index", stats=stats, lending_list="/people/foo/lists/OL1L")) assert '<div class="homeSplash"' in html #assert "Books to Read" in html assert "Return Cart" in html assert "Around the Library" in html assert "About the Project" in html
def GET(self): i = web.input(path=None) user = accounts.get_current_user() email = user and user.email template = render_template("support", email=email, url=i.path) template.v2 = True return template
def GET(self, typ = "new"): current_user = accounts.get_current_user() if not support_db: return render_template("admin/cases", None, None, True, False) i = web.input(sort="status", desc = "false", all = "false") sortby = i['sort'] desc = i['desc'] cases = support_db.get_all_cases(typ, summarise = False, sortby = sortby, desc = desc) if i['all'] == "false": cases = (x for x in cases if x.assignee == current_user.get_email()) summary = support_db.get_all_cases(typ, summarise = True, user = current_user.get_email()) else: summary = support_db.get_all_cases(typ, summarise = True) total = sum(int(x) for x in summary.values()) desc = desc == "false" and "true" or "false" return render_template("admin/cases", summary, total, cases, desc)
def GET(self): return render_template("publishers/index")
def GET(self): return render_template('search/authors.tmpl', self.get_results)
def GET(self): i = web.input(q='', offset='0', limit='10') lists = self.get_results(i.q, i.offset, i.limit) return render_template('search/lists.tmpl', q=i.q, lists=lists)
def GET(self): libraries = inlibrary.get_libraries() web.header("Content-Type", "text/plain") return delegate.RawText( render_template("libraries/locations", libraries))
def GET(self): return render_template("libraries/index", get_libraries_by_country())
def test_home_template(self, render_template, mock_site, monkeypatch): self.setup_monkeypatch(monkeypatch) docs = [ MockDoc(_id=datetime.datetime.now().strftime("counts-%Y-%m-%d"), human_edits=1, bot_edits=1, lists=1, visitors=1, loans=1, members=1, works=1, editions=1, ebooks=1, covers=1, authors=1, subjects=1) ] * 100 stats = dict(human_edits=Stats(docs, "human_edits", "human_edits"), bot_edits=Stats(docs, "bot_edits", "bot_edits"), lists=Stats(docs, "lists", "total_lists"), visitors=Stats(docs, "visitors", "visitors"), loans=Stats(docs, "loans", "loans"), members=Stats(docs, "members", "total_members"), works=Stats(docs, "works", "total_works"), editions=Stats(docs, "editions", "total_editions"), ebooks=Stats(docs, "ebooks", "total_ebooks"), covers=Stats(docs, "covers", "total_covers"), authors=Stats(docs, "authors", "total_authors"), subjects=Stats(docs, "subjects", "total_subjects")) mock_site.quicksave("/people/foo/lists/OL1L", "/type/list") def spoofed_generic_carousel(*args, **kwargs): return [{ "work": None, "key": "/books/OL1M", "url": "/books/OL1M", "title": "The Great Book", "authors": [web.storage({ "key": "/authors/OL1A", "name": "Some Author" })], "read_url": "http://archive.org/stream/foo", "borrow_url": "/books/OL1M/foo/borrow", "inlibrary_borrow_url": "/books/OL1M/foo/borrow", "cover_url": "" }] html = six.text_type( render_template("home/index", stats=stats, test=True)) headers = [ "Books We Love", "Recently Returned", "Kids", "Thrillers", "Romance", "Classic Books", "Textbooks" ] for h in headers: assert h in html assert "Around the Library" in html assert "About the Project" in html
def GET(self): stats = LoanStats() return render_template("libraries/stats", stats)
def GET(self): delegate.context.setdefault('bodyid', 'lists') return render_template("lists/home")
def GET(self): return render_template("subjects/index.html")
def GET(self): template = render_template("search/advancedsearch.html") template.v2 = True return template
def test_stats_template(self, render_template): # Make sure that it works fine without any input (skipping section) html = six.text_type(render_template("home/stats")) assert html == ""
def GET(self): delegate.context.setdefault('bodyid', 'subject') page = render_template("subjects/index.html") page.v2 = True return page
def GET(self): stats = admin.get_stats() return render_template("admin/index", stats)
def sendmail_with_template(template, to, cc=None, frm=None, **kwargs): msg = render_template(template, **kwargs) _sendmail(to, msg, cc=cc, frm=frm)
def GET(self): return render_template('account/email/forgot')
def GET(self): return render_template("libraries/index", self.get_branches())
def GET(self): return render_template("libraries/add")
def GET(self): return render_template("lists/home")
def GET(self): return render_template("search/advancedsearch.html")
def GET(self): return render_template('library_explorer')
def GET(self, key): doc = web.ctx.site.get(key) if doc is None or doc.type.key != '/type/list': raise web.notfound() return render_template("type/list/embed", doc)
def GET(self): return render_template('search/subjects.tmpl', self.get_results)
def GET(self): return render_template("status", status_info, feature_flags)
def render(self, doc, lists): return render_template("lists/lists.html", doc, lists)
def GET(self): return render_template("borrow/about")
def GET(self): i = web.input(key=[], merge_key=[]) keys = uniq(i.key) merge_keys = uniq(i.merge_key) assert all(k is not None for k in merge_keys) if not merge_keys: return render_template('merge/editions', keys) full_keys = ['/books/' + k for k in merge_keys] editions = [web.ctx.site.get('/books/' + k) for k in merge_keys] master = None for e in editions: if e.key == '/books/' + i.master: master = e break all_keys = set() for e in editions: for k in e: if e[k] is not None and e[k] != {}: all_keys.add(k) merged = {} possible_values = defaultdict(lambda: defaultdict(int)) k = 'publish_date' publish_dates = set(e[k] for e in editions if k in e and len(e[k]) != 4) k = 'pagination' all_pagination = set(e[k] for e in editions if e.get(k)) one_item_lists = {} for k in 'lc_classifications', 'publishers', 'contributions', 'series': one_item_lists[k] = set(e[k][0].strip('.') for e in editions if e.get(k) and len(set(e[k])) == 1) for k in ['other_titles', 'isbn_10', 'series']: if k not in all_keys: continue merged[k] = [] for e in editions: for v in e.get(k, []): if v not in merged[k]: possible_values[k][v] += 1 merged[k].append(v) k = 'ocaid' for e in editions: v = e.get(k) if not v: continue possible_values[k][v] += 1 if 'ia:' + v not in merged.get('source_records', []): merged.setdefault('source_records', []).append(v) k = 'identifiers' if k in all_keys: merged[k] = {} for e in editions: if k not in e: continue for a, b in e[k].items(): for c in b: if c in merged[k].setdefault(a, []): continue merged[k][a].append(c) any_publish_country = False k = 'publish_country' if k in all_keys: for e in editions: if e.get(k) and not e[k].strip().startswith('xx'): any_publish_country = True for k in 'source_records', 'ia_box_id': merged[k] = [] for e in editions: if e.get(k) and isinstance(e[k], six.string_types): e[k] = [e[k]] if e.get(k): assert isinstance(e[k], list) for sr in e.get(k, []): if sr not in merged[k]: merged[k].append(sr) for k in all_keys: if k in ('source_records', 'ia_box_id', 'identifiers', 'ocaid', 'other_titles', 'series'): continue uniq_values = defaultdict(list) for num, e in enumerate(editions): v = e.get(k) if v: if isinstance(v, list): for lv in v: possible_values[k][lv] += 1 elif not isinstance(v, dict): possible_values[k][v] += 1 if k == 'publish_date' and len( v) == 4 and v.isdigit and any( v in pd for pd in publish_dates): continue if k == 'pagination' and any( len(i) > len(v) and v in i for i in all_pagination): continue if k in one_item_lists and len(set(e.get( k, []))) == 1 and any( len(i) > len(v[0].strip('.')) and v[0].strip('.') in i for i in one_item_lists[k]): continue if k == 'publish_country' and any_publish_country and e.get( k, '').strip().startswith('xx'): continue if k == 'edition_name' and v.endswith(' ed edition'): v = v[:-len(' edition')] uniq_values[re_nonword.sub('', repr(v).lower())].append(num) if len(uniq_values) == 1: merged[k] = editions[uniq_values.values()[0][0]][k] continue if k == 'covers': assert all(isinstance(e[k], list) for e in editions if k in e) covers = set() for e in editions: if k in e: covers.update(c for c in e[k] if c != -1) merged['covers'] = sorted(covers) continue if k == 'notes': merged['notes'] = '' for e in editions: if e.get('notes'): merged['notes'] += e['notes'] + '\n' continue if k == 'ocaid': for e in editions: if e.get('ocaid'): #assert not e['ocaid'].endswith('goog') merged['ocaid'] = e['ocaid'] break assert merged['ocaid'] continue return render_template('merge/editions2', master, editions, all_keys, merged, possible_values)
def GET_updates_atom(self, list): web.header("Content-Type", 'application/atom+xml; charset="utf-8"') return render_template("lists/feed_updates.xml", list)
def _get_relatedcarousels_component(workid): if 'env' not in web.ctx: delegate.fakeload() work = web.ctx.site.get('/works/%s' % workid) or {} component = render_template('books/RelatedWorksCarousel', work) return {0: str(component)}
def GET(self): def get_results(q, offset=0, limit=100): q = escape_q(q) results = inside_search_select({ 'q': q, 'from': offset, 'size': limit }) # If there is any error in gettig the response, return the error if 'error' in results: return results # TODO: This chunk *seems* like it's not achieving anything -- try removing. If all good, # can collapse `if 'error' in results` condition above. # ekey_doc = {} # for doc in results['hits']['hits']: # ia = doc['fields']['identifier'][0] # q = {'type': '/type/edition', 'ocaid': ia} # ekeys = web.ctx.site.things(q) # if not ekeys: # del q['ocaid'] # q['source_records'] = 'ia:' + ia # ekeys = web.ctx.site.things(q) # if ekeys: # ekey_doc[ekeys[0]] = doc # editions = web.ctx.site.get_many(ekey_doc.keys()) # for e in editions: # ekey_doc[e['key']]['edition'] = e return results def inside_search_select(params): if not hasattr(config, 'plugin_inside'): return {'error': 'Unable to prepare search engine'} search_endpoint = config.plugin_inside['search_endpoint'] search_select = search_endpoint + '?' + urllib.urlencode( params, 'utf-8') # TODO: Update for Elastic # stats.begin("solr", url=search_select) try: json_data = urllib2.urlopen(search_select, timeout=30).read() logger = logging.getLogger("openlibrary.inside") logger.debug('URL: ' + search_select) except: return {'error': 'Unable to query search engine'} # TODO: Update for Elastic # stats.end() try: return simplejson.loads(json_data) except: return {'error': 'Error converting search engine data to JSON'} def quote_snippet(snippet): trans = { '\n': ' ', '{{{': '<b>', '}}}': '</b>', } re_trans = re.compile(r'(\n|\{\{\{|\}\}\})') return re_trans.sub(lambda m: trans[m.group(1)], web.htmlquote(snippet)) def editions_from_ia(ia): q = { 'type': '/type/edition', 'ocaid': ia, 'title': None, 'covers': None, 'works': None, 'authors': None } editions = web.ctx.site.things(q) if not editions: del q['ocaid'] q['source_records'] = 'ia:' + ia editions = web.ctx.site.things(q) return editions def read_from_archive(ia): meta_xml = 'http://archive.org/download/' + ia + '/' + ia + '_meta.xml' stats.begin("archive.org", url=meta_xml) try: xml_data = urllib2.urlopen(meta_xml, timeout=5) except: stats.end() return {} item = {} try: tree = etree.parse(xml_data) except etree.XMLSyntaxError: return {} finally: stats.end() root = tree.getroot() fields = ['title', 'creator', 'publisher', 'date', 'language'] for k in 'title', 'date', 'publisher': v = root.find(k) if v is not None: item[k] = v.text for k in 'creator', 'language', 'collection': v = root.findall(k) if len(v): item[k] = [i.text for i in v if i.text] return item return render_template('search/inside.tmpl', get_results, quote_snippet, editions_from_ia, read_from_archive)
def GET(self): template = render_template("status", status_info, feature_flags) template.v2 = True return template