Commit 94233608 authored by 's avatar

*** empty log message ***

parent 5b433e3c
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
############################################################################## ##############################################################################
"""Image object""" """Image object"""
__version__='$Revision: 1.49 $'[11:-2] __version__='$Revision: 1.50 $'[11:-2]
import Globals import Globals
from Globals import HTMLFile, MessageDialog from Globals import HTMLFile, MessageDialog
...@@ -112,7 +112,7 @@ from SimpleItem import Item_w__name__ ...@@ -112,7 +112,7 @@ from SimpleItem import Item_w__name__
from Globals import Persistent from Globals import Persistent
from Acquisition import Implicit from Acquisition import Implicit
from DateTime import DateTime from DateTime import DateTime
import string import string, struct
manage_addFileForm=HTMLFile('imageAdd', globals(),Kind='File',kind='file') manage_addFileForm=HTMLFile('imageAdd', globals(),Kind='File',kind='file')
def manage_addFile(self,id,file,title='',precondition='',REQUEST=None): def manage_addFile(self,id,file,title='',precondition='',REQUEST=None):
...@@ -162,38 +162,29 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -162,38 +162,29 @@ class File(Persistent,Implicit,PropertyManager,
{'id':'content_type', 'type':'string'}, {'id':'content_type', 'type':'string'},
) )
def __init__(self,id,title,file,content_type='application/octet-stream', def __init__(self, id, title, file, content_type='', precondition=''):
precondition=''):
try: headers=file.headers
except: headers=None
if headers is None:
if not content_type:
raise 'BadValue', 'No content type specified'
self.content_type=content_type
self.data=Pdata(file)
else:
if headers.has_key('content-type'):
self.content_type=headers['content-type']
else:
if not content_type:
raise 'BadValue', 'No content type specified'
self.content_type=content_type
self.data=Pdata(file.read())
self.__name__=id self.__name__=id
self.title=title self.title=title
if precondition: self.precondition=precondition self.precondition=precondition
self.size=len(self.data) headers=hasattr(file, 'headers') and file.headers or None
if (headers is None) and (not content_type):
def id(self): return self.__name__ raise 'BadValue', 'No content type specified.'
if headers.has_key('content-type'):
content_type=headers['content-type']
def index_html(self, REQUEST,RESPONSE): if not content_type:
raise 'BadValue', 'No content type specified.'
data=(headers is None) and file or file.read()
self._update_data(data, content_type)
def id(self):
return self.__name__
def index_html(self, REQUEST, RESPONSE):
""" """
The default view of the contents of the File or Image. The default view of the contents of a File or Image.
Returns the contents of the file or image. Also, sets the Returns the contents of the file or image. Also, sets the
'content-type' HTTP header to the objects content type. Content-Type HTTP header to the objects content type.
""" """
if self.precondition and hasattr(self,self.precondition): if self.precondition and hasattr(self,self.precondition):
...@@ -208,12 +199,18 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -208,12 +199,18 @@ class File(Persistent,Implicit,PropertyManager,
RESPONSE['content-type'] =self.content_type RESPONSE['content-type'] =self.content_type
return self.data return self.data
def view_image_or_file(self,URL1): def view_image_or_file(self, URL1):
""" """
The default view of the contents of the File or Image. The default view of the contents of the File or Image.
""" """
raise 'Redirect', URL1 raise 'Redirect', URL1
def _update_data(self, data, content_type=None):
if content_type is not None:
self.content_type=content_type
self.data=Pdata(data)
self.size=len(data)
def manage_edit(self,title,content_type,precondition='',REQUEST=None): def manage_edit(self,title,content_type,precondition='',REQUEST=None):
""" """
Changes the title and content type attributes of the File or Image. Changes the title and content type attributes of the File or Image.
...@@ -233,11 +230,10 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -233,11 +230,10 @@ class File(Persistent,Implicit,PropertyManager,
The file or images contents are replaced with the contents of 'file'. The file or images contents are replaced with the contents of 'file'.
""" """
try: self.content_type=file.headers['content-type'] if file.headers.has_key('content-type'):
except KeyError: pass content_type=file.headers['content-type']
data=file.read() else: content_type=None
self.data=Pdata(data) self._update_data(file.read(), content_type)
self.size=len(data)
if REQUEST: return MessageDialog( if REQUEST: return MessageDialog(
title ='Success!', title ='Success!',
message='Your changes have been saved', message='Your changes have been saved',
...@@ -247,17 +243,13 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -247,17 +243,13 @@ class File(Persistent,Implicit,PropertyManager,
def HEAD(self, REQUEST, RESPONSE): def HEAD(self, REQUEST, RESPONSE):
""" """ """ """
RESPONSE['content-type'] =self.content_type RESPONSE['content-type'] =self.content_type
RESPONSE['content-length']=self.getSize()
return '' return ''
def PUT(self, BODY, REQUEST): def PUT(self, BODY, REQUEST):
'handle PUT requests' """Handle HTTP PUT requests"""
self.data=Pdata(BODY) content_type=REQUEST.get('CONTENT_TYPE', None)
self.size=len(BODY) self._update_data(BODY, content_type)
try:
type=REQUEST['CONTENT_TYPE']
if type: self.content_type=type
except KeyError: pass
def getSize(self): def getSize(self):
"""Get the size of a file or image. """Get the size of a file or image.
...@@ -281,6 +273,8 @@ class File(Persistent,Implicit,PropertyManager, ...@@ -281,6 +273,8 @@ class File(Persistent,Implicit,PropertyManager,
"Get data for FTP download" "Get data for FTP download"
return self.data return self.data
manage_addImageForm=HTMLFile('imageAdd',globals(),Kind='Image',kind='image') manage_addImageForm=HTMLFile('imageAdd',globals(),Kind='Image',kind='image')
def manage_addImage(self,id,file,title='',REQUEST=None): def manage_addImage(self,id,file,title='',REQUEST=None):
""" """
...@@ -293,13 +287,15 @@ def manage_addImage(self,id,file,title='',REQUEST=None): ...@@ -293,13 +287,15 @@ def manage_addImage(self,id,file,title='',REQUEST=None):
if REQUEST is not None: return self.manage_main(self,REQUEST) if REQUEST is not None: return self.manage_main(self,REQUEST)
class Image(File): class Image(File):
"""Principia object for *Images*, can be GIF or JPEG. Has the same """Principia object for *Images*, can be GIF, PNG or JPEG. Has the
methods as File objects. Images also have a string representation same methods as File objects. Images also have a string representation
that renders an HTML 'IMG' tag. that renders an HTML 'IMG' tag.
""" """
meta_type='Image' meta_type='Image'
icon ='p_/image' icon='p_/image'
height=0
width=0
manage_options=({'label':'Edit', 'action':'manage_main'}, manage_options=({'label':'Edit', 'action':'manage_main'},
{'label':'Upload', 'action':'manage_uploadForm'}, {'label':'Upload', 'action':'manage_uploadForm'},
{'label':'Properties', 'action':'manage_propertiesForm'}, {'label':'Properties', 'action':'manage_propertiesForm'},
...@@ -313,8 +309,30 @@ class Image(File): ...@@ -313,8 +309,30 @@ class Image(File):
kind='image') kind='image')
manage=manage_main=manage_editForm manage=manage_main=manage_editForm
def _update_data(self, data, content_type=None):
if content_type is not None:
self.content_type=content_type
self.data=Pdata(data)
self.size=len(data)
# handle GIFs
if (self.size >= 10) and self.data[:6] in ('GIF87a', 'GIF89a'):
w, h = struct.unpack("<HH", self.data[6:10])
self.width=str(int(w))
self.height=str(int(h))
# handle PNGs
if (self.size >= 16) and (self.data[:8] == '\x89PNG\r\n\x1a\n'):
w, h = struct.unpack(">LL", self.data[8:16])
self.width=str(int(w))
self.height=str(int(h))
def __str__(self): def __str__(self):
return '<IMG SRC="%s" ALT="%s">' % (self.__name__, self.title_or_id()) width=self.width and ('width="%s" ' % self.width) or ''
height=self.height and ('height="%s" ' % self.height) or ''
return '<img src="%s" %s%salt="%s">' % (
self.absolute_url(), width, height, self.title_or_id()
)
def cookId(id, title, file): def cookId(id, title, file):
if not id and hasattr(file,'filename'): if not id and hasattr(file,'filename'):
...@@ -331,6 +349,9 @@ class Pdata(Persistent, Implicit): ...@@ -331,6 +349,9 @@ class Pdata(Persistent, Implicit):
def __init__(self, data): def __init__(self, data):
self.data=data self.data=data
def __getslice__(self, i, j):
return self.data[i:j]
def __str__(self): def __str__(self):
return self.data return self.data
......
...@@ -107,8 +107,8 @@ Aqueduct database adapters, etc. ...@@ -107,8 +107,8 @@ Aqueduct database adapters, etc.
This module can also be used as a simple template for implementing new This module can also be used as a simple template for implementing new
item types. item types.
$Id: SimpleItem.py,v 1.30 1999/01/22 23:12:31 amos Exp $''' $Id: SimpleItem.py,v 1.31 1999/01/27 19:11:33 brian Exp $'''
__version__='$Revision: 1.30 $'[11:-2] __version__='$Revision: 1.31 $'[11:-2]
import regex, sys, Globals, App.Management import regex, sys, Globals, App.Management
from DateTime import DateTime from DateTime import DateTime
...@@ -274,6 +274,23 @@ class Item(CopySource, App.Management.Tabs): ...@@ -274,6 +274,23 @@ class Item(CopySource, App.Management.Tabs):
stat=marshal.loads(self.manage_FTPstat(REQUEST)) stat=marshal.loads(self.manage_FTPstat(REQUEST))
return marshal.dumps((self.id,stat)) return marshal.dumps((self.id,stat))
def absolute_url(self):
"""Return an absolute url to the object. Note that the url
will reflect the acquisition path of the object if the object
has been acquired."""
obj=self
url=[]
while hasattr(obj, 'aq_parent') and hasattr(obj.aq_parent, 'id'):
id=callable(obj.id) and obj.id() or obj.id
url.append(id)
obj=obj.aq_parent
url.append(self.REQUEST.script)
url.reverse()
return join(url, '/')
class Item_w__name__(Item): class Item_w__name__(Item):
"""Mixin class to support common name/id functions""" """Mixin class to support common name/id functions"""
......
<HTML> <html>
<HEAD> <head>
<TITLE>View</TITLE> <title>View</title>
</HEAD> </head>
<BODY BGCOLOR="#FFFFFF" LINK="#000099" VLINK="#555555"> <body bgcolor="#FFFFFF" link="#000099" vlink="#555555">
<!--#var manage_tabs--> <!--#var manage_tabs-->
<IMG src="<!--#var id-->" ALT="<!--#var title_or_id-->"> <p>
<img src="<!--#var id-->" <!--#if width-->width="<!--#var width-->" <!--#/if
--><!--#if height-->height="<!--#var height-->" <!--#/if-->ALT="<!--#var
title_or_id-->">
</p>
</BODY> </body>
</HTML> </html>
...@@ -82,7 +82,7 @@ ...@@ -82,7 +82,7 @@
# file. # file.
# #
############################################################################## ##############################################################################
__version__='$Revision: 1.5 $'[11:-2] __version__='$Revision: 1.6 $'[11:-2]
import regex import regex
from string import atoi, atol, join, upper, split, strip, rfind from string import atoi, atol, join, upper, split, strip, rfind
...@@ -107,6 +107,10 @@ isCGI_NAME = { ...@@ -107,6 +107,10 @@ isCGI_NAME = {
'CONTENT_LENGTH' : 1, 'CONTENT_LENGTH' : 1,
}.has_key }.has_key
hide_key={'HTTP_AUTHORIZATION':1,
'HTTP_CGI_AUTHORIZATION': 1,
}.has_key
class Request: class Request:
"""\ """\
Model HTTP request data. Model HTTP request data.
...@@ -167,7 +171,6 @@ class Request: ...@@ -167,7 +171,6 @@ class Request:
else: b='' else: b=''
while b and b[0]=='/': b=b[1:] while b and b[0]=='/': b=b[1:]
if have_env('SERVER_URL'): if have_env('SERVER_URL'):
server_url=strip(environ['SERVER_URL']) server_url=strip(environ['SERVER_URL'])
else: else:
...@@ -193,34 +196,32 @@ class Request: ...@@ -193,34 +196,32 @@ class Request:
if script: self.script="%s/%s" % (server_url,script) if script: self.script="%s/%s" % (server_url,script)
else: self.script=server_url else: self.script=server_url
def get_header(self, name, default=None):
"""Return the named HTTP header, or an optional default
argument or None if the header is not found. Note that
both original and CGI-ified header names are recognized,
e.g. 'Content-Type', 'CONTENT_TYPE' and 'HTTP_CONTENT_TYPE'
should all return the Content-Type header, if available.
"""
environ=self.environ
name=upper(join(split(name,"-"),"_"))
val=environ.get(name, None)
if val is not None:
return val
if name[:5] != 'HTTP_':
name='HTTP_%s' % name
return environ.get(name, default)
def __setitem__(self,key,value): def __setitem__(self,key,value):
"""Set application variables """Set application variables
This method is used to set a variable in the requests "other" This method is used to set a variable in the requests "other"
category. category.
""" """
self.other[key]=value self.other[key]=value
set=__setitem__ set=__setitem__
def __str__(self):
def str(self,name):
dict=getattr(self,name)
return "%s:\n\t%s\n\n" % (
name,
join(
map(lambda k, d=dict: "%s: %s" % (k, `d[k]`), dict.keys()),
"\n\t"
)
)
return "%s\n%s\n" % (
str(self,'form'),str(self,'environ'))
__repr__=__str__
def __getitem__(self,key, def __getitem__(self,key,
default=isCGI_NAME, # Any special internal marker will do default=isCGI_NAME, # Any special internal marker will do
URLmatch=regex.compile('URL[0-9]$').match, URLmatch=regex.compile('URL[0-9]$').match,
...@@ -234,7 +235,6 @@ class Request: ...@@ -234,7 +235,6 @@ class Request:
other variables, form data, and then cookies. other variables, form data, and then cookies.
""" #" """ #"
other=self.other other=self.other
if other.has_key(key): if other.has_key(key):
if key=='REQUEST': return self if key=='REQUEST': return self
...@@ -252,7 +252,8 @@ class Request: ...@@ -252,7 +252,8 @@ class Request:
if isCGI_NAME(key) or key[:5] == 'HTTP_': if isCGI_NAME(key) or key[:5] == 'HTTP_':
environ=self.environ environ=self.environ
if environ.has_key(key): return environ[key] if environ.has_key(key) and (not hide_key(key)):
return environ[key]
return '' return ''
if key=='REQUEST': return self if key=='REQUEST': return self
...@@ -279,19 +280,55 @@ class Request: ...@@ -279,19 +280,55 @@ class Request:
def has_key(self,key): def has_key(self,key):
return self.get(key, Request) is not Request return self.get(key, Request) is not Request
def getHeader(self, name, default=None): def keys(self):
"""Return the named HTTP header, or an optional default keys = {}
argument or None if the header is not found. Note that for key in self.environ.keys():
both original and CGI-ified header names are recognized, if (isCGI_NAME(key) or key[:5] == 'HTTP_') and \
e.g. 'Content-Type', 'CONTENT_TYPE' and 'HTTP_CONTENT_TYPE' (not hide_key(key)):
should all return the Content-Type header, if available. keys[key] = 1
""" keys.update(self.other)
environ=self.environ lasturl = ""
name=upper(join(split(name,"-"),"_")) for n in "0123456789":
val=environ.get(name, None) key = "URL%s"%n
if val is not None: try:
return val if lasturl != self[key]:
if name[:5] != 'HTTP_': keys[key] = 1
name='HTTP_%s' % name lasturl = self[key]
return environ.get(name, default) else:
break
except KeyError:
pass
for n in "0123456789":
key = "BASE%s"%n
try:
if lasturl != self[key]:
keys[key] = 1
lasturl = self[key]
else:
break
except KeyError:
pass
return keys.keys()
def items(self):
result = []
for k in self.keys():
result.append((k, self.get(k)))
return result
def __str__(self):
def str(self,name):
dict=getattr(self,name)
data=[]
for key, val in dict.items():
if not hide_key(key):
data.append('%s: %s' % (key, `val`))
return "%s:\n\t%s\n\n" % (name, join(data, '\n\t'))
return "%s\n%s\n" % (str(self,'form'), str(self,'environ'))
__repr__=__str__
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment