django-vcstorage / src / vcstorage / fields.py

import os
from django.core.files.base import ContentFile
from django.db.models import signals, TextField, FileField

from vcstorage.storage import VcStorage

DEFAULT_UPLOAD_TO = '%(app_label)s/%(model_name)s/%(field_name)s'

class VcFileField(FileField):
    """
    A file field that uses the VcStorage for version control
    """
    def __init__(self, upload_to='', storage=None, **kwargs):
        if storage is None:
            storage = VcStorage()
        elif not isinstance(storage, VcStorage):
            raise TypeError("'storage' is not an instance of %s." % VcStorage)
        if not upload_to:
            upload_to = DEFAULT_UPLOAD_TO
        super(VcFileField, self).__init__(
            upload_to=upload_to, storage=storage, **kwargs)

    def pre_save(self, model_instance, add):
        "Returns field's value just before saving."
        file = super(VcFileField, self).pre_save(model_instance, add)
        if model_instance.pk:
            old_name = getattr(model_instance.__class__._default_manager.get(
                               pk=model_instance.pk), self.name)
            if file and file.name != old_name and \
                not model_instance.__class__._default_manager.filter(
                    **{self.name: old_name}).exclude(pk=model_instance.pk):
                        self.storage.delete(old_name)
        return file

    def generate_filename(self, instance, filename):
        format_kwargs = {
            'app_label': instance._meta.app_label,
            'model_name': instance._meta.object_name.lower(),
            'instance_pk': instance.pk,
            'field_name': self.attname,
        }
        self.upload_to = self.upload_to % format_kwargs
        return super(VcFileField, self).generate_filename(instance, filename)

class VcTextField(TextField):
    """
    """
    def __init__(self, *args, **kwargs):
        """
        Allow specifying a different format for the key used to identify
        versionized content in the model-definition.

        """
        self.storage = kwargs.pop('storage', VcStorage())
        self.key_format = kwargs.pop('key_format',
            os.path.join(DEFAULT_UPLOAD_TO, '%(instance_pk)s.txt'))
        # so we can figure out that this field is versionized
        super(VcTextField, self).__init__(self, *args, **kwargs)

    def get_internal_type(self):
        return "TextField"

    def post_save(self, instance=None, **kwargs):
        data = getattr(instance, self.attname).encode('utf-8')
        format_kwargs = get_format_kwargs(instance, self.attname)
        key = self.key_format % format_kwargs
        self.storage.save(key, ContentFile(data))

    def post_delete(self, instance=None, **kwargs):
        format_kwargs = get_format_kwargs(instance, self.attname)
        key = self.key_format % format_kwargs
        self.storage.delete(key)

    def contribute_to_class(self, cls, name):
        super(VcTextField, self).contribute_to_class(cls, name)
        signals.post_save.connect(self.post_save, sender=cls)
        signals.post_delete.connect(self.post_delete, sender=cls)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.