Skip to content

Commit

Permalink
Merge pull request #6 from kaerdsar/upstream
Browse files Browse the repository at this point in the history
SaaS Portal Demo
  • Loading branch information
Ivan Yelizariev committed Aug 13, 2015
2 parents d170b90 + 08fe960 commit 7466d56
Show file tree
Hide file tree
Showing 25 changed files with 315 additions and 105 deletions.
5 changes: 5 additions & 0 deletions saas_base/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SaaS Base
=========

Base models to use in saas environment.

1 change: 1 addition & 0 deletions saas_base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import models
10 changes: 10 additions & 0 deletions saas_base/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
'name': 'SaaS Base',
'version': '0.0.1',
'author': 'Cesar Lage',
'category': 'SaaS',
'website': 'https://it-projects.info',
'depends': ['saas_utils'],
'data': [],
'installable': True,
}
2 changes: 2 additions & 0 deletions saas_base/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import saas_base

11 changes: 11 additions & 0 deletions saas_base/models/saas_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
from openerp import api, models, fields


class SaasClient(models.AbstractModel):
_name = 'saas_base.client'

users_len = fields.Integer('Count users', readonly=True)
file_storage = fields.Integer('File storage (MB)', readonly=True)
db_storage = fields.Integer('DB storage (MB)', readonly=True)
expiration_datetime = fields.Datetime('Expiration', track_visibility='onchange')
2 changes: 1 addition & 1 deletion saas_portal/__openerp__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'author': 'Ivan Yelizariev',
'category': 'SaaS',
'website': 'https://it-projects.info',
'depends': ['oauth_provider', 'website', 'auth_signup', 'saas_utils'],
'depends': ['oauth_provider', 'website', 'auth_signup', 'saas_base', 'saas_utils'],
'data': [
'views/wizard.xml',
'views/saas_portal.xml',
Expand Down
3 changes: 1 addition & 2 deletions saas_portal/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def book_then_signup(self, **post):
return self.create_new_database(dbtemplate, full_dbname, organization=organization)

def create_new_database(self, plan_id):
scheme = request.httprequest.scheme
plan = request.env['saas_portal.plan'].sudo().browse(plan_id)
url = plan._create_new_database(scheme=scheme)[0]
url = plan._create_new_database()[0]
return request.redirect(url)

@http.route('/saas_portal/tenant', type='http', auth='public', website=True)
Expand Down
66 changes: 22 additions & 44 deletions saas_portal/models/saas_portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,21 @@ class SaasPortalServer(models.Model):
sequence = fields.Integer('Sequence')
active = fields.Boolean('Active', default=True)
request_scheme = fields.Selection([('http', 'http'), ('https', 'https')], 'Scheme', default='http', required=True)
request_port = fields.Integer('Request Port', default=80)
client_ids = fields.One2many('saas_portal.client', 'server_id', string='Clients')


@api.one
def _request_params(self, path='/web', scheme=None, state={}, scope=None, client_id=None):
def _request_params(self, path='/web', scheme=None, port=None, state={}, scope=None, client_id=None):
scheme = scheme or self.request_scheme
port = port or self.request_port
scope = scope or ['userinfo', 'force_login', 'trial', 'skiptheuse']
scope = ' '.join(scope)
client_id = client_id or self.env['saas_portal.client'].generate_client_id()
params = {
'scope': scope,
'state': simplejson.dumps(state),
'redirect_uri': '{scheme}://{saas_server}{path}'.format(scheme=scheme, saas_server=self.name, path=path),
'redirect_uri': '{scheme}://{saas_server}:{port}{path}'.format(scheme=scheme, port=port, saas_server=self.name, path=path),
'response_type': 'token',
'client_id': client_id,
}
Expand All @@ -56,8 +58,9 @@ def _request(self, **kwargs):
return url

@api.one
def _request_server(self, path=None, scheme=None, **kwargs):
def _request_server(self, path=None, scheme=None, port=None, **kwargs):
scheme = scheme or self.request_scheme
port = port or self.request_port
params = self._request_params(**kwargs)[0]
access_token = self.env['oauth.access_token'].sudo().search([('application_id', '=', self.oauth_application_id.id)], order='id DESC', limit=1)
access_token = access_token[0].token
Expand All @@ -66,13 +69,13 @@ def _request_server(self, path=None, scheme=None, **kwargs):
'access_token': access_token,
'expires_in': 3600,
})
url = '{scheme}://{saas_server}{path}?{params}'.format(scheme=scheme, saas_server=self.name, path=path, params=werkzeug.url_encode(params))
url = '{scheme}://{saas_server}:{port}{path}?{params}'.format(scheme=scheme, saas_server=self.name, port=port, path=path, params=werkzeug.url_encode(params))
return url

