[IMP] website: create special extension page redirect backend + own template
Before this commit: 1. You could create a page with supported extension in the path (eg: myfile.js) You would then be redirected to the webeditor (myfile.js?enable_editor..) But in this case, it would just redirect you to a plain text page without odoo layout because supported extension are rendered with a specific mimetype. (is it is a .js page, it will just render the page content as a js file) 2. These kind of page would still call t-layout that you should be removed in backend in order to make it work Now: 1. You will be automatically redirected to the back end to edit your file instead of landing on a text-only page. 2. Supported extension now have their own template (eg: .js file now have <script text=..> tag added automatically in their content)
This commit is contained in:
@@ -29,15 +29,17 @@ odoo._geoip_resolver = None
|
||||
# Slug API
|
||||
# ------------------------------------------------------------
|
||||
|
||||
def _guess_mimetype(ext=False, default='text/html'):
|
||||
def _guess_mimetype(ext=False, default=False):
|
||||
exts = {
|
||||
'.css': 'text/css',
|
||||
'.less': 'text/less',
|
||||
'.js': 'text/javascript',
|
||||
'.xml': 'text/xml',
|
||||
'.csv': 'text/csv',
|
||||
'.html': 'text/html',
|
||||
'.css': ['text/css', 'website.default_css'],
|
||||
'.less': ['text/less', 'website.default_less'],
|
||||
'.js': ['text/javascript', 'website.default_javascript'],
|
||||
'.xml': ['text/xml', 'website.default_xml'],
|
||||
'.csv': ['text/csv', 'website.default_csv'],
|
||||
'.html': ['text/html', False],
|
||||
}
|
||||
if not default:
|
||||
default = exts['.html']
|
||||
return ext is not False and exts.get(ext, default) or exts
|
||||
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import base64
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import logging
|
||||
import requests
|
||||
import werkzeug.utils
|
||||
@@ -16,7 +17,7 @@ import odoo
|
||||
from odoo import http, models, fields, _
|
||||
from odoo.http import request
|
||||
from odoo.tools import pycompat, OrderedSet
|
||||
from odoo.addons.http_routing.models.ir_http import slug
|
||||
from odoo.addons.http_routing.models.ir_http import slug, _guess_mimetype
|
||||
from odoo.addons.web.controllers.main import WebClient, Binary, Home
|
||||
from odoo.addons.portal.controllers.portal import pager as portal_pager
|
||||
|
||||
@@ -245,10 +246,20 @@ class Website(Home):
|
||||
|
||||
@http.route(['/website/add/', '/website/add/<path:path>'], type='http', auth="user", website=True)
|
||||
def pagenew(self, path="", noredirect=False, add_menu=False, template=False):
|
||||
# for supported mimetype, get correct default template
|
||||
_, ext = os.path.splitext(path)
|
||||
mimetype_template = _guess_mimetype(ext)[1]
|
||||
if not template:
|
||||
template = mimetype_template
|
||||
|
||||
template = template and dict(template=template) or {}
|
||||
url = request.env['website'].new_page(path, add_menu=add_menu, **template)
|
||||
page = request.env['website'].new_page(path, add_menu=add_menu, **template)
|
||||
url = page['url']
|
||||
if noredirect:
|
||||
return werkzeug.wrappers.Response(url, mimetype='text/plain')
|
||||
|
||||
if ext and ext != '.html': # redirect non html pages to backend to edit
|
||||
return werkzeug.utils.redirect('/web#id=' + str(page.get('view_id')) + '&view_type=form&model=ir.ui.view')
|
||||
return werkzeug.utils.redirect(url + "?enable_editor=1")
|
||||
|
||||
@http.route(['/website/snippets'], type='json', auth="user", website=True)
|
||||
|
||||
@@ -116,7 +116,7 @@ class Http(models.AbstractModel):
|
||||
# 'path': req_page[1:],
|
||||
'deletable': True,
|
||||
'main_object': mypage,
|
||||
}, mimetype=_guess_mimetype(ext))
|
||||
}, mimetype=_guess_mimetype(ext)[0])
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -97,7 +97,6 @@ class Website(models.Model):
|
||||
:param template : potential xml_id of the page to create
|
||||
:param namespace : module part of the xml_id if none, the template module name is used
|
||||
"""
|
||||
|
||||
if namespace:
|
||||
template_module = namespace
|
||||
else:
|
||||
@@ -105,6 +104,7 @@ class Website(models.Model):
|
||||
# completely arbitrary max_length
|
||||
page_url = '/' + slugify(name, max_length=200, path=True)
|
||||
page_key = self.get_unique_path(slugify(name, 50))
|
||||
result = dict({'url': page_url, 'view_id': False})
|
||||
|
||||
if not name:
|
||||
name = 'Home'
|
||||
@@ -125,6 +125,7 @@ class Website(models.Model):
|
||||
'website_ids': [(6, None, [self.get_current_website().id])],
|
||||
'view_id': view.id
|
||||
})
|
||||
result['view_id'] = view.id
|
||||
if add_menu:
|
||||
self.env['website.menu'].create({
|
||||
'name': name,
|
||||
@@ -133,7 +134,7 @@ class Website(models.Model):
|
||||
'page_id': page.id,
|
||||
'website_id': self.get_current_website().id,
|
||||
})
|
||||
return page_url
|
||||
return result
|
||||
|
||||
def get_unique_path(self, page_url):
|
||||
""" Given an url, return that url suffixed by counter if it already exists
|
||||
|
||||
@@ -534,6 +534,34 @@
|
||||
</t>
|
||||
</template>
|
||||
|
||||
<template id="default_javascript">
|
||||
<script type="text/javascript">
|
||||
if (0 > 1) {
|
||||
let it_cant_be = false;
|
||||
}
|
||||
</script>
|
||||
</template>
|
||||
<template id="default_xml">
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
</template>
|
||||
<template id="default_css">
|
||||
<style type="text/css">
|
||||
div#wrap div > h1{
|
||||
color: #875A7B;
|
||||
}
|
||||
</style>
|
||||
</template>
|
||||
<template id="default_less">
|
||||
<style type="text/less">
|
||||
div#wrap div > h1 {
|
||||
color: @odoo-brand-primary;
|
||||
}
|
||||
</style>
|
||||
</template>
|
||||
<template id="default_csv">
|
||||
1,2,3
|
||||
</template>
|
||||
|
||||
<template id="page_404">
|
||||
<t t-call="website.404">
|
||||
<div class="container">
|
||||
|
||||
@@ -85,7 +85,7 @@ class Event(models.Model):
|
||||
for sequence, (name, url, xml_id) in enumerate(self._get_menu_entries()):
|
||||
if name not in existing_page_names:
|
||||
if not url:
|
||||
newpath = self.env['website'].new_page(name + ' ' + self.name, template=xml_id, ispage=False)
|
||||
newpath = self.env['website'].new_page(name + ' ' + self.name, template=xml_id, ispage=False)['url']
|
||||
url = "/event/" + slug(self) + "/page/" + newpath[1:]
|
||||
self.env['website.menu'].create({
|
||||
'name': name,
|
||||
|
||||
Reference in New Issue
Block a user