def GET(self, key, value, suffix): key = key.lower() suffix = suffix or "" if key == "isbn": if len(value) == 13: key = "isbn_13" else: key = "isbn_10" elif key == "oclc": key = "oclc_numbers" elif key == "ia": key = "ocaid" if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith("." + web.ctx.encoding): ext = "." + web.ctx.encoding else: ext = "" if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] def redirect(key, ext, suffix): if ext: return web.found(key + ext) else: book = web.ctx.site.get(key) return web.found(book.url(suffix)) q = {"type": "/type/edition", key: value} try: result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) elif key =='ocaid': q = {"type": "/type/edition", 'source_records': 'ia:' + value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) q = {"type": "/type/volume", 'ia_id': value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) else: raise redirect("/books/ia:" + value, ext, suffix) web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False) except web.HTTPError: raise except: logger.error("unexpected error", exc_info=True) web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False)
def GET(self, key, value, suffix): key = key.lower() suffix = suffix or "" if key == "isbn": if len(value) == 13: key = "isbn_13" else: key = "isbn_10" elif key == "oclc": key = "oclc_numbers" elif key == "ia": key = "ocaid" if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith("." + web.ctx.encoding): ext = "." + web.ctx.encoding else: ext = "" if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] def redirect(key, ext, suffix): if ext: return web.found(key + ext) else: book = web.ctx.site.get(key) return web.found(book.url(suffix)) q = {"type": "/type/edition", key: value} try: result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) elif key == 'ocaid': q = {"type": "/type/edition", 'source_records': 'ia:' + value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) q = {"type": "/type/volume", 'ia_id': value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) else: raise redirect("/books/ia:" + value, ext, suffix) web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False) except web.HTTPError: raise except: logger.error("unexpected error", exc_info=True) web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False)
def __call__(self, handler): # temp hack to handle languages and users during upstream-to-www migration if web.ctx.path.startswith("/l/"): raise web.seeother("/languages/" + web.ctx.path[len("/l/"):]) if web.ctx.path.startswith("/user/"): if not web.ctx.site.get(web.ctx.path): raise web.seeother("/people/" + web.ctx.path[len("/user/"):]) real_path, readable_path = get_readable_path(web.ctx.site, web.ctx.path, self.patterns, encoding=web.ctx.encoding) #@@ web.ctx.path is either quoted or unquoted depends on whether the application is running #@@ using builtin-server or lighttpd. That is probably a bug in web.py. #@@ take care of that case here till that is fixed. # @@ Also, the redirection must be done only for GET requests. if readable_path != web.ctx.path and readable_path != urllib.parse.quote(web.utf8(web.ctx.path)) and web.ctx.method == "GET": raise web.redirect(web.safeunicode(readable_path) + web.safeunicode(web.ctx.query)) web.ctx.readable_path = readable_path web.ctx.path = real_path web.ctx.fullpath = web.ctx.path + web.ctx.query out = handler() V2_TYPES = ['works', 'books', 'people', 'authors', 'publishers', 'languages', 'account'] if out and any(web.ctx.path.startswith('/%s/' % _type) for _type in V2_TYPES): out.v2 = True # Exclude noindex items if web.ctx.get('exclude'): web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path) return out
def GET(self, username, key='loans'): """check if user's reading log is public""" i = web.input(page=1) user = web.ctx.site.get('/people/%s' % username) if not user: return render.notfound("User %s" % username, create=False) is_public = user.preferences().get('public_readlog', 'no') == 'yes' logged_in_user = accounts.get_current_user() if is_public or logged_in_user and logged_in_user.key.split('/')[-1] == username: readlog = ReadingLog(user=user) sponsorships = get_sponsored_editions(user) if key == 'sponsorships': books = (web.ctx.site.get( web.ctx.site.things({ 'type': '/type/edition', 'isbn_%s' % len(s['isbn']): s['isbn'] })[0]) for s in sponsorships) else: books = readlog.get_works(key, page=i.page) return render['account/books']( books, key, sponsorship_count=len(sponsorships), reading_log_counts=readlog.reading_log_counts, lists=readlog.lists, user=user, logged_in_user=logged_in_user, public=is_public ) raise web.seeother(user.key)
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) raise web.found(change.url())
def render(self, page=1, sort='desc', list=None): if not self.user: return render.notfound("User %s" % self.username, create=False) logged_in_user = accounts.get_current_user() is_logged_in_user = (logged_in_user and logged_in_user.key.split('/')[-1] == self.username) is_public = self.user.preferences().get('public_readlog', 'no') == 'yes' data = None if is_logged_in_user and self.key in self.ALL_KEYS: self.counts.update(PatronBooknotes.get_counts(self.username)) sponsorships = get_sponsored_editions(self.user) self.counts['sponsorships'] = len(sponsorships) if self.key == 'sponsorships': data = add_availability( web.ctx.site.get_many([ '/books/%s' % doc['openlibrary_edition'] for doc in sponsorships ])) if sponsorships else None elif self.key in self.READING_LOG_KEYS: data = add_availability( self.readlog.get_works(self.key, page=page, sort='created', sort_order=sort), mode="openlibrary_work", ) elif self.key == 'list': data = list else: data = self._prepare_data(logged_in_user) elif self.key in self.READING_LOG_KEYS and is_public: data = add_availability( self.readlog.get_works(self.key, page=page, sort='created', sort_order=sort), mode="openlibrary_work", ) if data is not None: return render['account/books']( data, self.key, self.counts, logged_in_user=logged_in_user, user=self.user, lists=self.lists, public=is_public, owners_page=is_logged_in_user, ) raise web.seeother(self.user.key)
def GET(self, key, value, suffix=''): key = key.lower() if key == 'oclc': key = 'oclc_numbers' elif key == 'ia': key = 'ocaid' if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith('.' + web.ctx.encoding): ext = '.' + web.ctx.encoding else: ext = '' if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] q = {'type': '/type/edition', key: value} result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) elif key == 'ocaid': # Try a range of ocaid alternatives: ocaid_alternatives = [ { 'type': '/type/edition', 'source_records': 'ia:' + value }, { 'type': '/type/volume', 'ia_id': value }, ] for q in ocaid_alternatives: result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) # Perform import, if possible from openlibrary.plugins.importapi.code import ia_importapi, BookImportError from openlibrary import accounts with accounts.RunAs('ImportBot'): try: ia_importapi.ia_import(value, require_marc=True) except BookImportError: logger.exception('Unable to import ia record') # Go the the record created, or to the dummy ia-wrapper record return web.found('/books/ia:' + value + ext) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False)
def GET(self, key, value, suffix=''): key = key.lower() if key == 'isbn': if len(value) == 13: key = 'isbn_13' else: key = 'isbn_10' elif key == 'oclc': key = 'oclc_numbers' elif key == 'ia': key = 'ocaid' if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith('.' + web.ctx.encoding): ext = '.' + web.ctx.encoding else: ext = '' if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] q = {'type': '/type/edition', key: value} result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) elif key == 'ocaid': # Try a range of ocaid alternatives: ocaid_alternatives = [{ 'type': '/type/edition', 'source_records': 'ia:' + value }, { 'type': '/type/volume', 'ia_id': value }] for q in ocaid_alternatives: result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) # If nothing matched, try this as a last resort: return web.found('/books/ia:' + value + ext) elif key.startswith('isbn'): try: ed_key = create_edition_from_amazon_metadata(value) except Exception as e: logger.error(e) return e.message if ed_key: return web.found(ed_key + ext) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False)
def GET(self, username, key='loans'): """check if user's reading log is public""" user = web.ctx.site.get('/people/%s' % username) if not user: return render.notfound("User %s" % username, create=False) if user.preferences().get('public_readlog', 'no') == 'yes': readlog = ReadingLog(user=user) works = readlog.get_works(key) return render['account/books']( works, key, reading_log=readlog.reading_log_counts, lists=readlog.lists, user=user) raise web.seeother(user.key)
def GET(self, key, value): key = key.lower() if key == "isbn": if len(value) == 13: key = "isbn_13" else: key = "isbn_10" elif key == "oclc": key = "oclc_numbers" elif key == "ia": key = "ocaid" if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith("." + web.ctx.encoding): ext = "." + web.ctx.encoding else: ext = "" q = {"type": "/type/edition", key: value} try: result = web.ctx.site.things(q) if result: raise web.seeother(result[0] + ext) elif key =='ocaid': q = {"type": "/type/edition", 'source_records': 'ia:' + value} result = web.ctx.site.things(q) if result: raise web.seeother(result[0] + ext) web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False) except web.HTTPError: raise except: web.ctx.status = "404 Not Found" return render.notfound(web.ctx.path, create=False)
def GET(self, username, key='loans'): user = web.ctx.site.get('/people/%s' % username) if not user: return render.notfound("User %s" % username, create=False) cur_user = accounts.get_current_user() if not cur_user or cur_user.key.split('/')[-1] != username: return render.permission_denied(web.ctx.path, 'Permission Denied') readlog = ReadingLog(user=user) works = readlog.get_works(key, page=1, limit=2000) works_json = [ { 'title': w.get('title'), 'key': w.key, 'author_keys': [a.author.key for a in w.get('authors', [])], 'first_publish_year': w.first_publish_year or None, 'subjects': w.get('subjects'), 'subject_people': w.get('subject_people'), 'subject_places': w.get('subject_places'), 'subject_times': w.get('subject_times'), } for w in works ] author_keys = set( a for work in works_json for a in work['author_keys'] ) authors_json = [ { 'key': a.key, 'name': a.name, 'birth_date': a.get('birth_date'), } for a in web.ctx.site.get_many(list(author_keys)) ] page = render['account/readinglog_stats']( json.dumps(works_json), json.dumps(authors_json), len(works_json), user.key, user.displayname, web.ctx.path.rsplit('/', 1)[0], key, lang=web.ctx.lang, ) page.v2 = True return page
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, username, key='loans'): """check if user's reading log is public""" i = web.input(page=1) user = web.ctx.site.get('/people/%s' % username) if not user: return render.notfound("User %s" % username, create=False) if user.preferences().get('public_readlog', 'no') == 'yes': readlog = ReadingLog(user=user) books = readlog.get_works(key, page=i.page) sponsorships = get_sponsored_editions(user) page = render['account/books']( books, key, sponsorship_count=len(sponsorships), reading_log_counts=readlog.reading_log_counts, lists=readlog.lists, user=user) page.v2 = True return page raise web.seeother(user.key)
def GET(self, isbn): # Preserve the url type (e.g. `.json`) and query params ext = '' if web.ctx.encoding and web.ctx.path.endswith('.' + web.ctx.encoding): ext = '.' + web.ctx.encoding if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] try: ed = Edition.from_isbn(isbn) if ed: return web.found(ed.key + ext) except Exception as e: logger.error(e) return repr(e) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False)
def GET(self, username, key='loans'): """check if user's reading log is public""" i = web.input(page=1, sort='desc') user = web.ctx.site.get('/people/%s' % username) if not user: return render.notfound("User %s" % username, create=False) is_public = user.preferences().get('public_readlog', 'no') == 'yes' logged_in_user = accounts.get_current_user() is_logged_in_user = (logged_in_user and logged_in_user.key.split('/')[-1] == username) if is_public or is_logged_in_user: readlog = ReadingLog(user=user) sponsorships = get_sponsored_editions(user) if key == 'sponsorships': books = (web.ctx.site.get( web.ctx.site.things({ 'type': '/type/edition', 'isbn_%s' % len(s['isbn']): s['isbn'] })[0]) for s in sponsorships) elif key == 'notes' and is_logged_in_user: books = PatronBooknotes(user).get_notes(page=int(i.page)) elif key == 'observations' and is_logged_in_user: books = PatronBooknotes(user).get_observations( page=int(i.page)) else: books = add_availability(readlog.get_works(key, page=i.page, sort='created', sort_order=i.sort), mode="openlibrary_work") booknotes_counts = PatronBooknotes.get_counts(username) return render['account/books']( books, key, sponsorship_count=len(sponsorships), reading_log_counts=readlog.reading_log_counts, lists=readlog.lists, user=user, logged_in_user=logged_in_user, public=is_public, sort_order=str(i.sort), booknotes_counts=booknotes_counts) raise web.seeother(user.key)
def GET(self, key, value, suffix=''): key = key.lower() if key == 'oclc': key = 'oclc_numbers' elif key == 'ia': key = 'ocaid' if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith('.' + web.ctx.encoding): ext = '.' + web.ctx.encoding else: ext = '' if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] q = {'type': '/type/edition', key: value} result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) elif key == 'ocaid': # Try a range of ocaid alternatives: ocaid_alternatives = [{ 'type': '/type/edition', 'source_records': 'ia:' + value }, { 'type': '/type/volume', 'ia_id': value }] for q in ocaid_alternatives: result = web.ctx.site.things(q) if result: return web.found(result[0] + ext) # If nothing matched, try this as a last resort: return web.found('/books/ia:' + value + ext) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False)
def GET(self, path): return render.notfound(path, create=False)
def GET(self, key, value, suffix): key = key.lower() suffix = suffix or '' if key == 'isbn': if len(value) == 13: key = 'isbn_13' else: key = 'isbn_10' elif key == 'oclc': key = 'oclc_numbers' elif key == 'ia': key = 'ocaid' if key != 'ocaid': # example: MN41558ucmf_6 value = value.replace('_', ' ') if web.ctx.encoding and web.ctx.path.endswith('.' + web.ctx.encoding): ext = '.' + web.ctx.encoding else: ext = '' if web.ctx.env.get('QUERY_STRING'): ext += '?' + web.ctx.env['QUERY_STRING'] def redirect(key, ext, suffix): if ext: return web.found(key + ext) else: book = web.ctx.site.get(key) return web.found(book.url(suffix)) q = {'type': '/type/edition', key: value} try: result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) elif key == 'ocaid': q = {'type': '/type/edition', 'source_records': 'ia:' + value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) q = {'type': '/type/volume', 'ia_id': value} result = web.ctx.site.things(q) if result: raise redirect(result[0], ext, suffix) else: raise redirect('/books/ia:' + value, ext, suffix) elif key.startswith('isbn'): ed_key = create_edition_from_amazon_metadata(value) if ed_key: raise web.seeother(ed_key) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False) except web.HTTPError: raise except: if key.startswith('isbn'): ed_key = create_edition_from_amazon_metadata(value) if ed_key: raise web.seeother(ed_key) logger.error('unexpected error', exc_info=True) web.ctx.status = '404 Not Found' return render.notfound(web.ctx.path, create=False)
def GET(self, path): print "account_others", path return render.notfound(path, create=False)