Skip to content

Commit

Permalink
[ADD] website_redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
yelizariev committed Apr 23, 2015
1 parent 310064a commit f4e133f
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 0 deletions.
8 changes: 8 additions & 0 deletions website_redirect/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Page redirections
=================

The module allows configure redirections rules. E.g.

contactus.php -> /page/website.contactus

Tested on Odoo 8.0 ea60fed97af1c139e4647890bf8f68224ea1665b
1 change: 1 addition & 0 deletions website_redirect/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import website_redirect_models
12 changes: 12 additions & 0 deletions website_redirect/__openerp__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
'name': "Page redirections",
'version': '1.0.0',
'author': 'Ivan Yelizariev',
'category': 'Custom',
'website': 'https://yelizariev.github.io',
'depends': ['website'],
'data': [
'website_redirect_views.xml',
],
'installable': True
}
46 changes: 46 additions & 0 deletions website_redirect/website_redirect_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from openerp import api, models, fields, SUPERUSER_ID
from openerp.http import request
import fnmatch
import werkzeug.utils

class website_redirect(models.Model):
_name = 'website.redirect'

_order = 'sequence,id'

name = fields.Char('Name')
active = fields.Boolean('Active', default=True)
sequence = fields.Integer('Sequence')
domain = fields.Char('Domain Name', placeholder='odoo.com', help='keep empty to apply rules for any domain')

rule_ids = fields.One2many('website.redirect.rule', 'redirect_id', string='Rules')

class website_redirect(models.Model):
_name = 'website.redirect.rule'

_order = 'sequence,id'
sequence = fields.Integer('Sequence')
pattern = fields.Char('From', help='Unix shell-style wildcards. Check https://docs.python.org/2/library/fnmatch.html for details')
target = fields.Char('To')
redirect_id = fields.Many2one('website.redirect')

class ir_http(models.AbstractModel):
_inherit = 'ir.http'

def _dispatch(self):
host = request.httprequest.environ.get('HTTP_HOST', '').split(':')[0]
www, _, h = host.partition('.')
if www == 'www':
host = h
path = request.httprequest.path
print 'path', path
redirect_ids = self.pool['website.redirect'].search(request.cr, SUPERUSER_ID, [])
for redirect in self.pool['website.redirect'].browse(request.cr, SUPERUSER_ID, redirect_ids):
if redirect.domain and redirect.domain != host:
continue

for rule in redirect.rule_ids:
if fnmatch.fnmatch(path, rule.pattern):
code = 302
return werkzeug.utils.redirect(rule.target, code)
return super(ir_http, self)._dispatch()
62 changes: 62 additions & 0 deletions website_redirect/website_redirect_views.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<openerp><data>

<record model="ir.ui.view" id="view_website_redirect_form">
<field name="name">website.redirect.form</field>
<field name="model">website.redirect</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Redirection">
<sheet>
<div class="oe_title">
<label for="name" class="oe_edit_only"/>
<h1>
<field name="name"/>
</h1>
</div>
<group>
<field name="domain"/>
<field name="active"/>
</group>
<notebook>
<page string="Rules">
<field name="rule_ids">
<tree editable="bottom">
<field name="sequence" widget="handle"/>
<field name="pattern"/>
<field name="target"/>
</tree>
</field>
</page>
</notebook>
</sheet>
</form>
</field>
</record>

<record id="view_website_redirect_tree" model="ir.ui.view">
<field name="name">website.redirect.tree</field>
<field name="model">website.redirect</field>
<field name="arch" type="xml">
<tree string="Redirections">
<field name="sequence" widget="handle"/>
<field name="name"/>
<field name="domain"/>
</tree>
</field>
</record>

<record id="action_website_redirect" model="ir.actions.act_window">
<field name="name">Redirections</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">website.redirect</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>


<menuitem id="menu_website_redirect" parent="base.menu_config"
sequence="90" action="action_website_redirect"/>


</data></openerp>

0 comments on commit f4e133f

Please sign in to comment.