From 2d61a310054b597828623410c81e7e5e8a924913 Mon Sep 17 00:00:00 2001 From: Ruud Date: Sat, 5 May 2012 22:43:07 +0200 Subject: [PATCH] Ask for old CouchPotato database in the wizard. closes #144 --- couchpotato/api.py | 4 +- couchpotato/core/plugins/movie/main.py | 4 +- couchpotato/core/plugins/v1importer/__init__.py | 6 +++ couchpotato/core/plugins/v1importer/form.html | 30 +++++++++++++ couchpotato/core/plugins/v1importer/main.py | 56 ++++++++++++++++++++++++ couchpotato/core/plugins/wizard/static/wizard.js | 29 ++++++++++-- 6 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 couchpotato/core/plugins/v1importer/__init__.py create mode 100644 couchpotato/core/plugins/v1importer/form.html create mode 100644 couchpotato/core/plugins/v1importer/main.py diff --git a/couchpotato/api.py b/couchpotato/api.py index 934fa94..b1dee1b 100644 --- a/couchpotato/api.py +++ b/couchpotato/api.py @@ -6,8 +6,8 @@ api = Blueprint('api', __name__) api_docs = {} api_docs_missing = [] -def addApiView(route, func, static = False, docs = None): - api.add_url_rule(route + ('' if static else '/'), endpoint = route.replace('.', '::') if route else 'index', view_func = func) +def addApiView(route, func, static = False, docs = None, **kwargs): + api.add_url_rule(route + ('' if static else '/'), endpoint = route.replace('.', '::') if route else 'index', view_func = func, **kwargs) if docs: api_docs[route[4:] if route[0:4] == 'api.' else route] = docs else: diff --git a/couchpotato/core/plugins/movie/main.py b/couchpotato/core/plugins/movie/main.py index 314f770..fd6cf38 100644 --- a/couchpotato/core/plugins/movie/main.py +++ b/couchpotato/core/plugins/movie/main.py @@ -270,7 +270,7 @@ class MoviePlugin(Plugin): 'movies': movies, }) - def add(self, params = {}, force_readd = True): + def add(self, params = {}, force_readd = True, search_after = True): library = fireEvent('library.add', single = True, attrs = params, update_after = False) @@ -316,7 +316,7 @@ class MoviePlugin(Plugin): movie_dict = m.to_dict(self.default_dict) - if force_readd or do_search: + if (force_readd or do_search) and search_after: fireEventAsync('searcher.single', movie_dict) return movie_dict diff --git a/couchpotato/core/plugins/v1importer/__init__.py b/couchpotato/core/plugins/v1importer/__init__.py new file mode 100644 index 0000000..40c1434 --- /dev/null +++ b/couchpotato/core/plugins/v1importer/__init__.py @@ -0,0 +1,6 @@ +from .main import V1Importer + +def start(): + return V1Importer() + +config = [] diff --git a/couchpotato/core/plugins/v1importer/form.html b/couchpotato/core/plugins/v1importer/form.html new file mode 100644 index 0000000..e27d1c7 --- /dev/null +++ b/couchpotato/core/plugins/v1importer/form.html @@ -0,0 +1,30 @@ + + + + + + + + + + + + + {% if message: %} + {{ message }} + {% else: %} +
+ +
+ {% endif %} + + \ No newline at end of file diff --git a/couchpotato/core/plugins/v1importer/main.py b/couchpotato/core/plugins/v1importer/main.py new file mode 100644 index 0000000..08f8ba9 --- /dev/null +++ b/couchpotato/core/plugins/v1importer/main.py @@ -0,0 +1,56 @@ +from couchpotato.api import addApiView +from couchpotato.core.event import fireEventAsync +from couchpotato.core.helpers.variable import getImdb +from couchpotato.core.logger import CPLog +from couchpotato.core.plugins.base import Plugin +from couchpotato.environment import Env +from flask.globals import request +from flask.helpers import url_for +import os + +log = CPLog(__name__) + + +class V1Importer(Plugin): + + def __init__(self): + addApiView('v1.import', self.fromOld, methods = ['GET', 'POST']) + + def fromOld(self): + + if request.method != 'POST': + return self.renderTemplate(__file__, 'form.html', url_for = url_for) + + file = request.files['old_db'] + + uploaded_file = os.path.join(Env.get('cache_dir'), 'v1_database.db') + + if os.path.isfile(uploaded_file): + os.remove(uploaded_file) + + file.save(uploaded_file) + + try: + import sqlite3 + conn = sqlite3.connect(uploaded_file) + + wanted = [] + + t = ('want',) + cur = conn.execute('SELECT status, imdb FROM Movie WHERE status=?', t) + for row in cur: + status, imdb = row + if getImdb(imdb): + wanted.append(imdb) + conn.close() + + wanted = set(wanted) + for imdb in wanted: + fireEventAsync('movie.add', {'identifier': imdb}, search_after = False) + + message = 'Successfully imported %s movie(s)' % len(wanted) + except Exception, e: + message = 'Failed: %s' % e + + return self.renderTemplate(__file__, 'form.html', url_for = url_for, message = message) + diff --git a/couchpotato/core/plugins/wizard/static/wizard.js b/couchpotato/core/plugins/wizard/static/wizard.js index e1e07a8..a4438cb 100644 --- a/couchpotato/core/plugins/wizard/static/wizard.js +++ b/couchpotato/core/plugins/wizard/static/wizard.js @@ -8,8 +8,28 @@ Page.Wizard = new Class({ headers: { 'welcome': { - 'title': 'Welcome to CouchPotato', - 'description': 'To get started, fill in each of the following settings as much as your can.' + 'title': 'Welcome to the new CouchPotato', + 'description': 'To get started, fill in each of the following settings as much as your can.
Maybe first start with importing your movies from the previous CouchPotato', + 'content': new Element('div', { + 'styles': { + 'margin': '0 0 0 30px' + } + }).adopt( + new Element('div', { + 'html': 'Select the data.db. It should be in your CouchPotato root directory.' + }), + self.import_iframe = new Element('iframe', { + 'styles': { + 'height': 40, + 'width': 300, + 'border': 0, + 'overflow': 'hidden' + } + }) + ), + 'event': function(){ + self.import_iframe.set('src', Api.createUrl('v1.import')) + } }, 'general': { 'title': 'General', @@ -105,7 +125,7 @@ Page.Wizard = new Class({ 'text': self.headers[group].title }), self.headers[group].description ? new Element('span.description', { - 'text': self.headers[group].description + 'html': self.headers[group].description }) : null, self.headers[group].content ? self.headers[group].content : null ).inject(form); @@ -132,6 +152,9 @@ Page.Wizard = new Class({ }) ).inject(tabs); } + + if(self.headers[group] && self.headers[group].event) + self.headers[group].event.call() }); // Remove toggle