~acsone-openerp/acsone-addons/7.0

« back to all changes in this revision

Viewing changes to html_widget_embedded_picture/controllers/main.py

  • Committer: Jonathan Nemry
  • Date: 2014-03-13 18:50:17 UTC
  • Revision ID: jonathan.nemry@acsone.eu-20140313185017-gnfglbgo4m429kaw
[CHG] Embedded Picture Fully Completed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
import cStringIO
 
3
import contextlib
 
4
import hashlib
 
5
import json
 
6
import logging
 
7
import os
 
8
import datetime
 
9
 
 
10
from sys import maxint
 
11
 
 
12
import psycopg2
 
13
import werkzeug
 
14
import werkzeug.exceptions
 
15
import werkzeug.utils
 
16
import werkzeug.wrappers
 
17
from PIL import Image
 
18
 
 
19
import openerp
 
20
from openerp.osv import fields
 
21
from openerp.addons.website.models import website
 
22
from openerp.addons.web import http
 
23
from openerp.addons.web.http import request, LazyResponse
 
24
 
 
25
logger = logging.getLogger(__name__)
 
26
 
 
27
# Completely arbitrary limits
 
28
MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT = IMAGE_LIMITS = (1024, 768)
 
29
 
 
30
 
 
31
class EmbeddedPicture(openerp.addons.web.controllers.main.Home):
 
32
 
 
33
    @http.route('/embedded/gen_img', type='http', auth='user', methods=['POST'])
 
34
    def gen_img(self, func, upload):
 
35
 
 
36
        img_tag = message = None
 
37
        try:
 
38
            image_data = upload.read()
 
39
            image = Image.open(cStringIO.StringIO(image_data))
 
40
            w, h = image.size
 
41
            if w*h > 42e6: # Nokia Lumia 1020 photo resolution
 
42
                raise ValueError(
 
43
                    u"Image size excessive, uploaded images must be smaller "
 
44
                    u"than 42 million pixel")
 
45
 
 
46
            data_uri = image_data.encode('base64').replace('\n', '')
 
47
            ext = upload.filename.split('.')[-1].lower()
 
48
            img_tag = '<img src="data:image/{0};base64,{1}">'.format(ext, data_uri)
 
49
        except Exception, e:
 
50
            logger.exception("Failed to upload image to attachment")
 
51
            message = unicode(e)
 
52
 
 
53
        return """<script type='text/javascript'>
 
54
            window.parent['%s'](%s, %s);
 
55
        </script>""" % (func, json.dumps(img_tag), json.dumps(message))
 
56
 
 
57
# vim:expandtab:tabstop=4:softtabstop=4:shiftwidth=4: