mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-16 02:02:58 -07:00
Add all content rating and label filters for guest
This commit is contained in:
parent
2aa059a170
commit
e147ce9039
14 changed files with 599 additions and 220 deletions
|
@ -14,7 +14,6 @@
|
|||
# along with PlexPy. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import cherrypy
|
||||
import threading
|
||||
|
||||
from plexpy import common
|
||||
|
||||
|
@ -28,7 +27,6 @@ def get_session_info():
|
|||
_session = {'user_id': None,
|
||||
'user': None,
|
||||
'user_group': 'admin',
|
||||
'user_libraries': None,
|
||||
'expiry': None}
|
||||
try:
|
||||
return cherrypy.session.get(SESSION_KEY, _session)
|
||||
|
@ -49,12 +47,49 @@ def get_session_user_id():
|
|||
_session = get_session_info()
|
||||
return str(_session['user_id']) if _session and _session['user_id'] else None
|
||||
|
||||
def get_session_libraries():
|
||||
def get_session_shared_libraries():
|
||||
"""
|
||||
Returns a tuple of section_id for the current logged in session
|
||||
"""
|
||||
_session = get_session_info()
|
||||
return _session['user_libraries'] if _session and _session['user_libraries'] else None
|
||||
from plexpy import users
|
||||
user_details = users.Users().get_details(user_id=get_session_user_id())
|
||||
return user_details['shared_libraries']
|
||||
|
||||
def get_session_library_filters():
|
||||
"""
|
||||
Returns a dict of library filters for the current logged in session
|
||||
|
||||
{'content_rating': ('PG', 'R')
|
||||
'labels': ('label1', label2')},
|
||||
|
||||
"""
|
||||
from plexpy import users
|
||||
filters = users.Users().get_filters(user_id=get_session_user_id())
|
||||
return filters
|
||||
|
||||
def get_session_library_filters_type(filters, media_type=None):
|
||||
"""
|
||||
Returns a dict of library filters for the current logged in session
|
||||
|
||||
{'content_rating': ('PG', 'R')
|
||||
'labels': ('label1', label2')},
|
||||
|
||||
"""
|
||||
if media_type == 'movie':
|
||||
filters = filters.get('filter_movies', ())
|
||||
elif media_type == 'show' or media_type == 'season' or media_type == 'episode':
|
||||
filters = filters.get('filter_tv', ())
|
||||
elif media_type == 'artist' or media_type == 'album' or media_type == 'track':
|
||||
filters = filters.get('filter_music', ())
|
||||
elif media_type == 'photo' or media_type == 'photoAlbum' or media_type == 'picture':
|
||||
filters = filters.get('filter_photos', ())
|
||||
else:
|
||||
filters = filters.get('filter_all', ())
|
||||
|
||||
content_rating = filters.get('content_rating', ())
|
||||
labels = filters.get('labels', ())
|
||||
|
||||
return content_rating, tuple(f.lower() for f in labels)
|
||||
|
||||
def allow_session_user(user_id):
|
||||
"""
|
||||
|
@ -69,74 +104,11 @@ def allow_session_library(section_id):
|
|||
"""
|
||||
Returns True or False if the section_id is allowed for the current logged in session
|
||||
"""
|
||||
session_library_ids = get_session_libraries()
|
||||
session_library_ids = get_session_shared_libraries()
|
||||
if session_library_ids and str(section_id) not in session_library_ids:
|
||||
return False
|
||||
return True
|
||||
|
||||
def filter_session_info(list_of_dicts, filter_key=None):
|
||||
"""
|
||||
Filters a list of dictionary items to only return the info for the current logged in session
|
||||
"""
|
||||
session_user_id = get_session_user_id()
|
||||
session_library_ids = get_session_libraries()
|
||||
|
||||
list_of_dicts = friendly_name_to_username(list_of_dicts)
|
||||
|
||||
if filter_key == 'user_id' and session_user_id:
|
||||
return [d for d in list_of_dicts if str(d.get('user_id','')) == session_user_id]
|
||||
|
||||
elif filter_key == 'section_id' and session_library_ids:
|
||||
return [d for d in list_of_dicts if str(d.get('section_id','')) in session_library_ids]
|
||||
|
||||
return list_of_dicts
|
||||
|
||||
def mask_session_info(list_of_dicts, mask_metadata=True):
|
||||
"""
|
||||
Masks user info in a list of dictionary items to only display info for the current logged in session
|
||||
"""
|
||||
session_user = get_session_user()
|
||||
session_user_id = get_session_user_id()
|
||||
session_library_ids = get_session_libraries()
|
||||
|
||||
keys_to_mask = {'user_id': '',
|
||||
'user': 'Plex User',
|
||||
'friendly_name': 'Plex User',
|
||||
'user_thumb': common.DEFAULT_USER_THUMB,
|
||||
'ip_address': 'N/A',
|
||||
'machine_id': '',
|
||||
'player': 'Player'
|
||||
}
|
||||
|
||||
metadata_to_mask = {'media_index': '',
|
||||
'parent_media_index': '',
|
||||
'art': common.DEFAULT_ART,
|
||||
'parent_thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'grandparent_thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'bif_thumb': '',
|
||||
'grandparent_title': '',
|
||||
'parent_title': '',
|
||||
'title': '',
|
||||
'rating_key': '',
|
||||
'parent_rating_key': '',
|
||||
'grandparent_rating_key': '',
|
||||
'year': ''
|
||||
}
|
||||
|
||||
list_of_dicts = friendly_name_to_username(list_of_dicts)
|
||||
|
||||
for d in list_of_dicts:
|
||||
if session_user_id and not (str(d.get('user_id')) == session_user_id or d.get('user') == session_user):
|
||||
for k, v in keys_to_mask.iteritems():
|
||||
if k in d: d[k] = keys_to_mask[k]
|
||||
|
||||
if mask_metadata and session_library_ids and str(d.get('section_id','')) not in session_library_ids:
|
||||
for k, v in metadata_to_mask.iteritems():
|
||||
if k in d: d[k] = metadata_to_mask[k]
|
||||
|
||||
return list_of_dicts
|
||||
|
||||
def friendly_name_to_username(list_of_dicts):
|
||||
"""
|
||||
Reverts the friendly name back to the username of the current logged in session
|
||||
|
@ -149,4 +121,125 @@ def friendly_name_to_username(list_of_dicts):
|
|||
if 'friendly_name' in d and d['friendly_name'] != session_user:
|
||||
d['friendly_name'] = session_user
|
||||
|
||||
return list_of_dicts
|
||||
|
||||
def filter_session_info(list_of_dicts, filter_key=None):
|
||||
"""
|
||||
Filters a list of dictionary items to only return the info for the current logged in session
|
||||
"""
|
||||
session_user_id = get_session_user_id()
|
||||
|
||||
if not session_user_id:
|
||||
return list_of_dicts
|
||||
|
||||
session_library_ids = get_session_shared_libraries()
|
||||
session_library_filters = get_session_library_filters()
|
||||
|
||||
list_of_dicts = friendly_name_to_username(list_of_dicts)
|
||||
|
||||
if filter_key == 'user_id' and session_user_id:
|
||||
return [d for d in list_of_dicts if str(d.get('user_id','')) == session_user_id]
|
||||
|
||||
elif filter_key == 'section_id' and session_library_ids:
|
||||
new_list_of_dicts = []
|
||||
|
||||
for d in list_of_dicts:
|
||||
if str(d.get('section_id','')) not in session_library_ids:
|
||||
continue
|
||||
|
||||
if d.get('media_type'):
|
||||
f_content_rating, f_labels = get_session_library_filters_type(session_library_filters,
|
||||
media_type=d['media_type'])
|
||||
|
||||
d_content_rating = d.get('content_rating', '').lower()
|
||||
d_labels = tuple(f.lower() for f in d.get('labels', ()))
|
||||
|
||||
if (not f_content_rating or set(d_content_rating).intersection(set(f_content_rating))) and \
|
||||
(not f_labels or set(d_labels).intersection(set(f_labels))):
|
||||
continue
|
||||
|
||||
new_list_of_dicts.append(d)
|
||||
|
||||
return new_list_of_dicts
|
||||
|
||||
return list_of_dicts
|
||||
|
||||
def mask_session_info(list_of_dicts, mask_metadata=True):
|
||||
"""
|
||||
Masks user info in a list of dictionary items to only display info for the current logged in session
|
||||
"""
|
||||
session_user_id = get_session_user_id()
|
||||
|
||||
if not session_user_id:
|
||||
return list_of_dicts
|
||||
|
||||
session_user = get_session_user()
|
||||
session_library_ids = get_session_shared_libraries()
|
||||
session_library_filters = get_session_library_filters()
|
||||
|
||||
keys_to_mask = {'user_id': '',
|
||||
'user': 'Plex User',
|
||||
'friendly_name': 'Plex User',
|
||||
'user_thumb': common.DEFAULT_USER_THUMB,
|
||||
'ip_address': 'N/A',
|
||||
'machine_id': '',
|
||||
'player': 'Player'
|
||||
}
|
||||
|
||||
metadata_to_mask = {'media_index': '0',
|
||||
'parent_media_index': '0',
|
||||
'art': common.DEFAULT_ART,
|
||||
'parent_thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'grandparent_thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'thumb': common.DEFAULT_POSTER_THUMB,
|
||||
'bif_thumb': '',
|
||||
'grandparent_title': 'Title',
|
||||
'parent_title': 'Title',
|
||||
'title': 'Title',
|
||||
'rating_key': '',
|
||||
'parent_rating_key': '',
|
||||
'grandparent_rating_key': '',
|
||||
'year': '',
|
||||
'last_played': 'Title'
|
||||
}
|
||||
|
||||
list_of_dicts = friendly_name_to_username(list_of_dicts)
|
||||
|
||||
for d in list_of_dicts:
|
||||
if session_user_id and not (str(d.get('user_id')) == session_user_id or d.get('user') == session_user):
|
||||
for k, v in keys_to_mask.iteritems():
|
||||
if k in d: d[k] = keys_to_mask[k]
|
||||
|
||||
if not mask_metadata:
|
||||
continue
|
||||
|
||||
if str(d.get('section_id','')) not in session_library_ids:
|
||||
for k, v in metadata_to_mask.iteritems():
|
||||
if k in d: d[k] = metadata_to_mask[k]
|
||||
continue
|
||||
|
||||
media_type = d.get('media_type')
|
||||
if media_type:
|
||||
f_content_rating, f_labels = get_session_library_filters_type(session_library_filters,
|
||||
media_type=d['media_type'])
|
||||
|
||||
if not f_content_rating and not f_labels:
|
||||
continue
|
||||
|
||||
d_content_rating = d.get('content_rating', '')
|
||||
d_labels = tuple(f.lower() for f in d.get('labels', ()))
|
||||
|
||||
if not f_content_rating and f_labels:
|
||||
if set(d_labels).intersection(set(f_labels)):
|
||||
continue
|
||||
elif f_content_rating and not f_labels:
|
||||
if d_content_rating in f_content_rating:
|
||||
continue
|
||||
elif f_content_rating and f_labels:
|
||||
if d_content_rating in f_content_rating or set(d_labels).intersection(set(f_labels)):
|
||||
continue
|
||||
|
||||
for k, v in metadata_to_mask.iteritems():
|
||||
if k in d: d[k] = metadata_to_mask[k]
|
||||
|
||||
return list_of_dicts
|
Loading…
Add table
Add a link
Reference in a new issue