Media

This package contains all media-related models: MediaFile and Directory

Directory

class integreat_cms.cms.models.media.directory.Directory(*args, **kwargs)[source]

Bases: AbstractBaseModel

Model representing a directory containing documents. This is only a virtual directory and does not necessarily exist on the actual file system. Each directory is tied to a region.

Parameters:
  • id (BigAutoField) – Primary key: ID

  • name (CharField) – Name

  • created_date (DateTimeField) – Creation date. The date and time when the directory was created

  • is_hidden (BooleanField) – Hidden. Whether the directory is hidden in the regional media library

Relationship fields:

Parameters:

Reverse relationships:

Parameters:
exception DoesNotExist[source]

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned[source]

Bases: MultipleObjectsReturned

get_repr() str[source]

This overwrites the default Django __repr__() method which would return <Directory: Directory object (id)>. It is used for logging.

Returns:

The canonical string representation of the directory

Return type:

str

classmethod search(region: Region, query: str) QuerySet[Directory][source]

Searches for all directories which match the given query in their name.

Parameters:
  • region (Region) – The searched region

  • query (str) – The query string used for filtering the regions

Returns:

A query for all matching objects

Return type:

QuerySet[Directory]

serialize() dict[str, Any][source]

This method creates a serialized version of that object for later use in AJAX and JSON.

Returns:

The serialized representation of the directory

Return type:

dict[str, Any]

Media File

This module contains the MediaFile model as well as the helper functions upload_path() and upload_path_thumbnail() which are used to determine the file system path to which the files should be uploaded.

class integreat_cms.cms.models.media.media_file.MediaFile(*args, **kwargs)[source]

Bases: AbstractBaseModel

The MediaFile model is used to store basic information about files which are uploaded to the CMS. This is only a virtual document and does not necessarily exist on the actual file system. Each document is tied to a region via its directory.

Parameters:
  • id (BigAutoField) – Primary key: ID

  • file (FileField) – File

  • thumbnail (FileField) – Thumbnail file

  • file_size (IntegerField) – File size

  • type (CharField) – File type

  • name (CharField) – Name

  • alt_text (CharField) – Description

  • uploaded_date (DateTimeField) – Uploaded date. The date and time when the media file was uploaded

  • last_modified (DateTimeField) – Last modified. The date and time when the physical media file was last modified

  • is_hidden (BooleanField) – Hidden. Whether the media file is hidden in the regional media library

Relationship fields:

Parameters:

Reverse relationships:

Parameters:
  • icon_regions (Reverse ForeignKey from Region) – All icon regions of this media file (related name of icon)

  • icon_organizations (Reverse ForeignKey from Organization) – All icon organizations of this media file (related name of icon)

  • pois (Reverse ForeignKey from POI) – All pois of this media file (related name of icon)

  • events (Reverse ForeignKey from Event) – All events of this media file (related name of icon)

  • pages (Reverse ForeignKey from Page) – All pages of this media file (related name of icon)

exception DoesNotExist[source]

Bases: ObjectDoesNotExist

exception MultipleObjectsReturned[source]

Bases: MultipleObjectsReturned

content_usages[source]

Check where this media file is embedded in the content

Returns:

list with the search result

get_repr() str[source]

This overwrites the default Django __repr__() method which would return <MediaFile: MediaFile object (id)>. It is used for logging.

Returns:

The canonical string representation of the document

Return type:

str

icon_usages[source]

Check where a media file is used as icon

Returns:

List of all objects that use this file as icon

is_deletable[source]

Check if a media file deletable

Returns:

Whether a file is deletable

is_embedded[source]

Check if a media file is embedded in the content

Returns:

Whether a file is embedded

is_icon[source]

Check if a media file is used as icon

Returns:

Whether a file is an icon

is_only_used_in_past_events[source]

Check if a media file is used in past events only

Returns:

if a media file is only used in past events

is_used[source]

Check if a media file is used

Returns:

Whether a file is used

objects = <django.db.models.manager.ManagerFromMediaFileQuerySet object>[source]

Django manager to access the ORM Use MediaFile.objects.all() to fetch all objects.

Custom model manager for media file objects

past_event_usages[source]

Count in how many past events this file is used

Returns:

count of usages in past events

classmethod search(region: Region, query: str) QuerySet[source]

Searches for all media files which match the given query in their name.

Parameters:
  • region (Region) – The searched region

  • query (str) – The query string used for filtering the media file

Returns:

A queryset for all matching objects

Return type:

QuerySet

serialize() dict[str, Any][source]

This methods creates a serialized string of that document. This can later be used in the AJAX calls.

Returns:

A serialized dictionary representation of the document for JSON concatenation

Return type:

dict[str, Any]

serialize_usages() dict[str, Any][source]

This methods creates a serialized dict of the file’s usages. This can later be used in the AJAX calls.

Returns:

A serialized dictionary representation of the file’s usages

Return type:

dict[str, Any]

property thumbnail_url: str | None[source]

Return the path of the image that should be used as the thumbnail.

Returns:

The path of the file. If the file is an image and no thumbnail could be generated the file itself will be returned.

property url: str | None[source]

Returns the path of the physical file

Returns:

The path of the file

class integreat_cms.cms.models.media.media_file.MediaFileQuerySet(model=None, query=None, using=None, hints=None)[source]

Bases: QuerySet

Custom queryset for media files

filter_unused() MediaFileQuerySet[source]

Filter for unused media files

Returns:

The queryset of unused media files

Return type:

MediaFileQuerySet

integreat_cms.cms.models.media.media_file.file_size_limit(value: FieldFile) None[source]

This function checks if the uploaded file exceeds the file size limit

Parameters:

value (FieldFile) – the size of upload file

Raises:

ValidationError – when the file size exceeds the size given in the settings.

Return type:

None

integreat_cms.cms.models.media.media_file.upload_path(instance: MediaFile, filename: str) str[source]

This function calculates the path for a file which is uploaded to the media library. It contains the region id, the current year and month as subdirectories and the filename. It also contains the current epoch time to make sure that no path will be used twice. It is just the provisionally requested path for the media storage. If it already exists, Django will automatically append a random string to make sure the file name is unique.

Parameters:
  • instance (MediaFile) – The media library object

  • filename (str) – The filename of media library object

Returns:

The upload path of the file

Return type:

str

integreat_cms.cms.models.media.media_file.upload_path_thumbnail(instance: MediaFile, filename: str) str[source]

This function derives the upload path of a thumbnail file from it’s original file. This makes it a bit easier to determine which thumbnail belongs to which file if there are multiple files with the same file name (in which case Django automatically appends a random string to the original’s file name).

E.g. if the files A.jpg and A_thumbnail.jpg already exist, and A.jpg is uploaded again, the resulting file names might be e.g. A_EOHRFQ2.jpg and A_thumbnail_IWxPiOq.jpg, while with this function, the thumbnail will be stored as A_EOHRFQ2_thumbnail.jpg, making it easier to examine these files on the file system.

Parameters:
  • instance (MediaFile) – The media library object

  • filename (str) – The (unused) initial filename of thumbnail

Returns:

The upload path of the thumbnail

Return type:

str