mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Scaffolding for per-user library restrictions
This commit is contained in:
parent
a0a32a214a
commit
758e73e9d9
@ -150,7 +150,7 @@ def book(ctx, rd, book_id, library_id):
|
||||
|
||||
If id_is_uuid is true then the book_id is assumed to be a book uuid instead.
|
||||
'''
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
with db.safe_read_lock:
|
||||
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
||||
oid = book_id
|
||||
@ -189,7 +189,7 @@ def books(ctx, rd, library_id):
|
||||
|
||||
If id_is_uuid is true then the book_id is assumed to be a book uuid instead.
|
||||
'''
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
with db.safe_read_lock:
|
||||
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
||||
ids = rd.query.get('ids')
|
||||
@ -240,7 +240,7 @@ def categories(ctx, rd, library_id):
|
||||
}
|
||||
|
||||
'''
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
with db.safe_read_lock:
|
||||
ans = {}
|
||||
categories = ctx.get_categories(rd, db)
|
||||
@ -331,7 +331,7 @@ def category(ctx, rd, encoded_name, library_id):
|
||||
http://manual.calibre-ebook.com/sub_groups.html
|
||||
'''
|
||||
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
with db.safe_read_lock:
|
||||
num, offset = get_pagination(rd.query)
|
||||
sort, sort_order = rd.query.get('sort'), rd.query.get('sort_order')
|
||||
@ -461,7 +461,7 @@ def books_in(ctx, rd, encoded_category, encoded_item, library_id):
|
||||
|
||||
Optional: ?num=100&offset=0&sort=title&sort_order=asc&get_additional_fields=
|
||||
'''
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
with db.safe_read_lock:
|
||||
try:
|
||||
dname, ditem = map(decode_name, (encoded_category, encoded_item))
|
||||
@ -547,7 +547,7 @@ def search(ctx, rd, library_id):
|
||||
|
||||
Optional: ?num=100&offset=0&sort=title&sort_order=asc&query=
|
||||
'''
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
query = rd.query.get('query')
|
||||
num, offset = get_pagination(rd.query)
|
||||
with db.safe_read_lock:
|
||||
@ -557,7 +557,7 @@ def search(ctx, rd, library_id):
|
||||
@endpoint('/ajax/library-info', postprocess=json)
|
||||
def library_info(ctx, rd):
|
||||
' Return info about available libraries '
|
||||
library_map, default_library = ctx.library_map
|
||||
library_map, default_library = ctx.library_info(rd)
|
||||
return {'library_map':library_map, 'default_library':default_library}
|
||||
|
||||
# }}}
|
||||
|
@ -54,7 +54,7 @@ def get_html(name, auto_reload_port, **replacements):
|
||||
|
||||
@endpoint('', auth_required=False)
|
||||
def index(ctx, rd):
|
||||
default_library = ctx.library_map[1]
|
||||
default_library = ctx.library_info(rd)[1]
|
||||
return rd.generate_static_output('/', partial(
|
||||
get_html, 'content-server/index.html', getattr(rd.opts, 'auto_reload_port', 0),
|
||||
ENTRY_POINT='book list',
|
||||
@ -62,11 +62,11 @@ def index(ctx, rd):
|
||||
DEFAULT_LIBRARY=json_dumps(default_library)
|
||||
))
|
||||
|
||||
def get_basic_query_data(ctx, query):
|
||||
db, library_id, library_map, default_library = get_library_data(ctx, query)
|
||||
def get_basic_query_data(ctx, rd):
|
||||
db, library_id, library_map, default_library = get_library_data(ctx, rd)
|
||||
skeys = db.field_metadata.sortable_field_keys()
|
||||
sorts, orders = [], []
|
||||
for x in query.get('sort', '').split(','):
|
||||
for x in rd.query.get('sort', '').split(','):
|
||||
if x:
|
||||
s, o = x.rpartition('.')[::2]
|
||||
if o and not s:
|
||||
@ -116,7 +116,7 @@ def interface_data(ctx, rd):
|
||||
'gui_last_modified_display_format':tweaks['gui_last_modified_display_format'],
|
||||
'use_roman_numerals_for_series_number': get_use_roman(),
|
||||
}
|
||||
ans['library_map'], ans['default_library'] = ctx.library_map
|
||||
ans['library_map'], ans['default_library'] = ctx.library_info(rd)
|
||||
ud = {}
|
||||
if rd.username:
|
||||
# Override session data with stored values for the authenticated user,
|
||||
@ -128,7 +128,7 @@ def interface_data(ctx, rd):
|
||||
usort = ud.get('sort')
|
||||
if usort:
|
||||
rd.query.set('sort', usort)
|
||||
ans['library_id'], db, sorts, orders = get_basic_query_data(ctx, rd.query)
|
||||
ans['library_id'], db, sorts, orders = get_basic_query_data(ctx, rd)
|
||||
ans['user_session_data'] = ud
|
||||
try:
|
||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
||||
@ -169,7 +169,7 @@ def more_books(ctx, rd):
|
||||
|
||||
Optional: ?num=50&library_id=<default library>
|
||||
'''
|
||||
db, library_id = get_library_data(ctx, rd.query)[:2]
|
||||
db, library_id = get_library_data(ctx, rd)[:2]
|
||||
|
||||
try:
|
||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
||||
@ -217,13 +217,13 @@ def get_books(ctx, rd):
|
||||
|
||||
Optional: ?library_id=<default library>&num=50&sort=timestamp.desc&search=''
|
||||
'''
|
||||
library_id, db, sorts, orders = get_basic_query_data(ctx, rd.query)
|
||||
library_id, db, sorts, orders = get_basic_query_data(ctx, rd)
|
||||
try:
|
||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
||||
except Exception:
|
||||
raise HTTPNotFound('Invalid number of books: %r' % rd.query.get('num'))
|
||||
searchq = rd.query.get('search', '')
|
||||
db = get_library_data(ctx, rd.query)[0]
|
||||
db = get_library_data(ctx, rd)[0]
|
||||
ans = {}
|
||||
mdata = ans['metadata'] = {}
|
||||
with db.safe_read_lock:
|
||||
@ -246,7 +246,7 @@ def book_metadata(ctx, rd, book_id):
|
||||
|
||||
Optional: ?library_id=<default library>
|
||||
'''
|
||||
library_id, db = get_basic_query_data(ctx, rd.query)[:2]
|
||||
library_id, db = get_basic_query_data(ctx, rd)[:2]
|
||||
book_ids = ctx.allowed_book_ids(rd, db)
|
||||
def notfound():
|
||||
raise HTTPNotFound(_('No book with id: %d in library') % book_id)
|
||||
@ -268,10 +268,10 @@ def tag_browser(ctx, rd):
|
||||
Optional: ?library_id=<default library>&sort_tags_by=name&partition_method=first letter
|
||||
&collapse_at=25&dont_collapse=&hide_empty_categories=
|
||||
'''
|
||||
db, library_id = get_library_data(ctx, rd.query)[:2]
|
||||
db, library_id = get_library_data(ctx, rd)[:2]
|
||||
etag = '%s||%s||%s' % (db.last_modified(), rd.username, library_id)
|
||||
etag = hashlib.sha1(etag.encode('utf-8')).hexdigest()
|
||||
def generate():
|
||||
db, library_id = get_library_data(ctx, rd.query)[:2]
|
||||
db, library_id = get_library_data(ctx, rd)[:2]
|
||||
return json(ctx, rd, tag_browser, categories_as_json(ctx, rd, db))
|
||||
return rd.etagged_dynamic_response(etag, generate)
|
||||
|
@ -21,7 +21,7 @@ from calibre.ebooks.metadata.opf2 import metadata_to_opf
|
||||
from calibre.library.save_to_disk import find_plugboard
|
||||
from calibre.srv.errors import HTTPNotFound
|
||||
from calibre.srv.routes import endpoint, json
|
||||
from calibre.srv.utils import http_date
|
||||
from calibre.srv.utils import http_date, get_db
|
||||
from calibre.utils.config_base import tweaks
|
||||
from calibre.utils.date import timestampfromdt
|
||||
from calibre.utils.img import scale_image, image_from_data
|
||||
@ -234,7 +234,7 @@ def get(ctx, rd, what, book_id, library_id):
|
||||
book_id = int(book_id)
|
||||
except Exception:
|
||||
raise HTTPNotFound('Book with id %r does not exist' % book_id)
|
||||
db = ctx.get_library(library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
if db is None:
|
||||
raise HTTPNotFound('Library %r not found' % library_id)
|
||||
with db.safe_read_lock:
|
||||
|
@ -96,11 +96,12 @@ class Context(object):
|
||||
def finalize_session(self, endpoint, data, output):
|
||||
pass
|
||||
|
||||
def get_library(self, library_id=None):
|
||||
def get_library(self, data, library_id=None):
|
||||
# TODO: Restrict the libraries based on data.username
|
||||
return self.library_broker.get(library_id)
|
||||
|
||||
@property
|
||||
def library_map(self):
|
||||
def library_info(self, data):
|
||||
# TODO: Restrict the libraries based on data.username
|
||||
return self.library_broker.library_map, self.library_broker.default_library
|
||||
|
||||
def allowed_book_ids(self, data, db):
|
||||
|
@ -328,7 +328,7 @@ class CategoryGroupFeed(NavFeed):
|
||||
class RequestContext(object):
|
||||
|
||||
def __init__(self, ctx, rd):
|
||||
self.db, self.library_id, self.library_map, self.default_library = get_library_data(ctx, rd.query)
|
||||
self.db, self.library_id, self.library_map, self.default_library = get_library_data(ctx, rd)
|
||||
self.ctx, self.rd = ctx, rd
|
||||
|
||||
def url_for(self, path, **kwargs):
|
||||
|
@ -25,7 +25,7 @@ class ContentTest(LibraryBaseTest):
|
||||
def test_ajax_book(self): # {{{
|
||||
'Test /ajax/book'
|
||||
with self.create_server() as server:
|
||||
db = server.handler.router.ctx.get_library()
|
||||
db = server.handler.router.ctx.library_broker.get(None)
|
||||
conn = server.connect()
|
||||
request = partial(make_request, conn, prefix='/ajax/book')
|
||||
|
||||
@ -50,7 +50,7 @@ class ContentTest(LibraryBaseTest):
|
||||
def test_ajax_categories(self): # {{{
|
||||
'Test /ajax/categories and /ajax/search'
|
||||
with self.create_server() as server:
|
||||
db = server.handler.router.ctx.get_library()
|
||||
db = server.handler.router.ctx.library_broker.get(None)
|
||||
conn = server.connect()
|
||||
request = partial(make_request, conn)
|
||||
|
||||
|
@ -66,7 +66,7 @@ class ContentTest(LibraryBaseTest):
|
||||
def test_get(self): # {{{
|
||||
'Test /get'
|
||||
with self.create_server() as server:
|
||||
db = server.handler.router.ctx.get_library()
|
||||
db = server.handler.router.ctx.library_broker.get(None)
|
||||
conn = server.connect()
|
||||
|
||||
def get(what, book_id, library_id=None, q=''):
|
||||
|
@ -470,18 +470,18 @@ class ReadOnlyFileBuffer(object):
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
def get_db(ctx, library_id):
|
||||
db = ctx.get_library(library_id)
|
||||
def get_db(ctx, rd, library_id):
|
||||
db = ctx.get_library(rd, library_id)
|
||||
if db is None:
|
||||
raise HTTPNotFound('Library %r not found' % library_id)
|
||||
return db
|
||||
|
||||
def get_library_data(ctx, query):
|
||||
library_id = query.get('library_id')
|
||||
library_map, default_library = ctx.library_map
|
||||
def get_library_data(ctx, rd):
|
||||
library_id = rd.query.get('library_id')
|
||||
library_map, default_library = ctx.library_info(rd)
|
||||
if library_id not in library_map:
|
||||
library_id = default_library
|
||||
db = get_db(ctx, library_id)
|
||||
db = get_db(ctx, rd, library_id)
|
||||
return db, library_id, library_map, default_library
|
||||
|
||||
class Offsets(object):
|
||||
|
Loading…
x
Reference in New Issue
Block a user