1 # -*- coding: utf-8 -*-
10 from openerp.addons.web import http
11 from openerp.addons.web.http import request
13 import werkzeug.exceptions
14 import werkzeug.wrappers
18 logger = logging.getLogger(__name__)
21 def auth_method_public():
22 registry = openerp.modules.registry.RegistryManager.get(request.db)
23 if not request.session.uid:
24 request.uid = registry['website'].get_public_user().id
26 request.uid = request.session.uid
27 http.auth_methods['public'] = auth_method_public
30 class Website(openerp.addons.web.controllers.main.Home):
31 @http.route('/', type='http', auth="admin")
32 def index(self, **kw):
33 return self.page("website.homepage")
35 @http.route('/admin', type='http', auth="none")
36 def admin(self, *args, **kw):
37 return super(Website, self).index(*args, **kw)
39 @http.route('/pagenew/<path:path>', type='http', auth="admin")
40 def pagenew(self, path):
41 imd = request.registry['ir.model.data']
42 view = request.registry['ir.ui.view']
43 view_model, view_id = imd.get_object_reference(request.cr, request.uid, 'website', 'default_page')
44 newview_id = view.copy(request.cr, request.uid, view_id)
45 newview = view.browse(request.cr, request.uid, newview_id, context={})
47 'arch': newview.arch.replace("website.default_page", path),
48 'name': "page/%s" % path
51 module, idname = path.split('.')
55 imd.create(request.cr, request.uid, {
58 'model': 'ir.ui.view',
62 return werkzeug.utils.redirect("/page/%s" % path)
64 @http.route('/page/<path:path>', type='http', auth="admin")
65 def page(self, path, **kwargs):
66 website = request.registry.get("website")
67 values = website.get_rendering_context({
71 html = website.render(path, values)
73 html = website.render('website.404', values)
76 @http.route('/website/customize_template_toggle', type='json', auth='admin') # FIXME: auth
77 def customize_template_set(self, view_id):
78 view_obj = request.registry.get("ir.ui.view")
79 view = view_obj.browse(request.cr, request.uid, int(view_id), context=request.context)
81 print '*', view.inherit_id
84 value = view.inherit_option_id and view.inherit_option_id.id or False
85 print '*', view.inherit_id, 'no', value, view
86 view_obj.write(request.cr, request.uid, [view_id], {
88 }, context=request.context)
89 print 'Wrote', value, 'on', view_id
92 @http.route('/website/customize_template_get', type='json', auth='admin') # FIXME: auth
93 def customize_template_get(self, xml_id):
94 view = request.registry.get("ir.ui.view")
95 views = view._views_get(request.cr, request.uid, xml_id, request.context)
99 if v.inherit_option_id:
100 if v.inherit_option_id.id not in done:
102 'name': v.inherit_option_id.name,
106 done[v.inherit_option_id.id] = True
111 'active': v.inherit_id.id == v.inherit_option_id.id
115 @http.route('/website/attach', type='http', auth='admin') # FIXME: auth
116 def attach(self, CKEditorFuncNum, CKEditor, langCode, upload):
117 req = request.httprequest
118 if req.method != 'POST':
119 return werkzeug.exceptions.MethodNotAllowed(valid_methods=['POST'])
123 attachment_id = request.registry['ir.attachment'].create(request.cr, request.uid, {
124 'name': upload.filename,
125 'datas': base64.encodestring(upload.read()),
126 'datas_fname': upload.filename,
127 'res_model': 'ir.ui.view',
129 # FIXME: auth=user... no good.
130 url = '/website/attachment/%d' % attachment_id
132 logger.exception("Failed to upload image to attachment")
135 return """<script type='text/javascript'>
136 window.parent.CKEDITOR.tools.callFunction(%d, %s, %s);
137 </script>""" % (int(CKEditorFuncNum), json.dumps(url), json.dumps(message))
139 @http.route('/website/attachment/<int:id>', type='http', auth="admin")
140 def attachment(self, id):
141 # FIXME: can't use Binary.image because auth=user and website attachments need to be public
142 attachment = request.registry['ir.attachment'].browse(
143 request.cr, request.uid, id, request.context)
145 buf = cStringIO.StringIO(base64.decodestring(attachment.datas))
147 image = Image.open(buf)
148 image.thumbnail((1024, 768), Image.ANTIALIAS)
150 response = werkzeug.wrappers.Response(status=200, mimetype={
152 'JPEG': 'image/jpeg',
155 image.save(response.stream, image.format)
159 @http.route('/website/image', type='http', auth="public")
160 def image(self, model, id, field, **kw):
161 last_update = '__last_update'
162 Model = request.registry[model]
163 headers = [('Content-Type', 'image/png')]
164 etag = request.httprequest.headers.get('If-None-Match')
165 hashed_session = hashlib.md5(request.session_id).hexdigest()
166 retag = hashed_session
169 date = Model.read(request.cr, request.uid, [id], [last_update], request.context)[0].get(last_update)
170 if hashlib.md5(date).hexdigest() == etag:
171 return werkzeug.wrappers.Response(status=304)
173 res = Model.read(request.cr, request.uid, [id], [last_update, field], request.context)[0]
174 retag = hashlib.md5(res.get(last_update)).hexdigest()
175 image_base64 = res.get(field)
178 resize = kw.get('resize').split(',')
179 if len(resize) == 2 and int(resize[0]) and int(resize[1]):
180 width = int(resize[0])
181 height = int(resize[1])
182 # resize maximum 500*500
183 if width > 500: width = 500
184 if height > 500: height = 500
185 image_base64 = openerp.tools.image_resize_image(base64_source=image_base64, size=(width, height), encoding='base64', filetype='PNG')
187 image_data = base64.b64decode(image_base64)
189 image_data = open(os.path.join(http.addons_manifest['web']['addons_path'], 'web', 'static', 'src', 'img', 'placeholder.png'), 'rb').read()
191 headers.append(('ETag', retag))
192 headers.append(('Content-Length', len(image_data)))
194 ncache = int(kw.get('cache'))
195 headers.append(('Cache-Control', 'no-cache' if ncache == 0 else 'max-age=%s' % (ncache)))
198 return request.make_response(image_data, headers)
200 @http.route(['/website/publish/'], type='http', auth="public")
201 def publish(self, **post):
202 _id = int(post['id'])
203 _object = request.registry[post['object']]
205 obj = _object.browse(request.cr, request.uid, _id)
206 _object.write(request.cr, request.uid, [_id], {'website_published': not obj.website_published})
207 obj = _object.browse(request.cr, request.uid, _id)
209 return obj.website_published and "1" or "0"
211 # vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: