From 11ea9b4e91f79bf913a8031e76a1dcb77d9bca8f Mon Sep 17 00:00:00 2001 From: Dean Gardiner Date: Mon, 2 Dec 2013 23:26:31 +1300 Subject: [PATCH] related_libraries are now only included on searches and added the root_library attribute --- couchpotato/core/media/__init__.py | 12 ++++++++++-- couchpotato/core/settings/model.py | 28 ++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/couchpotato/core/media/__init__.py b/couchpotato/core/media/__init__.py index 84545e4..3894fbf 100644 --- a/couchpotato/core/media/__init__.py +++ b/couchpotato/core/media/__init__.py @@ -1,5 +1,6 @@ from couchpotato import get_session from couchpotato.core.event import addEvent, fireEventAsync, fireEvent +from couchpotato.core.helpers.variable import mergeDicts from couchpotato.core.plugins.base import Plugin from couchpotato.core.settings.model import Media @@ -11,12 +12,19 @@ class MediaBase(Plugin): default_dict = { 'profile': {'types': {'quality': {}}}, 'releases': {'status': {}, 'quality': {}, 'files':{}, 'info': {}}, - 'library': {'titles': {}, 'files':{}, 'related_libraries': {}}, + 'library': {'titles': {}, 'files':{}}, 'files': {}, 'status': {}, 'category': {}, } + search_dict = mergeDicts(default_dict, { + 'library': { + 'related_libraries': {}, + 'root_library': {} + }, + }) + def initType(self): addEvent('media.types', self.getType) @@ -28,7 +36,7 @@ class MediaBase(Plugin): def onComplete(): db = get_session() media = db.query(Media).filter_by(id = id).first() - fireEventAsync('%s.searcher.single' % media.type, media.to_dict(self.default_dict), on_complete = self.createNotifyFront(id)) + fireEventAsync('%s.searcher.single' % media.type, media.to_dict(self.search_dict), on_complete = self.createNotifyFront(id)) db.expire_all() return onComplete diff --git a/couchpotato/core/settings/model.py b/couchpotato/core/settings/model.py index e6fe342..82dbdbe 100644 --- a/couchpotato/core/settings/model.py +++ b/couchpotato/core/settings/model.py @@ -90,6 +90,7 @@ class Media(Entity): files = ManyToMany('File', cascade = 'all, delete-orphan', single_parent = True) + class Library(Entity): """""" using_options(inheritance = 'multi') @@ -130,30 +131,44 @@ class Library(Entity): return libraries # Merge the results into a dict ({type: [,...]}) + root_key = None results = {} for key, library in libraries: + if root_key is None: + root_key = key + if key not in results: results[key] = [] results[key].append(library) - return results + return root_key, results def to_dict(self, deep = None, exclude = None): if not exclude: exclude = [] if not deep: deep = {} - include_related = deep.pop('related_libraries', None) is not None + include_related = False + include_root = False + + if any(x in deep for x in ['related_libraries', 'root_library']): + deep = deep.copy() + + include_related = deep.pop('related_libraries', None) is not None + include_root = deep.pop('root_library', None) is not None orig_dict = super(Library, self).to_dict(deep = deep, exclude = exclude) # Include related libraries (parents and children) if include_related: - # Fetch and serialize all the child and parent libraries + # Fetch child and parent libraries and determine root type + root_key, related_libraries = self.getRelated(include_self = False, merge=True) + + # Serialize libraries related_libraries = dict([ (key, [library.to_dict(deep, exclude) for library in libraries]) - for (key, libraries) in self.getRelated(include_self = False, merge=True).items() + for (key, libraries) in related_libraries.items() ]) # Add a reference to the current library dict into related_libraries @@ -164,6 +179,11 @@ class Library(Entity): # Update the dict for this library orig_dict['related_libraries'] = related_libraries + + if include_root: + root_library = related_libraries.get(root_key) + orig_dict['root_library'] = root_library[0] if len(root_library) else None + return orig_dict