mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
...
This commit is contained in:
parent
47334ca59b
commit
5d4628a993
@ -12,6 +12,14 @@ import apsw
|
|||||||
from calibre.constants import config_dir
|
from calibre.constants import config_dir
|
||||||
from calibre.utils.config import to_json, from_json
|
from calibre.utils.config import to_json, from_json
|
||||||
|
|
||||||
|
def as_json(data):
|
||||||
|
return json.dumps(data, ensure_ascii=False, default=to_json)
|
||||||
|
|
||||||
|
def load_json(raw):
|
||||||
|
try:
|
||||||
|
return json.loads(raw, object_hook=from_json)
|
||||||
|
except Exception:
|
||||||
|
return {}
|
||||||
|
|
||||||
class UserManager(object):
|
class UserManager(object):
|
||||||
|
|
||||||
@ -36,7 +44,7 @@ class UserManager(object):
|
|||||||
pw TEXT NOT NULL,
|
pw TEXT NOT NULL,
|
||||||
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
|
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
|
||||||
session_data TEXT NOT NULL DEFAULT "{}",
|
session_data TEXT NOT NULL DEFAULT "{}",
|
||||||
restriction TEXT NOT NULL DEFAULT "",
|
restriction TEXT NOT NULL DEFAULT "{}",
|
||||||
readonly TEXT NOT NULL DEFAULT "n",
|
readonly TEXT NOT NULL DEFAULT "n",
|
||||||
misc_data TEXT NOT NULL DEFAULT "{}",
|
misc_data TEXT NOT NULL DEFAULT "{}",
|
||||||
UNIQUE(name)
|
UNIQUE(name)
|
||||||
@ -55,17 +63,14 @@ class UserManager(object):
|
|||||||
with self.lock:
|
with self.lock:
|
||||||
for data, in self.conn.cursor().execute(
|
for data, in self.conn.cursor().execute(
|
||||||
'SELECT data FROM users WHERE name=?', (username,)):
|
'SELECT data FROM users WHERE name=?', (username,)):
|
||||||
try:
|
return load_json(data)
|
||||||
return json.loads(data, object_hook=from_json)
|
return {}
|
||||||
except Exception:
|
|
||||||
break
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def set_session_data(self, username, data):
|
def set_session_data(self, username, data):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
conn = self.conn
|
conn = self.conn
|
||||||
c = conn.cursor()
|
c = conn.cursor()
|
||||||
data = json.dumps(data, ensure_ascii=False, default=to_json)
|
data = as_json(data)
|
||||||
if isinstance(data, bytes):
|
if isinstance(data, bytes):
|
||||||
data = data.decode('utf-8')
|
data = data.decode('utf-8')
|
||||||
c.execute('UPDATE users SET data=? WHERE name=?', (data, username))
|
c.execute('UPDATE users SET data=? WHERE name=?', (data, username))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user