Skip to content

Commit

Permalink
Merge pull request #4 from kaerdsar/develop
Browse files Browse the repository at this point in the history
merge kaerdsar/develop
  • Loading branch information
Ivan Yelizariev committed Jul 9, 2015
2 parents dd7cc16 + e2003fa commit 0e59cf6
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 148 deletions.
48 changes: 46 additions & 2 deletions saas_client/controllers/main.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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)
1 change: 0 additions & 1 deletion saas_portal/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])]
Expand Down
14 changes: 13 additions & 1 deletion saas_portal/models/saas_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
58 changes: 26 additions & 32 deletions saas_portal/models/wizard.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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):
Expand All @@ -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'
}


Expand Down
121 changes: 62 additions & 59 deletions saas_portal/views/saas_portal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<openerp>
<data>
<menuitem name="SaaS" id="menu_base_saas" sequence="30" />
<menuitem id="menu_saas" parent="menu_base_saas" name="SaaS" sequence="1" />
<menuitem id="menu_saas" parent="menu_base_saas" name="SaaS" sequence="1" />


<!-- Client -->
Expand All @@ -13,11 +13,12 @@
<field name="priority">4</field>
<field name="arch" type="xml">
<tree string="Clients">
<field name="name"/>
<field name="name"/>
<field name="state"/>
<field name="users_len"/>
<field name="file_storage"/>
<field name="db_storage"/>
<field name="last_connection"/>
<field name="users_len"/>
<field name="file_storage"/>
<field name="db_storage"/>
<field name="expiration_datetime"/>
<button name="edit_database" type="object" icon="gtk-edit" attrs="{'invisible':[('state', 'in', ['deleted', 'draft'])]}"/>
<!--<button name="upgrade_db" type="object" icon="gtk-execute" />-->
Expand Down Expand Up @@ -77,7 +78,7 @@
<field name="view_mode">tree,form</field>
</record>

<menuitem action="action_clients" id="menu_clients" parent="menu_saas" sequence="10"/>
<menuitem action="action_clients" id="menu_clients" parent="menu_saas" sequence="10"/>

<!-- Server -->

Expand Down Expand Up @@ -139,22 +140,22 @@

<menuitem action="saas_portal.action_server" id="menu_server" parent="saas_portal.menu_saas" sequence="20"/>

<!-- Plan -->
<record id="view_plans_tree" model="ir.ui.view">
<!-- Plan -->

<record id="view_plans_tree" model="ir.ui.view">
<field name="name">saas_portal.plans.tree</field>
<field name="model">saas_portal.plan</field>
<field name="priority">4</field>
<field name="arch" type="xml">
<tree string="Plans">
<field name="sequence" invisible="1"/>
<field name="name"/>
<field name="template_id"/>
<field name="state" />
<button name="create_template" type="object" states="draft" icon="gtk-apply" />
<button name="edit_template" type="object" states="confirmed" icon="gtk-edit" />
<field name="name"/>
<field name="template_id"/>
<field name="state" />
<button name="create_template" type="object" states="draft" icon="gtk-apply" />
<button name="edit_template" type="object" states="confirmed" icon="gtk-edit" />
<!--<button name="upgrade_template" type="object" states="confirmed" icon="gtk-execute" />-->
<button name="delete_template" type="object" states="confirmed" icon="gtk-cancel" />
<button name="delete_template" type="object" states="confirmed" icon="gtk-cancel" />
</tree>
</field>
</record>
Expand All @@ -164,7 +165,7 @@
<field name="name">saas_portal.plans.form</field>
<field name="model">saas_portal.plan</field>
<field name="arch" type="xml">
<form string="plans">
<form string="plans">
<header>
<button string="Create template DB" name="create_template" type="object" states="draft" icon="gtk-apply" class="oe_highlight"/>
<button string="Edit template DB" name="edit_template" type="object" states="confirmed" icon="gtk-edit" class="oe_highlight"/>
Expand Down Expand Up @@ -212,7 +213,7 @@
</notebook>
-->
</sheet>
</form>
</form>
</field>
</record>

