1: from OFS.Folder import Folder
2: from OFS.Image import File
3: from OFS.Image import cookId
4: from Globals import DTMLFile, InitializeClass,package_home
5: from Products.PageTemplates.PageTemplateFile import PageTemplateFile
6: from AccessControl import getSecurityManager
7: from Products.PageTemplates.PageTemplate import PageTemplate
8: from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
9: from AccessControl import ClassSecurityInfo
10: import os.path
11:
12:
13: def sortv(x,y):
14: return cmp(x[0],y[0])
15:
16: class versionedFileFolder(Folder):
17: """Folder with versioned files"""
18:
19:
20: meta_type = "versionedFileFolder"
21:
22: security= ClassSecurityInfo()
23: security.declareProtected('AUTHENTICATED_USER','addFileForm')
24:
25: manage_options = Folder.manage_options+(
26: {'label':'Generate Index.html','action':'generateIndexHTML'},
27: {'label':'Generate history_template.html','action':'generateHistoryHTML'},
28: )
29:
30: def helpDownload(self):
31: """download help"""
32:
33: pt=PageTemplateFile('Products/versionedFile/zpt/helpDownload').__of__(self)
34: return pt()
35:
36: def generateIndexHTML(self,RESPONSE=None):
37: """lege standard index.html an"""
38:
39:
40:
41:
42: if not hasattr(self,'index.html'):
43: zt=ZopePageTemplate('index.html')
44: self._setObject('index.html',zt)
45: default_content_fn = os.path.join(package_home(globals()),
46: 'zpt/versionFileFolderMain.zpt')
47: text = open(default_content_fn).read()
48: zt.pt_edit(text, 'text/html')
49:
50: else:
51: return "already exists!"
52:
53: if RESPONSE is not None:
54: RESPONSE.redirect('manage_main')
55:
56:
57: def generateHistoryHTML(self,RESPONSE=None):
58: """lege standard index.html an"""
59:
60:
61:
62:
63: if not hasattr(self,'history_template.html'):
64: zt=ZopePageTemplate('history_template.html')
65: self._setObject('history_template.html',zt)
66: default_content_fn = os.path.join(package_home(globals()),
67: 'zpt/versionHistory.zpt')
68: text = open(default_content_fn).read()
69: zt.pt_edit(text, 'text/html')
70:
71: else:
72: return "already exists!"
73:
74: if RESPONSE is not None:
75: RESPONSE.redirect('manage_main')
76:
77: def getVersionedFiles(self,sortField='title'):
78: """get all versioned files"""
79:
80: def sortName(x,y):
81: return cmp(x[1].title,y[1].title)
82:
83: def sortDate(x,y):
84: return cmp(x[1].getLastVersion().bobobase_modification_time,y[1].getLastVersion().bobobase_modification_time)
85:
86: def sortAuthor(x,y):
87:
88: return cmp(x[1].getLastVersion().lastEditor(),y[1].getLastVersion().lastEditor())
89:
90: versionedFiles=self.ZopeFind(self,obj_metatypes=['versionedFile'])
91:
92: if sortField=='title':
93: versionedFiles.sort(sortName)
94: elif sortField=='date':
95: versionedFiles.sort(sortDate)
96: elif sortField=='author':
97: versionedFiles.sort(sortAuthor)
98:
99: return versionedFiles
100:
101:
102: def header_html(self):
103: """zusätzlicher header"""
104: ext=self.ZopeFind(self,obj_ids=["header.html"])
105: if ext:
106: return ext[0][1]()
107: else:
108: return ""
109:
110: def index_html(self):
111: """main"""
112: ext=self.ZopeFind(self,obj_ids=["index.html"])
113: if ext:
114: return ext[0][1]()
115:
116: pt=PageTemplateFile('Products/versionedFile/zpt/versionFileFolderMain').__of__(self)
117: return pt()
118:
119:
120: def addFileForm(self):
121: """add a file"""
122: out=DTMLFile('dtml/newFileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version='1').__of__(self)
123: return out()
124:
125:
126: def addFile(self,vC,file,author,newName='',content_type='',RESPONSE=None):
127: """ add a new file"""
128: if newName=='':
129: id=file.filename
130: else:
131: id=newName
132:
133: vC=self.REQUEST.form['vC']
134: manage_addVersionedFile(self,id,'','')
135: ob=self._getOb(id)
136: ob.title=id
137: file2=file
138: ob.manage_addVersionedFileObject(id,vC,author,file2,content_type=content_type)
139:
140: RESPONSE.redirect(self.REQUEST['URL1'])
141:
142:
143: manage_addVersionedFileFolderForm=DTMLFile('dtml/folderAdd', globals())
144:
145:
146: def manage_addVersionedFileFolder(self, id, title='',
147: createPublic=0,
148: createUserF=0,
149: REQUEST=None):
150: """Add a new Folder object with id *id*.
151:
152: If the 'createPublic' and 'createUserF' parameters are set to any true
153: value, an 'index_html' and a 'UserFolder' objects are created respectively
154: in the new folder.
155: """
156: ob=versionedFileFolder()
157: ob.id=str(id)
158: ob.title=title
159: self._setObject(id, ob)
160: ob=self._getOb(id)
161:
162: checkPermission=getSecurityManager().checkPermission
163:
164: if createUserF:
165: if not checkPermission('Add User Folders', ob):
166: raise Unauthorized, (
167: 'You are not authorized to add User Folders.'
168: )
169: ob.manage_addUserFolder()
170:
171:
172: if REQUEST is not None:
173: return self.manage_main(self, REQUEST, update_menu=1)
174:
175:
176:
177: class versionedFileObject(File):
178: """File Object im Folder"""
179:
180: meta_type = "versionedFileObject"
181:
182: manage_editForm =DTMLFile('dtml/fileEdit',globals(),
183: Kind='File',kind='file')
184: manage_editForm._setName('manage_editForm')
185:
186:
187:
188: def download(self):
189: """download and lock"""
190:
191:
192: self.content_type="application/octet-stream"
193: self.REQUEST.RESPONSE.redirect(self.absolute_url())
194:
195: def downloadLocked(self):
196: """download and lock"""
197:
198:
199: if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User':
200: return "please login first"
201: if not self.aq_parent.lockedBy=="":
202: return "cannot be locked because is already locked by %s"%self.lockedBy
203: self.aq_parent.lockedBy=self.REQUEST['AUTHENTICATED_USER']
204:
205: self.content_type="application/octet-stream"
206: self.REQUEST.RESPONSE.redirect(self.absolute_url())
207:
208: def setVersionNumber(self,versionNumber):
209: """set version"""
210: self.versionNumber=versionNumber
211:
212: def getVersionNumber(self):
213: """get version"""
214: return self.versionNumber
215:
216: def lastEditor(self):
217: """last Editor"""
218: if hasattr(self,'author'):
219: return self.author
220: else:
221: jar=self._p_jar
222: oid=self._p_oid
223:
224: if jar is None or oid is None: return None
225:
226: return jar.db().history(oid)[0]['user_name']
227:
228:
229:
230:
231: manage_addVersionedFileObjectForm=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject', version='1')
232:
233: def manage_addVersionedFileObject(self,id,vC='',author='', file='',title='',precondition='', content_type='',
234: REQUEST=None):
235: """Add a new File object.
236:
237: Creates a new File object 'id' with the contents of 'file'"""
238:
239: id=str(id)
240: title=str(title)
241: content_type=str(content_type)
242: precondition=str(precondition)
243:
244: id, title = cookId(id, title, file)
245:
246: self=self.this()
247:
248: # First, we create the file without data:
249: self._setObject(id, versionedFileObject(id,title,'',content_type, precondition))
250: self._getOb(id).versionComment=str(vC)
251: setattr(self._getOb(id),'author',author)
252:
253: # Now we "upload" the data. By doing this in two steps, we
254: # can use a database trick to make the upload more efficient.
255: if file:
256: self._getOb(id).manage_upload(file)
257: if content_type:
258: self._getOb(id).content_type=content_type
259:
260: if REQUEST is not None:
261: REQUEST['RESPONSE'].redirect(self.absolute_url()+'/manage_main')
262:
263:
264:
265:
266: class versionedFile(Folder):
267: """Versioniertes File"""
268:
269: def __init__(self, id, title, lockedBy,author):
270: """init"""
271: self.id=id
272: self.title=title
273: self.lockedBy=lockedBy
274: self.author=author
275:
276: meta_type="versionedFile"
277:
278: def getLastVersion(self):
279: """Last Version"""
280: tmp=0
281: lastVersion=None
282:
283: for version in self.ZopeFind(self):
284:
285: if hasattr(version[1],'versionNumber'):
286:
287: if int(version[1].versionNumber) > tmp:
288: tmp=int(version[1].versionNumber,)
289: lastVersion=version[1]
290: return lastVersion
291:
292: def index_html(self):
293: """main view"""
294: lastVersion=self.getLastVersion()
295: #return "File:"+self.title+" Version:%i"%lastVersion.versionNumber," modified:",lastVersion.bobobase_modification_time()," size:",lastVersion.getSize(),"modified by:",lastVersion.lastEditor()
296: return "File: %s Version:%i modified:%s size:%s modified by:%s"%(self.title,lastVersion.versionNumber,lastVersion.bobobase_modification_time(),lastVersion.getSize(),lastVersion.lastEditor())
297:
298: def getVersion(self):
299: tmp=0
300: for version in self.ZopeFind(self):
301:
302: if hasattr(version[1],'versionNumber'):
303:
304: if int(version[1].versionNumber) > tmp:
305: tmp=int(version[1].versionNumber,)
306: return tmp+1
307:
308: security= ClassSecurityInfo()
309: security.declareProtected('AUTHENTICATED_USER','unlock')
310:
311: def history(self):
312: """history"""
313:
314: ext=self.ZopeFind(self.aq_parent,obj_ids=["history_template.html"])
315: if ext:
316: return getattr(self,ext[0][1].getId())()
317:
318: pt=PageTemplateFile('Products/versionedFile/zpt/versionHistory').__of__(self)
319: return pt()
320:
321: def getVersions(self):
322: """get all versions"""
323: ret=[]
324: for version in self.ZopeFind(self):
325: if hasattr(version[1],'versionNumber'):
326: ret.append((version[1].versionNumber,version[1]))
327: ret.sort(sortv)
328: return ret
329:
330: security.declareProtected('AUTHENTICATED_USER','unlock')
331: def unlock(self,RESPONSE):
332: """unlock"""
333: if str(self.lockedBy) in [str(self.REQUEST['AUTHENTICATED_USER'])]:
334: self.lockedBy=''
335: RESPONSE.redirect(self.REQUEST['URL2'])
336: else:
337: return "Sorry, not locked by you! (%s,%s)"%(self.lockedBy,self.REQUEST['AUTHENTICATED_USER'])
338:
339:
340: security.declareProtected('AUTHENTICATED_USER','addVersionedFileObjectForm')
341:
342: def addVersionedFileObjectForm(self):
343: """add a new version"""
344:
345: if str(self.REQUEST['AUTHENTICATED_USER']) in ["Anonymous User"]:
346: return "please login first"
347: if (self.lockedBy==self.REQUEST['AUTHENTICATED_USER']) or (self.lockedBy==''):
348: out=DTMLFile('dtml/fileAdd', globals(),Kind='VersionedFileObject',kind='versionedFileObject',version=self.getVersion()).__of__(self)
349: return out()
350: else:
351: return "Sorry file is locked by somebody else"
352:
353: def manage_addVersionedFileObject(self,id,vC,author,file='',title='',precondition='', content_type='',changeName='no',newName='', RESPONSE=None):
354: """add"""
355:
356: vC=self.REQUEST['vC']
357: author=self.REQUEST['author']
358:
359: if changeName=="yes":
360: self.title=file.filename[0:]
361:
362: if not newName=='':
363: self.title=newName[0:]
364:
365: id="V%i"%self.getVersion()+"_"+self.title
366: manage_addVersionedFileObject(self,id,vC,author,file,"V%i"%self.getVersion()+"_"+self.title,precondition, content_type)
367: objs=self.ZopeFind(self,obj_ids=[id])[0][1].setVersionNumber(int(self.getVersion()))
368:
369: if RESPONSE:
370: RESPONSE.redirect(self.REQUEST['URL2'])
371:
372: security.declareProtected('AUTHENTICATED_USER','downloadLocked')
373:
374: def download(self):
375: """download and lock"""
376: self.getLastVersion().content_type="application/octet-stream"
377: self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId())
378:
379: def downloadLocked(self):
380: """download and lock"""
381: if self.REQUEST['AUTHENTICATED_USER']=='Anonymous User':
382: return "please login first"
383: if not self.lockedBy=="":
384: return "cannot be locked because is already locked by %s"%self.lockedBy
385: self.lockedBy=self.REQUEST['AUTHENTICATED_USER']
386: self.getLastVersion().content_type="application/octet-stream"
387: self.REQUEST.RESPONSE.redirect(self.REQUEST['URL1']+'/'+self.getId()+'/'+self.getLastVersion().getId())
388:
389: def manage_addVersionedFileForm(self):
390: """interface for adding the OSAS_root"""
391: pt=PageTemplateFile('Products/versionedFile/zpt/addVersionedFile.zpt').__of__(self)
392: return pt()
393:
394: def manage_addVersionedFile(self,id,title,lockedBy, author=None, RESPONSE=None):
395: """add the OSAS_root"""
396: newObj=versionedFile(id,title,lockedBy,author)
397: self._setObject(id,newObj)
398:
399: if RESPONSE is not None:
400: RESPONSE.redirect('manage_main')
401:
402:
403: InitializeClass(versionedFile)
404: InitializeClass(versionedFileFolder)
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>