# -*- coding: utf-8 -*-
import cStringIO
-import contextlib
-import hashlib
+import datetime
+from itertools import islice
import json
import logging
-import os
-import datetime
+import re
from sys import maxint
-import werkzeug
-import werkzeug.exceptions
import werkzeug.utils
import werkzeug.wrappers
from PIL import Image
import openerp
-from openerp.osv import fields
-from openerp.addons.website.models import website
from openerp.addons.web import http
from openerp.http import request, Response
# Completely arbitrary limits
MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
-
+LOC_PER_SITEMAP = 45000
+SITEMAP_CACHE_TIME = datetime.timedelta(hours=12)
class Website(openerp.addons.web.controllers.main.Home):
#------------------------------------------------------
# TODO: can't we just put auth=public, ... in web client ?
return super(Website, self).web_login(*args, **kw)
- @http.route('/page/<page:page>', type='http', auth="public", website=True, multilang=True)
+ @http.route('/page/<path:page>', type='http', auth="public", website=True, multilang=True)
def page(self, page, **opt):
values = {
'path': page,
return request.render(page, values)
- @http.route(['/robots.txt'], type='http', auth="public", website=True)
+ @http.route(['/robots.txt'], type='http', auth="public")
def robots(self):
return request.render('website.robots', {'url_root': request.httprequest.url_root}, mimetype='text/plain')
- @http.route('/sitemap', type='http', auth='public', website=True, multilang=True)
- def sitemap(self):
- return request.render('website.sitemap', {
- 'pages': request.website.enumerate_pages()
- })
-
@http.route('/sitemap.xml', type='http', auth="public", website=True)
- def sitemap_xml(self):
- values = {
- 'pages': request.website.enumerate_pages()
- }
- headers = {
- 'Content-Type': 'application/xml;charset=utf-8',
- }
- return request.render('website.sitemap_xml', values, headers=headers)
+ def sitemap_xml_index(self):
+ cr, uid, context = request.cr, openerp.SUPERUSER_ID, request.context
+ ira = request.registry['ir.attachment']
+ iuv = request.registry['ir.ui.view']
+ mimetype ='application/xml;charset=utf-8'
+ content = None
+
+ def create_sitemap(url, content):
+ ira.create(cr, uid, dict(
+ datas=content.encode('base64'),
+ mimetype=mimetype,
+ type='binary',
+ name=url,
+ url=url,
+ ), context=context)
+
+ sitemap = ira.search_read(cr, uid, [('url', '=' , '/sitemap.xml'), ('type', '=', 'binary')], ('datas', 'create_date'), context=context)
+ if sitemap:
+ # Check if stored version is still valid
+ server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
+ create_date = datetime.datetime.strptime(sitemap[0]['create_date'], server_format)
+ delta = datetime.datetime.now() - create_date
+ if delta < SITEMAP_CACHE_TIME:
+ content = sitemap[0]['datas'].decode('base64')
+
+ if not content:
+ # Remove all sitemaps in ir.attachments as we're going to regenerated them
+ sitemap_ids = ira.search(cr, uid, [('url', '=like' , '/sitemap%.xml'), ('type', '=', 'binary')], context=context)
+ if sitemap_ids:
+ ira.unlink(cr, uid, sitemap_ids, context=context)
+
+ pages = 0
+ first_page = None
+ locs = request.website.enumerate_pages()
+ while True:
+ start = pages * LOC_PER_SITEMAP
+ loc_slice = islice(locs, start, start + LOC_PER_SITEMAP)
+ urls = iuv.render(cr, uid, 'website.sitemap_locs', dict(locs=loc_slice), context=context)
+ if urls.strip():
+ page = iuv.render(cr, uid, 'website.sitemap_xml', dict(content=urls), context=context)
+ if not first_page:
+ first_page = page
+ pages += 1
+ create_sitemap('/sitemap-%d.xml' % pages, page)
+ else:
+ break
+ if not pages:
+ return request.not_found()
+ elif pages == 1:
+ content = first_page
+ else:
+ # Sitemaps must be split in several smaller files with a sitemap index
+ content = iuv.render(cr, uid, 'website.sitemap_index_xml', dict(
+ pages=range(1, pages + 1),
+ url_root=request.httprequest.url_root,
+ ), context=context)
+ create_sitemap('/sitemap.xml', content)
+
+ return request.make_response(content, [('Content-Type', mimetype)])
#------------------------------------------------------
# Edit
'url': "/page/" + xml_id,
'parent_id': id,
}, context=request.context)
- url = "/page/" + xml_id
+ # Reverse action in order to allow shortcut for /page/<website_xml_id>
+ url = "/page/" + re.sub(r"^website\.", '', xml_id)
+
if noredirect:
return werkzeug.wrappers.Response(url, mimetype='text/plain')
return werkzeug.utils.redirect(url)
values = {}
if 'website_published' in _object._all_columns:
values['website_published'] = not obj.website_published
- if 'website_published_datetime' in _object._all_columns and values.get('website_published'):
- values['website_published_datetime'] = fields.datetime.now()
_object.write(request.cr, request.uid, [_id],
values, context=request.context)
return request.website.kanban_col(**post)
def placeholder(self, response):
- # file_open may return a StringIO. StringIO can be closed but are
- # not context managers in Python 2 though that is fixed in 3
- with contextlib.closing(openerp.tools.misc.file_open(
- os.path.join('web', 'static', 'src', 'img', 'placeholder.png'),
- mode='rb')) as f:
- response.data = f.read()
- return response.make_conditional(request.httprequest)
+ return request.registry['website']._image_placeholder(response)
@http.route([
'/website/image',
The requested field is assumed to be base64-encoded image data in
all cases.
"""
- Model = request.registry[model]
-
response = werkzeug.wrappers.Response()
+ return request.registry['website']._image(
+ request.cr, request.uid, model, id, field, response, max_width, max_height)
- id = int(id)
-
- ids = Model.search(request.cr, request.uid,
- [('id', '=', id)], context=request.context) \
- or Model.search(request.cr, openerp.SUPERUSER_ID,
- [('id', '=', id), ('website_published', '=', True)], context=request.context)
-
- if not ids:
- return self.placeholder(response)
-
- presized = '%s_big' % field
- concurrency = '__last_update'
- [record] = Model.read(request.cr, openerp.SUPERUSER_ID, [id],
- [concurrency, field, presized],
- context=request.context)
-
- if concurrency in record:
- server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
- try:
- response.last_modified = datetime.datetime.strptime(
- record[concurrency], server_format + '.%f')
- except ValueError:
- # just in case we have a timestamp without microseconds
- response.last_modified = datetime.datetime.strptime(
- record[concurrency], server_format)
-
- # Field does not exist on model or field set to False
- if not record.get(field):
- # FIXME: maybe a field which does not exist should be a 404?
- return self.placeholder(response)
-
- response.set_etag(hashlib.sha1(record[field]).hexdigest())
- response.make_conditional(request.httprequest)
-
- # conditional request match
- if response.status_code == 304:
- return response
-
- data = (record.get(presized) or record[field]).decode('base64')
-
- image = Image.open(cStringIO.StringIO(data))
- response.mimetype = Image.MIME[image.format]
-
- # record provides a pre-resized version of the base field, use that
- # directly
- if record.get(presized):
- response.set_data(data)
- return response
-
- fit = int(max_width), int(max_height)
- w, h = image.size
- max_w, max_h = fit
-
- if w < max_w and h < max_h:
- response.set_data(data)
- else:
- image.thumbnail(fit, Image.ANTIALIAS)
- image.save(response.stream, image.format)
- # invalidate content-length computed by make_conditional as
- # writing to response.stream does not do it (as of werkzeug 0.9.3)
- del response.headers['Content-Length']
-
- return response
#------------------------------------------------------
# Server actions
action = ServerActions.browse(cr, uid, action_id, context=context)
if action.state == 'code' and action.website_published:
action_res = ServerActions.run(cr, uid, [action_id], context=context)
- if isinstance(action_res, Response):
+ if isinstance(action_res, werkzeug.wrappers.Response):
res = action_res
if res:
return res
return request.redirect('/')
-# vim:et: