Back to index

plone3  3.1.7
locking.py
Go to the documentation of this file.
00001 
00002 from Acquisition import aq_inner
00003 from Products.Five import BrowserView
00004 from Products.CMFCore.utils import getToolByName
00005 
00006 from DateTime import DateTime
00007 from datetime import timedelta
00008 
00009 from plone.locking.interfaces import ILockable
00010 from Products.CMFPlone import PloneMessageFactory as _
00011 
00012 class LockingOperations(BrowserView):
00013     """Lock acquisition and stealing operations
00014     """
00015 
00016     def force_unlock(self, redirect=True):
00017         """Steal the lock.
00018 
00019         If redirect is True, redirect back to the context URL, i.e. reload
00020         the page.
00021         """
00022         lockable = ILockable(self.context)
00023         lockable.unlock()
00024         if redirect:
00025             self.request.RESPONSE.redirect(self.context.absolute_url())
00026 
00027     def safe_unlock(self):
00028         """Unlock the object if the current user has the lock
00029         """
00030         lockable = ILockable(self.context)
00031         if lockable.can_safely_unlock():
00032             lockable.unlock()
00033 
00034 class LockingInformation(BrowserView):
00035     """Lock information
00036     """
00037 
00038     def is_locked(self):
00039         lockable = ILockable(aq_inner(self.context))
00040         return lockable.locked()
00041 
00042     def is_locked_for_current_user(self):
00043         """True if this object is locked for the current user (i.e. the
00044         current user is not the lock owner)
00045         """
00046         lockable = ILockable(aq_inner(self.context))
00047         # Faster version - we rely on the fact that can_safely_unlock() is
00048         # True even if the object is not locked
00049         return not lockable.can_safely_unlock()
00050         # return lockable.locked() and not lockable.can_safely_unlock()
00051 
00052     def lock_is_stealable(self):
00053         """Find out if the lock is stealable
00054         """
00055         lockable = ILockable(self.context)
00056         return lockable.stealable()
00057 
00058     def lock_info(self):
00059         """Get information about the current lock, a dict containing:
00060 
00061         creator - the id of the user who created the lock
00062         fullname - the full name of the lock creator
00063         author_page - a link to the home page of the author
00064         time - the creation time of the lock
00065         time_difference - a string representing the time since the lock was
00066         acquired.
00067         """
00068 
00069         portal_membership = getToolByName(self.context, 'portal_membership')
00070         portal_url = getToolByName(self.context, 'portal_url')
00071         lockable = ILockable(aq_inner(self.context))
00072         url = portal_url()
00073         for info in lockable.lock_info():
00074             creator = info['creator']
00075             time = info['time']
00076             token = info['token']
00077             lock_type = info['type']
00078             # Get the fullname, but remember that the creator may not
00079             # be a member, but only Authenticated or even anonymous.
00080             # Same for the author_page
00081             fullname = ''
00082             author_page = ''
00083             member = portal_membership.getMemberById(creator)
00084             if member:
00085                 fullname = member.getProperty('fullname', '')
00086                 author_page = "%s/author/%s" % (url, creator)
00087             if fullname == '':
00088                 fullname = creator or _('label_an_anonymous_user',
00089                                         u'an anonymous user')
00090             time_difference = self._getNiceTimeDifference(time)
00091 
00092             return {
00093                 'creator'         : creator,
00094                 'fullname'        : fullname,
00095                 'author_page'     : author_page,
00096                 'time'            : time,
00097                 'time_difference' : time_difference,
00098                 'token'           : token,
00099                 'type'            : lock_type,
00100             }
00101 
00102     def _getNiceTimeDifference(self, baseTime):
00103         now = DateTime()
00104         days = int(round(now - DateTime(baseTime)))
00105         delta = timedelta(now - DateTime(baseTime))
00106         days = delta.days
00107         hours = int(delta.seconds / 3600)
00108         minutes = (delta.seconds - (hours * 3600)) /60
00109 
00110         dateString = ""
00111         if days == 0:
00112             if hours == 0:
00113                 if delta.seconds < 120:
00114                     dateString = "1 minute"
00115                 else:
00116                     dateString = "%s minutes" % minutes
00117             elif hours == 1:
00118                 dateString = "%s hour and %s minutes" % (hours, minutes)
00119             else:
00120                 dateString = "%s hours and %s minutes" % (hours, minutes)
00121         else:
00122             if days == 1:
00123                 dateString = "%s day and %s hours" % (days, hours)
00124             else:
00125                 dateString = "%s days and %s hours" % (days, hours)
00126         return dateString