-
+
+
-
+
-
+
-
+
diff --git a/addons/auth_oauth/controllers/main.py b/addons/auth_oauth/controllers/main.py
index ae01df5f6f7..26016f3f0a1 100644
--- a/addons/auth_oauth/controllers/main.py
+++ b/addons/auth_oauth/controllers/main.py
@@ -4,28 +4,25 @@ import simplejson
import werkzeug.urls
import werkzeug.utils
-import openerp.modules.registry
-import openerp.addons.web.controllers.main
-import openerp.addons.web.common.http as openerpweb
-from openerp import SUPERUSER_ID
+import openerp
_logger = logging.getLogger(__name__)
-class OAuthController(openerpweb.Controller):
+class OAuthController(openerp.addons.web.http.Controller):
_cp_path = '/auth_oauth'
- @openerpweb.jsonrequest
+ @openerp.addons.web.http.jsonrequest
def list_providers(self, req, dbname):
try:
registry = openerp.modules.registry.RegistryManager.get(dbname)
with registry.cursor() as cr:
providers = registry.get('auth.oauth.provider')
- l = providers.read(cr, SUPERUSER_ID, providers.search(cr, SUPERUSER_ID, [('enabled','=',True)]))
+ l = providers.read(cr, openerp.SUPERUSER_ID, providers.search(cr, openerp.SUPERUSER_ID, [('enabled','=',True)]))
except Exception:
l = []
return l
- @openerpweb.httprequest
+ @openerp.addons.web.http.httprequest
def signin(self, req, **kw):
state = simplejson.loads(kw['state'])
dbname = state['d']
@@ -34,7 +31,7 @@ class OAuthController(openerpweb.Controller):
with registry.cursor() as cr:
try:
u = registry.get('res.users')
- credentials = u.auth_oauth(cr, SUPERUSER_ID, provider, kw)
+ credentials = u.auth_oauth(cr, openerp.SUPERUSER_ID, provider, kw)
cr.commit()
return openerp.addons.web.controllers.main.login_and_redirect(req, *credentials)
except AttributeError:
@@ -43,7 +40,7 @@ class OAuthController(openerpweb.Controller):
url = "/#action=login&oauth_error=1"
except Exception,e:
# signup error
- _logger.exception('oops')
+ _logger.exception("OAuth2: %s" % str(e))
url = "/#action=login&oauth_error=2"
return openerp.addons.web.controllers.main.set_cookie_and_redirect(req, url)
diff --git a/addons/auth_oauth/res_users.py b/addons/auth_oauth/res_users.py
index fd26e89c2e3..9c1f88bd7f4 100644
--- a/addons/auth_oauth/res_users.py
+++ b/addons/auth_oauth/res_users.py
@@ -15,13 +15,17 @@ class res_users(osv.Model):
_inherit = 'res.users'
_columns = {
- 'oauth_provider_id': fields.many2one('auth.oauth.provider','OAuth Provider'),
+ 'oauth_provider_id': fields.many2one('auth.oauth.provider', 'OAuth Provider'),
'oauth_uid': fields.char('OAuth User ID', help="Oauth Provider user_id"),
- 'oauth_access_token': fields.char('OAuth Token', readonly=True),
+ 'oauth_access_token': fields.char('OAuth Access Token', readonly=True),
}
+ _sql_constraints = [
+ ('uniq_users_oauth_provider_oauht_uid', 'unique(auth_provider_id, auth_uid)', 'OAuth UID must be unique per provider'),
+ ]
+
def auth_oauth_rpc(self, cr, uid, endpoint, access_token, context=None):
- params = urllib.urlencode({ 'access_token': access_token })
+ params = urllib.urlencode({'access_token': access_token})
if urlparse.urlparse(endpoint)[4]:
url = endpoint + '&' + params
else:
@@ -41,22 +45,26 @@ class res_users(osv.Model):
validation = self.auth_oauth_rpc(cr, uid, p.validation_endpoint, access_token)
if validation.get("error"):
- raise openerp.exceptions.AccessDenied
+ raise Exception(validation['error'])
if p.data_endpoint:
data = self.auth_oauth_rpc(cr, uid, p.data_endpoint, access_token)
validation.update(data)
# required
oauth_uid = validation['user_id']
if not oauth_uid:
- raise openerp.exceptions.AccessDenied
+ raise openerp.exceptions.AccessDenied()
email = validation.get('email', 'provider_%d_user_%d' % (p.id, oauth_uid))
# optional
name = validation.get('name', email)
- res = self.search(cr, uid, [("oauth_uid", "=", oauth_uid)])
+ res = self.search(cr, uid, [("oauth_uid", "=", oauth_uid), ('oauth_provider_id', '=', provider)])
if res:
- self.write(cr, uid, res[0], { 'oauth_access_token': access_token })
+ assert len(res) == 1
+ self.write(cr, uid, res[0], {'oauth_access_token': access_token})
else:
- # New user
+ # New user if signup module available
+ if not hasattr(self, '_signup_create_user'):
+ raise openerp.exceptions.AccessDenied()
+
new_user = {
'name': name,
'login': email,
@@ -66,7 +74,9 @@ class res_users(osv.Model):
'oauth_access_token': access_token,
'active': True,
}
- self.auth_signup_create(cr, uid, new_user)
+ # TODO pass signup token to allow attach new user to right partner
+ self._signup_create_user(cr, uid, new_user)
+
credentials = (cr.dbname, email, access_token)
return credentials
@@ -74,7 +84,7 @@ class res_users(osv.Model):
try:
return super(res_users, self).check_credentials(cr, uid, password)
except openerp.exceptions.AccessDenied:
- res = self.search(cr, SUPERUSER_ID, [('id','=',uid),('oauth_access_token','=',password)])
+ res = self.search(cr, SUPERUSER_ID, [('id', '=', uid), ('oauth_access_token', '=', password)])
if not res:
raise
diff --git a/addons/auth_openid/controllers/main.py b/addons/auth_openid/controllers/main.py
index ebcaab4bf25..7ee562d4d29 100644
--- a/addons/auth_openid/controllers/main.py
+++ b/addons/auth_openid/controllers/main.py
@@ -23,24 +23,21 @@ import logging
import os
import tempfile
import urllib
-from openerp import SUPERUSER_ID
import werkzeug.urls
import werkzeug.exceptions
-from openerp.modules.registry import RegistryManager
-from openerp.addons.web.controllers.main import login_and_redirect, set_cookie_and_redirect
-try:
- import openerp.addons.web.common.http as openerpweb
-except ImportError:
- import web.common.http as openerpweb # noqa
-
from openid import oidutil
from openid.store import filestore
from openid.consumer import consumer
from openid.cryptutil import randomString
from openid.extensions import ax, sreg
+import openerp
+from openerp import SUPERUSER_ID
+from openerp.modules.registry import RegistryManager
+from openerp.addons.web.controllers.main import login_and_redirect, set_cookie_and_redirect
+
from .. import utils
_logger = logging.getLogger(__name__)
@@ -70,7 +67,7 @@ class GoogleAppsAwareConsumer(consumer.GenericConsumer):
return super(GoogleAppsAwareConsumer, self).complete(message, endpoint, return_to)
-class OpenIDController(openerpweb.Controller):
+class OpenIDController(openerp.addons.web.http.Controller):
_cp_path = '/auth_openid/login'
_store = filestore.FileOpenIDStore(_storedir)
@@ -118,7 +115,7 @@ class OpenIDController(openerpweb.Controller):
def _get_realm(self, req):
return req.httprequest.host_url
- @openerpweb.httprequest
+ @openerp.addons.web.http.httprequest
def verify_direct(self, req, db, url):
result = self._verify(req, db, url)
if 'error' in result:
@@ -127,7 +124,7 @@ class OpenIDController(openerpweb.Controller):
return werkzeug.utils.redirect(result['value'])
return result['value']
- @openerpweb.jsonrequest
+ @openerp.addons.web.http.jsonrequest
def verify(self, req, db, url):
return self._verify(req, db, url)
@@ -157,7 +154,7 @@ class OpenIDController(openerpweb.Controller):
form_html = request.htmlMarkup(realm, redirect_to)
return {'action': 'post', 'value': form_html, 'session_id': req.session_id}
- @openerpweb.httprequest
+ @openerp.addons.web.http.httprequest
def process(self, req, **kw):
session = getattr(req.session, 'openid_session', None)
if not session:
@@ -225,7 +222,7 @@ class OpenIDController(openerpweb.Controller):
return set_cookie_and_redirect(req, '/#action=login&loginerror=1')
- @openerpweb.jsonrequest
+ @openerp.addons.web.http.jsonrequest
def status(self, req):
session = getattr(req.session, 'openid_session', {})
return {'status': session.get('status'), 'message': session.get('message')}
diff --git a/addons/auth_reset_password/controllers/main.py b/addons/auth_reset_password/controllers/main.py
index 5185a65c3ce..639945cfc09 100644
--- a/addons/auth_reset_password/controllers/main.py
+++ b/addons/auth_reset_password/controllers/main.py
@@ -18,20 +18,20 @@
# along with this program. If not, see
#
##############################################################################
-
-from openerp import SUPERUSER_ID
-from openerp.modules.registry import RegistryManager
-import openerp.addons.web.common.http as openerpweb
+import logging
import werkzeug
-import logging
+import openerp
+from openerp import SUPERUSER_ID
+from openerp.modules.registry import RegistryManager
+
_logger = logging.getLogger(__name__)
-class Controller(openerpweb.Controller):
+class Controller(openerp.addons.web.http.Controller):
_cp_path = '/auth_reset_password'
- @openerpweb.httprequest
+ @openerp.addons.web.http.httprequest
def reset_password(self, req, dbname, login):
""" retrieve user, and perform reset password """
url = '/'
diff --git a/addons/auth_signup/controllers/main.py b/addons/auth_signup/controllers/main.py
index 56e5e910212..54dcc5b4362 100644
--- a/addons/auth_signup/controllers/main.py
+++ b/addons/auth_signup/controllers/main.py
@@ -18,32 +18,31 @@
# along with this program. If not, see
#
##############################################################################
-
-from openerp import SUPERUSER_ID
-from openerp.modules.registry import RegistryManager
-from openerp.addons.web.controllers.main import login_and_redirect
-import openerp.addons.web.common.http as openerpweb
+import logging
import werkzeug
-import logging
+import openerp
+from openerp.modules.registry import RegistryManager
+from openerp.addons.web.controllers.main import login_and_redirect
+
_logger = logging.getLogger(__name__)
-class Controller(openerpweb.Controller):
+class Controller(openerp.addons.web.http.Controller):
_cp_path = '/auth_signup'
- @openerpweb.jsonrequest
+ @openerp.addons.web.http.jsonrequest
def retrieve(self, req, dbname, token):
""" retrieve the user info (name, login or email) corresponding to a signup token """
registry = RegistryManager.get(dbname)
user_info = None
with registry.cursor() as cr:
res_partner = registry.get('res.partner')
- user_info = res_partner.signup_retrieve_info(cr, SUPERUSER_ID, token)
+ user_info = res_partner.signup_retrieve_info(cr, openerp.SUPERUSER_ID, token)
return user_info
- @openerpweb.httprequest
- def signup(self, req, dbname, token, name, login, password):
+ @openerp.addons.web.http.httprequest
+ def signup(self, req, dbname, token, name, login, password, state=''):
""" sign up a user (new or existing), and log it in """
url = '/'
registry = RegistryManager.get(dbname)
@@ -51,9 +50,9 @@ class Controller(openerpweb.Controller):
try:
res_users = registry.get('res.users')
values = {'name': name, 'login': login, 'password': password}
- credentials = res_users.signup(cr, SUPERUSER_ID, values, token)
+ credentials = res_users.signup(cr, openerp.SUPERUSER_ID, values, token)
cr.commit()
- return login_and_redirect(req, *credentials)
+ return login_and_redirect(req, *credentials, redirect_url='/#%s'%state)
except Exception as e:
# signup error
_logger.exception('error when signup')
diff --git a/addons/auth_signup/res_users.py b/addons/auth_signup/res_users.py
index 7b663bd0e33..59196c4da87 100644
--- a/addons/auth_signup/res_users.py
+++ b/addons/auth_signup/res_users.py
@@ -18,6 +18,10 @@
# along with this program. If not, see
#
##############################################################################
+import random
+import time
+import urllib
+import urlparse
import openerp
from openerp.osv import osv, fields
@@ -25,11 +29,6 @@ from openerp import SUPERUSER_ID
from openerp.tools.misc import DEFAULT_SERVER_DATETIME_FORMAT
from openerp.tools.safe_eval import safe_eval
-import time
-import random
-import urllib
-import urlparse
-
def random_token():
# the token has an entropy of about 120 bits (6 bits/char * 20 chars)
chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
@@ -38,7 +37,6 @@ def random_token():
def now():
return time.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
-
class res_partner(osv.Model):
_inherit = 'res.partner'
@@ -132,8 +130,6 @@ class res_partner(osv.Model):
res['email'] = partner.email or ''
return res
-
-
class res_users(osv.Model):
_inherit = 'res.users'
diff --git a/addons/auth_signup/static/src/js/auth_signup.js b/addons/auth_signup/static/src/js/auth_signup.js
index 509fafa35cc..0974b73292d 100644
--- a/addons/auth_signup/static/src/js/auth_signup.js
+++ b/addons/auth_signup/static/src/js/auth_signup.js
@@ -90,6 +90,7 @@ openerp.auth_signup = function(instance) {
name: name,
login: login,
password: password,
+ state: $.param(this.params)
};
var url = "/auth_signup/signup?" + $.param(params);
window.location = url;
diff --git a/addons/base_import/__openerp__.py b/addons/base_import/__openerp__.py
index eff20bc90ab..f5a707650d8 100644
--- a/addons/base_import/__openerp__.py
+++ b/addons/base_import/__openerp__.py
@@ -26,13 +26,14 @@ Re-implement openerp's file import system:
'author': 'OpenERP SA',
'depends': ['base'],
'installable': True,
- 'auto_install': False, # set to true and allow uninstall?
+ 'auto_install': True,
'css': [
'static/lib/select2/select2.css',
'static/src/css/import.css',
],
'js': [
'static/lib/select2/select2.js',
+ 'static/lib/javascript-state-machine/state-machine.js',
'static/src/js/import.js',
],
'qweb': ['static/src/xml/import.xml'],
diff --git a/addons/base_import/controllers.py b/addons/base_import/controllers.py
index 85ff993e92e..c831ffd229d 100644
--- a/addons/base_import/controllers.py
+++ b/addons/base_import/controllers.py
@@ -1,15 +1,12 @@
# -*- coding: utf-8 -*-
import simplejson
-try:
- import openerp.addons.web.common.http as openerpweb
-except ImportError:
- import web.common.http as openerpweb
+import openerp
-class ImportController(openerpweb.Controller):
+class ImportController(openerp.addons.web.http.Controller):
_cp_path = '/base_import'
- @openerpweb.httprequest
+ @openerp.addons.web.http.httprequest
def set_file(self, req, file, import_id, jsonp='callback'):
import_id = int(import_id)
diff --git a/addons/base_import/models.py b/addons/base_import/models.py
index 1e5ad002b84..996dbf7964d 100644
--- a/addons/base_import/models.py
+++ b/addons/base_import/models.py
@@ -83,6 +83,10 @@ class ir_import(orm.TransientModel):
}]
fields_got = self.pool[model].fields_get(cr, uid, context=context)
for name, field in fields_got.iteritems():
+ # an empty string means the field is deprecated, @deprecated must
+ # be absent or False to mean not-deprecated
+ if field.get('deprecated', False) is not False:
+ continue
if field.get('readonly'):
states = field.get('states')
if not states:
@@ -97,7 +101,7 @@ class ir_import(orm.TransientModel):
'id': name,
'name': name,
'string': field['string'],
- # Y U NO ALWAYS HAVE REQUIRED
+ # Y U NO ALWAYS HAS REQUIRED
'required': bool(field.get('required')),
'fields': [],
}
@@ -124,8 +128,8 @@ class ir_import(orm.TransientModel):
"""
csv_iterator = csv.reader(
StringIO(record.file),
- quotechar=options['quoting'],
- delimiter=options['separator'])
+ quotechar=str(options['quoting']),
+ delimiter=str(options['separator']))
csv_nonempty = itertools.ifilter(None, csv_iterator)
# TODO: guess encoding with chardet? Or https://github.com/aadsm/jschardet
encoding = options.get('encoding', 'utf-8')
@@ -307,22 +311,14 @@ class ir_import(orm.TransientModel):
except ValueError, e:
return [{
'type': 'error',
- 'message': str(e),
+ 'message': unicode(e),
'record': False,
}]
- try:
- _logger.info('importing %d rows...', len(data))
- (code, record, message, _wat) = self.pool[record.res_model].import_data(
- cr, uid, import_fields, data, context=context)
- _logger.info('done')
-
- except Exception, e:
- _logger.exception("Import failed")
- # TODO: remove when exceptions stop being an "expected"
- # behavior of import_data on some (most) invalid
- # input.
- code, record, message = -1, None, str(e)
+ _logger.info('importing %d rows...', len(data))
+ import_result = self.pool[record.res_model].load(
+ cr, uid, import_fields, data, context=context)
+ _logger.info('done')
# If transaction aborted, RELEASE SAVEPOINT is going to raise
# an InternalError (ROLLBACK should work, maybe). Ignore that.
@@ -339,14 +335,4 @@ class ir_import(orm.TransientModel):
except psycopg2.InternalError:
pass
- if code != -1:
- return []
-
- # TODO: add key for error location?
- # TODO: error not within normal preview, how to display? Re-preview
- # with higher ``count``?
- return [{
- 'type': 'error',
- 'message': message,
- 'record': record or False
- }]
+ return import_result['messages']
diff --git a/addons/base_import/static/lib/javascript-state-machine/LICENSE b/addons/base_import/static/lib/javascript-state-machine/LICENSE
new file mode 100644
index 00000000000..8ad703ca4ef
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Jake Gordon and contributors
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+
diff --git a/addons/base_import/static/lib/javascript-state-machine/README.md b/addons/base_import/static/lib/javascript-state-machine/README.md
new file mode 100644
index 00000000000..64c045e69d7
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/README.md
@@ -0,0 +1,327 @@
+Javascript Finite State Machine (v2.1.0)
+========================================
+
+This standalone javascript micro-framework provides a finite state machine for your pleasure.
+
+ * You can find the [code here](https://github.com/jakesgordon/javascript-state-machine)
+ * You can find a [description here](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/)
+ * You can find a [working demo here](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/example/)
+
+Download
+========
+
+You can download [state-machine.js](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.js),
+or the [minified version](https://github.com/jakesgordon/javascript-state-machine/raw/master/state-machine.min.js)
+
+Alternatively:
+
+ git clone git@github.com:jakesgordon/javascript-state-machine
+
+
+ * All code is in state-machine.js
+ * Minified version provided in state-machine.min.js
+ * No 3rd party library is required
+ * Demo can be found in /index.html
+ * QUnit tests can be found in /test/index.html
+
+Usage
+=====
+
+Include `state-machine.min.js` in your application.
+
+In its simplest form, create a standalone state machine using:
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ events: [
+ { name: 'warn', from: 'green', to: 'yellow' },
+ { name: 'panic', from: 'yellow', to: 'red' },
+ { name: 'calm', from: 'red', to: 'yellow' },
+ { name: 'clear', from: 'yellow', to: 'green' }
+ ]});
+
+... will create an object with a method for each event:
+
+ * fsm.warn() - transition from 'green' to 'yellow'
+ * fsm.panic() - transition from 'yellow' to 'red'
+ * fsm.calm() - transition from 'red' to 'yellow'
+ * fsm.clear() - transition from 'yellow' to 'green'
+
+along with the following members:
+
+ * fsm.current - contains the current state
+ * fsm.is(s) - return true if state `s` is the current state
+ * fsm.can(e) - return true if event `e` can be fired in the current state
+ * fsm.cannot(e) - return true if event `e` cannot be fired in the current state
+
+Multiple 'from' and 'to' states for a single event
+==================================================
+
+If an event is allowed **from** multiple states, and always transitions to the same
+state, then simply provide an array of states in the `from` attribute of an event. However,
+if an event is allowed from multiple states, but should transition **to** a different
+state depending on the current state, then provide multiple event entries with
+the same name:
+
+ var fsm = StateMachine.create({
+ initial: 'hungry',
+ events: [
+ { name: 'eat', from: 'hungry', to: 'satisfied' },
+ { name: 'eat', from: 'satisfied', to: 'full' },
+ { name: 'eat', from: 'full', to: 'sick' },
+ { name: 'rest', from: ['hungry', 'satisfied', 'full', 'sick'], to: 'hungry' },
+ ]});
+
+This example will create an object with 2 event methods:
+
+ * fsm.eat()
+ * fsm.rest()
+
+The `rest` event will always transition to the `hungry` state, while the `eat` event
+will transition to a state that is dependent on the current state.
+
+>> NOTE: The `rest` event could use a wildcard '*' for the 'from' state if it should be
+allowed from any current state.
+
+>> NOTE: The `rest` event in the above example can also be specified as multiple events with
+the same name if you prefer the verbose approach.
+
+Callbacks
+=========
+
+4 callbacks are available if your state machine has methods using the following naming conventions:
+
+ * onbefore**event** - fired before the event
+ * onleave**state** - fired when leaving the old state
+ * onenter**state** - fired when entering the new state
+ * onafter**event** - fired after the event
+
+You can affect the event in 3 ways:
+
+ * return `false` from an `onbeforeevent` handler to cancel the event.
+ * return `false` from an `onleavestate` handler to cancel the event.
+ * return `ASYNC` from an `onleavestate` handler to perform an asynchronous state transition (see next section)
+
+For convenience, the 2 most useful callbacks can be shortened:
+
+ * on**event** - convenience shorthand for onafter**event**
+ * on**state** - convenience shorthand for onenter**state**
+
+In addition, a generic `onchangestate()` callback can be used to call a single function for _all_ state changes:
+
+All callbacks will be passed the same arguments:
+
+ * **event** name
+ * **from** state
+ * **to** state
+ * _(followed by any arguments you passed into the original event method)_
+
+Callbacks can be specified when the state machine is first created:
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ events: [
+ { name: 'warn', from: 'green', to: 'yellow' },
+ { name: 'panic', from: 'yellow', to: 'red' },
+ { name: 'calm', from: 'red', to: 'yellow' },
+ { name: 'clear', from: 'yellow', to: 'green' }
+ ],
+ callbacks: {
+ onpanic: function(event, from, to, msg) { alert('panic! ' + msg); },
+ onclear: function(event, from, to, msg) { alert('thanks to ' + msg); },
+ ongreen: function(event, from, to) { document.body.className = 'green'; },
+ onyellow: function(event, from, to) { document.body.className = 'yellow'; },
+ onred: function(event, from, to) { document.body.className = 'red'; },
+ }
+ });
+
+ fsm.panic('killer bees');
+ fsm.clear('sedatives in the honey pots');
+ ...
+
+Additionally, they can be added and removed from the state machine at any time:
+
+ fsm.ongreen = null;
+ fsm.onyellow = null;
+ fsm.onred = null;
+ fsm.onchangestate = function(event, from, to) { document.body.className = to; };
+
+Asynchronous State Transitions
+==============================
+
+Sometimes, you need to execute some asynchronous code during a state transition and ensure the
+new state is not entered until your code has completed.
+
+A good example of this is when you transition out of a `menu` state, perhaps you want to gradually
+fade the menu away, or slide it off the screen and don't want to transition to your `game` state
+until after that animation has been performed.
+
+You can now return `StateMachine.ASYNC` from your `onleavestate` handler and the state machine
+will be _'put on hold'_ until you are ready to trigger the transition using the new `transition()`
+method.
+
+For example, using jQuery effects:
+
+ var fsm = StateMachine.create({
+
+ initial: 'menu',
+
+ events: [
+ { name: 'play', from: 'menu', to: 'game' },
+ { name: 'quit', from: 'game', to: 'menu' }
+ ],
+
+ callbacks: {
+
+ onentermenu: function() { $('#menu').show(); },
+ onentergame: function() { $('#game').show(); },
+
+ onleavemenu: function() {
+ $('#menu').fadeOut('fast', function() {
+ fsm.transition();
+ });
+ return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in fadeOut callback above)
+ },
+
+ onleavegame: function() {
+ $('#game').slideDown('slow', function() {
+ fsm.transition();
+ };
+ return StateMachine.ASYNC; // tell StateMachine to defer next state until we call transition (in slideDown callback above)
+ }
+
+ }
+ });
+
+
+State Machine Classes
+=====================
+
+You can also turn all instances of a _class_ into an FSM by applying
+the state machine functionality to the prototype, including your callbacks
+in your prototype, and providing a `startup` event for use when constructing
+instances:
+
+ MyFSM = function() { // my constructor function
+ this.startup();
+ };
+
+ MyFSM.prototype = {
+
+ onpanic: function(event, from, to) { alert('panic'); },
+ onclear: function(event, from, to) { alert('all is clear'); },
+
+ // my other prototype methods
+
+ };
+
+ StateMachine.create({
+ target: MyFSM.prototype,
+ events: [
+ { name: 'startup', from: 'none', to: 'green' },
+ { name: 'warn', from: 'green', to: 'yellow' },
+ { name: 'panic', from: 'yellow', to: 'red' },
+ { name: 'calm', from: 'red', to: 'yellow' },
+ { name: 'clear', from: 'yellow', to: 'green' }
+ ]});
+
+
+This should be easy to adjust to fit your appropriate mechanism for object construction.
+
+Initialization Options
+======================
+
+How the state machine should initialize can depend on your application requirements, so
+the library provides a number of simple options.
+
+By default, if you dont specify any initial state, the state machine will be in the `'none'`
+state and you would need to provide an event to take it out of this state:
+
+ var fsm = StateMachine.create({
+ events: [
+ { name: 'startup', from: 'none', to: 'green' },
+ { name: 'panic', from: 'green', to: 'red' },
+ { name: 'calm', from: 'red', to: 'green' },
+ ]});
+ alert(fsm.current); // "none"
+ fsm.startup();
+ alert(fsm.current); // "green"
+
+If you specify the name of your initial event (as in all the earlier examples), then an
+implicit `startup` event will be created for you and fired when the state machine is constructed.
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ events: [
+ { name: 'panic', from: 'green', to: 'red' },
+ { name: 'calm', from: 'red', to: 'green' },
+ ]});
+ alert(fsm.current); // "green"
+
+If your object already has a `startup` method you can use a different name for the initial event
+
+ var fsm = StateMachine.create({
+ initial: { state: 'green', event: 'init' },
+ events: [
+ { name: 'panic', from: 'green', to: 'red' },
+ { name: 'calm', from: 'red', to: 'green' },
+ ]});
+ alert(fsm.current); // "green"
+
+Finally, if you want to wait to call the initial state transition event until a later date you
+can `defer` it:
+
+ var fsm = StateMachine.create({
+ initial: { state: 'green', event: 'init', defer: true },
+ events: [
+ { name: 'panic', from: 'green', to: 'red' },
+ { name: 'calm', from: 'red', to: 'green' },
+ ]});
+ alert(fsm.current); // "none"
+ fsm.init();
+ alert(fsm.current); // "green"
+
+Of course, we have now come full circle, this last example is pretty much functionally the
+same as the first example in this section where you simply define your own startup event.
+
+So you have a number of choices available to you when initializing your state machine.
+
+Handling Failures
+======================
+
+By default, if you try to call an event method that is not allowed in the current state, the
+state machine will throw an exception. If you prefer to handle the problem yourself, you can
+define a custom `error` handler:
+
+ var fsm = StateMachine.create({
+ initial: 'green',
+ error: function(eventName, from, to, args, errorCode, errorMessage) {
+ return 'event ' + eventName + ' was naughty :- ' + errorMessage;
+ },
+ events: [
+ { name: 'panic', from: 'green', to: 'red' },
+ { name: 'calm', from: 'red', to: 'green' },
+ ]});
+ alert(fsm.calm()); // "event calm was naughty :- event not allowed in current state green"
+
+Release Notes
+=============
+
+See [RELEASE NOTES](https://github.com/jakesgordon/javascript-state-machine/blob/master/RELEASE_NOTES.md) file.
+
+License
+=======
+
+See [LICENSE](https://github.com/jakesgordon/javascript-state-machine/blob/master/LICENSE) file.
+
+Contact
+=======
+
+If you have any ideas, feedback, requests or bug reports, you can reach me at
+[jake@codeincomplete.com](mailto:jake@codeincomplete.com), or via
+my website: [Code inComplete](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/)
+
+
+
+
+
diff --git a/addons/base_import/static/lib/javascript-state-machine/RELEASE_NOTES.md b/addons/base_import/static/lib/javascript-state-machine/RELEASE_NOTES.md
new file mode 100644
index 00000000000..06abf402d38
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/RELEASE_NOTES.md
@@ -0,0 +1,32 @@
+Version 2.1.0 (January 7th 2012)
+--------------------------------
+
+ * Wrapped in self executing function to be more easily used with loaders like `require.js` or `curl.js` (issue #15)
+ * Allow event to be cancelled by returning `false` from `onleavestate` handler (issue #13) - WARNING: this breaks backward compatibility for async transitions (you now need to return `StateMachine.ASYNC` instead of `false`)
+ * Added explicit return values for event methods (issue #12)
+ * Added support for wildcard events that can be fired 'from' any state (issue #11)
+ * Added support for no-op events that transition 'to' the same state (issue #5)
+ * extended custom error callback to handle any exceptions caused by caller provided callbacks
+ * added custom error callback to override exception when an illegal state transition is attempted (thanks to cboone)
+ * fixed typos (thanks to cboone)
+ * fixed issue #4 - ensure before/after event hooks are called even if the event doesn't result in a state change
+
+Version 2.0.0 (August 19th 2011)
+--------------------------------
+
+ * adding support for asynchronous state transitions (see README) - with lots of qunit tests (see test/async.js).
+ * consistent arguments for ALL callbacks, first 3 args are ALWAYS event name, from state and to state, followed by whatever arguments the user passed to the original event method.
+ * added a generic `onchangestate(event,from,to)` callback to detect all state changes with a single function.
+ * allow callbacks to be declared at creation time (instead of having to attach them afterwards)
+ * renamed 'hooks' => 'callbacks'
+ * [read more...](http://codeincomplete.com/posts/2011/8/19/javascript_state_machine_v2/)
+
+Version 1.2.0 (June 21st 2011)
+------------------------------
+ * allows the same event to transition to different states, depending on the current state (see 'Multiple...' section in README.md)
+ * [read more...](http://codeincomplete.com/posts/2011/6/21/javascript_state_machine_v1_2_0/)
+
+Version 1.0.0 (June 1st 2011)
+-----------------------------
+ * initial version
+ * [read more...](http://codeincomplete.com/posts/2011/6/1/javascript_state_machine/)
diff --git a/addons/base_import/static/lib/javascript-state-machine/Rakefile b/addons/base_import/static/lib/javascript-state-machine/Rakefile
new file mode 100644
index 00000000000..beb8702a7f0
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/Rakefile
@@ -0,0 +1,8 @@
+
+desc "create minified version of state-machine.js"
+task :minify do
+ require File.expand_path(File.join(File.dirname(__FILE__), 'minifier/minifier'))
+ Minifier.enabled = true
+ Minifier.minify('state-machine.js')
+end
+
diff --git a/addons/base_import/static/lib/javascript-state-machine/index.html b/addons/base_import/static/lib/javascript-state-machine/index.html
new file mode 100644
index 00000000000..2d6cb62617b
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/index.html
@@ -0,0 +1,39 @@
+
+
+
+ Javascript Finite State Machine
+
+
+
+
+
+
+
+
+
Finite State Machine
+
+
+
+
+
+
+
+
+
+
+
+
+ dashed lines are asynchronous state transitions (3 seconds)
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/addons/base_import/static/lib/javascript-state-machine/state-machine.js b/addons/base_import/static/lib/javascript-state-machine/state-machine.js
new file mode 100644
index 00000000000..0d503ee7bdb
--- /dev/null
+++ b/addons/base_import/static/lib/javascript-state-machine/state-machine.js
@@ -0,0 +1,155 @@
+(function (window) {
+
+ StateMachine = {
+
+ //---------------------------------------------------------------------------
+
+ VERSION: "2.1.0",
+
+ //---------------------------------------------------------------------------
+
+ Result: {
+ SUCCEEDED: 1, // the event transitioned successfully from one state to another
+ NOTRANSITION: 2, // the event was successfull but no state transition was necessary
+ CANCELLED: 3, // the event was cancelled by the caller in a beforeEvent callback
+ ASYNC: 4, // the event is asynchronous and the caller is in control of when the transition occurs
+ },
+
+ Error: {
+ INVALID_TRANSITION: 100, // caller tried to fire an event that was innapropriate in the current state
+ PENDING_TRANSITION: 200, // caller tried to fire an event while an async transition was still pending
+ INVALID_CALLBACK: 300, // caller provided callback function threw an exception
+ },
+
+ WILDCARD: '*',
+ ASYNC: 'async',
+
+ //---------------------------------------------------------------------------
+
+ create: function(cfg, target) {
+
+ var initial = (typeof cfg.initial == 'string') ? { state: cfg.initial } : cfg.initial; // allow for a simple string, or an object with { state: 'foo', event: 'setup', defer: true|false }
+ var fsm = target || cfg.target || {};
+ var events = cfg.events || [];
+ var callbacks = cfg.callbacks || {};
+ var map = {};
+
+ var add = function(e) {
+ var from = (e.from instanceof Array) ? e.from : (e.from ? [e.from] : [StateMachine.WILDCARD]); // allow 'wildcard' transition if 'from' is not specified
+ map[e.name] = map[e.name] || {};
+ for (var n = 0 ; n < from.length ; n++)
+ map[e.name][from[n]] = e.to || from[n]; // allow no-op transition if 'to' is not specified
+ };
+
+ if (initial) {
+ initial.event = initial.event || 'startup';
+ add({ name: initial.event, from: 'none', to: initial.state });
+ }
+
+ for(var n = 0 ; n < events.length ; n++)
+ add(events[n]);
+
+ for(var name in map) {
+ if (map.hasOwnProperty(name))
+ fsm[name] = StateMachine.buildEvent(name, map[name]);
+ }
+
+ for(var name in callbacks) {
+ if (callbacks.hasOwnProperty(name))
+ fsm[name] = callbacks[name]
+ }
+
+ fsm.current = 'none';
+ fsm.is = function(state) { return this.current == state; };
+ fsm.can = function(event) { return !this.transition && (map[event].hasOwnProperty(this.current) || map[event].hasOwnProperty(StateMachine.WILDCARD)); }
+ fsm.cannot = function(event) { return !this.can(event); };
+ fsm.error = cfg.error || function(name, from, to, args, error, msg) { throw msg; }; // default behavior when something unexpected happens is to throw an exception, but caller can override this behavior if desired (see github issue #3)
+
+ if (initial && !initial.defer)
+ fsm[initial.event]();
+
+ return fsm;
+
+ },
+
+ //===========================================================================
+
+ doCallback: function(fsm, func, name, from, to, args) {
+ if (func) {
+ try {
+ return func.apply(fsm, [name, from, to].concat(args));
+ }
+ catch(e) {
+ return fsm.error(name, from, to, args, StateMachine.Error.INVALID_CALLBACK, "an exception occurred in a caller-provided callback function");
+ }
+ }
+ },
+
+ beforeEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onbefore' + name], name, from, to, args); },
+ afterEvent: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onafter' + name] || fsm['on' + name], name, from, to, args); },
+ leaveState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onleave' + from], name, from, to, args); },
+ enterState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onenter' + to] || fsm['on' + to], name, from, to, args); },
+ changeState: function(fsm, name, from, to, args) { return StateMachine.doCallback(fsm, fsm['onchangestate'], name, from, to, args); },
+
+
+ buildEvent: function(name, map) {
+ return function() {
+
+ var from = this.current;
+ var to = map[from] || map[StateMachine.WILDCARD] || from;
+ var args = Array.prototype.slice.call(arguments); // turn arguments into pure array
+
+ if (this.transition)
+ return this.error(name, from, to, args, StateMachine.Error.PENDING_TRANSITION, "event " + name + " inappropriate because previous transition did not complete");
+
+ if (this.cannot(name))
+ return this.error(name, from, to, args, StateMachine.Error.INVALID_TRANSITION, "event " + name + " inappropriate in current state " + this.current);
+
+ if (false === StateMachine.beforeEvent(this, name, from, to, args))
+ return StateMachine.CANCELLED;
+
+ if (from === to) {
+ StateMachine.afterEvent(this, name, from, to, args);
+ return StateMachine.NOTRANSITION;
+ }
+
+ // prepare a transition method for use EITHER lower down, or by caller if they want an async transition (indicated by an ASYNC return value from leaveState)
+ var fsm = this;
+ this.transition = function() {
+ fsm.transition = null; // this method should only ever be called once
+ fsm.current = to;
+ StateMachine.enterState( fsm, name, from, to, args);
+ StateMachine.changeState(fsm, name, from, to, args);
+ StateMachine.afterEvent( fsm, name, from, to, args);
+ };
+
+ var leave = StateMachine.leaveState(this, name, from, to, args);
+ if (false === leave) {
+ this.transition = null;
+ return StateMachine.CANCELLED;
+ }
+ else if ("async" === leave) {
+ return StateMachine.ASYNC;
+ }
+ else {
+ if (this.transition)
+ this.transition(); // in case user manually called transition() but forgot to return ASYNC
+ return StateMachine.SUCCEEDED;
+ }
+
+ };
+ }
+
+ }; // StateMachine
+
+ //===========================================================================
+
+ if ("function" === typeof define) {
+ define("statemachine", [], function() { return StateMachine; });
+ }
+ else {
+ window.StateMachine = StateMachine;
+ }
+
+}(this));
+
diff --git a/addons/base_import/static/src/css/import.css b/addons/base_import/static/src/css/import.css
index f6258f5aa50..21e598ff707 100644
--- a/addons/base_import/static/src/css/import.css
+++ b/addons/base_import/static/src/css/import.css
@@ -10,7 +10,8 @@
.oe_import .oe_import_grid,
.oe_import .oe_import_error_report,
.oe_import .oe_import_with_file,
-.oe_import .oe_import_noheaders {
+.oe_import .oe_import_noheaders,
+.oe_import .oe_import_report_more {
display: none;
}
@@ -19,7 +20,8 @@
}
.oe_import.oe_import_error .oe_import_error_report,
.oe_import.oe_import_with_file .oe_import_with_file,
-.oe_import.oe_import_noheaders .oe_import_noheaders {
+.oe_import.oe_import_noheaders .oe_import_noheaders,
+.oe_import .oe_import_report_showmore .oe_import_report_more {
display: block;
}
@@ -29,6 +31,9 @@
.oe_import .oe_import_error_report ul .oe_import_report_warning {
background-color: #FEFFD9;
}
+.oe_import .oe_import_error_report ul .oe_import_report_info {
+ background-color: #d3ffd3;
+}
.oe_import .oe_import_noheaders {
color: #888;
@@ -47,7 +52,7 @@
}
.oe_import .oe_import_options label {
display: inline-block;
- width: 10em;
+ width: 8em;
text-align: right;
}
diff --git a/addons/base_import/static/src/js/import.js b/addons/base_import/static/src/js/import.js
index c2deb660ff2..0a60fe18ffb 100644
--- a/addons/base_import/static/src/js/import.js
+++ b/addons/base_import/static/src/js/import.js
@@ -47,31 +47,35 @@ openerp.base_import = function (instance) {
this._super.apply(this, arguments);
if(add_button) {
this.$buttons.on('click', '.oe_list_button_import', function() {
- new instance.web.DataImport(self, self.dataset).open();
+ self.do_action({
+ type: 'ir.actions.client',
+ tag: 'import',
+ params: {
+ model: self.dataset.model
+ }
+ }, void 0, void 0, function () {
+ self.reload();
+ });
return false;
});
}
}
});
- instance.web.DataImport = instance.web.Dialog.extend({
+ instance.web.client_actions.add(
+ 'import', 'instance.web.DataImport');
+ instance.web.DataImport = instance.web.Widget.extend({
template: 'ImportView',
- dialog_title: _lt("Import Data"),
opts: [
{name: 'encoding', label: _lt("Encoding:"), value: 'utf-8'},
{name: 'separator', label: _lt("Separator:"), value: ','},
{name: 'quoting', label: _lt("Quoting:"), value: '"'}
],
events: {
- 'change .oe_import_grid input': 'import_dryrun',
- 'change input.oe_import_file': 'file_update',
- 'change input.oe_import_has_header, .oe_import_options input': 'settings_updated',
- 'click a.oe_import_csv': function (e) {
- e.preventDefault();
- },
- 'click a.oe_import_export': function (e) {
- e.preventDefault();
- },
+ // 'change .oe_import_grid input': 'import_dryrun',
+ 'change .oe_import_file': 'loaded_file',
+ 'click .oe_import_file_reload': 'loaded_file',
+ 'change input.oe_import_has_header, .oe_import_options input': 'settings_changed',
'click a.oe_import_toggle': function (e) {
e.preventDefault();
var $el = $(e.target);
@@ -79,30 +83,84 @@ openerp.base_import = function (instance) {
? $el.next()
: $el.parent().next())
.toggle();
+ },
+ 'click .oe_import_report a.oe_import_report_count': function (e) {
+ e.preventDefault();
+ $(e.target).parent().toggleClass('oe_import_report_showmore');
+ },
+ 'click .oe_import_moreinfo_action a': function (e) {
+ e.preventDefault();
+ // #data will parse the attribute on its own, we don't like
+ // that sort of things
+ var action = JSON.parse($(e.target).attr('data-action'));
+ // FIXME: when JS-side clean_action
+ action.views = _(action.views).map(function (view) {
+ var id = view[0], type = view[1];
+ return [
+ id,
+ type !== 'tree' ? type
+ : action.view_type === 'form' ? 'list'
+ : 'tree'
+ ];
+ });
+ this.do_action(_.extend(action, {
+ target: 'new',
+ flags: {
+ search_view: true,
+ display_title: true,
+ pager: true,
+ list: {selectable: false}
+ }
+ }));
+ },
+ // buttons
+ 'click .oe_import_validate': 'validate',
+ 'click .oe_import_import': 'import',
+ 'click .oe_import_cancel': function (e) {
+ e.preventDefault();
+ this.exit();
}
},
- init: function (parent, dataset) {
+ init: function (parent, params) {
var self = this;
- this._super(parent, {
- buttons: [
- {text: _t("Import File"), click: function () {
- self.do_import();
- }, 'class': 'oe_import_dialog_button'}
- ]
- });
- this.res_model = parent.model;
+ this._super.apply(this, arguments);
+ this.res_model = params.model;
// import object id
this.id = null;
this.Import = new instance.web.Model('base_import.import');
},
start: function () {
var self = this;
- return this.Import.call('create', [{
- 'res_model': this.res_model
- }]).then(function (id) {
- self.id = id;
- self.$('input[name=import_id]').val(id);
- });
+ this.setup_encoding_picker();
+
+ return $.when(
+ this._super(),
+ this.Import.call('create', [{
+ 'res_model': this.res_model
+ }]).then(function (id) {
+ self.id = id;
+ self.$('input[name=import_id]').val(id);
+ })
+ )
+ },
+ setup_encoding_picker: function () {
+ this.$('input.oe_import_encoding').select2({
+ width: '160px',
+ query: function (q) {
+ var make = function (term) { return {id: term, text: term}; };
+ var suggestions = _.map(
+ ('utf-8 utf-16 windows-1252 latin1 latin2 big5 ' +
+ 'gb18030 shift_jis windows-1251 koir8_r').split(/\s+/),
+ make);
+ if (q.term) {
+ suggestions.unshift(make(q.term));
+ }
+ q.callback({results: suggestions});
+ },
+ initSelection: function (e, c) {
+ return c({id: 'utf-8', text: 'utf-8'});
+ }
+ }).select2('val', 'utf-8');
},
import_options: function () {
@@ -118,34 +176,52 @@ openerp.base_import = function (instance) {
},
//- File & settings change section
- file_update: function (e) {
+ onfile_loaded: function () {
+ this.$('.oe_import_button').prop('disabled', true);
if (!this.$('input.oe_import_file').val()) { return; }
this.$el.removeClass('oe_import_preview oe_import_error');
jsonp(this.$el, {
url: '/base_import/set_file'
- }, this.proxy('settings_updated'));
+ }, this.proxy('settings_changed'));
},
- settings_updated: function () {
+ onpreviewing: function () {
+ var self = this;
+ this.$('.oe_import_button').prop('disabled', true);
this.$el.addClass('oe_import_with_file');
// TODO: test that write // succeeded?
- this.Import.call(
- 'parse_preview', [this.id, this.import_options()])
- .then(this.proxy('preview'));
- },
- preview: function (result) {
+ this.$el.removeClass('oe_import_preview_error oe_import_error');
this.$el.toggleClass(
'oe_import_noheaders',
!this.$('input.oe_import_has_header').prop('checked'));
- if (result.error) {
- this.$el.addClass('oe_import_error');
- this.$('.oe_import_error_report').html(
+ this.Import.call(
+ 'parse_preview', [this.id, this.import_options()])
+ .then(function (result) {
+ var signal = result.error ? 'preview_failed' : 'preview_succeeded';
+ self[signal](result);
+ });
+ },
+ onpreview_error: function (event, from, to, result) {
+ this.$('.oe_import_options').show();
+ this.$el.addClass('oe_import_preview_error oe_import_error');
+ this.$('.oe_import_error_report').html(
QWeb.render('ImportView.preview.error', result));
- return;
- }
+ },
+ onpreview_success: function (event, from, to, result) {
+ this.$('.oe_import_import').removeClass('oe_highlight');
+ this.$('.oe_import_validate').addClass('oe_highlight');
+ this.$('.oe_import_button').prop('disabled', false);
this.$el.addClass('oe_import_preview');
this.$('table').html(QWeb.render('ImportView.preview', result));
+ if (result.headers.length === 1) {
+ this.$('.oe_import_options').show();
+ this.onresults(null, null, null, [{
+ type: 'warning',
+ message: _t("A single column was found in the file, this often means the file separator is incorrect")
+ }]);
+ }
+
var $fields = this.$('.oe_import_fields input');
this.render_fields_matches(result, $fields);
var data = this.generate_fields_completion(result);
@@ -180,7 +256,6 @@ openerp.base_import = function (instance) {
width: 'resolve',
dropdownCssClass: 'oe_import_selector'
});
- this.import_dryrun();
},
generate_fields_completion: function (root) {
var basic = [];
@@ -252,40 +327,116 @@ openerp.base_import = function (instance) {
//- import itself
call_import: function (options) {
- var self = this;
var fields = this.$('.oe_import_fields input.oe_import_match_field').map(function (index, el) {
return $(el).select2('val') || false;
}).get();
return this.Import.call(
'do', [this.id, fields, this.import_options()], options);
},
- import_dryrun: function () {
-// this.call_import({ dryrun: true })
-// .then(this.proxy('render_import_errors'));
+ onvalidate: function () {
+ return this.call_import({ dryrun: true })
+ .then(this.proxy('validated'));
},
- do_import: function () {
+ onimport: function () {
var self = this;
- this.call_import({ dryrun: false }).then(function (errors) {
- if (_.isEmpty(errors)) {
- if (self.getParent().reload_content) {
- self.getParent().reload_content();
- }
- self.close();
+ return this.call_import({ dryrun: false }).then(function (message) {
+ if (!_.any(message, function (message) {
+ return message.type === 'error' })) {
+ self['import_succeeded']();
return;
}
- self.render_import_errors(errors);
+ self['import_failed'](message);
});
},
- render_import_errors: function (errors) {
- if (_.isEmpty(errors)) {
- this.$el.removeClass('oe_import_error');
- return;
+ onimported: function () {
+ this.exit();
+ },
+ exit: function () {
+ this.do_action({
+ type: 'ir.actions.client',
+ tag: 'history_back'
+ });
+ },
+ onresults: function (event, from, to, message) {
+ var no_messages = _.isEmpty(message);
+ this.$('.oe_import_import').toggleClass('oe_highlight', no_messages);
+ this.$('.oe_import_validate').toggleClass('oe_highlight', !no_messages);
+ if (no_messages) {
+ message.push({
+ type: 'info',
+ message: _t("Everything seems valid.")
+ });
}
- // import failed (or maybe just warnings, if we ever get
- // warnings?)
+ // row indexes come back 0-indexed, spreadsheets
+ // display 1-indexed.
+ var offset = 1;
+ // offset more if header
+ if (this.import_options().headers) { offset += 1; }
+
this.$el.addClass('oe_import_error');
this.$('.oe_import_error_report').html(
- QWeb.render('ImportView.error', {errors: errors}));
+ QWeb.render('ImportView.error', {
+ errors: _(message).groupBy('message'),
+ at: function (rows) {
+ var from = rows.from + offset;
+ var to = rows.to + offset;
+ if (from === to) {
+ return _.str.sprintf(_t("at row %d"), from);
+ }
+ return _.str.sprintf(_t("between rows %d and %d"),
+ from, to);
+ },
+ more: function (n) {
+ return _.str.sprintf(_t("(%d more)"), n);
+ },
+ info: function (msg) {
+ if (typeof msg === 'string') {
+ return _.str.sprintf(
+ '