core.models.Attachment module

Module with the Attachment model class.

class core.models.Attachment.Attachment(*args, **kwargs)[source]

Bases: ExportModelOperationsMixin('attachment'), DownloadMixin, ThumbnailMixin, URLMixin, FavoriteModelMixin, FilePathModelMixin, TimestampModelMixin, Model

Database model for an attachment file in a mail.

BASENAME = 'attachment'
DELETE_NOTICE = 'This will only delete the record of this attachment, not of the email.'
DELETE_NOTICE_PLURAL = 'This will only delete the records of these attachments, not of their emails.'
file_name

The filename of the attachment.

content_disposition

The disposition of the file. Typically ‘attachment’, ‘inline’ or ‘’.

content_id

The MIME subtype of the file.

content_maintype

The MIME maintype of the file.

content_subtype

The MIME subtype of the file.

datasize

The filesize of the attachment.

email: ForeignKey

The mail that the attachment was found in. Deletion of that email deletes this attachment.

__str__()[source]

Returns a string representation of the model data.

Return type:

str

Returns:

The string representation of the attachment, using file_name and email.

save(*args, **kwargs)[source]

Extended :django:django.models.Model.save() method.

Saves the data to storage if configured.

Return type:

None

Parameters:
_get_storage_file_name()[source]

Create the filename for the stored attachment.

Return type:

str

share_to_paperless()[source]

Sends this attachment to the Paperless server of its user.

Return type:

str

Returns:

The uuid string of the Paperless consumer task for the document.

Raises:
exception DoesNotExist

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned

Bases: MultipleObjectsReturned

_meta = <Options for Attachment>
created

The datetime the model instance was created. Is set automatically.

email_id
file_path

The relative path in the storage where the file is stored. Can be null if no file has been saved (null does not collide with the unique constraint.).

get_next_by_created(*, field=<django.db.models.fields.DateTimeField: created>, is_next=True, **kwargs)
get_next_by_updated(*, field=<django.db.models.fields.DateTimeField: updated>, is_next=True, **kwargs)
get_previous_by_created(*, field=<django.db.models.fields.DateTimeField: created>, is_next=False, **kwargs)
get_previous_by_updated(*, field=<django.db.models.fields.DateTimeField: updated>, is_next=False, **kwargs)
id

A wrapper for a deferred-loading field. When the value is read from this object the first time, the query is executed.

is_favorite

Flags favorite accounts. False by default.

objects = <django.db.models.manager.Manager object>
share_to_immich()[source]

Sends this attachment to the Immich server of its user.

Return type:

str

Returns:

The response by Immich with the id string of the stored immich image.

Raises:
updated

The datetime the model instance entry was last updated. Is set automatically.

property has_thumbnail: bool

Whether the attachment has a mimetype that can be embedded into html.

References

https://stackoverflow.com/questions/51107683/which-mime-types-can-be-displayed-in-browser

property thumbnail: str[source]

Builds the html thumbnail for the attachment.

Returns:

The html for the thumbnail. The empty string if the attachment has no thumbnail.

property content_type: str

Reconstructs the full MIME content type of the attachment.

Returns:

The attachments content type if known, else “”.

property is_shareable_to_paperless: bool

Whether the attachment has a mimetype that can be processed by a paperless server.

References

https://docs.paperless-ngx.com/faq/#what-file-types-does-paperless-ngx-support

property is_shareable_to_immich: bool

Whether the attachment has a mimetype that can be processed by a Immich server.

References

https://immich.app/docs/features/supported-formats/

classmethod create_from_email_message(email_message, email)[source]

Creates :class:`core.models.Attachment`s from an email message.

Parameters:
  • email_message (EmailMessage) – The email_message to get and create all attachments from.

  • email (Email) – The email model created from the email_message.

Return type:

list[Attachment]

Returns:

A list of core.models.Attachment in the email message.

static queryset_as_file(queryset)[source]

Processes the files of the emails in the queryset into a temporary file.

Parameters:

queryset (QuerySet) – The email queryset to compile into a file.

Return type:

_TemporaryFileWrapper

Returns:

The temporary file wrapper.

Raises:

Attachment.DoesNotExist – If the queryset is empty.