Skip to content

Commit cf7da98

Browse files
committed
[FIX] website_multi: create sitemap by website
Before this commit, the first website which ask to generate the sitemap (with 12h cache) will force his own domain, and page was not filtered. Now, genereate 1 sitemap by website, and the enumeration of page take the website_id in consideration and use key instead of xml_id.
1 parent 944dd79 commit cf7da98

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

website_multi/controllers/main.py

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import re
22

33
import werkzeug
4-
4+
import openerp
5+
import datetime
6+
from itertools import islice
57
from openerp.addons.web import http
68
from openerp.http import request
7-
from openerp.addons.website.controllers.main import Website
9+
from openerp.addons.website.controllers.main import Website, SITEMAP_CACHE_TIME, LOC_PER_SITEMAP
810

911

1012
class website_multi(Website):
@@ -42,3 +44,71 @@ def pagenew(self, path, noredirect=False, add_menu=None):
4244
return werkzeug.wrappers.Response(url, mimetype='text/plain')
4345

4446
return werkzeug.utils.redirect(url)
47+
48+
@http.route()
49+
def sitemap_xml_index(self):
50+
cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
51+
current_website = request.website
52+
ira = request.registry['ir.attachment']
53+
iuv = request.registry['ir.ui.view']
54+
mimetype ='application/xml;charset=utf-8'
55+
content = None
56+
57+
def create_sitemap(url, content):
58+
ira.create(cr, uid, dict(
59+
datas=content.encode('base64'),
60+
mimetype=mimetype,
61+
type='binary',
62+
name=url,
63+
url=url,
64+
), context=context)
65+
66+
dom = [('url', '=' , '/sitemap-%d.xml' % current_website.id), ('type', '=', 'binary')]
67+
sitemap = ira.search_read(cr, uid, dom, ('datas', 'create_date'), limit=1, context=context)
68+
69+
if sitemap:
70+
# Check if stored version is still valid
71+
server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
72+
create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
73+
delta = datetime.datetime.now() - create_date
74+
if delta < SITEMAP_CACHE_TIME:
75+
content = sitemap[0]['datas'].decode('base64')
76+
77+
if not content:
78+
# Remove all sitemaps in ir.attachments as we're going to regenerated them
79+
dom = [('type', '=', 'binary'), '|', ('url', '=like' , '/sitemap-%d-%%.xml' % current_website.id),
80+
('url', '=' , '/sitemap-%d.xml' % current_website.id)]
81+
sitemap_ids = ira.search(cr, uid, dom, context=context)
82+
if sitemap_ids:
83+
ira.unlink(cr, uid, sitemap_ids, context=context)
84+
85+
pages = 0
86+
first_page = None
87+
locs = current_website.sudo(user=current_website.user_id.id).enumerate_pages()
88+
while True:
89+
values = {
90+
'locs': islice(locs, 0, LOC_PER_SITEMAP),
91+
'url_root': request.httprequest.url_root[:-1],
92+
}
93+
urls = iuv.render(cr, uid, 'website.sitemap_locs', values, context=context)
94+
if urls.strip():
95+
page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
96+
if not first_page:
97+
first_page = page
98+
pages += 1
99+
create_sitemap('/sitemap-%d-%d.xml' % (current_website.id, pages), page)
100+
else:
101+
break
102+
if not pages:
103+
return request.not_found()
104+
elif pages == 1:
105+
content = first_page
106+
else:
107+
# Sitemaps must be split in several smaller files with a sitemap index
108+
content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
109+
pages=range(1, pages + 1),
110+
url_root=request.httprequest.url_root,
111+
), context=context)
112+
create_sitemap('/sitemap-%d.xml' % current_website.id, content)
113+
114+
return request.make_response(content, [('Content-Type', mimetype)])

website_multi/models/website.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from openerp.addons.website.models.website import slugify
55
from openerp.addons.web.http import request
66
from werkzeug.exceptions import NotFound
7-
7+
import werkzeug
88

99
class website(orm.Model):
1010

@@ -114,3 +114,29 @@ def _auth_method_public(self):
114114
dummy, request.uid = self.pool['ir.model.data'].get_object_reference(request.cr, openerp.SUPERUSER_ID, 'base', 'public_user')
115115
else:
116116
request.uid = request.session.uid
117+
118+
def _get_converters(self):
119+
converters = super(ir_http, self)._get_converters()
120+
converters['page'] = PageMultiWebsiteConverter
121+
return converters
122+
123+
124+
class PageMultiWebsiteConverter(werkzeug.routing.PathConverter):
125+
def generate(self, cr, uid, query=None, args={}, context=None):
126+
View = request.registry['ir.ui.view']
127+
dom = [('page', '=', True), '|', ('website_id', '=', request.website.id), ('website_id', '=', False)]
128+
views = View.search_read(cr, uid, dom, fields=['key', 'xml_id', 'priority','write_date'], order='name', context=context)
129+
130+
for view in views:
131+
key = view['key'] or view['xml_id'] or ''
132+
xid = key.startswith('website.') and key[8:] or key
133+
134+
if xid=='homepage': continue
135+
if query and query.lower() not in xid.lower(): continue
136+
record = {'loc': xid}
137+
if view['priority'] != 16:
138+
record['__priority'] = min(round(view['priority'] / 32.0, 1), 1)
139+
if view['write_date']:
140+
record['__lastmod'] = view['write_date'][:10]
141+
yield record
142+

0 commit comments

Comments
 (0)