Package: django-medialibrary
Welcome to the documentation for django-medialibrary!django-medialibraryis a pluggable django app that is able to store different media types (audio, video, image) and several versions of a given file in a transparent way.The basic problam to solve is to store, retrieve and manage several versions of the same file in a seemless way. E.g. a user uploads a video that you will have to transcode into different formats. For the user you would like to show only a single media in his list of uploaded files, but you still want to serve all the generated files when necessary, moreover, if the user decides to delete his media you would like to delete all its versions.The idea for this app is to have all this in an app-indepedent and easy to use and extend way.FeaturesUnique entry point for every media type,user.medialibrary.shelf_setDifferent shelfs with possible different login for every media type. Theshelf_setcontains different models, each derived frommedialibrary.models.Shelf.Simple access to the original file.:myShelf = AudioShelf.objects.get(pk=1)
myShelf.audio_set.all()  # returns the list of audio files
isinstance(myShelf.original, Audio)  # return True, if a file is attached to myShelf
myShelf.original.size, myShelf.original.urlCustom manager to query the shelves.Generic relationships to bind the shelves to any object in the project, using the ShelfRelation modelEvery shelf type can have an ALLOWED_FORMATS property that lists the allowed extensions to be save in the shelf.VideoShelves can have thumbnail images (actually ImageShelf instances) attached. Only one of these can be marked as selected. The myVideoShelf.thumbnail property returns the selected thumbnail.Frontend APIThere is no html frontend on purpose as we are using this app through APIs. The provided APIs out of the box are/audio/,/video/,/image/- to upload and list media elements of a given type/<pk>/- to get detailed info about a single media element/<pk>/add/- adds a relationship to the media element, expects a json of the form:{
        'model': 'app_name.model_name',
        'object_id': 1
}whereapp_name.model_nameis thenatural keyof the model you are attaching.CustomizationsBesides the general django settings for file storage, there is a custom setting, the upload_to method used in the FileFields.:import datetime
def setup_s3_route(instance, filename=None):
    today = datetime.datetime.today()
    return 'media/%s-%02d-%02d/%s' % (today.year, today.month, today.day,
                                          filename)
from medialibrary import utils
utils.setup_upload_route = setup_s3_routeYou can set a custom argument for thelimit_choices_toattribute on the ShelfRelation model. This way you can define which apps are allowed to have relationships to you media elements. (e.g. if you remove the attached records when you remove the media, you probably don’t want to attach django permission records to media). The default isno restricions!:from medialibrary import utils
from django.db import models
utils.content_type_restriction = models.Q(app_label='auth', model='user')Adding new media typesThe medialibrary can be easily extended with new media types. Here is an example to add a new DocumentShelf model:from medialibrary.models import Shelf

class DocumentShelf(Shelf):

    ALLOWED_FORMATS = ('doc', 'docx', 'pdf', 'odf')

    def file_set():
        doc = "The file_set property."
        def fget(self):
            return self.audio_set
        return locals()
    file_set = property(**file_set())After this simple model definition, the user’suser.medialibrary.shelf_setwill contain DocumentShelf instances whenever appropriate.Installationpip installdjango-medialibraryor you canfind the project on githubRunning the standalone testsYou can run the tests via:#create a virtualenv and activate itpip install-rtest_requirements.txtpython runtests.pySponsorsThis app was written atPulilabwhile we were working onVidzor.