diff --git a/saas_client/controllers/main.py b/saas_client/controllers/main.py index 1b677593d..a42a044cb 100644 --- a/saas_client/controllers/main.py +++ b/saas_client/controllers/main.py @@ -1,10 +1,11 @@ # -*- coding: utf-8 -*- import werkzeug -from openerp import http +from openerp import http, SUPERUSER_ID from openerp.http import request -from openerp.addons.auth_oauth.controllers import main as oauth import simplejson + + class SaasClient(http.Controller): @http.route(['/saas_client/new_database', @@ -16,3 +17,46 @@ def new_database(self, **post): state['p'] = request.env.ref('saas_server.saas_oauth_provider').id params['state'] = simplejson.dumps(state) return werkzeug.utils.redirect('/auth_oauth/signin?%s' % werkzeug.url_encode(params)) + + @http.route('/saas_client/upgrade_database', type='http', auth='none') + def upgrade_database(self, **post): + try: + db = request.httprequest.host.replace('.', '_') + pwd = config.get('tenant_passwd') + uid = request.session.authenticate(db, 'admin', pwd) + if uid: + module = request.registry['ir.module.module'] + # 1. Update addons + update_addons = post.get('update_addons', '').split(',') + if update_addons: + upids = module.search(request.cr, SUPERUSER_ID, + [('name', 'in', update_addons)]) + if upids: + module.button_immediate_upgrade(request.cr, SUPERUSER_ID, upids) + # 2. Install addons + install_addons = post.get('install_addons', '').split(',') + if install_addons: + inids = module.search(request.cr, SUPERUSER_ID, + [('name', 'in', install_addons)]) + if inids: + module.button_immediate_install(request.cr, SUPERUSER_ID, inids) + # 3. Uninstall addons + uninstall_addons = post.get('uninstall_addons', '').split(',') + if uninstall_addons: + unids = module.search(request.cr, SUPERUSER_ID, + [('name', 'in', uninstall_addons)]) + if unids: + module.button_immediate_uninstall(request.cr, SUPERUSER_ID, unids) + # 4. Run fixes + fixes = post.get('fixes', '').split(',') + for fix in fixes: + if fix: + model, method = fix.split('-') + getattr(request.registry[model], method)(request.cr, + SUPERUSER_ID) + status_code = 200 + else: + status_code = 400 + except: + status_code = 500 + return werkzeug.wrappers.Response(status=status_code) diff --git a/saas_portal/controllers/main.py b/saas_portal/controllers/main.py index c2a5285a5..f8c4876d4 100644 --- a/saas_portal/controllers/main.py +++ b/saas_portal/controllers/main.py @@ -130,7 +130,6 @@ def web_login(self, *args, **kw): @http.route() def web_auth_reset_password(self, *args, **kw): - kw['reset'] = True if kw.get('login', False): user = request.registry.get('res.users') domain = [('login', '=', kw['login'])] diff --git a/saas_portal/models/saas_portal.py b/saas_portal/models/saas_portal.py index 694230d13..92ef39a49 100644 --- a/saas_portal/models/saas_portal.py +++ b/saas_portal/models/saas_portal.py @@ -271,15 +271,27 @@ def generate_client_id(self): ('pending','Pending'), ('deleted','Deleted')], 'State', default='draft', track_visibility='onchange') - + expiration_datetime = fields.Datetime('Expiration', track_visibility='onchange') expired = fields.Boolean('Expiration', compute='_get_expired') + last_connection = fields.Char(compute='_get_last_connection', + string='Last Connection', size=64) _sql_constraints = [ ('name_uniq', 'unique (name)', 'Record for this database already exists!'), ('client_id_uniq', 'unique (client_id)', 'client_id should be unique!'), ] + @api.one + def _get_last_connection(self): + oat = self.pool.get('oauth.access_token') + to_search = [('application_id', '=', self.id)] + access_token_ids = oat.search(self.env.cr, self.env.uid, to_search) + if access_token_ids: + access_token = oat.browse(self.env.cr, self.env.uid, + access_token_ids[0]) + self.last_connection = access_token.user_id.login_date + @api.one def action_update_stats(self): self.server_id.action_update_stats() diff --git a/saas_portal/models/wizard.py b/saas_portal/models/wizard.py index be2ca6c52..80af7cfbf 100644 --- a/saas_portal/models/wizard.py +++ b/saas_portal/models/wizard.py @@ -1,7 +1,13 @@ # -*- coding: utf-8 -*- +import requests +import werkzeug +import datetime + import openerp -from openerp import models, fields, api, SUPERUSER_ID from openerp.addons.saas_utils import connector, database +from openerp.addons.web.http import request +from openerp.tools import config +from openerp import models, fields, api, SUPERUSER_ID from openerp import http @@ -14,8 +20,9 @@ class SaasConfig(models.TransientModel): server_id = fields.Many2one('saas_portal.server', string='Server') update_addons = fields.Char('Update Addons', size=256) install_addons = fields.Char('Install Addons', size=256) + uninstall_addons = fields.Char('Uninstall Addons', size=256) fix_ids = fields.One2many('saas.config.fix', 'config_id', 'Fixes') - description = fields.Text('Description') + description = fields.Text('Result') @api.multi def execute_action(self): @@ -30,42 +37,29 @@ def delete_database(self): return self.database_id.delete_database() def upgrade_database(self, cr, uid, obj, context=None): - dbs = obj.database and [obj.database] or database.get_market_dbs() - uaddons = obj.update_addons and obj.update_addons.split(',') or [] - update_domain = [('name', 'in', uaddons)] - iaddons = obj.install_addons and obj.install_addons.split(',') or [] - install_domain = [('name', 'in', iaddons)] - no_update_dbs = [] - for db_name in dbs: - try: - registry = openerp.modules.registry.RegistryManager.get(db_name) - with registry.cursor() as rcr: - # update database.uuid - openerp.service.db._drop_conn(rcr, db_name) - module = registry['ir.module.module'] - # 1. Update existing modules - uaids = module.search(rcr, SUPERUSER_ID, update_domain) - if uaids: - module.button_upgrade(rcr, SUPERUSER_ID, uaids) - # 2. Install new modules - iaids = module.search(rcr, SUPERUSER_ID, install_domain) - if iaids: - module.button_immediate_install(rcr, SUPERUSER_ID, iaids) - # 3. Execute methods - for fix in obj.fix_ids: - getattr(registry[fix.model], fix.method)(rcr, SUPERUSER_ID) - except: - no_update_dbs.append(db_name) - if no_update_dbs: - desc = 'These databases were not updated: %s', ', '.join(no_update_dbs) - self.write(cr, uid, obj.id, {'description': desc}) + res = {} + scheme = request.httprequest.scheme + payload = { + 'update_addons': obj.update_addons, + 'install_addons': obj.install_addons, + 'uninstall_addons': obj.uninstall_addons, + 'fixes': ','.join(['%s-%s' % (x.model, x.method) for x in obj.fix_ids]) + } + + + dbs = obj.database and obj.database.split(',') or database.get_market_dbs(False) + for db in dbs: + url = '{scheme}://{domain}/saas_client/upgrade_database'.format(scheme=scheme, domain=db.replace('_', '.')) + r = requests.post(url, data=payload) + res[db] = r.status_code + self.write(cr, uid, obj.id, {'description': str(res)}) return { 'type': 'ir.actions.act_window', 'view_type': 'form', 'view_mode': 'form', 'res_model': 'saas.config', 'res_id': obj.id, - 'target': 'new', + 'target': 'new' } diff --git a/saas_portal/views/saas_portal.xml b/saas_portal/views/saas_portal.xml index abd17d3b3..745173d45 100644 --- a/saas_portal/views/saas_portal.xml +++ b/saas_portal/views/saas_portal.xml @@ -2,7 +2,7 @@ - + @@ -13,11 +13,12 @@ 4 - + - - - + + + +