-
Notifications
You must be signed in to change notification settings - Fork 0
/
_versionmanager.py
1406 lines (1265 loc) · 63.4 KB
/
_versionmanager.py
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
################################################################################
# _versionmanager.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
from AccessControl import ClassSecurityInfo
from App.special_dtml import HTMLFile
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
import Globals
import copy
import operator
import sys
import time
import urllib
import zExceptions
# Product Imports.
import _globals
import _blobfields
import _confmanager
import _zmsattributecontainer
# ------------------------------------------------------------------------------
# _versionmanager.setChangedBy:
#
# Applies information about user-id and date of change.
# ------------------------------------------------------------------------------
def setChangedBy(self, REQUEST, createWorkAttrCntnr=True):
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
auth_user = REQUEST.get('AUTHENTICATED_USER',None)
if auth_user is not None:
#-- Create new work-version.
if createWorkAttrCntnr:
has_version_work = self.version_work_id is not None and self.version_work_id != self.version_live_id and hasattr( self, self.version_work_id)
has_version_live = self.version_live_id is not None and hasattr( self, self.version_live_id)
if has_version_work:
oldAttrCntnr = getattr( self, self.version_work_id)
elif has_version_live:
oldAttrCntnr = getattr( self, self.version_live_id)
if ((lang == prim_lang or self.getDCCoverage(REQUEST).find('.%s'%lang) > 0) and self.getHistory()) or not has_version_work:
request = {'lang':'*'}
newAttrCntnr = _zmsattributecontainer.manage_addZMSAttributeContainer(self)
_globals.writeLog( self, "[setChangedBy]: Create new work-version: %s"%newAttrCntnr.id)
self.cloneObjAttrs(oldAttrCntnr,newAttrCntnr,request)
self.version_work_id = newAttrCntnr.id
#-- Set minor-version.
if ((lang == prim_lang or self.getDCCoverage(REQUEST).endswith('.%s'%lang)) and self.getHistory()) or not has_version_work:
try:
req = {'lang':lang,'preview':'preview','fetchReqBuff':False}
minor_version = self.getObjProperty( 'minor_version', req) + 1
except:
minor_version = 1
self.setObjProperty( 'minor_version' ,minor_version, lang)
_globals.writeLog( self, "[setChangedBy]: Set minor-version: %i"%minor_version)
#-- Set properties.
self.setObjProperty( 'change_uid' ,str(auth_user) ,lang)
self.setObjProperty( 'change_dt' ,_globals.getDateTime( time.time()) ,lang)
# ------------------------------------------------------------------------------
# _versionmanager.setCreatedBy:
#
# Applies information about user-id and date of creation.
# ------------------------------------------------------------------------------
def setCreatedBy(self, REQUEST):
auth_user = REQUEST.get('AUTHENTICATED_USER',None)
if auth_user is not None:
#-- Set properties.
_globals.writeLog( self, "[setCreatedBy]: Set created by: %s"%str(auth_user))
self.setObjProperty( 'created_uid' ,str(auth_user))
self.setObjProperty( 'created_dt' ,_globals.getDateTime( time.time()))
# ------------------------------------------------------------------------------
# _versionmanager.getObjStateName:
#
# Returns name object state in given language.
# ------------------------------------------------------------------------------
def getObjStateName(obj_state, lang):
obj_state_name = obj_state
if lang is not None:
obj_state_name += '_'+lang.upper()
return obj_state_name
################################################################################
################################################################################
###
### class VersionItem
###
################################################################################
################################################################################
class VersionItem:
# Create a SecurityInfo for this class. We will use this
# in the rest of our class definition to make security
# assertions.
security = ClassSecurityInfo()
# Management Interface.
# ---------------------
version_object_state = HTMLFile('dtml/versionmanager/version_object_state',globals())
zmi_version_object_state = PageTemplateFile('zpt/versionmanager/zmi_version_object_state',globals())
# --------------------------------------------------------------------------
# VersionItem.tagObjVersions:
#
# Tag object-versions.
# --------------------------------------------------------------------------
def tagObjVersions(self, master_version, REQUEST, checkPending=None):
_globals.writeLog( self, "[tagObjVersions]")
count = 1
if checkPending is None:
self.tagObjVersions( master_version, REQUEST, checkPending=True)
self.tagObjVersions( master_version, REQUEST, checkPending=False)
else:
if checkPending:
#-- Check for pending changes.
if self.getObjStateNames(REQUEST):
raise zExceptions.InternalError("Can't tagObjVersions: %s@%s has pending changes %s"%(self.meta_id,self.absolute_url(),str(self.getObjStateNames(REQUEST))))
else:
#-- Tag master-version.
obj_version = None
if obj_version is None and self.version_live_id is not None and hasattr( self, self.version_live_id):
obj_version = getattr( self, self.version_live_id)
if obj_version is None and self.version_work_id is not None and hasattr( self, self.version_work_id):
obj_version = getattr( self, self.version_work_id)
if 'change_history' in self.getObjAttrs().keys():
change_history = []
record = {}
record[ 'version_dt'] = _globals.getDateTime( time.time())
record[ 'version_uid'] = str( REQUEST.get( 'AUTHENTICATED_USER'))
record[ 'master_version'] = master_version
record[ 'major_version'] = 0
change_history.append( record)
setattr( obj_version, 'change_history', change_history)
setattr( obj_version, 'master_version', master_version)
setattr( obj_version, 'major_version', 0)
setattr( obj_version, 'minor_version', 0)
self.version_live_id = obj_version.id
self.version_work_id = None
ids = []
for obj_version in self.getObjVersions():
if obj_version.id != self.version_live_id:
ids.append( obj_version.id)
self.manage_delObjects( ids=ids)
#-- Recursion.
for ob in self.getChildNodes():
count += ob.tagObjVersions( master_version, REQUEST, checkPending)
return count
"""
############################################################################
# Workflow
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.getObjStates:
#
# Returns a list of object-states (including language suffixes).
# --------------------------------------------------------------------------
def getObjStates(self):
# Get object-states.
if not hasattr(self,'__work_state__'):
self.__work_state__ = _globals.MyClass()
states = getattr(self.__work_state__,'states',[])
# Make object-states unique.
d = {}
map(lambda x: operator.setitem(d, x, None), states)
states = d.keys()
# Return object-states.
return states
# --------------------------------------------------------------------------
# VersionItem.getWfStates
# --------------------------------------------------------------------------
def getWfStates(self, REQUEST):
states = self.getObjStates()
lang = REQUEST.get('lang',None)
obj_states = []
wfActivitiesIds = self.getWfActivitiesIds()
for obj_state in wfActivitiesIds:
obj_state_name = getObjStateName(obj_state,lang)
if obj_state_name in states:
obj_states.append(obj_state)
if len( obj_states) == 0 and \
len( wfActivitiesIds) > 0 and \
lang is not None and \
( 'STATE_NEW_%s'%lang.upper() in states or \
'STATE_MODIFIED_%s'%lang.upper() in states or \
'STATE_DELETED_%s'%lang.upper() in states):
obj_states.append( wfActivitiesIds[0])
return obj_states
# --------------------------------------------------------------------------
# VersionItem.getVersionItems
#
# Returns all version-items.
# --------------------------------------------------------------------------
def getVersionItems(self, REQUEST):
children = []
if not self.getAutocommit():
types = self.getMetaobjIds(sort=0)+['*']
for metaobjAttrId in self.getMetaobjAttrIds( self.meta_id, types=types):
for child in self.getObjChildren( metaobjAttrId , REQUEST):
if not child.isVersionContainer():
children.append( child)
children.extend( child.getVersionItems( REQUEST))
return children
# --------------------------------------------------------------------------
# VersionItem.isObjModified:
#
# Returns true if version-item is modified, false otherwise.
# --------------------------------------------------------------------------
def isObjModified(self, REQUEST):
return self.inObjStates( [ 'STATE_NEW', 'STATE_MODIFIED', 'STATE_DELETED'], REQUEST)
# --------------------------------------------------------------------------
# VersionItem.syncObjModifiedChildren:
#
# Returns true if object has modified children, false otherwise.
# --------------------------------------------------------------------------
def syncObjModifiedChildren(self, REQUEST, depth=0):
obj_state = 'STATE_MODIFIED_OBJS'
rtnVal = False
for child in self.getVersionItems( REQUEST):
if child.isObjModified(REQUEST):
rtnVal = True
else:
rtnVal = child.syncObjModifiedChildren( REQUEST, depth+1)
if rtnVal:
break
if depth==0:
if rtnVal:
if not self.inObjStates( [ obj_state], REQUEST):
self.setObjState( obj_state, REQUEST[ 'lang'])
else:
if self.inObjStates( [ obj_state], REQUEST):
self.delObjStates( [ obj_state], REQUEST)
return rtnVal
# --------------------------------------------------------------------------
# VersionItem.hasObjModifiedChildren:
#
# Returns true if object has modified children, false otherwise.
# --------------------------------------------------------------------------
def hasObjModifiedChildren(self, REQUEST, depth=0):
obj_state = 'STATE_MODIFIED_OBJS'
return self.inObjStates( [ obj_state], REQUEST)
# --------------------------------------------------------------------------
# VersionItem.resetObjTranslation:
#
# Resets translation.
# --------------------------------------------------------------------------
def resetObjTranslation(self):
prim_lang = self.getPrimaryLanguage()
for ob in self.getObjVersions():
for lang in self.getLangIds():
if lang != prim_lang:
setattr(ob,'change_uid_%s'%lang,'')
# --------------------------------------------------------------------------
# VersionItem.initializeWorkVersion:
#
# Initializes work-version of object-attributes.
# --------------------------------------------------------------------------
def initializeWorkVersion(self):
# States.
self.__work_state__ = _globals.MyClass()
# Create new work-version.
if len(self.objectValues(['ZMSAttributeContainer']))==0:
newAttrCntnr = _zmsattributecontainer.manage_addZMSAttributeContainer(self)
_globals.writeLog( self, "[initializeWorkVersion]: Create new work-version: %s"%newAttrCntnr.id)
self.version_work_id = newAttrCntnr.id
self.version_live_id = None
# --------------------------------------------------------------------------
# VersionItem.isCommitted:
#
# Checks if object can by displayed.
# --------------------------------------------------------------------------
def isCommitted(self, REQUEST):
if _globals.isPreviewRequest( REQUEST):
committed = not self.inObjStates( [ 'STATE_DELETED'], REQUEST)
else:
committed = not self.inObjStates( [ 'STATE_NEW'], REQUEST)
return committed
"""
############################################################################
# API object-state
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.setObjState:
#
# Sets object-state.
# --------------------------------------------------------------------------
def setObjState(self, obj_state, lang):
state = getObjStateName(obj_state,lang)
states = self.getObjStates()
if not state in states:
states.append(state)
self.__work_state__.states = copy.deepcopy(states)
self.__work_state__ = copy.deepcopy(self.__work_state__)
# --------------------------------------------------------------------------
# VersionItem.delObjStates:
#
# Deletes object-state of this object.
# --------------------------------------------------------------------------
def delObjStates(self, obj_states=[], REQUEST={}):
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
states = self.getObjStates()
for obj_state in obj_states:
while obj_state in states:
del states[states.index(obj_state)]
while getObjStateName(obj_state,lang) in states:
del states[states.index(getObjStateName(obj_state,lang))]
self.__work_state__.states = copy.deepcopy(states)
self.__work_state__ = copy.deepcopy(self.__work_state__)
# --------------------------------------------------------------------------
# VersionItem.resetObjStates:
#
# Resets object-state of this object.
# --------------------------------------------------------------------------
def resetObjStates(self, REQUEST=None):
if REQUEST is None:
self.version_live_id = self.version_work_id
self.version_work_id = None
self.__work_state__.states = []
self.__work_state__ = copy.deepcopy(self.__work_state__)
else:
self.delObjStates( [ 'STATE_NEW', 'STATE_MODIFIED', 'STATE_MODIFIED_OBJS', 'STATE_DELETED'], REQUEST)
# --------------------------------------------------------------------------
# VersionItem.inObjStates:
#
# Checks if given states are in object-states of this object.
# --------------------------------------------------------------------------
def inObjStates(self, obj_states, REQUEST):
states = self.getObjStates()
states.extend(self.getObjStateNames(REQUEST))
if len(states) > 0:
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
for obj_state in obj_states:
obj_state_name = getObjStateName( obj_state, lang)
if obj_state_name in states:
return True
return False
# --------------------------------------------------------------------------
# VersionItem.filteredObjStates:
#
# Checks if given states are in object-states of this object.
# --------------------------------------------------------------------------
def filteredObjStates(self, REQUEST):
obj_states = []
states = self.getObjStates()
if len(states) > 0:
lang = REQUEST.get('lang',self.getPrimaryLanguage())
for obj_state in [ 'STATE_NEW', 'STATE_MODIFIED', 'STATE_DELETED']:
obj_state_name = getObjStateName(obj_state,lang)
if obj_state_name in states:
obj_states.append( obj_state)
return obj_states
# --------------------------------------------------------------------------
# VersionItem.getObjStateNames:
#
# Returns a list of normalized object-states (language suffixes stripped off).
# --------------------------------------------------------------------------
def getObjStateNames(self, REQUEST):
lang = REQUEST.get('lang')
# Current Object.
states = self.getObjStates()
obj_states = []
for obj_state in [ 'STATE_NEW', 'STATE_MODIFIED', 'STATE_DELETED']:
obj_state_name = getObjStateName(obj_state,lang)
if obj_state_name in states:
obj_states.append(obj_state_name)
# Return value.
return obj_states
"""
############################################################################
# Change object-state.
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.setObjStateNew
# --------------------------------------------------------------------------
def setObjStateNew(self, REQUEST, reset=1):
obj_state = 'STATE_NEW'
if reset: self.initializeWorkVersion()
setChangedBy( self, REQUEST)
setCreatedBy( self, REQUEST)
#-- Set Master-Version.
parent = self.getParentNode()
master_version = 0
if parent is not None:
master_version = parent.getObjProperty( 'master_version', REQUEST)
self.setObjProperty( 'master_version', master_version)
self.setObjState(obj_state,REQUEST['lang'])
#-- Set Theme.
_confmanager.set_zmi_theme(self)
# --------------------------------------------------------------------------
# VersionItem.setObjStateModified
# --------------------------------------------------------------------------
def setObjStateModified(self, REQUEST):
obj_state = 'STATE_MODIFIED'
setChangedBy( self, REQUEST)
self.setObjState(obj_state,REQUEST['lang'])
# --------------------------------------------------------------------------
# VersionItem.setObjStateDeleted
# --------------------------------------------------------------------------
def setObjStateDeleted(self, REQUEST):
obj_state = 'STATE_DELETED'
setChangedBy( self, REQUEST)
self.setObjState(obj_state,REQUEST['lang'])
"""
############################################################################
#
# Commit
#
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.onChangeObj:
# --------------------------------------------------------------------------
def onChangeObj(self, REQUEST, forced=False, do_history=True):
_globals.writeLog( self, "[onChangeObj]")
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
##### Trigger thumbnail generation of image fields ####
_blobfields.thumbnailImageFields( self, lang, REQUEST)
##### Trigger custom onChangeObj-Event (if there is one) ####
_globals.triggerEvent( self, 'onChangeObjEvt', preview=True, REQUEST=REQUEST)
##### Commit or initiate workflow transition ####
if self.getAutocommit() or forced:
self.commitObj(REQUEST,forced,do_history)
else:
self.autoWfTransition(REQUEST)
_globals.writeLog( self, "[onChangeObj]: Finished!")
# --------------------------------------------------------------------------
# VersionItem.onSynchronizeObj:
# --------------------------------------------------------------------------
def onSynchronizeObj(self, REQUEST):
_globals.writeLog( self, "[onSynchronizeObj]")
#-- Catalog
ob = self.getCatalogItem()
obs = REQUEST.get('ZMS_SYNCHRONIZE_CATALOG',[])
if ob not in obs:
obs.append(ob)
try:
REQUEST.set('ZMS_SYNCHRONIZE_CATALOG',obs)
except:
REQUEST['ZMS_SYNCHRONIZE_CATALOG'] = obs
#-- Access
self.synchronizePublicAccess()
#-- References
self.synchronizeRefToObjs()
self.synchronizeRefByObjs()
# --------------------------------------------------------------------------
# VersionItem.commitObjChanges
# --------------------------------------------------------------------------
def _commitObjChanges(self, parent, REQUEST, forced=False, do_history=True, do_delete=True):
_globals.writeLog( self, "[_commitObjChanges]: forced=%s, do_history=%s, do_delete=%s"%(str(forced),str(do_history),str(do_delete)))
delete = False
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
##### Trigger custom beforeCommitObjChanges-Event (if there is one) ####
_globals.triggerEvent( self, 'beforeCommitObjChangesEvt', preview=True, REQUEST=REQUEST)
##### Commit delete. ####
if self.inObjStates(['STATE_DELETED'],REQUEST):
if do_delete:
parent.moveObjsToTrashcan([self.id], REQUEST)
delete = True
##### Commit modifications. ####
modified = self.getObjStateNames(REQUEST) or forced
if modified:
if (lang == prim_lang or self.getDCCoverage(REQUEST).find('.%s'%lang) > 0) and (self.getHistory() and do_history):
version_hist_id = None
if self.getObjStateNames(REQUEST) and \
self.version_work_id is not None:
# Clone current live-version to history-version.
if self.version_live_id is not None and self.version_live_id in self.objectIds(['ZMSAttributeContainer']):
request = {'lang':'*'}
histAttrCntnr = _zmsattributecontainer.manage_addZMSAttributeContainer(self)
self.cloneObjAttrs(getattr(self,self.version_live_id),histAttrCntnr,request)
version_hist_id = histAttrCntnr.id
# Replace current live-version by work-version.
if self.version_work_id is not None and self.version_work_id in self.objectIds(['ZMSAttributeContainer']):
self.version_live_id = self.version_work_id
self.version_work_id = None
# Increase version-number.
major_version = self.getObjProperty( 'major_version', REQUEST)
self.setObjProperty( 'major_version', major_version + 1)
self.setObjProperty( 'minor_version' ,0)
# Remove previous minor-versions.
ids = []
for ob_version in self.getObjVersions():
if ob_version.id != version_hist_id and \
ob_version.getObjProperty('major_version',REQUEST) == major_version:
ids.append( ob_version.id)
_globals.writeLog( self, "[_commitObjChanges]: Remove previous minor-versions: ids=%s"%str(ids))
self.manage_delObjects( ids=ids)
else:
if self.getObjStateNames(REQUEST) and \
self.version_work_id is not None:
if self.version_live_id is None:
self.version_live_id = self.version_work_id
if self.getAutocommit():
# Replace current live-version by work-version.
self.version_live_id = self.version_work_id
self.version_work_id = None
elif self.version_live_id != self.version_work_id:
# Clone current work-version to live-version.
self.cloneObjAttrs(getattr(self,self.version_work_id),getattr(self,self.version_live_id),REQUEST)
# Reset version-number.
if self.getHistory() and not do_history:
self.setObjProperty( 'major_version', 0)
self.setObjProperty( 'minor_version' ,0)
##### Commit version-items. ####
if not delete:
ids = []
for child in self.getVersionItems( REQUEST):
delete_child = child._commitObjChanges( self, REQUEST, False, do_history, False)
if delete_child:
ids.append( child.id)
if len( ids) > 0:
self.moveObjsToTrashcan( ids, REQUEST)
##### Reset object-state. ####
self.resetObjStates(REQUEST)
# Remove work-version.
attrCntnrIds = self.objectIds(['ZMSAttributeContainer'])
if (self.getAutocommit() or len( filter( lambda x: x.startswith( 'STATE_'), self.getObjStates())) == 0) and getattr( self, 'version_live_id', None) is not None:
if self.version_live_id in attrCntnrIds:
ids = []
if self.version_live_id != self.version_work_id and self.version_work_id in attrCntnrIds:
ids.append( self.version_work_id)
if self.getAutocommit() and not (self.getHistory() and do_history):
for id in attrCntnrIds:
if id != self.version_live_id and id != self.version_work_id:
ids.append( id)
self.version_work_id = None
if len( ids) > 0:
_globals.writeLog( self, "[_commitObjChanges]: Remove work-version: ids=%s"%str(ids))
self.manage_delObjects( ids=ids)
elif self.version_work_id in attrCntnrIds:
self.version_live_id = self.version_work_id
self.version_work_id = None
##### Synchronize listeners. ####
if modified:
self.onSynchronizeObj(REQUEST)
##### Trigger custom afterCommitObjChanges-Event (if there is one) ####
_globals.triggerEvent( self, 'afterCommitObjChangesEvt', preview=True, REQUEST=REQUEST)
# Return flag for deleted objects.
return delete
def commitObjChanges(self, parent, REQUEST, forced=False, do_history=True, do_delete=True):
_globals.writeLog( self, "[commitObjChanges]: forced=%s, do_history=%s, do_delete=%s"%(str(forced),str(do_history),str(do_delete)))
delete = self._commitObjChanges( parent, REQUEST, forced, do_history, do_delete)
##### Synchronize catalog. ####
obs = REQUEST.get('ZMS_SYNCHRONIZE_CATALOG',[])
for ob in obs:
ob.synchronizeSearch(REQUEST)
# Return flag for deleted objects.
return delete
"""
############################################################################
#
# Rollback
#
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.rollbackObjChanges
# --------------------------------------------------------------------------
def _rollbackObjChanges(self, parent, REQUEST, forced=0, do_delete=True):
_globals.writeLog( self, "[_rollbackObjChanges]")
delete = False
prim_lang = self.getPrimaryLanguage()
lang = REQUEST.get('lang',prim_lang)
##### Trigger custom beforeRollbackObjChanges-Event (if there is one) ####
_globals.triggerEvent( self, 'beforeRollbackObjChangesEvt', preview=True, REQUEST=REQUEST)
##### Rollback insert. ####
# Self.
if self.inObjStates(['STATE_NEW'],REQUEST):
if do_delete:
parent.moveObjsToTrashcan([self.id], REQUEST)
delete = True
##### Rollback modifications. ####
modified = self.getObjStateNames(REQUEST) or forced
if modified and not delete:
if (lang == prim_lang or self.getDCCoverage(REQUEST).find('.%s'%lang) > 0) and self.getHistory():
# Reset work-version.
self.version_work_id = None
# Current version-number.
major_version = self.getObjProperty( 'major_version', REQUEST)
# Remove next minor-versions.
ids = []
for ob_version in self.getObjVersions():
if ob_version.getObjProperty('major_version',REQUEST) == major_version and \
ob_version.getObjProperty('minor_version',REQUEST) > 0:
ids.append( ob_version.id)
_globals.writeLog( self, "[_rollbackObjChanges]: Remove next minor-versions: ids=%s"%str(ids))
self.manage_delObjects( ids=ids)
else:
if self.getObjStateNames(REQUEST) and \
self.version_work_id is not None:
if self.version_live_id is None:
self.version_live_id = self.version_work_id
if self.getAutocommit():
# Remove current work-version.
self.version_work_id = None
elif self.version_live_id is not None and self.version_live_id != self.version_work_id:
# Clone current live-version to work-version.
_globals.writeLog( self, "[_rollbackObjChanges]: Clone current live-version '%s' to work-version '%s'"%(self.version_live_id,self.version_work_id))
self.cloneObjAttrs(getattr(self,self.version_live_id),getattr(self,self.version_work_id),REQUEST)
##### Rollback version-items. ####
if not delete:
ids = []
for child in self.getVersionItems( REQUEST):
delete_child = child._rollbackObjChanges( self, REQUEST, 0, False)
if delete_child:
ids.append( child.id)
if len( ids) > 0:
self.moveObjsToTrashcan( ids, REQUEST)
##### Reset object-state. ####
self.resetObjStates(REQUEST)
# Remove work-version.
attrCntnrIds = self.objectIds(['ZMSAttributeContainer'])
if (self.getAutocommit() or len( filter( lambda x: x.startswith( 'STATE_'), self.getObjStates())) == 0) and getattr( self, 'version_live_id', None) is not None:
if self.version_live_id in attrCntnrIds:
ids = []
if self.version_live_id != self.version_work_id and self.version_work_id in attrCntnrIds:
ids.append( self.version_work_id)
if self.getAutocommit() and not self.getHistory():
for id in attrCntnrIds:
if id != self.version_live_id and id != self.version_work_id:
ids.append( id)
self.version_work_id = None
if len( ids) > 0:
_globals.writeLog( self, "[_rollbackObjChanges]: Remove work-version: ids=%s"%str(ids))
self.manage_delObjects( ids=ids)
elif self.version_work_id in attrCntnrIds:
self.version_live_id = self.version_work_id
self.version_work_id = None
##### Synchronize listeners. ####
if modified and not delete:
self.onSynchronizeObj(REQUEST)
##### Trigger custom afterRollbackObjChanges-Event (if there is one) ####
_globals.triggerEvent( self, 'afterRollbackObjChangesEvt', preview=True, REQUEST=REQUEST)
# Return flag for deleted objects.
return delete
def rollbackObjChanges(self, parent, REQUEST, forced=0, do_delete=True):
delete = self._rollbackObjChanges( parent, REQUEST, forced, do_delete)
##### Synchronize catalog. ####
obs = REQUEST.get('ZMS_SYNCHRONIZE_CATALOG',[])
for ob in obs:
ob.synchronizeSearch(REQUEST)
# Return flag for deleted objects.
return delete
"""
############################################################################
#
# History
#
############################################################################
"""
# --------------------------------------------------------------------------
# VersionItem.packHistory:
#
# Pack history.
# --------------------------------------------------------------------------
def packHistory(self):
count = 0
if not self.getHistory():
#-- Remove version-attribute-containers.
ids = []
for id in self.objectIds(['ZMSAttributeContainer']):
if id not in [ self.version_work_id, self.version_live_id]:
ids.append( id)
count += len( ids)
self.manage_delObjects( ids=ids)
#-- Remove version-attributes.
for key in ['master_version','major_version', 'minor_version', 'change_history']:
for id in [ self.version_work_id, self.version_live_id]:
if id is not None:
ob = getattr( self, id, None)
if ob is not None:
try:
delattr( ob, key)
except:
pass
#-- Recursion.
for ob in self.objectValues( self.dGlobalAttrs.keys()):
count += ob.packHistory()
return count
# --------------------------------------------------------------------------
# VersionItem.getHistory:
#
# Returns true if history is active, false otherwise.
# --------------------------------------------------------------------------
def getHistory( self):
active = True
if active:
active = active and self.getConfProperty('ZMS.Version.active',0)==1
if active:
baseurl = self.getDocumentElement().absolute_url()
url = self.absolute_url()
if len( url) >= len( baseurl):
url = url[ len( baseurl)+1:]
url = '$'+url
found = False
nodes = self.getConfProperty('ZMS.Version.nodes',['{$}'])
for node in nodes:
if node[1:-1] == '$' or (url+'/').find(node[1:-1]+'/') == 0:
found = True
break
active = active and found
return active
# --------------------------------------------------------------------------
# VersionItem.ajaxBodyContentObjHistory:
# --------------------------------------------------------------------------
security.declareProtected('View', 'ajaxBodyContentObjHistory')
def ajaxBodyContentObjHistory(self, version_nr, REQUEST):
"""
Returns ajax-xml with body-content of object-history for given version-nr.
"""
#-- Get versions.
master_version = int( version_nr[ :version_nr.find( '.')])
major_version = int( version_nr[ version_nr.find( '.')+1: version_nr.rfind( '.')])
minor_version = int( version_nr[ version_nr.rfind( '.')+1:])
REQUEST.set( 'ZMS_VERSION_%s'%self.id, None)
version_dt = None
if 'change_history' in self.getObjAttrs().keys():
change_history = copy.copy( self.getObjProperty( 'change_history', REQUEST))
change_history.reverse()
for item in change_history:
if version_dt is None and \
item.get( 'master_version', 0) <= master_version and \
item.get( 'major_version', 0) <= major_version:
version_dt = item[ 'version_dt']
break
#-- Build xml.
RESPONSE = REQUEST.RESPONSE
content_type = 'text/xml; charset=utf-8'
filename = 'ajaxBodyContentObjHistory.xml'
RESPONSE.setHeader('Content-Type',content_type)
RESPONSE.setHeader('Content-Disposition','inline;filename="%s"'%filename)
RESPONSE.setHeader('Cache-Control', 'no-cache')
RESPONSE.setHeader('Pragma', 'no-cache')
self.f_standard_html_request( self, REQUEST)
xml = self.getXmlHeader()
xml += '<BodyContentObjHistory version_nr="%s">\n'%version_nr
if REQUEST.get('revision'):
obj_version = self.getObjHistory( version_nr, REQUEST, False)
REQUEST.set( 'ZMS_VERSION_%s'%self.id, obj_version.id)
xml += '<title><![CDATA[%s]]></title>'%_globals.html_quote(obj_version.getTitle(REQUEST))
xml += '<description><![CDATA[%s]]></description>'%_globals.html_quote(obj_version.getDCDescription(REQUEST))
obj_history = self.getObjHistory( version_nr, REQUEST)
for history_version in obj_history:
if history_version.isVisible(REQUEST):
xml += '<ObjHistory'
xml += ' id="%s"'%history_version.id
xml += ' change_uid="%s"'%history_version.getObjProperty('change_uid',REQUEST)
xml += ' change_dt="%s"'%self.getLangFmtDate(history_version.getObjProperty('change_dt',REQUEST),REQUEST['lang'],'SHORTDATETIME_FMT')
xml += ' version="%i.%i.%i"'%(history_version.getObjProperty('master_version',REQUEST),history_version.getObjProperty('major_version',REQUEST),history_version.getObjProperty('minor_version',REQUEST))
xml += '>'
xml += '<![CDATA[' + history_version.getBodyContent( REQUEST) + ']]>'
xml += '</ObjHistory>\n'
else:
if self.meta_type in [ 'ZMS', 'ZMSCustom']:
obj_version = self.getObjHistory( version_nr, REQUEST, False)
REQUEST.set( 'ZMS_VERSION_%s'%self.id, obj_version.id)
xml += '<ObjHistory'
xml += ' id="%s"'%obj_version.id
xml += ' title="%s"'%_globals.html_quote(obj_version.getTitle(REQUEST))
xml += ' description="%s"'%_globals.html_quote(obj_version.getDCDescription(REQUEST))
xml += '>'
xml += '<![CDATA[' + obj_version.getBodyContent( REQUEST) + ']]>'
xml += '</ObjHistory>\n'
else:
obj_history = self.getObjHistory( version_nr, REQUEST)
for history_version in obj_history:
if history_version.isActive(REQUEST):
xml += '<ObjHistory id="' + history_version.id + '">'
xml += '<![CDATA[' + history_version.renderShort( REQUEST) + ']]>'
xml += '</ObjHistory>\n'
xml += "</BodyContentObjHistory>\n"
return xml
# --------------------------------------------------------------------------
# VersionItem.getObjHistory:
#
# Returns object-history for given version-nr.
# --------------------------------------------------------------------------
def getObjHistory(self, version_nr, REQUEST, children=True, deleted=True):
_globals.writeLog( self, '[getObjHistory]: version_nr=%s'%str(version_nr))
obs = []
ZMS_VERSION = REQUEST.get( 'ZMS_VERSION_%s'%self.id)
master_version = int( version_nr[ :version_nr.find( '.')])
major_version = int( version_nr[ version_nr.find( '.')+1: version_nr.rfind( '.')])
minor_version = int( version_nr[ version_nr.rfind( '.')+1:])
REQUEST.set( 'ZMS_VERSION_%s'%self.id, None)
version_dt = None
change_history = []
if 'change_history' in self.getObjAttrs().keys():
change_history = copy.copy( self.getObjProperty( 'change_history', REQUEST))
change_history.reverse()
for item in change_history:
if version_dt is None and \
item.get( 'master_version', 0) <= master_version and \
item.get( 'major_version', 0) <= major_version:
version_dt = item[ 'version_dt']
break
_globals.writeLog( self, '[getObjHistory]: version_dt=%s'%str(version_dt))
found = False
last_ob_version = None
for ob_version in self.getObjVersions():
REQUEST.set( 'ZMS_VERSION_%s'%self.id, ob_version.id)
last_ob_version = ob_version
ob_version_master_version = getattr( ob_version, 'master_version', 0)
ob_version_major_version = getattr( ob_version, 'major_version', 0)
ob_version_minor_version = getattr( ob_version, 'minor_version', 0)
ob_version_nr = '%i.%i.%i'%(ob_version_master_version,ob_version_major_version,ob_version_minor_version)
ob_version_change_dt = ob_version.getObjProperty( 'change_dt', REQUEST, {'fetchReqBuff':0})
if ob_version_nr <= version_nr:
if not children:
_globals.writeLog( self, '[getObjHistory]: return %s'%str(last_ob_version.id))
return last_ob_version
for ob_child in self.getVersionItems( REQUEST):
for ob_child_version in ob_child.getObjVersions():
REQUEST.set( 'ZMS_VERSION_%s'%ob_child.id, ob_child_version.id)
ob_child_master_version = getattr( ob_child_version, 'master_version', 0)
ob_child_major_version = getattr( ob_child_version, 'major_version', 0)
ob_child_minor_version = getattr( ob_child_version, 'minor_version', 0)
ob_child_nr = '%i.%i.%i'%(ob_child_master_version,ob_child_major_version,ob_child_minor_version)
ob_child_change_dt = ob_child.getObjProperty( 'change_dt', REQUEST, {'fetchReqBuff':0})
if ( version_dt is None and \
((ob_child_master_version == 0 and ob_child_major_version == 0 and ob_child_minor_version == 0) or \
(len( change_history) == 0 and ob_version_change_dt >= ob_child_change_dt))) or \
( version_dt >= ob_child_change_dt) or \
( minor_version > 0 and ob_child_minor_version > 0) or \
( minor_version > 0 and ob_child_change_dt > version_dt):
obs.append( ob_child_version)
break
found = True
break
if not found:
if not children:
_globals.writeLog( self, '[getObjHistory]: return %s'%str(last_ob_version.id))
return last_ob_version
for ob_child in self.getVersionItems( REQUEST):
for ob_child_version in ob_child.getObjVersions():
REQUEST.set( 'ZMS_VERSION_%s'%ob_child.id, ob_child_version.id)
ob_child_master_version = getattr( ob_child_version, 'master_version', 0)
ob_child_major_version = getattr( ob_child_version, 'major_version', 0)
ob_child_minor_version = getattr( ob_child_version, 'minor_version', 0)
ob_child_nr = '%i.%i.%i'%(ob_child_master_version,ob_child_major_version,ob_child_minor_version)
ob_child_change_dt = ob_child.getObjProperty( 'change_dt', REQUEST)
if ( version_dt is None and ob_child_master_version == 0 and ob_child_major_version == 0 and ob_child_minor_version == 0) or \
( ob_child_change_dt <= version_dt):
obs.append( ob_child_version)
break
REQUEST.set( 'ZMS_VERSION_%s'%self.id, ZMS_VERSION)
return obs
# --------------------------------------------------------------------------
# VersionItem.getObjVersion:
#
# Returns attribute-container. If Http-Request has key >ZMS_VERSION< the
# desired version is returned, if Http-Request has key >preview< the work-
# version is returned, else the live-version is returned.
# --------------------------------------------------------------------------
def getObjVersion(self, REQUEST={}):
try:
ob = None
id = REQUEST.get( 'ZMS_VERSION_%s'%self.id, None)
if id is not None:
return getattr( self, id)
elif REQUEST.get('preview') == 'preview':
if self.version_work_id is not None:
ob = getattr(self, self.version_work_id, None)
else:
if self.version_live_id is not None:
ob = getattr(self, self.version_live_id, None)
if ob is None:
if self.version_work_id is not None:
ob = getattr(self, self.version_work_id, None)
if ob is None:
if self.version_live_id is not None:
ob = getattr(self, self.version_live_id, None)
s = ob.id # Never delete this line!
return ob
except:
raise zExceptions.InternalError(_globals.writeError( self, '[getObjVersion]: an unexpected error occured!'))
# --------------------------------------------------------------------------
# VersionItem.getObjVersions:
#
# Returns all attribute-containers.
# --------------------------------------------------------------------------
def getObjVersions(self):
try:
obs = []
for ob in self.objectValues(['ZMSAttributeContainer']):
master_version = getattr( ob,'master_version',0)
if type(master_version) is not int: master_version = 0
major_version = getattr( ob,'major_version',0)