related_libraries are now only included on searches and added the root_library attribute

This commit is contained in:
Dean Gardiner
2013-12-02 23:26:31 +13:00
parent e8a2139ecf
commit 11ea9b4e91
2 changed files with 34 additions and 6 deletions
+10 -2
View File
@@ -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
+24 -4
View File
@@ -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: [<library>,...]})
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