@api.multi
def action_redirect_to_server(self):
r = self[0]
url = '{scheme}://{saas_server}{path}'.format(scheme=r.request_scheme, saas_server=r.name, path='/web')
url = '{scheme}://{saas_server}:{port}{path}'.format(scheme=r.request_scheme, saas_server=r.name, port=r.request_port, path='/web')
return {
'type': 'ir.actions.act_url',
'target': 'new',
Expand Down Expand Up @@ -148,6 +151,9 @@ def _default_tz(self):
dbname_template = fields.Char('DB Names', help='Template for db name. Use %i for numbering. Ignore if you use manually created db names', placeholder='crm-%i.odoo.com')
server_id = fields.Many2one('saas_portal.server', string='SaaS Server',
help='User this saas server or choose random')

website_description = fields.Text('Website description')
logo = fields.Binary('Logo')


@api.one
Expand All @@ -167,7 +173,7 @@ def _new_database_vals(self, vals):
return vals

@api.one
def _create_new_database(self, scheme='http', dbname=None, client_id=None, partner_id=None):
def _create_new_database(self, dbname=None, client_id=None, partner_id=None):
server = self.server_id
if not server:
server = self.env['saas_portal.server'].get_saas_server()
Expand All @@ -192,14 +198,19 @@ def _create_new_database(self, scheme='http', dbname=None, client_id=None, partn
client = self.env['saas_portal.client'].create(vals)
client_id = client.client_id

scheme = server.request_scheme
port = server.request_port
state = {
'd': client.name,
'r': '%s://%s/web' % (scheme, client.name),
'db_template': self.template_id.name,
'e': client.expiration_datetime,
'r': '%s://%s:%s/web' % (scheme, port, client.name),
}
if self.template_id:
state.update({'db_template': self.template_id.name})
scope = ['userinfo', 'force_login', 'trial', 'skiptheuse']
url = server._request(path='/saas_server/new_database',
scheme=scheme,
port=port,
state=state,
client_id=client_id,
scope=scope,)[0]
Expand All @@ -213,7 +224,7 @@ def generate_dbname(self, raise_error=True):
raise exceptions.Warning(_('Template for db name is not configured'))
return ''
id = str(random.randint(100, 10000))
return self.dbname_template.replace('%i', id)
return self.dbname_template.replace('%i', str(1234))

@api.multi
def create_template(self):
Expand Down Expand Up @@ -262,7 +273,7 @@ def upgrade_template(self, cr, uid, ids, context=None):

@api.multi
def delete_template(self):
return self[0].template_id.delete_db()
return self[0].template_id.delete_database()

class OauthApplication(models.Model):
_inherit = 'oauth.application'
Expand Down Expand Up @@ -349,50 +360,17 @@ def _delete_database_server(self, force_delete=False):
if res.status_code != 500:
self.state = 'deleted'

def delete_db(self, cr, uid, ids, context=None):
obj = self.browse(cr, uid, ids[0])
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'saas.config',
'target': 'new',
'context': {
'default_action': 'delete',
'default_server_id': obj.server_id.id,
'default_database_id': obj.id,
}
}

def upgrade_db(self, cr, uid, ids, context=None):
obj = self.browse(cr, uid, ids[0])
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'saas.config',
'target': 'new',
'context': {
'default_action': 'upgrade',
'default_database': obj.name
}
}


class SaasPortalClient(models.Model):
_name = 'saas_portal.client'
_description = 'Client'
_rec_name = 'name'

_inherit = ['mail.thread', 'saas_portal.database']
_inherit = ['mail.thread', 'saas_portal.database', 'saas_base.client']

name = fields.Char(required=True)
partner_id = fields.Many2one('res.partner', string='Partner', track_visibility='onchange')
plan_id = fields.Many2one('saas_portal.plan', string='Plan', track_visibility='onchange')
users_len = fields.Integer('Count users', readonly=True)
file_storage = fields.Integer('File storage (MB)', readonly=True)
db_storage = fields.Integer('DB storage (MB)', readonly=True)
expiration_datetime = fields.Datetime('Expiration', track_visibility='onchange')
expired = fields.Boolean('Expiration', compute='_get_expired')

@api.one
Expand Down
88 changes: 48 additions & 40 deletions saas_portal/views/saas_portal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
<menuitem name="SaaS" id="menu_base_saas" sequence="30" />
<menuitem id="menu_saas" parent="menu_base_saas" name="SaaS" sequence="1" />

<!-- Database -->

<record id="view_databases_form" model="ir.ui.view">
<field name="name">saas_portal.databases.form</field>
<field name="model">saas_portal.database</field>
<field name="arch" type="xml">
<form string="plans">
<sheet>
<group>
<field name="name" />
<field name="state" />
<field name="client_id" readonly="1" />
</group>
</sheet>
</form>
</field>
</record>

