source: main/waeup.sirp/branches/ulif-images/src/waeup/sirp/widgets/passportwidget.py @ 10009

Last change on this file since 10009 was 5529, checked in by uli, 14 years ago

Add passportwidget, which sould be removed soon.

File size: 5.9 KB
Line 
1##
2## resultswidget.py
3## Login : <uli@pu.smp.net>
4## Started on  Tue Jul 20 02:04:30 2010 Uli Fouquet
5## $Id$
6##
7## Copyright (C) 2010 Uli Fouquet
8## This program is free software; you can redistribute it and/or modify
9## it under the terms of the GNU General Public License as published by
10## the Free Software Foundation; either version 2 of the License, or
11## (at your option) any later version.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18## You should have received a copy of the GNU General Public License
19## along with this program; if not, write to the Free Software
20## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21##
22"""A widget to display IObject instances in forms.
23"""
24import grok
25import os
26import zope.file.file
27import zope.file.upload
28from zope.app.file.interfaces import IImage
29from zope.browserpage import ViewPageTemplateFile
30from zope.formlib.interfaces import IDisplayWidget
31from zope.formlib.objectwidget import ObjectWidget, ObjectWidgetView
32from zope.formlib.widget import renderElement, DisplayWidget
33from zope.formlib.widgets import FileWidget
34from zope.formlib.utility import setUpWidgets
35from zope.interface import implements, implementsOnly
36from zope.schema import getFieldNamesInOrder
37from zope.publisher.interfaces.browser import IBrowserRequest
38from zope.traversing.browser import absoluteURL
39
40
41class WAeUPPassportWidgetView(ObjectWidgetView):
42    template = ViewPageTemplateFile('objectwidget.pt')
43
44class PassportImage(object):
45    grok.implements(IImage)
46
47    _data = None
48    _file = None
49    contenttype = 'image/jpeg'
50    path = None
51   
52    def __init__(self, data=None, fileobj=None, contenttype='image/jpeg'):
53        print "INIT"
54        self.contenttype = contenttype
55        self._data = data
56        if data is not None:
57            self._data = None
58            return
59        if fileobj is not None:
60            self._file = fileobj
61            return
62        self.path = os.path.join(
63            os.path.dirname(os.path.dirname(__file__)),
64            'browser', 'static', 'placeholder_m.jpg')
65
66    @property
67    def contentType(self):
68        return self.contenttype
69
70    @property
71    def data(self):
72        print "DATA"
73        if self._file is not None:
74            print "FILE"
75            return self._file.open().read()
76        if self.path:
77            print "PATH"
78            return open(self.path, 'rb').read()
79        print "NONE"
80        return self._data
81
82    def getSize(self):
83        if self._file is not None:
84            return self._file.size
85        if self.path is not None:
86            info = os.stat(self.path)
87            return info.st_size
88        return len(self._data)
89
90class PassportWidget(FileWidget):
91
92    def __call__(self):
93        result = super(PassportWidget, self).__call__()
94        url = absoluteURL(self.context.context, self.request) + '/passport.jpg'
95        image = renderElement(
96            'img',
97            src=url,
98            width='100px',
99            )
100        #import pdb; pdb.set_trace()
101        return '%s <br />%s' % (image, result)
102
103    def _toFieldValue(self, input):
104        if input is None or input == '':
105            return self.context.missing_value
106        if getattr(input, 'filename', False):
107            contenttype = input.headers.get('Content-Type')
108            file_ = zope.file.file.File()
109            zope.file.upload.updateBlob(file_, input)
110            return PassportImage(fileobj=file_, contenttype=contenttype)
111            return file_
112        return self.context.missing_value
113        try:
114            seek = input.seek
115            read = input.read
116        except AttributeError, e:
117            raise ConversionError(_('Form input is not a file object'), e)
118        else:
119            seek(0)
120            data = read()
121            if data or getattr(input, 'filename', ''):
122                return PassportImage(data)
123                return data
124            else:
125                return self.context.missing_value
126
127    def no__init__(self, context, request, factory, **kw):
128        super(PassportWidget, self).__init__(context, request)
129
130        # define view that renders the widget
131        self.view = self._getView(request)
132
133        # factory used to create content that this widget (field)
134        # represents
135        self.factory = factory
136
137        # handle foo_widget specs being passed in
138        self.names = getFieldNamesInOrder(self.context.schema)
139        for k, v in kw.items():
140            if k.endswith('_widget'):
141                setattr(self, k, v)
142
143        # set up my subwidgets
144        self._setUpWidgets()
145
146    def nosubwidgets(self):
147        result = [self.getSubWidget(name) for name in self.names]
148        return result
149
150    def no_setUpWidgets(self):
151        return self._setUpEditWidgets()
152
153    def no_getView(self, request):
154        return WAeUPObjectWidgetView(self, request)
155
156#class WAeUPObjectDisplayWidget(WAeUPObjectWidget):
157#
158#    implementsOnly(IDisplayWidget)
159#
160#    def _setUpDisplayWidgets(self):
161#        # subwidgets need a new name
162#        setUpWidgets(self, self.context.schema, IDisplayWidget,
163#                         prefix=self.name, names=self.names,
164#                         context=self.context)
165#
166#    def _setUpWidgets(self):
167#        return self._setUpDisplayWidgets()
168
169class PassportDisplayWidget(DisplayWidget):
170
171    grok.implements(IDisplayWidget)
172   
173    def __call__(self):
174        #result = super(PassportDisplayWidget, self).__call__()
175        url = absoluteURL(self.context, self.request) + '/passport.jpg'
176        #url = absoluteURL(self.context.context, self.request) + '/passport.jpg'
177        image = renderElement(
178            'img',
179            src=url,
180            width='100px',
181            )
182        #import pdb; pdb.set_trace()
183        #print "IMAGE: ", image
184        return '<br />%s' % (image,)# result)
185        return '%s <br />%s' % (image, result)
Note: See TracBrowser for help on using the repository browser.