"""
Application for AJAX file upload in admin interface and other cases.
You can use it to enable WYSIWYG images embedding or any other cases, when you need file URL before form posting

Features
    File validation (file size, extension, etc.)
    User permissions validation
    File preprocessing (image compression)
    Crack protection. To attach file you need download it at first
    Temporary downloaded file cleanup
    Easy customization on any architecture level: templates, widgets, form fields, upload forms, model fields, etc.
    South app. integration
    Original image widgets and upload forms
    Original file and image attachment objects with admin interface as inlines
    
Architecture
    Widgets
        Renders control elements(buttons) and uploaded file info, html part of AJAX server response
        Has a lot of options, knows URL for AJAX file upload. To avoid url dispatcher cycling URL is evaluted in runtime.
        File widget and image widget are distinguished
    Views
        Receive data, initialize upload form and delegate validation and file processing.
        On success uses widget to render html part(image preview, url, etc.) of json response.
        Highly customizable - form class, initialize parameters and widget can be specified externaly, for example in URL scheme
    Forms
        On AJAX file upload request view calls upload form validation. On success file is preprocessed and saved to the file system.
        After that file will be marked as temporary with help of TempFile model records.
        On base form saving FileUploadFormField will check last uploaded file for temporary mark (protection reasons).
        File and image upload forms are distinguished
    Models
        On object saving "update" method is called. Old files will be marked as temporary, new one will be unmarked.
        To perform this "update" you will need to call it at appropriate time.
        Defined model field has appropriate form field and default widget. Widget can be selected at initialization moment.
        TempFile models contains records about temporary uploaded and old files.
    Admin
        Some predefined classes
    Management
        'clean_temp_files' command to remove expired files

Quick start. How to enable AJAX file upload via admin interface
    1. Connect fileupload.urls module in yours project urls.py
    2. Inherit your model class from fileupload.models.FilesHolderModel and include in it FileUploadModelField.
    To upload images you need to set FileUploadModelField additional attribute widget=fileupload.default_image_upload_widget.
    For example:
        class Img(fileupload.FilesHolderModel):
            image = FileUploadModelField('image', widget=fileupload.default_image_upload_widget)
    3. To enable admin interface you need to inherit model admin class from FilesHolderModelAdmin
    4. To enable file upload in some other cases - call obj.update() method instead of obj.save()

Customization. Example of kml uploading
    1. Define custom widget, form and view
        widget.py:
            from fileupload.widgets import FileUploadWidget
            
            class KMLUploadWidget(FileUploadWidget):
                LOAD_URL = lambda self: reverse('load_kml_via_admin')

        forms.py:
            from django import forms
            from fileupload.forms import FileUploadForm

            class KMLUploadForm(FileUploadForm):
                MAX_FILE_SIZE = 5E+5
                VALID_FILE_EXTENSIONS = ('kml', ) 
                MEDIA_FOLDER = 'kml'

                def validate_file(self):
                    # some specific validation
                    super(KMLUploadForm, self).validate_file()

        urls.py:
            from django.conf.urls.defaults import *
            from fileupload.views import load_temporary_file
            from widgets import KMLUploadWidget
            from forms import KMLUploadForm

            urlpatterns = patterns('',
                url(r'^load_kml_via_admin/$', load_temporary_file, kwargs = {
                    'form_class': KMLUploadForm,
                    'form_attrs': {},
                    'widget': KMLUploadWidget(),
                }, name='load_kml_via_admin'),
            )

    2. Define any model you need with usage of FileUploadModelField
        models.py:
            from fileupload.models import FileUploadModelField, FilesHolderModel
            from widgets import KMLUploadWidget

            class KML(FilesHolderModel):
                kml_file = FileUploadModelField(u'kml file', widget=KMLUploadWidget(attrs = {
                    'ALLOW_CHANGE': False,
                    'ALLOW_DELETE': False,
                }))

Requirements
    Django 1.2 and above: needs admin jQuery inline forms
    Optionaly needs sorl-thumbnail application for original templates usage
"""

from widgets import FileUploadWidget, ImageUploadWidget

default_file_upload_widget = FileUploadWidget()
default_image_upload_widget = ImageUploadWidget()
