File: ttwobject.py

package info (click to toggle)
zope-ttwtype 0.9.1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 108 kB
  • ctags: 58
  • sloc: python: 272; makefile: 47
file content (122 lines) | stat: -rw-r--r-- 3,066 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
""" TTW Object

a generic object for holding generic data :-)

(C) 2003 by Christian Scholz/MrTopf of COM.lounge

"""


from AccessControl import ClassSecurityInfo, getSecurityManager
from Products.CMFCore.PortalContent import PortalContent
from Products.CMFCore import CMFCorePermissions
from Products.CMFCore.utils import getToolByName
from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
from Products.CMFDefault.SkinnedFolder  import SkinnedFolder
from Globals import PersistentMapping, InitializeClass

# list of field types to be used for a fulltext index

SEARCHABLE_FIELD_TYPES=[
    'StringField',
    'TextAreaField',
    'EmailField',
    'RawTextAreaField',
    'LinesField',
    'LinkField'
]


class TTWObject(PortalContent, DefaultDublinCoreImpl):
    """ a generic object for ttw creation """

    __implements__ = ( PortalContent.__implements__
		     , DefaultDublinCoreImpl.__implements__
		     )
    
    security=ClassSecurityInfo()

    meta_type="TTWObject"

    def __init__(self,id,portal_type,title=''):
	""" create a new type """
	DefaultDublinCoreImpl.__init__(self,title=title)
	
	self.portal_type=portal_type.id
	self.id=id
	self.title=title

	# get the field definition
	form=portal_type.form

	self._fieldnames=[]
	for field in form.get_fields():
	    self._fieldnames.append(field.id)


    def set(self, field, value):
	""" set a value """
	setattr(self,field,value)

    def get(self,field,default=None):
	""" get a field value """
	return getattr(self,field,default)

    def getFields(self):
	""" return the list formulator field objects """
	return self.getTypeInfo().getForm().get_fields()

    def fieldNames(self):
	""" return list of field names """
	fields=self.getFields()
	return map(lambda x: x.id, fields)

    def store(self,field,value):
	""" a frontend to set in order to catch FileUploads """
	from ZPublisher.HTTPRequest import FileUpload
	from OFS.Image import File, Image
	from imghdr import what

	# this should be configurable at some point
	# to choose which object to create
	if isinstance(value,FileUpload):
	    # test for image type
	    if what(value) in ('gif','jpeg','png'):
		# create an Image instance
		value=Image(field,field,value)
	    else:
		# create a File instance
		value=File(field,field,value)

	# now really store it
	self.set(field,value)

    def edit(self, **kwargs):
	""" update object data """
	keys=kwargs.keys()
	for field in self.fieldNames():
	    if field in keys:
		self.store(field,kwargs[field])
	
	# set Title
	if 'title' in keys:
	    self.title=kwargs['title']
	self.reindexObject()

    # make everything searchable
    def SearchableText(self):
	""" return an indexable text """
	index=[self.title_or_id()]
	for f in self.getFields():
	    if f.meta_type in SEARCHABLE_FIELD_TYPES:
		index.append(self.get(f.id))
	index=map(str, index)
	index=' '.join(index)
	return index

InitializeClass(TTWObject)

def manage_addTTWObject(self, id, portal_type, title=''):
    """ add a new TTW object """
    o=TTWObject(id,portal_type,title)
    self._setObject(id,o)