2013-07-27 11:52:51 +02:00
|
|
|
import os.path
|
2013-07-10 20:04:22 +02:00
|
|
|
import Missing
|
2013-07-08 19:30:33 +02:00
|
|
|
from AccessControl import getSecurityManager
|
2013-07-02 14:49:30 +02:00
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
from five import grok
|
|
|
|
from z3c.table import interfaces
|
|
|
|
from zope.i18nmessageid import MessageFactory
|
|
|
|
from zope.i18n import translate
|
|
|
|
import z3c.table.table
|
|
|
|
import z3c.table.column
|
|
|
|
from Products.CMFCore.WorkflowCore import WorkflowException
|
2013-07-15 18:55:24 +02:00
|
|
|
import plone.api
|
2013-07-02 14:49:30 +02:00
|
|
|
|
|
|
|
from collective.dms.basecontent import _
|
|
|
|
|
|
|
|
PMF = MessageFactory('plone')
|
|
|
|
|
|
|
|
grok.templatedir('templates')
|
|
|
|
|
|
|
|
|
|
|
|
class Column(z3c.table.column.Column, grok.MultiAdapter):
|
|
|
|
grok.baseclass()
|
|
|
|
grok.provides(interfaces.IColumn)
|
|
|
|
|
|
|
|
|
2013-07-10 20:04:22 +02:00
|
|
|
def get_value(item, attribute, default=None):
|
|
|
|
try:
|
2013-10-09 09:27:50 +02:00
|
|
|
value = getattr(item, attribute)
|
2013-07-10 20:04:22 +02:00
|
|
|
if value is Missing.Value:
|
|
|
|
return default
|
|
|
|
except AttributeError:
|
|
|
|
obj = item.getObject()
|
|
|
|
value = getattr(obj, attribute, default)
|
|
|
|
|
|
|
|
if callable(value):
|
|
|
|
value = value()
|
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2013-07-02 14:49:30 +02:00
|
|
|
class DateColumn(Column):
|
|
|
|
grok.baseclass()
|
|
|
|
attribute = NotImplemented
|
|
|
|
|
2013-07-10 20:04:22 +02:00
|
|
|
def renderCell(self, item):
|
|
|
|
value = get_value(item, self.attribute)
|
|
|
|
return self.table.format_date(value)
|
2013-07-02 14:49:30 +02:00
|
|
|
|
|
|
|
|
2013-07-19 20:04:36 +02:00
|
|
|
class DateTimeColumn(Column):
|
|
|
|
grok.baseclass()
|
|
|
|
attribute = NotImplemented
|
|
|
|
|
|
|
|
def renderCell(self, item):
|
|
|
|
value = get_value(item, self.attribute)
|
|
|
|
return self.table.format_date(value, long_format=True)
|
|
|
|
|
|
|
|
|
2013-07-02 14:49:30 +02:00
|
|
|
class PrincipalColumn(Column):
|
|
|
|
grok.baseclass()
|
|
|
|
attribute = NotImplemented
|
|
|
|
|
2013-07-02 16:06:36 +02:00
|
|
|
def renderCell(self, item):
|
2013-07-10 20:04:22 +02:00
|
|
|
value = get_value(item, self.attribute, default=())
|
2013-07-02 16:06:36 +02:00
|
|
|
|
|
|
|
if not isinstance(value, (list, tuple)):
|
|
|
|
value = (value,)
|
|
|
|
|
2013-07-15 18:55:24 +02:00
|
|
|
gtool = getToolByName(plone.api.portal.get(), 'portal_groups')
|
|
|
|
mtool = getToolByName(plone.api.portal.get(), 'portal_membership')
|
2013-07-02 14:49:30 +02:00
|
|
|
principals = []
|
2013-07-02 16:06:36 +02:00
|
|
|
for principal_id in value:
|
|
|
|
user = mtool.getMemberById(principal_id)
|
2013-07-02 14:49:30 +02:00
|
|
|
if user is not None:
|
|
|
|
principals.append(user.getProperty('fullname', None) or user.getId())
|
|
|
|
else:
|
|
|
|
group = gtool.getGroupById(principal_id)
|
|
|
|
if group is not None:
|
|
|
|
principals.append(group.getProperty('title', None) or group.getId())
|
|
|
|
|
|
|
|
return ', '.join(principals).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
class LinkColumn(z3c.table.column.LinkColumn, Column):
|
|
|
|
grok.baseclass()
|
|
|
|
|
|
|
|
def getLinkURL(self, item):
|
|
|
|
"""Setup link url."""
|
|
|
|
if self.linkName is not None:
|
|
|
|
return '%s/%s' % (item.getURL(), self.linkName)
|
|
|
|
return item.getURL()
|
|
|
|
|
|
|
|
|
|
|
|
class TitleColumn(LinkColumn):
|
|
|
|
grok.baseclass()
|
|
|
|
header = PMF("Title")
|
|
|
|
weight = 10
|
|
|
|
|
|
|
|
def getLinkContent(self, item):
|
2013-07-15 18:55:24 +02:00
|
|
|
title = get_value(item, 'Title')
|
2013-07-16 12:53:27 +02:00
|
|
|
if isinstance(title, unicode):
|
2013-07-15 18:55:24 +02:00
|
|
|
return title
|
2013-07-15 15:09:09 +02:00
|
|
|
else:
|
2013-07-16 11:22:54 +02:00
|
|
|
return unicode(title, 'utf-8', 'ignore')
|
2013-07-02 14:49:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
class IconColumn(object):
|
2014-02-05 17:34:00 +01:00
|
|
|
cssClasses = {'td': 'icon-column'}
|
|
|
|
|
2013-07-02 14:49:30 +02:00
|
|
|
def getLinkContent(self, item):
|
2013-07-19 19:47:51 +02:00
|
|
|
content = super(IconColumn, self).getLinkContent(item)
|
2013-07-02 14:49:30 +02:00
|
|
|
return u"""<img title="%s" src="%s" />""" % (
|
2013-07-19 19:47:51 +02:00
|
|
|
content,
|
2013-07-02 14:49:30 +02:00
|
|
|
'%s/%s' % (self.table.portal_url, self.iconName))
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteColumn(IconColumn, LinkColumn):
|
|
|
|
grok.baseclass()
|
|
|
|
header = u""
|
|
|
|
weight = 9
|
|
|
|
linkName = "delete_confirmation"
|
|
|
|
linkContent = PMF('Delete')
|
|
|
|
linkCSS = 'edm-delete-popup'
|
2014-01-22 15:51:09 +01:00
|
|
|
iconName = "++resource++delete_icon.png"
|
2013-07-02 14:49:30 +02:00
|
|
|
linkContent = PMF(u"Delete")
|
|
|
|
|
2013-07-21 11:33:22 +02:00
|
|
|
def actionAvailable(self, item):
|
2013-07-08 19:30:33 +02:00
|
|
|
obj = item.getObject()
|
|
|
|
sm = getSecurityManager()
|
2013-07-21 11:33:22 +02:00
|
|
|
return sm.checkPermission('Delete objects', obj)
|
|
|
|
|
|
|
|
def renderCell(self, item):
|
|
|
|
if not self.actionAvailable(item):
|
2013-07-08 19:30:33 +02:00
|
|
|
return u""
|
|
|
|
|
|
|
|
return super(DeleteColumn, self).renderCell(item)
|
|
|
|
|
2013-07-02 14:49:30 +02:00
|
|
|
|
|
|
|
class DownloadColumn(IconColumn, LinkColumn):
|
|
|
|
grok.baseclass()
|
|
|
|
header = u""
|
|
|
|
weight = 1
|
|
|
|
linkName = "@@download"
|
|
|
|
iconName = "download_icon.png"
|
|
|
|
linkContent = _(u"Download file")
|
|
|
|
|
|
|
|
|
2013-07-08 19:30:59 +02:00
|
|
|
class ExternalEditColumn(IconColumn, LinkColumn):
|
|
|
|
grok.baseclass()
|
|
|
|
header = u""
|
|
|
|
weight = 3
|
|
|
|
linkName = "@@external_edit"
|
2013-10-11 15:37:05 +02:00
|
|
|
iconName = "++resource++extedit_icon.png"
|
2013-07-08 19:30:59 +02:00
|
|
|
linkContent = PMF(u"Edit with external application")
|
|
|
|
|
2013-07-21 11:33:22 +02:00
|
|
|
def actionAvailable(self, item):
|
2013-07-08 19:30:59 +02:00
|
|
|
obj = item.getObject()
|
|
|
|
sm = getSecurityManager()
|
|
|
|
if not sm.checkPermission('Modify portal content', obj):
|
2013-07-21 11:33:22 +02:00
|
|
|
return False
|
2013-07-08 19:30:59 +02:00
|
|
|
|
2013-07-27 11:52:51 +02:00
|
|
|
if obj.file is None:
|
|
|
|
return False
|
|
|
|
|
|
|
|
ext = os.path.splitext(obj.file.filename)[-1].lower()
|
|
|
|
if ext in (u'.pdf', u'.jpg', '.jpeg'):
|
|
|
|
return False
|
|
|
|
|
2013-07-08 19:30:59 +02:00
|
|
|
if not obj.restrictedTraverse('@@externalEditorEnabled').available():
|
2013-07-21 11:33:22 +02:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def renderCell(self, item):
|
|
|
|
if not self.actionAvailable(item):
|
2013-07-08 19:30:59 +02:00
|
|
|
return u""
|
|
|
|
|
|
|
|
return super(ExternalEditColumn, self).renderCell(item)
|
|
|
|
|
|
|
|
|
|
|
|
class EditColumn(IconColumn, LinkColumn):
|
|
|
|
grok.baseclass()
|
|
|
|
header = u""
|
|
|
|
weight = 2
|
|
|
|
linkName = "edit"
|
|
|
|
iconName = "edit.png"
|
|
|
|
linkContent = PMF(u"Edit")
|
|
|
|
linkCSS = 'overlay-form-reload'
|
|
|
|
|
2013-07-21 11:33:22 +02:00
|
|
|
def actionAvailable(self, item):
|
2013-07-08 19:30:59 +02:00
|
|
|
obj = item.getObject()
|
|
|
|
sm = getSecurityManager()
|
2013-07-21 11:33:22 +02:00
|
|
|
return sm.checkPermission('Modify portal content', obj)
|
|
|
|
|
|
|
|
def renderCell(self, item):
|
|
|
|
if not self.actionAvailable(item):
|
2013-07-08 19:30:59 +02:00
|
|
|
return u""
|
|
|
|
|
|
|
|
return super(EditColumn, self).renderCell(item)
|
|
|
|
|
|
|
|
|
2013-07-02 14:49:30 +02:00
|
|
|
class StateColumn(Column):
|
|
|
|
grok.baseclass()
|
|
|
|
header = PMF(u"State")
|
|
|
|
weight = 50
|
|
|
|
|
2013-07-02 16:06:36 +02:00
|
|
|
def renderCell(self, item):
|
2013-07-02 14:49:30 +02:00
|
|
|
try:
|
|
|
|
wtool = self.table.wtool
|
2013-07-15 18:55:24 +02:00
|
|
|
portal_type = get_value(item, 'portal_type')
|
|
|
|
review_state = get_value(item, 'review_state')
|
2013-07-21 10:58:25 +02:00
|
|
|
if not review_state:
|
|
|
|
return u""
|
2013-07-15 18:55:24 +02:00
|
|
|
state_title = wtool.getTitleForStateOnType(review_state,
|
|
|
|
portal_type)
|
2013-07-02 14:49:30 +02:00
|
|
|
return translate(PMF(state_title), context=self.request)
|
|
|
|
except WorkflowException:
|
|
|
|
return u""
|
2013-10-09 09:27:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LabelColumn(Column):
|
|
|
|
grok.baseclass()
|
|
|
|
attribute = NotImplemented
|
|
|
|
|
|
|
|
def renderCell(self, item):
|
|
|
|
value = get_value(item, self.attribute)
|
|
|
|
if value is None:
|
|
|
|
value = ''
|
|
|
|
return value
|