/usr/share/pyshared/zope/app/container/browser/contents.py is in python-zope.app.container 3.9.2-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | ##############################################################################
#
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""View Class for the Container's Contents view.
"""
__docformat__ = 'restructuredtext'
import urllib
from zope.component import queryMultiAdapter
from zope.event import notify
from zope.exceptions.interfaces import UserError
from zope.security.interfaces import Unauthorized
from zope.security import canWrite
from zope.size.interfaces import ISized
from zope.traversing.interfaces import TraversalError
from zope.publisher.browser import BrowserView
from zope.browserpage.viewpagetemplatefile import ViewPageTemplateFile
from zope.dublincore.interfaces import IZopeDublinCore
from zope.dublincore.interfaces import IDCDescriptiveProperties
from zope.copypastemove.interfaces import IPrincipalClipboard
from zope.copypastemove.interfaces import IObjectCopier, IObjectMover
from zope.copypastemove.interfaces import IContainerItemRenamer
from zope.annotation.interfaces import IAnnotations
from zope.lifecycleevent import ObjectModifiedEvent, Attributes
from zope.traversing.api import getName, getPath, joinPath, traverse
from zope.app.container.i18n import ZopeMessageFactory as _
from zope.app.container.browser.adding import Adding
from zope.app.container.interfaces import IContainer, DuplicateIDError
from zope.app.container.interfaces import IContainerNamesContainer
class Contents(BrowserView):
__used_for__ = IContainer
error = ''
message = ''
normalButtons = False
specialButtons = False
supportsRename = False
def listContentInfo(self):
request = self.request
if "container_cancel_button" in request:
if "type_name" in request:
del request.form['type_name']
if "rename_ids" in request and "new_value" in request:
del request.form['rename_ids']
if "retitle_id" in request and "new_value" in request:
del request.form['retitle_id']
return self._normalListContentsInfo()
elif "container_rename_button" in request and not request.get("ids"):
self.error = _("You didn't specify any ids to rename.")
elif "container_add_button" in request:
if "single_type_name" in request \
and "single_new_value" in request:
request.form['type_name'] = request['single_type_name']
request.form['new_value'] = request['single_new_value']
self.addObject()
elif 'single_type_name' in request \
and 'single_new_value' not in request:
request.form['type_name'] = request['single_type_name']
request.form['new_value'] = ""
self.addObject()
elif "type_name" in request and "new_value" in request:
self.addObject()
elif "rename_ids" in request and "new_value" in request:
self.renameObjects()
elif "retitle_id" in request and "new_value" in request:
self.changeTitle()
elif "container_cut_button" in request:
self.cutObjects()
elif "container_copy_button" in request:
self.copyObjects()
elif "container_paste_button" in request:
self.pasteObjects()
elif "container_delete_button" in request:
self.removeObjects()
else:
return self._normalListContentsInfo()
if self.error:
return self._normalListContentsInfo()
status = request.response.getStatus()
if status not in (302, 303):
# Only redirect if nothing else has
request.response.redirect(request.URL)
return ()
def normalListContentInfo(self):
return self._normalListContentsInfo()
def _normalListContentsInfo(self):
request = self.request
self.specialButtons = (
'type_name' in request or
'rename_ids' in request or
('container_rename_button' in request
and request.get("ids")) or
'retitle_id' in request
)
self.normalButtons = not self.specialButtons
info = map(self._extractContentInfo, self.context.items())
self.supportsCut = info
self.supportsCopy = info
self.supportsDelete = info
self.supportsPaste = self.pasteable()
self.supportsRename = (
self.supportsCut and
not IContainerNamesContainer.providedBy(self.context)
)
return info
def _extractContentInfo(self, item):
request = self.request
rename_ids = {}
if "container_rename_button" in request:
for rename_id in request.get('ids', ()):
rename_ids[rename_id] = rename_id
elif "rename_ids" in request:
for rename_id in request.get('rename_ids', ()):
rename_ids[rename_id] = rename_id
retitle_id = request.get('retitle_id')
id, obj = item
info = {}
info['id'] = info['cb_id'] = id
info['object'] = obj
info['url'] = urllib.quote(id.encode('utf-8'))
info['rename'] = rename_ids.get(id)
info['retitle'] = id == retitle_id
zmi_icon = queryMultiAdapter((obj, self.request), name='zmi_icon')
if zmi_icon is None:
info['icon'] = None
else:
info['icon'] = zmi_icon()
dc = IZopeDublinCore(obj, None)
if dc is not None:
info['retitleable'] = canWrite(dc, 'title')
info['plaintitle'] = not info['retitleable']
title = self.safe_getattr(dc, 'title', None)
if title:
info['title'] = title
formatter = self.request.locale.dates.getFormatter(
'dateTime', 'short')
created = self.safe_getattr(dc, 'created', None)
if created is not None:
info['created'] = formatter.format(created)
modified = self.safe_getattr(dc, 'modified', None)
if modified is not None:
info['modified'] = formatter.format(modified)
else:
info['retitleable'] = 0
info['plaintitle'] = 1
sized_adapter = ISized(obj, None)
if sized_adapter is not None:
info['size'] = sized_adapter
return info
def safe_getattr(self, obj, attr, default):
"""Attempts to read the attr, returning default if Unauthorized."""
try:
return getattr(obj, attr, default)
except Unauthorized:
return default
def renameObjects(self):
"""Given a sequence of tuples of old, new ids we rename"""
request = self.request
ids = request.get("rename_ids")
newids = request.get("new_value")
renamer = IContainerItemRenamer(self.context)
for oldid, newid in zip(ids, newids):
if newid != oldid:
renamer.renameItem(oldid, newid)
def changeTitle(self):
"""Given a sequence of tuples of old, new ids we rename"""
request = self.request
id = request.get("retitle_id")
new = request.get("new_value")
item = self.context[id]
dc = IDCDescriptiveProperties(item)
dc.title = new
notify(ObjectModifiedEvent(item, Attributes(IZopeDublinCore, 'title')))
def hasAdding(self):
"""Returns true if an adding view is available."""
adding = queryMultiAdapter((self.context, self.request), name="+")
return (adding is not None)
def addObject(self):
request = self.request
if IContainerNamesContainer.providedBy(self.context):
new = ""
else:
new = request["new_value"]
adding = queryMultiAdapter((self.context, self.request), name="+")
if adding is None:
adding = Adding(self.context, request)
else:
# Set up context so that the adding can build a url
# if the type name names a view.
# Note that we can't so this for the "adding is None" case
# above, because there is no "+" view.
adding.__parent__ = self.context
adding.__name__ = '+'
adding.action(request['type_name'], new)
def removeObjects(self):
"""Remove objects specified in a list of object ids"""
request = self.request
ids = request.get('ids')
if not ids:
self.error = _("You didn't specify any ids to remove.")
return
container = self.context
for id in ids:
del container[id]
def copyObjects(self):
"""Copy objects specified in a list of object ids"""
request = self.request
ids = request.get('ids')
if not ids:
self.error = _("You didn't specify any ids to copy.")
return
container_path = getPath(self.context)
# For each item, check that it can be copied; if so, save the
# path of the object for later copying when a destination has
# been selected; if not copyable, provide an error message
# explaining that the object can't be copied.
items = []
for id in ids:
ob = self.context[id]
copier = IObjectCopier(ob)
if not copier.copyable():
m = {"name": id}
title = getDCTitle(ob)
if title:
m["title"] = title
self.error = _(
"Object '${name}' (${title}) cannot be copied",
mapping=m)
else:
self.error = _("Object '${name}' cannot be copied",
mapping=m)
return
items.append(joinPath(container_path, id))
# store the requested operation in the principal annotations:
clipboard = getPrincipalClipboard(self.request)
clipboard.clearContents()
clipboard.addItems('copy', items)
def cutObjects(self):
"""move objects specified in a list of object ids"""
request = self.request
ids = request.get('ids')
if not ids:
self.error = _("You didn't specify any ids to cut.")
return
container_path = getPath(self.context)
# For each item, check that it can be moved; if so, save the
# path of the object for later moving when a destination has
# been selected; if not movable, provide an error message
# explaining that the object can't be moved.
items = []
for id in ids:
ob = self.context[id]
mover = IObjectMover(ob)
if not mover.moveable():
m = {"name": id}
title = getDCTitle(ob)
if title:
m["title"] = title
self.error = _(
"Object '${name}' (${title}) cannot be moved",
mapping=m)
else:
self.error = _("Object '${name}' cannot be moved",
mapping=m)
return
items.append(joinPath(container_path, id))
# store the requested operation in the principal annotations:
clipboard = getPrincipalClipboard(self.request)
clipboard.clearContents()
clipboard.addItems('cut', items)
def pasteable(self):
"""Decide if there is anything to paste
"""
target = self.context
clipboard = getPrincipalClipboard(self.request)
items = clipboard.getContents()
for item in items:
try:
obj = traverse(target, item['target'])
except TraversalError:
pass
else:
if item['action'] == 'cut':
mover = IObjectMover(obj)
moveableTo = self.safe_getattr(mover, 'moveableTo', None)
if moveableTo is None or not moveableTo(target):
return False
elif item['action'] == 'copy':
copier = IObjectCopier(obj)
copyableTo = self.safe_getattr(copier, 'copyableTo', None)
if copyableTo is None or not copyableTo(target):
return False
else:
raise
return True
def pasteObjects(self):
"""Paste ojects in the user clipboard to the container
"""
target = self.context
clipboard = getPrincipalClipboard(self.request)
items = clipboard.getContents()
moved = False
not_pasteable_ids = []
for item in items:
duplicated_id = False
try:
obj = traverse(target, item['target'])
except TraversalError:
pass
else:
if item['action'] == 'cut':
mover = IObjectMover(obj)
try:
mover.moveTo(target)
moved = True
except DuplicateIDError:
duplicated_id = True
elif item['action'] == 'copy':
copier = IObjectCopier(obj)
try:
copier.copyTo(target)
except DuplicateIDError:
duplicated_id = True
else:
raise
if duplicated_id:
not_pasteable_ids.append(getName(obj))
if moved:
# Clear the clipboard if we do a move, but not if we only do a copy
clipboard.clearContents()
if not_pasteable_ids != []:
# Show the ids of objects that can't be pasted because
# their ids are already taken.
# TODO Can't we add a 'copy_of' or something as a prefix
# instead of raising an exception ?
raise UserError(
_("The given name(s) %s is / are already being used" %(
str(not_pasteable_ids))))
def hasClipboardContents(self):
"""Interogate the ``PrinicipalAnnotation`` to see if clipboard
contents exist."""
if not self.supportsPaste:
return False
# touch at least one item to in clipboard confirm contents
clipboard = getPrincipalClipboard(self.request)
items = clipboard.getContents()
for item in items:
try:
traverse(self.context, item['target'])
except TraversalError:
pass
else:
return True
return False
contents = ViewPageTemplateFile('contents.pt')
contentsMacros = contents
_index = ViewPageTemplateFile('index.pt')
def index(self):
if 'index.html' in self.context:
self.request.response.redirect('index.html')
return ''
return self._index()
class JustContents(Contents):
"""Like Contents, but does't delegate to item named index.html"""
def index(self):
return self._index()
def getDCTitle(ob):
dc = IDCDescriptiveProperties(ob, None)
if dc is None:
return None
else:
return dc.title
def getPrincipalClipboard(request):
"""Return the clipboard based on the request."""
user = request.principal
annotations = IAnnotations(user)
return IPrincipalClipboard(annotations)
|