trac.attachment – Attachments for Trac resources¶
This module contains the Attachment model class and the
AttachmentModule component which manages file attachments for any
kind of Trac resources. Currently, the wiki pages, tickets and
milestones all support file attachments. You can use the same utility
methods from the AttachmentModule as they do for easily adding
attachments to other kinds of resources.
See also the
attach_file_form.html
and
attachment.html templates
which can be used to display the attachments.
Interfaces¶
-
class
trac.attachment.IAttachmentChangeListener¶ Bases:
trac.core.InterfaceExtension point interface for components that require notification when attachments are created, deleted, renamed or reparented.
See also trac.attachment.IAttachmentChangeListener extension point
-
attachment_added()¶ Called when an attachment is added.
-
attachment_deleted()¶ Called when an attachment is deleted.
-
attachment_moved(old_parent_realm, old_parent_id, old_filename)¶ Called when an attachment is moved.
-
-
class
trac.attachment.IAttachmentManipulator¶ Bases:
trac.core.InterfaceExtension point interface for components that need to manipulate attachments.
Unlike change listeners, a manipulator can reject changes being committed to the database.
See also trac.attachment.IAttachmentManipulator extension point
-
prepare_attachment(attachment, fields)¶ Not currently called, but should be provided for future compatibility.
-
validate_attachment(attachment)¶ Validate an attachment after upload but before being stored in Trac environment.
Must return a list of
(field, message)tuples, one for each problem detected.fieldcan be any ofdescription,username,filename,content, orNoneto indicate an overall problem with the attachment. Therefore, a return value of[]means everything is OK.
-
-
class
trac.attachment.ILegacyAttachmentPolicyDelegate¶ Bases:
trac.core.InterfaceInterface that can be used by plugins to seamlessly participate to the legacy way of checking for attachment permissions.
This should no longer be necessary once it becomes easier to setup fine-grained permissions in the default permission store.
See also trac.attachment.ILegacyAttachmentPolicyDelegate extension point
Classes¶
-
class
trac.attachment.Attachment(env, parent_realm_or_attachment_resource, parent_id=None, filename=None)¶ Bases:
objectRepresents an attachment (new or existing).
-
delete()¶ Delete the attachment, both the record in the database and the file itself.
-
move(new_realm=None, new_id=None, new_filename=None)¶ Move the attachment, changing one or more of its parent realm, parent id and filename.
The new parent resource must exist.
Since: 1.3.2
-
insert(filename, fileobj, size, t=None)¶ Create a new Attachment record and save the file content.
-
classmethod
select(env, parent_realm, parent_id)¶ Iterator yielding all
Attachmentinstances attached to resource identified byparent_realmandparent_id.Returns: a tuple containing the filename,description,size,timeandauthor.
-
classmethod
delete_all(env, parent_realm, parent_id)¶ Delete all attachments of a given resource.
-
classmethod
reparent_all(env, parent_realm, parent_id, new_realm, new_id)¶ Reparent all attachments of a given resource to another resource.
-
Components¶
-
class
trac.attachment.AttachmentModule¶ Bases:
trac.core.Component-
change_listeners¶ List of components that implement
IAttachmentChangeListener
-
manipulators¶ List of components that implement
IAttachmentManipulator
-
max_size¶ Maximum allowed file size (in bytes) for attachments.
-
max_zip_size¶ Maximum allowed total size (in bytes) for an attachment list to be downloadable as a
zip. Set this to -1 to disable download aszip. (‘’since 1.0’’)
-
render_unsafe_content¶ Whether attachments should be rendered in the browser, or only made downloadable.
Pretty much any file may be interpreted as HTML by the browser, which allows a malicious user to attach a file containing cross-site scripting attacks.
For public sites where anonymous users can create attachments it is recommended to leave this option disabled.
-
viewable_attachments(context)¶ Return the list of viewable attachments in the given context.
Parameters: context – the RenderingContextcorresponding to the parentResourcefor the attachments
-
attachment_data(context)¶ Return a data dictionary describing the list of viewable attachments in the current context.
-
get_history(start, stop, realm)¶ Return an iterable of tuples describing changes to attachments on a particular object realm.
The tuples are in the form (change, realm, id, filename, time, description, author).
changecan currently only becreated.FIXME: no iterator
-
get_timeline_events(req, resource_realm, start, stop)¶ Return an event generator suitable for ITimelineEventProvider.
Events are changes to attachments on resources of the given
resource_realm.realm.
-
get_search_results(req, resource_realm, terms)¶ Return a search result generator suitable for ISearchSource.
Search results are attachments on resources of the given
resource_realm.realmwhose filename, description or author match the given terms.
-
-
class
trac.attachment.AttachmentAdmin¶ Bases:
trac.core.Componenttrac-admin command provider for attachment administration.