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.
|
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:
|
with db.safe_read_lock:
|
||||||
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
||||||
oid = book_id
|
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.
|
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:
|
with db.safe_read_lock:
|
||||||
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
id_is_uuid = rd.query.get('id_is_uuid', 'false')
|
||||||
ids = rd.query.get('ids')
|
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:
|
with db.safe_read_lock:
|
||||||
ans = {}
|
ans = {}
|
||||||
categories = ctx.get_categories(rd, db)
|
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
|
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:
|
with db.safe_read_lock:
|
||||||
num, offset = get_pagination(rd.query)
|
num, offset = get_pagination(rd.query)
|
||||||
sort, sort_order = rd.query.get('sort'), rd.query.get('sort_order')
|
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=
|
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:
|
with db.safe_read_lock:
|
||||||
try:
|
try:
|
||||||
dname, ditem = map(decode_name, (encoded_category, encoded_item))
|
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=
|
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')
|
query = rd.query.get('query')
|
||||||
num, offset = get_pagination(rd.query)
|
num, offset = get_pagination(rd.query)
|
||||||
with db.safe_read_lock:
|
with db.safe_read_lock:
|
||||||
@ -557,7 +557,7 @@ def search(ctx, rd, library_id):
|
|||||||
@endpoint('/ajax/library-info', postprocess=json)
|
@endpoint('/ajax/library-info', postprocess=json)
|
||||||
def library_info(ctx, rd):
|
def library_info(ctx, rd):
|
||||||
' Return info about available libraries '
|
' 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}
|
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)
|
@endpoint('', auth_required=False)
|
||||||
def index(ctx, rd):
|
def index(ctx, rd):
|
||||||
default_library = ctx.library_map[1]
|
default_library = ctx.library_info(rd)[1]
|
||||||
return rd.generate_static_output('/', partial(
|
return rd.generate_static_output('/', partial(
|
||||||
get_html, 'content-server/index.html', getattr(rd.opts, 'auto_reload_port', 0),
|
get_html, 'content-server/index.html', getattr(rd.opts, 'auto_reload_port', 0),
|
||||||
ENTRY_POINT='book list',
|
ENTRY_POINT='book list',
|
||||||
@ -62,11 +62,11 @@ def index(ctx, rd):
|
|||||||
DEFAULT_LIBRARY=json_dumps(default_library)
|
DEFAULT_LIBRARY=json_dumps(default_library)
|
||||||
))
|
))
|
||||||
|
|
||||||
def get_basic_query_data(ctx, query):
|
def get_basic_query_data(ctx, rd):
|
||||||
db, library_id, library_map, default_library = get_library_data(ctx, query)
|
db, library_id, library_map, default_library = get_library_data(ctx, rd)
|
||||||
skeys = db.field_metadata.sortable_field_keys()
|
skeys = db.field_metadata.sortable_field_keys()
|
||||||
sorts, orders = [], []
|
sorts, orders = [], []
|
||||||
for x in query.get('sort', '').split(','):
|
for x in rd.query.get('sort', '').split(','):
|
||||||
if x:
|
if x:
|
||||||
s, o = x.rpartition('.')[::2]
|
s, o = x.rpartition('.')[::2]
|
||||||
if o and not s:
|
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'],
|
'gui_last_modified_display_format':tweaks['gui_last_modified_display_format'],
|
||||||
'use_roman_numerals_for_series_number': get_use_roman(),
|
'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 = {}
|
ud = {}
|
||||||
if rd.username:
|
if rd.username:
|
||||||
# Override session data with stored values for the authenticated user,
|
# Override session data with stored values for the authenticated user,
|
||||||
@ -128,7 +128,7 @@ def interface_data(ctx, rd):
|
|||||||
usort = ud.get('sort')
|
usort = ud.get('sort')
|
||||||
if usort:
|
if usort:
|
||||||
rd.query.set('sort', 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
|
ans['user_session_data'] = ud
|
||||||
try:
|
try:
|
||||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
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>
|
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:
|
try:
|
||||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
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=''
|
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:
|
try:
|
||||||
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
num = int(rd.query.get('num', DEFAULT_NUMBER_OF_BOOKS))
|
||||||
except Exception:
|
except Exception:
|
||||||
raise HTTPNotFound('Invalid number of books: %r' % rd.query.get('num'))
|
raise HTTPNotFound('Invalid number of books: %r' % rd.query.get('num'))
|
||||||
searchq = rd.query.get('search', '')
|
searchq = rd.query.get('search', '')
|
||||||
db = get_library_data(ctx, rd.query)[0]
|
db = get_library_data(ctx, rd)[0]
|
||||||
ans = {}
|
ans = {}
|
||||||
mdata = ans['metadata'] = {}
|
mdata = ans['metadata'] = {}
|
||||||
with db.safe_read_lock:
|
with db.safe_read_lock:
|
||||||
@ -246,7 +246,7 @@ def book_metadata(ctx, rd, book_id):
|
|||||||
|
|
||||||
Optional: ?library_id=<default library>
|
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)
|
book_ids = ctx.allowed_book_ids(rd, db)
|
||||||
def notfound():
|
def notfound():
|
||||||
raise HTTPNotFound(_('No book with id: %d in library') % book_id)
|
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
|
Optional: ?library_id=<default library>&sort_tags_by=name&partition_method=first letter
|
||||||
&collapse_at=25&dont_collapse=&hide_empty_categories=
|
&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 = '%s||%s||%s' % (db.last_modified(), rd.username, library_id)
|
||||||
etag = hashlib.sha1(etag.encode('utf-8')).hexdigest()
|
etag = hashlib.sha1(etag.encode('utf-8')).hexdigest()
|
||||||
def generate():
|
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 json(ctx, rd, tag_browser, categories_as_json(ctx, rd, db))
|
||||||
return rd.etagged_dynamic_response(etag, generate)
|
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.library.save_to_disk import find_plugboard
|
||||||
from calibre.srv.errors import HTTPNotFound
|
from calibre.srv.errors import HTTPNotFound
|
||||||
from calibre.srv.routes import endpoint, json
|
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.config_base import tweaks
|
||||||
from calibre.utils.date import timestampfromdt
|
from calibre.utils.date import timestampfromdt
|
||||||
from calibre.utils.img import scale_image, image_from_data
|
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)
|
book_id = int(book_id)
|
||||||
except Exception:
|
except Exception:
|
||||||
raise HTTPNotFound('Book with id %r does not exist' % book_id)
|
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:
|
if db is None:
|
||||||
raise HTTPNotFound('Library %r not found' % library_id)
|
raise HTTPNotFound('Library %r not found' % library_id)
|
||||||
with db.safe_read_lock:
|
with db.safe_read_lock:
|
||||||
|
@ -96,11 +96,12 @@ class Context(object):
|
|||||||
def finalize_session(self, endpoint, data, output):
|
def finalize_session(self, endpoint, data, output):
|
||||||
pass
|
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)
|
return self.library_broker.get(library_id)
|
||||||
|
|
||||||
@property
|
def library_info(self, data):
|
||||||
def library_map(self):
|
# TODO: Restrict the libraries based on data.username
|
||||||
return self.library_broker.library_map, self.library_broker.default_library
|
return self.library_broker.library_map, self.library_broker.default_library
|
||||||
|
|
||||||
def allowed_book_ids(self, data, db):
|
def allowed_book_ids(self, data, db):
|
||||||
|
@ -328,7 +328,7 @@ class CategoryGroupFeed(NavFeed):
|
|||||||
class RequestContext(object):
|
class RequestContext(object):
|
||||||
|
|
||||||
def __init__(self, ctx, rd):
|
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
|
self.ctx, self.rd = ctx, rd
|
||||||
|
|
||||||
def url_for(self, path, **kwargs):
|
def url_for(self, path, **kwargs):
|
||||||
|
@ -25,7 +25,7 @@ class ContentTest(LibraryBaseTest):
|
|||||||
def test_ajax_book(self): # {{{
|
def test_ajax_book(self): # {{{
|
||||||
'Test /ajax/book'
|
'Test /ajax/book'
|
||||||
with self.create_server() as server:
|
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()
|
conn = server.connect()
|
||||||
request = partial(make_request, conn, prefix='/ajax/book')
|
request = partial(make_request, conn, prefix='/ajax/book')
|
||||||
|
|
||||||
@ -50,7 +50,7 @@ class ContentTest(LibraryBaseTest):
|
|||||||
def test_ajax_categories(self): # {{{
|
def test_ajax_categories(self): # {{{
|
||||||
'Test /ajax/categories and /ajax/search'
|
'Test /ajax/categories and /ajax/search'
|
||||||
with self.create_server() as server:
|
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()
|
conn = server.connect()
|
||||||
request = partial(make_request, conn)
|
request = partial(make_request, conn)
|
||||||
|
|
||||||
|
@ -66,7 +66,7 @@ class ContentTest(LibraryBaseTest):
|
|||||||
def test_get(self): # {{{
|
def test_get(self): # {{{
|
||||||
'Test /get'
|
'Test /get'
|
||||||
with self.create_server() as server:
|
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()
|
conn = server.connect()
|
||||||
|
|
||||||
def get(what, book_id, library_id=None, q=''):
|
def get(what, book_id, library_id=None, q=''):
|
||||||
|
@ -470,18 +470,18 @@ class ReadOnlyFileBuffer(object):
|
|||||||
def close(self):
|
def close(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def get_db(ctx, library_id):
|
def get_db(ctx, rd, library_id):
|
||||||
db = ctx.get_library(library_id)
|
db = ctx.get_library(rd, library_id)
|
||||||
if db is None:
|
if db is None:
|
||||||
raise HTTPNotFound('Library %r not found' % library_id)
|
raise HTTPNotFound('Library %r not found' % library_id)
|
||||||
return db
|
return db
|
||||||
|
|
||||||
def get_library_data(ctx, query):
|
def get_library_data(ctx, rd):
|
||||||
library_id = query.get('library_id')
|
library_id = rd.query.get('library_id')
|
||||||
library_map, default_library = ctx.library_map
|
library_map, default_library = ctx.library_info(rd)
|
||||||
if library_id not in library_map:
|
if library_id not in library_map:
|
||||||
library_id = default_library
|
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
|
return db, library_id, library_map, default_library
|
||||||
|
|
||||||
class Offsets(object):
|
class Offsets(object):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user