Expand All @@ -224,73 +225,75 @@
<field name="view_mode">tree,form</field>
</record>

<menuitem action="action_plans" id="menu_plans" parent="saas_portal.menu_saas" sequence="30"/>
<!-- Config -->
<record id="action_database_form" model="ir.ui.view">
<menuitem action="action_plans" id="menu_plans" parent="saas_portal.menu_saas" sequence="30"/>

<!-- Config -->

<record id="action_database_form" model="ir.ui.view">
<field name="name">action.database.form</field>
<field name="model">saas.config</field>
<field name="arch" type="xml">
<form string="Configure Database" create="false" edit="false" delete="false">
<group>
<field name="action" readonly="1"/>
<field name="update_addons" attrs="{'invisible': [('action', '!=', 'upgrade')]}"/>
<field name="install_addons" attrs="{'invisible': [('action', '!=', 'upgrade')]}"/>
<field name="database_id" readonly="1"/>
<field name="server_id" readonly="1"/>
</group>
<group attrs="{'invisible': [('action', '!=', 'upgrade')]}">
<field name="fix_ids" colspan="4" >
<tree string="Fixes" editable="bottom">
<field name="model" />
<field name="method" />
</tree>
</field>
</group>
<form string="Configure Database" create="false" edit="false" delete="false">
<group>
<field name="id" invisible="1" />
<field name="action" readonly="1"/>
<field name="update_addons" attrs="{'invisible': [('action', '!=', 'upgrade')]}"/>
<field name="install_addons" attrs="{'invisible': [('action', '!=', 'upgrade')]}"/>
<field name="uninstall_addons" attrs="{'invisible': [('action', '!=', 'upgrade')]}"/>
<field name="database_id" />
<field name="server_id" readonly="1"/>
</group>
<group attrs="{'invisible': [('action', '!=', 'upgrade')]}">
<field name="fix_ids" colspan="4" >
<tree string="Fixes" editable="bottom">
<field name="model" />
<field name="method" />
</tree>
</field>
</group>
<!--
<separator />
<group>
<field name="description" />
<field name="description" attrs="{'invisible': [('id', '=', False)]}" />
</group>
-->
<footer>
<button name="execute_action" string="Execute" type="object" class="oe_highlight"/>
or
<button string="Close" class="oe_link" special="cancel"/>
</footer>
<footer>
<button name="execute_action" string="Execute" type="object" class="oe_highlight"/>
or
<button string="Close" class="oe_link" special="cancel"/>
</footer>
</form>
</field>
</record>
<record id="action_upgrade_clients" model="ir.actions.act_window">

<record id="action_upgrade_clients" model="ir.actions.act_window">
<field name="name">Upgrade Clients</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">saas.config</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
<field name="context">{'default_action': 'upgrade'}</field>
<field name="target">new</field>
<field name="context">{'default_action': 'upgrade'}</field>
</record>

<!--<menuitem action="action_upgrade_clients" id="menu_upgrade_clients" parent="saas_portal.menu_saas" sequence="100"/>-->

<!-- Upgrade Databases -->
<record id="action_updb" model="ir.actions.act_window">
<!-- Upgrade Databases -->

<record id="action_updb" model="ir.actions.act_window">
<field name="name">Upgrade Databases</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">saas.config</field>
<field name="view_type">form</field>
<field name="target">new</field>
<field name="target">new</field>
</record>

<record id="action_binding_updb" model="ir.values">
<field name="name">Upgrade Databases</field>
<field name="key2">client_action_multi</field>
<field name="model">saas.config</field>
<field name="value_unpickle" eval="'ir.actions.act_window,'+str(action_updb)"/>
</record>

<record id="action_binding_updb" model="ir.values">
<field name="name">Upgrade Databases</field>
<field name="key2">client_action_multi</field>
<field name="model">saas.config</field>
<field name="value_unpickle" eval="'ir.actions.act_window,'+str(action_updb)"/>
</record>

</data>
</openerp>
Loading

0 comments on commit 0e59cf6

Please sign in to comment.