Commit 94233608 authored by 's avatar

*** empty log message ***

parent 5b433e3c
......@@ -102,7 +102,7 @@
##############################################################################
"""Image object"""
__version__='$Revision: 1.49 $'[11:-2]
__version__='$Revision: 1.50 $'[11:-2]
import Globals
from Globals import HTMLFile, MessageDialog
......@@ -112,7 +112,7 @@ from SimpleItem import Item_w__name__
from Globals import Persistent
from Acquisition import Implicit
from DateTime import DateTime
import string
import string, struct
manage_addFileForm=HTMLFile('imageAdd', globals(),Kind='File',kind='file')
def manage_addFile(self,id,file,title='',precondition='',REQUEST=None):
......@@ -162,38 +162,29 @@ class File(Persistent,Implicit,PropertyManager,
{'id':'content_type', 'type':'string'},
)
def __init__(self,id,title,file,content_type='application/octet-stream',
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())
def __init__(self, id, title, file, content_type='', precondition=''):
self.__name__=id
self.title=title
if precondition: self.precondition=precondition
self.size=len(self.data)
def id(self): return self.__name__
def index_html(self, REQUEST,RESPONSE):
self.precondition=precondition
headers=hasattr(file, 'headers') and file.headers or None
if (headers is None) and (not content_type):
raise 'BadValue', 'No content type specified.'
if headers.has_key('content-type'):
content_type=headers['content-type']
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
'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):
......@@ -208,12 +199,18 @@ class File(Persistent,Implicit,PropertyManager,
RESPONSE['content-type'] =self.content_type
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.
"""
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):
"""
Changes the title and content type attributes of the File or Image.
......@@ -233,11 +230,10 @@ class File(Persistent,Implicit,PropertyManager,
The file or images contents are replaced with the contents of 'file'.
"""
try: self.content_type=file.headers['content-type']
except KeyError: pass
data=file.read()
self.data=Pdata(data)
self.size=len(data)
if file.headers.has_key('content-type'):
content_type=file.headers['content-type']
else: content_type=None
self._update_data(file.read(), content_type)
if REQUEST: return MessageDialog(
title ='Success!',
message='Your changes have been saved',
......@@ -247,17 +243,13 @@ class File(Persistent,Implicit,PropertyManager,
def HEAD(self, REQUEST, RESPONSE):
""" """
RESPONSE['content-type'] =self.content_type
RESPONSE['content-length']=self.getSize()
return ''
def PUT(self, BODY, REQUEST):
'handle PUT requests'
self.data=Pdata(BODY)
self.size=len(BODY)
try:
type=REQUEST['CONTENT_TYPE']
if type: self.content_type=type
except KeyError: pass
"""Handle HTTP PUT requests"""
content_type=REQUEST.get('CONTENT_TYPE', None)
self._update_data(BODY, content_type)
def getSize(self):
"""Get the size of a file or image.
......@@ -281,6 +273,8 @@ class File(Persistent,Implicit,PropertyManager,
"Get data for FTP download"
return self.data
manage_addImageForm=HTMLFile('imageAdd',globals(),Kind='Image',kind='image')
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)
class Image(File):
"""Principia object for *Images*, can be GIF or JPEG. Has the same
methods as File objects. Images also have a string representation
"""Principia object for *Images*, can be GIF, PNG or JPEG. Has the
same methods as File objects. Images also have a string representation
that renders an HTML 'IMG' tag.
"""
meta_type='Image'
icon ='p_/image'
icon='p_/image'
height=0
width=0
manage_options=({'label':'Edit', 'action':'manage_main'},
{'label':'Upload', 'action':'manage_uploadForm'},
{'label':'Properties', 'action':'manage_propertiesForm'},
......@@ -313,8 +309,30 @@ class Image(File):
kind='image')
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):
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):
if not id and hasattr(file,'filename'):
......@@ -331,6 +349,9 @@ class Pdata(Persistent, Implicit):
def __init__(self, data):
self.data=data
def __getslice__(self, i, j):
return self.data[i:j]
def __str__(self):
return self.data
......
......@@ -107,8 +107,8 @@ Aqueduct database adapters, etc.
This module can also be used as a simple template for implementing new
item types.
$Id: SimpleItem.py,v 1.30 1999/01/22 23:12:31 amos Exp $'''
__version__='$Revision: 1.30 $'[11:-2]
$Id: SimpleItem.py,v 1.31 1999/01/27 19:11:33 brian Exp $'''
__version__='$Revision: 1.31 $'[11:-2]
import regex, sys, Globals, App.Management
from DateTime import DateTime
......@@ -274,6 +274,23 @@ class Item(CopySource, App.Management.Tabs):
stat=marshal.loads(self.manage_FTPstat(REQUEST))
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):
"""Mixin class to support common name/id functions"""
......
<HTML>
<HEAD>
<TITLE>View</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" LINK="#000099" VLINK="#555555">
<html>
<head>
<title>View</title>
</head>
<body bgcolor="#FFFFFF" link="#000099" vlink="#555555">
<!--#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>
</HTML>
</body>
</html>
......@@ -82,7 +82,7 @@
# file.
#
##############################################################################
__version__='$Revision: 1.5 $'[11:-2]
__version__='$Revision: 1.6 $'[11:-2]
import regex
from string import atoi, atol, join, upper, split, strip, rfind
......@@ -107,6 +107,10 @@ isCGI_NAME = {
'CONTENT_LENGTH' : 1,
}.has_key
hide_key={'HTTP_AUTHORIZATION':1,
'HTTP_CGI_AUTHORIZATION': 1,
}.has_key
class Request:
"""\
Model HTTP request data.
......@@ -167,7 +171,6 @@ class Request:
else: b=''
while b and b[0]=='/': b=b[1:]
if have_env('SERVER_URL'):
server_url=strip(environ['SERVER_URL'])
else:
......@@ -193,34 +196,32 @@ class Request:
if script: self.script="%s/%s" % (server_url,script)
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):
"""Set application variables
This method is used to set a variable in the requests "other"
category.
"""
self.other[key]=value
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,
default=isCGI_NAME, # Any special internal marker will do
URLmatch=regex.compile('URL[0-9]$').match,
......@@ -234,7 +235,6 @@ class Request:
other variables, form data, and then cookies.
""" #"
other=self.other
if other.has_key(key):
if key=='REQUEST': return self
......@@ -252,7 +252,8 @@ class Request:
if isCGI_NAME(key) or key[:5] == 'HTTP_':
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 ''
if key=='REQUEST': return self
......@@ -279,19 +280,55 @@ class Request:
def has_key(self,key):
return self.get(key, Request) is not Request
def getHeader(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 keys(self):
keys = {}
for key in self.environ.keys():
if (isCGI_NAME(key) or key[:5] == 'HTTP_') and \
(not hide_key(key)):
keys[key] = 1
keys.update(self.other)
lasturl = ""
for n in "0123456789":
key = "URL%s"%n
try:
if lasturl != self[key]:
keys[key] = 1
lasturl = self[key]
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