From ce289995ff54efdeddb29dda885b5d8082baa033 Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Thu, 9 Apr 2020 23:15:08 -0700 Subject: [PATCH] Add user is_active to database --- plexpy/__init__.py | 19 ++++++++++++++----- plexpy/plextv.py | 2 ++ plexpy/users.py | 7 +++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/plexpy/__init__.py b/plexpy/__init__.py index 60bffef3..24b58f8e 100644 --- a/plexpy/__init__.py +++ b/plexpy/__init__.py @@ -663,11 +663,11 @@ def dbcheck(): c_db.execute( 'CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, ' 'user_id INTEGER DEFAULT NULL UNIQUE, username TEXT NOT NULL, friendly_name TEXT, ' - 'thumb TEXT, custom_avatar_url TEXT, email TEXT, is_admin INTEGER DEFAULT 0, is_home_user INTEGER DEFAULT NULL, ' - 'is_allow_sync INTEGER DEFAULT NULL, is_restricted INTEGER DEFAULT NULL, do_notify INTEGER DEFAULT 1, ' - 'keep_history INTEGER DEFAULT 1, deleted_user INTEGER DEFAULT 0, allow_guest INTEGER DEFAULT 0, ' - 'user_token TEXT, server_token TEXT, shared_libraries TEXT, filter_all TEXT, filter_movies TEXT, filter_tv TEXT, ' - 'filter_music TEXT, filter_photos TEXT)' + 'thumb TEXT, custom_avatar_url TEXT, email TEXT, is_active INTEGER DEFAULT 1, is_admin INTEGER DEFAULT 0, ' + 'is_home_user INTEGER DEFAULT NULL, is_allow_sync INTEGER DEFAULT NULL, is_restricted INTEGER DEFAULT NULL, ' + 'do_notify INTEGER DEFAULT 1, keep_history INTEGER DEFAULT 1, deleted_user INTEGER DEFAULT 0, ' + 'allow_guest INTEGER DEFAULT 0, user_token TEXT, server_token TEXT, shared_libraries TEXT, ' + 'filter_all TEXT, filter_movies TEXT, filter_tv TEXT, filter_music TEXT, filter_photos TEXT)' ) # library_sections table :: This table keeps record of the servers library sections @@ -1733,6 +1733,15 @@ def dbcheck(): 'ALTER TABLE users ADD COLUMN is_admin INTEGER DEFAULT 0' ) + # Upgrade users table from earlier versions + try: + c_db.execute('SELECT is_active FROM users') + except sqlite3.OperationalError: + logger.debug(u"Altering database. Updating database table users.") + c_db.execute( + 'ALTER TABLE users ADD COLUMN is_active INTEGER DEFAULT 1' + ) + # Upgrade notify_log table from earlier versions try: c_db.execute('SELECT poster_url FROM notify_log') diff --git a/plexpy/plextv.py b/plexpy/plextv.py index 1405a937..e389cf07 100644 --- a/plexpy/plextv.py +++ b/plexpy/plextv.py @@ -396,6 +396,7 @@ class PlexTV(object): "username": helpers.get_xml_attr(a, 'username'), "thumb": helpers.get_xml_attr(a, 'thumb'), "email": helpers.get_xml_attr(a, 'email'), + "is_active": 1, "is_admin": 1, "is_home_user": helpers.get_xml_attr(a, 'home'), "is_allow_sync": 1, @@ -423,6 +424,7 @@ class PlexTV(object): "username": helpers.get_xml_attr(a, 'title'), "thumb": helpers.get_xml_attr(a, 'thumb'), "email": helpers.get_xml_attr(a, 'email'), + "is_active": 1, "is_admin": 0, "is_home_user": helpers.get_xml_attr(a, 'home'), "is_allow_sync": helpers.get_xml_attr(a, 'allowSync'), diff --git a/plexpy/users.py b/plexpy/users.py index e4b36cd5..5c78e0c0 100644 --- a/plexpy/users.py +++ b/plexpy/users.py @@ -34,7 +34,11 @@ def refresh_users(): if result: monitor_db = database.MonitorDatabase() + # Keep track of user_id to update is_active status + user_ids = [0] # Local user always considered active + for item in result: + user_ids.append(helpers.cast_to_int(item['user_id'])) if item.get('shared_libraries'): item['shared_libraries'] = ';'.join(item['shared_libraries']) @@ -58,6 +62,9 @@ def refresh_users(): monitor_db.upsert('users', item, keys_dict) + query = 'UPDATE users SET is_active = 0 WHERE user_id NOT IN ({})'.format(', '.join(['?'] * len(user_ids))) + monitor_db.action(query=query, args=user_ids) + logger.info(u"Tautulli Users :: Users list refreshed.") return True else: