def get_recently_watched(self, user_id=None, limit='10'): if not session.allow_session_user(user_id): return [] monitor_db = database.MonitorDatabase() recently_watched = [] if not limit.isdigit(): limit = '10' try: if str(user_id).isdigit(): query = 'SELECT session_history.id, session_history.media_type, ' \ 'session_history.rating_key, session_history.parent_rating_key, session_history.grandparent_rating_key, ' \ 'title, parent_title, grandparent_title, thumb, parent_thumb, grandparent_thumb, media_index, parent_media_index, ' \ 'year, started, user ' \ 'FROM session_history_metadata ' \ 'JOIN session_history ON session_history_metadata.id = session_history.id ' \ 'WHERE user_id = ? ' \ 'GROUP BY (CASE WHEN session_history.media_type = "track" THEN session_history.parent_rating_key ' \ ' ELSE session_history.rating_key END) ' \ 'ORDER BY started DESC LIMIT ?' result = monitor_db.select(query, args=[user_id, limit]) else: result = [] except Exception as e: logger.warn(u"PlexPy Users :: Unable to execute database query for get_recently_watched: %s." % e) result = [] for row in result: if row['media_type'] == 'episode' and row['parent_thumb']: thumb = row['parent_thumb'] elif row['media_type'] == 'episode': thumb = row['grandparent_thumb'] else: thumb = row['thumb'] recent_output = {'row_id': row['id'], 'media_type': row['media_type'], 'rating_key': row['rating_key'], 'parent_rating_key': row['parent_rating_key'], 'grandparent_rating_key': row['grandparent_rating_key'], 'title': row['title'], 'parent_title': row['parent_title'], 'grandparent_title': row['grandparent_title'], 'thumb': thumb, 'media_index': row['media_index'], 'parent_media_index': row['parent_media_index'], 'year': row['year'], 'time': row['started'], 'user': row['user'] } recently_watched.append(recent_output) return recently_watched
def get_watch_time_stats(self, user_id=None): if not session.allow_session_user(user_id): return [] monitor_db = database.MonitorDatabase() time_queries = [1, 7, 30, 0] user_watch_time_stats = [] for days in time_queries: try: if days > 0: if str(user_id).isdigit(): query = 'SELECT (SUM(stopped - started) - ' \ ' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \ 'COUNT(id) AS total_plays ' \ 'FROM session_history ' \ 'WHERE datetime(stopped, "unixepoch", "localtime") >= datetime("now", "-%s days", "localtime") ' \ 'AND user_id = ?' % days result = monitor_db.select(query, args=[user_id]) else: result = [] else: if str(user_id).isdigit(): query = 'SELECT (SUM(stopped - started) - ' \ ' SUM(CASE WHEN paused_counter is null THEN 0 ELSE paused_counter END)) as total_time, ' \ 'COUNT(id) AS total_plays ' \ 'FROM session_history ' \ 'WHERE user_id = ?' result = monitor_db.select(query, args=[user_id]) else: result = [] except Exception as e: logger.warn( u"Tautulli Users :: Unable to execute database query for get_watch_time_stats: %s." % e) result = [] for item in result: if item['total_time']: total_time = item['total_time'] total_plays = item['total_plays'] else: total_time = 0 total_plays = 0 row = { 'query_days': days, 'total_time': total_time, 'total_plays': total_plays } user_watch_time_stats.append(row) return user_watch_time_stats
def set_last_seen(device_token=None): db = database.MonitorDatabase() last_seen = int(time.time()) try: result = db.action('UPDATE mobile_devices SET last_seen = ? WHERE device_token = ?', args=[last_seen, device_token]) except Exception as e: logger.warn(u"Tautulli MobileApp :: Failed to set last_seen time for device: %s." % e) return
def delete_login_log(self): monitor_db = database.MonitorDatabase() try: logger.info(u"Tautulli Users :: Clearing login logs from database.") monitor_db.action('DELETE FROM user_login') monitor_db.action('VACUUM') return True except Exception as e: logger.warn(u"Tautulli Users :: Unable to execute database query for delete_login_log: %s." % e) return False
def set_newsletter_config(newsletter_id=None, agent_id=None, **kwargs): if str(agent_id).isdigit(): agent_id = int(agent_id) else: logger.error(u"Tautulli Newsletters :: Unable to set exisiting newsletter: invalid agent_id %s." % agent_id) return False agent = next((a for a in available_newsletter_agents() if a['id'] == agent_id), None) if not agent: logger.error(u"Tautulli Newsletters :: Unable to retrieve existing newsletter agent: invalid agent_id %s." % agent_id) return False config_prefix = 'newsletter_config_' email_config_prefix = 'newsletter_email_' newsletter_config = {k[len(config_prefix):]: kwargs.pop(k) for k in kwargs.keys() if k.startswith(config_prefix)} email_config = {k[len(email_config_prefix):]: kwargs.pop(k) for k in kwargs.keys() if k.startswith(email_config_prefix)} subject = kwargs.pop('subject') body = kwargs.pop('body') message = kwargs.pop('message') agent_class = get_agent_class(agent_id=agent['id'], config=newsletter_config, email_config=email_config, subject=subject, body=body, message=message) keys = {'id': newsletter_id} values = {'agent_id': agent['id'], 'agent_name': agent['name'], 'agent_label': agent['label'], 'friendly_name': kwargs.get('friendly_name', ''), 'newsletter_config': json.dumps(agent_class.config), 'email_config': json.dumps(agent_class.email_config), 'subject': agent_class.subject, 'body': agent_class.body, 'message': agent_class.message, 'cron': kwargs.get('cron'), 'active': kwargs.get('active') } db = database.MonitorDatabase() try: db.upsert(table_name='newsletters', key_dict=keys, value_dict=values) logger.info(u"Tautulli Newsletters :: Updated newsletter agent: %s (newsletter_id %s)." % (agent['label'], newsletter_id)) newsletter_handler.schedule_newsletters(newsletter_id=newsletter_id) return True except Exception as e: logger.warn(u"Tautulli Newsletters :: Unable to update newsletter agent: %s." % e) return False
def delete_newsletter(newsletter_id=None): db = database.MonitorDatabase() if str(newsletter_id).isdigit(): logger.debug( "Tautulli Newsletters :: Deleting newsletter_id %s from the database." % newsletter_id) result = db.action('DELETE FROM newsletters WHERE id = ?', args=[newsletter_id]) return True else: return False
def get_newsletter_config(newsletter_id=None, mask_passwords=False): if str(newsletter_id).isdigit(): newsletter_id = int(newsletter_id) else: logger.error( "Tautulli Newsletters :: Unable to retrieve newsletter config: invalid newsletter_id %s." % newsletter_id) return None db = database.MonitorDatabase() result = db.select_single('SELECT * FROM newsletters WHERE id = ?', args=[newsletter_id]) if not result: return None try: config = json.loads(result.pop('newsletter_config', '{}')) email_config = json.loads(result.pop('email_config', '{}')) subject = result.pop('subject') body = result.pop('body') message = result.pop('message') newsletter_agent = get_agent_class( newsletter_id=newsletter_id, newsletter_id_name=result['id_name'], agent_id=result['agent_id'], config=config, email_config=email_config, subject=subject, body=body, message=message) except Exception as e: logger.error( "Tautulli Newsletters :: Failed to get newsletter config options: %s." % e) return if mask_passwords: newsletter_agent.email_config = helpers.mask_config_passwords( newsletter_agent.email_config) result['subject'] = newsletter_agent.subject result['body'] = newsletter_agent.body result['message'] = newsletter_agent.message result['config'] = newsletter_agent.config result['email_config'] = newsletter_agent.email_config result['config_options'] = newsletter_agent.return_config_options( mask_passwords=mask_passwords) result[ 'email_config_options'] = newsletter_agent.return_email_config_options( mask_passwords=mask_passwords) return result
def delete_mobile_device(mobile_device_id=None): db = database.MonitorDatabase() if mobile_device_id: logger.debug( u"Tautulli MobileApp :: Deleting device_id %s from the database." % mobile_device_id) result = db.action('DELETE FROM mobile_devices WHERE id = ?', args=[mobile_device_id]) return True else: return False
def get_mobile_device_config(mobile_device_id=None): if str(mobile_device_id).isdigit(): mobile_device_id = int(mobile_device_id) else: logger.error(u"Tautulli MobileApp :: Unable to retrieve mobile device config: invalid mobile_device_id %s." % mobile_device_id) return None db = database.MonitorDatabase() result = db.select_single('SELECT * FROM mobile_devices WHERE id = ?', args=[mobile_device_id]) return result
def generate_newsletter_uuid(): uuid = '' uuid_exists = 0 db = database.MonitorDatabase() while not uuid or uuid_exists: uuid = plexpy.generate_uuid()[:8] result = db.select_single( 'SELECT EXISTS(SELECT uuid FROM newsletter_log WHERE uuid = ?) as uuid_exists', [uuid]) uuid_exists = result['uuid_exists'] return uuid
def _dic_from_query(self, query): db = database.MonitorDatabase() rows = db.select(query) rows_as_dic = [] for row in rows: row_as_dic = dict(zip(row.keys(), row)) rows_as_dic.append(row_as_dic) return rows_as_dic
def refresh_users(): logger.info("Tautulli Users :: Requesting users list refresh...") result = plextv.PlexTV().get_full_users_list() server_id = plexpy.CONFIG.PMS_IDENTIFIER if not server_id: logger.error( "Tautulli Users :: No PMS identifier, cannot refresh users. Verify server in settings." ) return 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']) elif item.get('server_token'): libs = libraries.Libraries().get_sections() item['shared_libraries'] = ';'.join( [str(l['section_id']) for l in libs]) keys_dict = {"user_id": item.pop('user_id')} # Check if we've set a custom avatar if so don't overwrite it. if keys_dict['user_id']: avatar_urls = monitor_db.select( 'SELECT thumb, custom_avatar_url ' 'FROM users WHERE user_id = ?', [keys_dict['user_id']]) if avatar_urls: if not avatar_urls[0]['custom_avatar_url'] or \ avatar_urls[0]['custom_avatar_url'] == avatar_urls[0]['thumb']: item['custom_avatar_url'] = item['thumb'] else: item['custom_avatar_url'] = item['thumb'] monitor_db.upsert('users', key_dict=keys_dict, value_dict=item) 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("Tautulli Users :: Users list refreshed.") return True else: logger.warn("Tautulli Users :: Unable to refresh users list.") return False
def set_official(device_id, onesignal_id): db = database.MonitorDatabase() official = validate_onesignal_id(onesignal_id=onesignal_id) try: result = db.action( 'UPDATE mobile_devices SET official = ? WHERE device_id = ?', args=[official, device_id]) except Exception as e: logger.warn( "Tautulli MobileApp :: Failed to set official flag for device: %s." % e) return
def set_config(self, section_id=None, custom_thumb='', do_notify=1, keep_history=1, do_notify_created=1): if section_id: monitor_db = database.MonitorDatabase() key_dict = {'section_id': section_id} value_dict = {'custom_thumb_url': custom_thumb, 'do_notify': do_notify, 'do_notify_created': do_notify_created, 'keep_history': keep_history} try: monitor_db.upsert('library_sections', value_dict, key_dict) except Exception as e: logger.warn(u"PlexPy Libraries :: Unable to execute database query for set_config: %s." % e)
def import_users(): logger.debug("Tautulli Importer :: Importing Plexivity Users...") monitor_db = database.MonitorDatabase() query = 'INSERT OR IGNORE INTO users (user_id, username) ' \ 'SELECT user_id, user ' \ 'FROM session_history WHERE user_id != 1 GROUP BY user_id' try: monitor_db.action(query) logger.debug("Tautulli Importer :: Users imported.") except: logger.debug("Tautulli Importer :: Failed to import users.")
def get_player_stats(self, user_id=None, grouping=None): if not session.allow_session_user(user_id): return [] if grouping is None: grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES monitor_db = database.MonitorDatabase() player_stats = [] result_id = 0 group_by = 'reference_id' if grouping else 'id' try: if str(user_id).isdigit(): query = 'SELECT player, COUNT(DISTINCT %s) as total_plays, (SUM(stopped - started) - ' \ 'SUM(CASE WHEN paused_counter IS NULL THEN 0 ELSE paused_counter END)) AS total_time, ' \ 'platform ' \ 'FROM session_history ' \ 'WHERE user_id = ? ' \ 'GROUP BY player ' \ 'ORDER BY total_plays DESC, total_time DESC' % group_by result = monitor_db.select(query, args=[user_id]) else: result = [] except Exception as e: logger.warn( "Tautulli Users :: Unable to execute database query for get_player_stats: %s." % e) result = [] for item in result: # Rename Mystery platform names platform = common.PLATFORM_NAME_OVERRIDES.get( item['platform'], item['platform']) platform_name = next((v for k, v in common.PLATFORM_NAMES.items() if k in platform.lower()), 'default') row = { 'player_name': item['player'], 'platform': platform, 'platform_name': platform_name, 'total_plays': item['total_plays'], 'total_time': item['total_time'], 'result_id': result_id } player_stats.append(row) result_id += 1 return player_stats
def get_user_stats(self, section_id=None, grouping=None): if not session.allow_session_library(section_id): return [] if grouping is None: grouping = plexpy.CONFIG.GROUP_HISTORY_TABLES monitor_db = database.MonitorDatabase() user_stats = [] group_by = 'session_history.reference_id' if grouping else 'session_history.id' try: if str(section_id).isdigit(): query = 'SELECT (CASE WHEN users.friendly_name IS NULL OR TRIM(users.friendly_name) = "" ' \ 'THEN users.username ELSE users.friendly_name END) AS friendly_name, ' \ 'users.user_id, users.thumb, users.custom_avatar_url AS custom_thumb, ' \ 'COUNT(DISTINCT %s) AS user_count ' \ 'FROM session_history ' \ 'JOIN session_history_metadata ON session_history_metadata.id = session_history.id ' \ 'JOIN users ON users.user_id = session_history.user_id ' \ 'WHERE section_id = ? ' \ 'GROUP BY users.user_id ' \ 'ORDER BY user_count DESC' % group_by result = monitor_db.select(query, args=[section_id]) else: result = [] except Exception as e: logger.warn( "Tautulli Libraries :: Unable to execute database query for get_user_stats: %s." % e) result = [] for item in result: if item['custom_thumb'] and item['custom_thumb'] != item['thumb']: user_thumb = item['custom_thumb'] elif item['thumb']: user_thumb = item['thumb'] else: user_thumb = common.DEFAULT_USER_THUMB row = { 'friendly_name': item['friendly_name'], 'user_id': item['user_id'], 'user_thumb': user_thumb, 'total_plays': item['user_count'] } user_stats.append(row) return session.mask_session_info(user_stats, mask_metadata=False)
def get_library_details(section_id=section_id, server_id=server_id): monitor_db = database.MonitorDatabase() try: if str(section_id).isdigit(): query = 'SELECT id AS row_id, server_id, section_id, section_name, section_type, ' \ 'count, parent_count, child_count, ' \ 'thumb AS library_thumb, custom_thumb_url AS custom_thumb, art AS library_art, ' \ 'custom_art_url AS custom_art, is_active, ' \ 'do_notify, do_notify_created, keep_history, deleted_section ' \ 'FROM library_sections ' \ 'WHERE section_id = ? AND server_id = ? ' result = monitor_db.select(query, args=[section_id, server_id]) else: result = [] except Exception as e: logger.warn("Tautulli Libraries :: Unable to execute database query for get_details: %s." % e) result = [] library_details = {} if result: for item in result: if item['custom_thumb'] and item['custom_thumb'] != item['library_thumb']: library_thumb = item['custom_thumb'] elif item['library_thumb']: library_thumb = item['library_thumb'] else: library_thumb = common.DEFAULT_COVER_THUMB if item['custom_art'] and item['custom_art'] != item['library_art']: library_art = item['custom_art'] else: library_art = item['library_art'] library_details = {'row_id': item['row_id'], 'server_id': item['server_id'], 'section_id': item['section_id'], 'section_name': item['section_name'], 'section_type': item['section_type'], 'library_thumb': library_thumb, 'library_art': library_art, 'count': item['count'], 'parent_count': item['parent_count'], 'child_count': item['child_count'], 'is_active': item['is_active'], 'do_notify': item['do_notify'], 'do_notify_created': item['do_notify_created'], 'keep_history': item['keep_history'], 'deleted_section': item['deleted_section'] } return library_details
def get_user_id(self, user=None): if user: try: monitor_db = database.MonitorDatabase() query = 'SELECT user_id FROM users WHERE username = ?' result = monitor_db.select_single(query, args=[user]) if result: return result['user_id'] else: return None except: return None return None
def set_config(self, user_id=None, friendly_name='', custom_thumb='', do_notify=1, keep_history=1, allow_guest=1): if str(user_id).isdigit(): monitor_db = database.MonitorDatabase() key_dict = {'user_id': user_id} value_dict = {'friendly_name': friendly_name, 'custom_avatar_url': custom_thumb, 'do_notify': do_notify, 'keep_history': keep_history, 'allow_guest': allow_guest } try: monitor_db.upsert('users', value_dict, key_dict) except Exception as e: logger.warn(u"Tautulli Users :: Unable to execute database query for set_config: %s." % e)
def delete_duplicate_libraries(self): monitor_db = database.MonitorDatabase() # Refresh the PMS_URL to make sure the server_id is updated plextv.get_server_resources() server_id = plexpy.CONFIG.PMS_IDENTIFIER try: logger.debug("Tautulli Libraries :: Deleting libraries where server_id does not match %s." % server_id) monitor_db.action('DELETE FROM library_sections WHERE server_id != ?', [server_id]) return 'Deleted duplicate libraries from the database.' except Exception as e: logger.warn("Tautulli Libraries :: Unable to delete duplicate libraries: %s." % e)
def add_newsletter_config(agent_id=None, **kwargs): if str(agent_id).isdigit(): agent_id = int(agent_id) else: logger.error( "Tautulli Newsletters :: Unable to add new newsletter: invalid agent_id %s." % agent_id) return False agent = next( (a for a in available_newsletter_agents() if a['id'] == agent_id), None) if not agent: logger.error( "Tautulli Newsletters :: Unable to retrieve new newsletter agent: invalid agent_id %s." % agent_id) return False agent_class = get_agent_class(agent_id=agent['id']) keys = {'id': None} values = { 'agent_id': agent['id'], 'agent_name': agent['name'], 'agent_label': agent['label'], 'id_name': '', 'friendly_name': '', 'newsletter_config': json.dumps(agent_class.config), 'email_config': json.dumps(agent_class.email_config), 'subject': agent_class.subject, 'body': agent_class.body, 'message': agent_class.message } db = database.MonitorDatabase() try: db.upsert(table_name='newsletters', key_dict=keys, value_dict=values) newsletter_id = db.last_insert_id() logger.info( "Tautulli Newsletters :: Added new newsletter agent: %s (newsletter_id %s)." % (agent['label'], newsletter_id)) blacklist_logger() return newsletter_id except Exception as e: logger.warn( "Tautulli Newsletters :: Unable to add newsletter agent: %s." % e) return False
def undelete(self, user_id=None, username=None): monitor_db = database.MonitorDatabase() try: if user_id and str(user_id).isdigit(): query = 'SELECT * FROM users WHERE user_id = ?' result = monitor_db.select(query=query, args=[user_id]) if result: logger.info( u"Tautulli Users :: Re-adding user with id %s to database." % user_id) monitor_db.action( 'UPDATE users SET deleted_user = 0 WHERE user_id = ?', [user_id]) monitor_db.action( 'UPDATE users SET keep_history = 1 WHERE user_id = ?', [user_id]) monitor_db.action( 'UPDATE users SET do_notify = 1 WHERE user_id = ?', [user_id]) return True else: return False elif username: query = 'SELECT * FROM users WHERE username = ?' result = monitor_db.select(query=query, args=[username]) if result: logger.info( u"Tautulli Users :: Re-adding user with username %s to database." % username) monitor_db.action( 'UPDATE users SET deleted_user = 0 WHERE username = ?', [username]) monitor_db.action( 'UPDATE users SET keep_history = 1 WHERE username = ?', [username]) monitor_db.action( 'UPDATE users SET do_notify = 1 WHERE username = ?', [username]) return True else: return False except Exception as e: logger.warn( u"Tautulli Users :: Unable to execute database query for undelete: %s." % e)
def set_official(device_id, onesignal_id): db = database.MonitorDatabase() official = validate_onesignal_id(onesignal_id=onesignal_id) platform = 'android' if official > 0 else None try: result = db.action( 'UPDATE mobile_devices ' 'SET official = ?, platform = coalesce(platform, ?) ' 'WHERE device_id = ?', args=[official, platform, device_id]) except Exception as e: logger.warn( "Tautulli MobileApp :: Failed to set official flag for device: %s." % e) return
def get_newsletters(newsletter_id=None): where = where_id = '' args = [] if newsletter_id: where = 'WHERE ' if newsletter_id: where_id += 'id = ?' args.append(newsletter_id) where += ' AND '.join([w for w in [where_id] if w]) db = database.MonitorDatabase() result = db.select('SELECT id, agent_id, agent_name, agent_label, ' 'friendly_name, cron, active FROM newsletters %s' % where, args=args) return result
def delete(self, user_id=None): monitor_db = database.MonitorDatabase() try: if str(user_id).isdigit(): self.delete_all_history(user_id) logger.info(u"Tautulli Users :: Deleting user with id %s from database." % user_id) monitor_db.action('UPDATE users SET deleted_user = 1 WHERE user_id = ?', [user_id]) monitor_db.action('UPDATE users SET keep_history = 0 WHERE user_id = ?', [user_id]) monitor_db.action('UPDATE users SET do_notify = 0 WHERE user_id = ?', [user_id]) return 'Deleted user with id %s.' % user_id else: return 'Unable to delete user, user_id not valid.' except Exception as e: logger.warn(u"Tautulli Users :: Unable to execute database query for delete: %s." % e)
def undelete(self, section_id=None, section_name=None): monitor_db = database.MonitorDatabase() try: if section_id and section_id.isdigit(): logger.info( u"PlexPy Libraries :: Re-adding library with id %s to database." % section_id) monitor_db.action( 'UPDATE library_sections SET deleted_section = 0 WHERE section_id = ?', [section_id]) monitor_db.action( 'UPDATE library_sections SET keep_history = 1 WHERE section_id = ?', [section_id]) monitor_db.action( 'UPDATE library_sections SET do_notify = 1 WHERE section_id = ?', [section_id]) monitor_db.action( 'UPDATE library_sections SET do_notify_created = 1 WHERE section_id = ?', [section_id]) return 'Re-added library with id %s.' % section_id elif section_name: logger.info( u"PlexPy Libraries :: Re-adding library with name %s to database." % section_name) monitor_db.action( 'UPDATE library_sections SET deleted_section = 0 WHERE section_name = ?', [section_name]) monitor_db.action( 'UPDATE library_sections SET keep_history = 1 WHERE section_name = ?', [section_name]) monitor_db.action( 'UPDATE library_sections SET do_notify = 1 WHERE section_name = ?', [section_name]) monitor_db.action( 'UPDATE library_sections SET do_notify_created = 1 WHERE section_name = ?', [section_name]) return 'Re-added library with section_name %s.' % section_name else: return 'Unable to re-add library, section_id or section_name not valid.' except Exception as e: logger.warn( u"PlexPy Libraries :: Unable to execute database query for undelete: %s." % e)
def add_mobile_device(device_id=None, device_name=None, device_token=None, platform=None, version=None, friendly_name=None, onesignal_id=None): db = database.MonitorDatabase() keys = {'device_id': device_id} values = { 'device_name': device_name, 'device_token': device_token, 'platform': platform, 'version': version, 'onesignal_id': onesignal_id } if friendly_name: values['friendly_name'] = friendly_name try: result = db.upsert(table_name='mobile_devices', key_dict=keys, value_dict=values) blacklist_logger() except Exception as e: logger.warn( "Tautulli MobileApp :: Failed to register mobile device in the database: %s." % e) return if result == 'insert': logger.info( "Tautulli MobileApp :: Registered mobile device '%s' in the database." % device_name) else: logger.info( "Tautulli MobileApp :: Re-registered mobile device '%s' in the database." % device_name) set_last_seen(device_token=device_token) threading.Thread(target=set_official, args=[device_id, onesignal_id]).start() return True
def get_mobile_devices(device_id=None, device_token=None): where = where_id = where_token = '' args = [] if device_id or device_token: where = 'WHERE ' if device_id: where_id += 'device_id = ?' args.append(device_id) if device_token: where_token = 'device_token = ?' args.append(device_token) where += ' AND '.join([w for w in [where_id, where_token] if w]) db = database.MonitorDatabase() result = db.select('SELECT * FROM mobile_devices %s' % where, args=args) return result
def set_user_login(self, user_id=None, user=None, user_group=None, ip_address=None, host=None, user_agent=None): if user_id is None or str(user_id).isdigit(): monitor_db = database.MonitorDatabase() keys = {'timestamp': int(time.time()), 'user_id': user_id} values = {'user': user, 'user_group': user_group, 'ip_address': ip_address, 'host': host, 'user_agent': user_agent} try: monitor_db.upsert(table_name='user_login', key_dict=keys, value_dict=values) except Exception as e: logger.warn(u"PlexPy Users :: Unable to execute database query for set_login_log: %s." % e)