<!-- Client -->

Expand Down Expand Up @@ -133,6 +150,7 @@
<field name="client_id"/>
<field name="active"/>
<field name="request_scheme"/>
<field name="request_port"/>
</group>
</group>
<field name="client_ids" readonly="1">
Expand Down Expand Up @@ -198,46 +216,36 @@
<button string="Create Client" type="action" name="%(action_create_client)d"/>
<field name="state" widget="statusbar"/>
</header>
<sheet>
<label for="name" class="oe_edit_only"/>
<h1>
<field name="name" class="oe_inline"/>
</h1>
<field name="summary" class="oe_inline"/>

<group>
<group string="Technical Settings">
<field name="server_id"/>
<field name="template_id"/>
<!--
role_id should be deleted or moved to separate module
<field name="role_id"/>
-->
</group>
<group string="Plan Settings">
<field name="dbname_template"/>
<field name="expiration"/>
</group>
</group>

<separator string="Odoo DB Settings"/>
<group col="4">
<field name="demo"/>
<field name="lang"/>
<field name="tz"/>
</group>

<!--
<notebook>
<page string="Required Addons" >
<field name="required_addons_ids" nolabel="1" />
</page>
<page string="Optional Addons" >
<field name="optional_addons_ids" nolabel="1" />
</page>
</notebook>
-->
</sheet>
<sheet>
<div class="oe_left" style="width: 500px;">
<field name="logo" widget="image" class="oe_avatar oe_left"/>
<div class="oe_title" style="width: 390px;">
<label class="oe_edit_only" for="name" string="Name"/>
<h1><field name="name" class="oe_inline"/></h1>
<field name="summary"/>
</div>
</div>
<group>
<group string="Technical Settings">
<field name="server_id"/>
<field name="template_id"/>
</group>
<group string="Plan Settings">
<field name="dbname_template"/>
<field name="expiration"/>
</group>
</group>
<separator string="Odoo DB Settings"/>
<group col="4">
<field name="demo"/>
<field name="lang"/>
<field name="tz"/>
</group>
<separator string="Website Settings"/>
<group>
<field name="website_description" col="4"/>
</group>
</sheet>
</form>
</field>
</record>
Expand Down
5 changes: 5 additions & 0 deletions saas_portal_demo/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SaaS Portal Demo
================

Module to create pages with plan description an a link to test a demo for a few hours.

2 changes: 2 additions & 0 deletions saas_portal_demo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import controllers
import models
14 changes: 14 additions & 0 deletions saas_portal_demo/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
'name': 'SaaS Portal Demo',
'version': '0.1',
'author': 'Cesar Lage',
'category': 'SaaS',
'website': 'https://it-projects.info',
'depends': ['saas_portal'],
'data': [
'security/ir.model.access.csv',
'views/saas_portal.xml',
'views/website.xml'
],
'installable': True,
}
1 change: 1 addition & 0 deletions saas_portal_demo/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import main
37 changes: 37 additions & 0 deletions saas_portal_demo/controllers/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# -*- coding: utf-8 -*-
import werkzeug
from openerp.addons.web import http
from openerp.addons.web.http import request
from openerp.addons.web.controllers.main import login_redirect
from openerp.addons.saas_portal.controllers.main import SaasPortal


def signup_redirect():
url = '/web/signup?'
redirect_url = '%s?%s' % (request.httprequest.base_url, werkzeug.urls.url_encode(request.params))
return """<html><head><script>
window.location = '%sredirect=' + encodeURIComponent("%s");
</script></head></html>
""" % (url, redirect_url)


class SaasPortalDemo(SaasPortal):

@http.route(['/demo/<string:version>/<string:plan_url>'], type='http', auth='public', website=True)
def show_plan(self, version, plan_url, **post):
domain = [('odoo_version', '=', version), ('page_url', '=', plan_url),
('state', '=', 'confirmed')]
plan = request.env['saas_portal.plan'].sudo().search(domain)
if not plan:
# TODO: maybe in this case we can redirect to saas_portal_templates.select_template
return request.website.render("saas_portal_demo.unavailable_plan")
values = {'plan': plan[0]}
return request.website.render("saas_portal_demo.show_plan", values)

@http.route('/demo/new_database', type='http', auth='public', website=True)
def new_demo_database(self, **post):
if not request.session.uid:
return signup_redirect()
plan_id = int(post.get('plan_id'))

return self.create_new_database(plan_id)
2 changes: 2 additions & 0 deletions saas_portal_demo/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import saas_portal

Loading

0 comments on commit 7466d56

Please sign in to comment.