Back to index

plone3  3.1.7
content.py
Go to the documentation of this file.
00001 from AccessControl import getSecurityManager
00002 from Acquisition import aq_inner
00003 
00004 from zope.component import getMultiAdapter, queryMultiAdapter
00005 from plone.memoize.instance import memoize
00006 
00007 from plone.app.layout.viewlets import ViewletBase
00008 
00009 from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
00010 from Products.CMFCore.utils import getToolByName
00011 
00012 from Products.CMFPlone import PloneMessageFactory as _
00013 
00014 from Products.CMFCore.WorkflowCore import WorkflowException
00015 from Products.CMFPlone.utils import log
00016 import logging
00017 
00018 
00019 class DocumentActionsViewlet(ViewletBase):
00020     def update(self):
00021         super(DocumentActionsViewlet, self).update()
00022 
00023         self.context_state = getMultiAdapter((self.context, self.request),
00024                                              name=u'plone_context_state')
00025         plone_utils = getToolByName(self.context, 'plone_utils')
00026         self.getIconFor = plone_utils.getIconFor
00027         self.actions = self.context_state.actions().get('document_actions', None)
00028 
00029     index = ViewPageTemplateFile("document_actions.pt")
00030 
00031 
00032 class DocumentBylineViewlet(ViewletBase):
00033     def update(self):
00034         super(DocumentBylineViewlet, self).update()
00035         self.context_state = getMultiAdapter((self.context, self.request),
00036                                              name=u'plone_context_state')
00037         self.tools = getMultiAdapter((self.context, self.request),
00038                                      name='plone_tools')
00039 
00040     @memoize
00041     def show(self):
00042         properties = self.tools.properties()
00043         site_properties = getattr(properties, 'site_properties')
00044         anonymous = self.portal_state.anonymous()
00045         allowAnonymousViewAbout = site_properties.getProperty('allowAnonymousViewAbout', True)
00046         return not anonymous or allowAnonymousViewAbout
00047 
00048     @memoize
00049     def locked_icon(self):
00050         if not getSecurityManager().checkPermission('Modify portal content',
00051                                                     self.context):
00052             return ""
00053 
00054         locked = False
00055         lock_info = queryMultiAdapter((self.context, self.request),
00056                                       name='plone_lock_info')
00057         if lock_info is not None:
00058             locked = lock_info.is_locked()
00059         else:
00060             context = aq_inner(self.context)
00061             lockable = getattr(context.aq_explicit, 'wl_isLocked', None) is not None
00062             locked = lockable and context.wl_isLocked()
00063 
00064         if not locked:
00065             return ""
00066 
00067         portal = self.portal_state.portal()
00068         icon = portal.restrictedTraverse('lock_icon.gif')
00069         return icon.tag(title='Locked')
00070 
00071     @memoize
00072     def creator(self):
00073         return self.context.Creator()
00074 
00075     @memoize
00076     def author(self):
00077         membership = self.tools.membership()
00078         return membership.getMemberInfo(self.creator())
00079 
00080     @memoize
00081     def authorname(self):
00082         author = self.author()
00083         return author and author['fullname'] or self.creator()
00084 
00085     @memoize
00086     def isExpired(self):
00087         portal = self.portal_state.portal()
00088         return portal.restrictedTraverse('isExpired')(self.context)
00089 
00090     @memoize
00091     def toLocalizedTime(self, time, long_format=None):
00092         """Convert time to localized time
00093         """
00094         util = getToolByName(self.context, 'translation_service')
00095         return util.ulocalized_time(time, long_format, self.context,
00096                                     domain='plonelocales')
00097 
00098     index = ViewPageTemplateFile("document_byline.pt")
00099 
00100 
00101 class WorkflowHistoryViewlet(ViewletBase):
00102     def update(self):
00103         super(WorkflowHistoryViewlet, self).update()
00104         self.tools = getMultiAdapter((self.context, self.request),
00105                                      name='plone_tools')
00106     @memoize
00107     def workflowHistory(self):
00108         """Return workflow history of this context.
00109 
00110         Taken from plone_scripts/getWorkflowHistory.py
00111         """
00112         workflow = self.tools.workflow()
00113         membership = self.tools.membership()
00114 
00115         history = []
00116 
00117         # check if the current user has the proper permissions
00118         if (membership.checkPermission('Request review', self.context) or
00119             membership.checkPermission('Review portal content', self.context)):
00120             try:
00121                 # get total history
00122                 review_history = workflow.getInfoFor(self.context, 'review_history')
00123 
00124                 # filter out the irrelevant stuff
00125                 review_history = [r for r in review_history if r['action']]
00126                 for r in review_history:
00127                     r['transition_title'] = workflow.getTitleForTransitionOnType(r['action'],
00128                                                                                  self.context.portal_type)
00129                     actorid = r['actor']
00130                     r['actorid'] = actorid
00131                     if actorid is None:
00132                         # action performed by an anonymous user
00133                         r['actor'] = {'username': _(u'label_anonymous_user', default=u'Anonymous User')}
00134                         r['actor_home'] = ''
00135                     else:
00136                         r['actor'] = membership.getMemberInfo(actorid)
00137                         if r['actor'] is not None:
00138                             r['actor_home'] = self.site_url + '/author/' + actorid
00139                         else:
00140                             # member info is not available
00141                             # the user was probably deleted
00142                             r['actor_home'] = ''
00143                 history = review_history
00144                 history.reverse()
00145 
00146             except WorkflowException:
00147                 log( 'plone.app.layout.viewlets.content: '
00148                      '%s has no associated workflow' % self.context.absolute_url(), severity=logging.DEBUG)
00149 
00150         return history
00151 
00152     index = ViewPageTemplateFile("review_history.pt")