Default Object Classes for Uploads

Setting object classes for content types

When uploading content to the CMS by means of the Content Browser, an object class that fits the content type needs to be determined. For this and for application-specific purposes, you can register the object classes to be used for specific content types:

scrivito.register_default_obj_class_for_content_type(mapping)

Provide a mapping that consists of key-value pairs where each pair is made up of a content type pattern as the key and an object class name as the value. The content type patterns may include wildcards either in the file type part or both content type parts. When looking up an object class, the most specific match will be used. The more wildcards the key of a pair contains, the less specific it is. You can register one or more defaults at once and also override the existing defaults.

The Scrivito SDK initially provides two mappings: 

  • image/* -> 'Image'
  • */* -> 'Download'

The corresponding models, Image and Download, are added to an application by the install generator.

Looking up an object class

To look up a default, you can use scrivito.default_obj_class_for_content_type(content_type), passing the content type in question as a string. Examples:

Copy
scrivito.register_default_obj_class_for_content_type({
  'audio/mp3': 'MyMp3', // Register a default for all MP3s.
  'audio/*': 'MyAudio' // Register a default for all other audio files.
});

scrivito.register_default_obj_class_for_content_type({
  'image/*': 'MyImage', // Override factory default for image files.
  'image/png': 'MyPng', // Register default for PNG images.
  '*/*': 'MyDownload' // Override factory default for all other files.
});

scrivito.default_obj_class_for_content_type('image/png');
//> 'MyPng'
scrivito.default_obj_class_for_content_type('image/gif');
//> 'MyImage'
scrivito.default_obj_class_for_content_type('audio/wav');
//> 'MyAudio'
scrivito.default_obj_class_for_content_type('spam/eggs');
//> 'MyDownload'