Back to index

plone3  3.1.7
info.py
Go to the documentation of this file.
00001 from plone.app.linkintegrity.interfaces import ILinkIntegrityInfo
00002 from plone.app.linkintegrity.utils import encodeInts
00003 from plone.app.linkintegrity.utils import decodeInts
00004 from zope.interface import implements
00005 from zope.component import queryUtility
00006 from Acquisition import aq_base
00007 from Products.CMFCore.interfaces import IPropertiesTool
00008 
00009 
00010 class LinkIntegrityInfo(object):
00011     """ adapter for browserrequests to temporarily store information
00012         related to link integrity in the request object """
00013     implements(ILinkIntegrityInfo)
00014 
00015     attribute = marker = 'link_integrity_info'
00016 
00017     def __init__(self, context):
00018         self.context = context      # the context is the request
00019 
00020     def integrityCheckingEnabled(self):
00021         """ determine if link integrity checking for the site is enabled """
00022         ptool = queryUtility(IPropertiesTool)
00023         enabled = False
00024         if ptool is not None:
00025             props = getattr(ptool, 'site_properties', None)
00026             if props is not None:
00027                 enabled = props.getProperty('enable_link_integrity_checks', False)
00028         return enabled
00029 
00030     def getIntegrityInfo(self):
00031         """ return stored information regarding link integrity """
00032         return getattr(self.context, self.attribute, {})
00033 
00034     def setIntegrityInfo(self, info):
00035         """ store information regarding link integrity """
00036         setattr(self.context, self.attribute, info)
00037 
00038     def getIntegrityBreaches(self):
00039         """ return stored information regarding link integrity breaches
00040             after removing circular references, confirmed items etc """
00041         deleted = self.getDeletedItems()
00042         breaches = dict(self.getIntegrityInfo().get('breaches', {}))
00043         targets = breaches.keys()
00044         for target, sources in breaches.items():    # first remove deleted sources
00045             for source in list(sources):
00046                 if source in targets or source in deleted:
00047                     sources.remove(source)
00048         for target, sources in breaches.items():    # then remove "empty" targets
00049             if not sources or self.isConfirmedItem(target):
00050                 del breaches[target]
00051         return breaches
00052 
00053     def setIntegrityBreaches(self, breaches):
00054         """ store information regarding link integrity breaches """
00055         info = self.getIntegrityInfo()
00056         info['breaches'] = breaches
00057         self.setIntegrityInfo(info)     # unnecessary, but sticking to the api
00058 
00059     def getDeletedItems(self):
00060         """ return information about all items deleted during the request """
00061         return self.getIntegrityInfo().get('deleted', set())
00062 
00063     def addDeletedItem(self, item):
00064         """ remember an item deleted during the request """
00065         info = self.getIntegrityInfo()
00066         info.setdefault('deleted', set()).add(item)
00067         self.setIntegrityInfo(info)     # unnecessary, but sticking to the api
00068 
00069     def getEnvMarker(self):
00070         """ return the marker string used to pass the already confirmed
00071             items across the retry exception """
00072         return self.marker
00073 
00074     def confirmedItems(self):
00075         """ return internal list of confirmed items """
00076         confirmed = self.context.environ.get(self.marker, [])
00077         if confirmed == 'all':
00078             confirmed = ['all']
00079         elif confirmed:
00080             confirmed = decodeInts(confirmed)
00081         return confirmed
00082 
00083     def isConfirmedItem(self, obj):
00084         """ indicate if the removal of the given object was confirmed """
00085         confirmed = self.confirmedItems()
00086         return id(aq_base(obj)) in confirmed or 'all' in confirmed
00087 
00088     def encodeConfirmedItems(self, additions):
00089         """ return the list of previously confirmed (for removeal) items,
00090             optionally adding the given items, encoded for usage in a form """
00091         confirmed = self.confirmedItems()
00092         for obj in additions:
00093             confirmed.append(id(aq_base(obj)))
00094         return encodeInts(confirmed)
00095 
00096     def moreEventsToExpect(self):
00097         attr = 'link_integrity_events_counter'
00098         counter = getattr(self.context, attr, 0) + 1    # nr of events so far
00099         setattr(self.context, attr, counter)            # save for next time
00100         expected = self.context.get('link_integrity_events_to_expect', 0)
00101         return counter < expected
00102