# Path: buckets/widgets.py
# class S3FileUploadWidget(widgets.TextInput):
#     default_html = (
#         '<div class="s3-buckets {uploaded_class}"'
#         '     data-upload-to="{upload_to}" {accepted_types}>'
#         '   {mime_lookup}'
#         '   <div class="file-links">'
#         '       <a class="file-link" href="{file_url}">{file_name}</a>'
#         '       <a class="file-remove" href="#">%s</a>'
#         '   </div>'
#         '   <input class="file-url" type="hidden" value="{file_url}"'
#         '          id="{element_id}" name="{name}" />'
#         '   <input class="file-input" type="file" />'
#         '</div>'
#     ) % _("(Remove)")
# 
#     class Media:
#         js = (
#             'buckets/js/script.js',
#         )
#         css = {
#             'all': (
#                 'buckets/css/buckets.css',
#             )
#         }
# 
#     def __init__(self, upload_to='', accepted_types=None, *args, **kwargs):
#         self.html = kwargs.pop('html', self.default_html)
#         self.upload_to = upload_to
#         self.accepted_types = accepted_types
# 
#         super(S3FileUploadWidget, self).__init__(*args, **kwargs)
# 
#     def render(self, name, value, attrs={}, renderer=None):
#         if isinstance(value, str):
#             file_url = value
#         else:
#             file_url = value.url if value else ''
# 
#         accepted_types = ''
#         if self.accepted_types:
#             accepted_types = 'data-accepted-types="{}"'.format(
#                 ','.join(self.accepted_types)
#             )
# 
#         mime_lookup = ''
#         if hasattr(settings, 'MIME_LOOKUPS'):
#             mime_lookup = ('<script>var MIME_LOOKUPS = ' +
#                            json.dumps(settings.MIME_LOOKUPS) + '</script>')
# 
#         output = self.html.format(
#             name=name,
#             file_url=file_url,
#             element_id=self.build_attrs(attrs).get('id'),
#             file_name=basename(file_url) if file_url else '',
#             uploaded_class=('uploaded' if file_url else ''),
#             upload_to=self.upload_to,
#             accepted_types=accepted_types,
#             mime_lookup=mime_lookup
#         )
# 
#         return mark_safe(output)


import os
from django.utils.translation import ugettext as _
from django.core.files.storage import default_storage
from django.db import models
from .widgets import S3FileUploadWidget
            f = instance.__dict__.get(self.field.name)
            if f:
                o = f.url
            instance.__dict__[self.field.name] = S3File(value,
                                                        self.field,
                                                        original_url=o)


class S3FileField(models.Field):
    """ Stores and provides access to a file stored in an AWS S3 bucket. To set
        the value of an `S3FileField` you usually provide the URL of the file
        in the bucket. Internally this URL is converted to a `S3File`, which
        provides access to the actual file on S3."""
    attr_class = S3File
    descriptor_class = S3FileDescriptor

    description = _("A file stored in an AWS S3 buckets.")

    def __init__(self, upload_to='', storage=None, accepted_types=None,
                 *args, **kwargs):
        self.storage = storage or default_storage
        self.upload_to = upload_to
        self.accepted_types = accepted_types

        kwargs['max_length'] = kwargs.get('max_length', 200)
        super(S3FileField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
        return 'CharField'


# Path: buckets/widgets.py
# class S3FileUploadWidget(widgets.TextInput):
#     default_html = (
#         '<div class="s3-buckets {uploaded_class}"'
#         '     data-upload-to="{upload_to}" {accepted_types}>'
#         '   {mime_lookup}'
#         '   <div class="file-links">'
#         '       <a class="file-link" href="{file_url}">{file_name}</a>'
#         '       <a class="file-remove" href="#">%s</a>'
#         '   </div>'
#         '   <input class="file-url" type="hidden" value="{file_url}"'
#         '          id="{element_id}" name="{name}" />'
#         '   <input class="file-input" type="file" />'
#         '</div>'
#     ) % _("(Remove)")
# 
#     class Media:
#         js = (
#             'buckets/js/script.js',
#         )
#         css = {
#             'all': (
#                 'buckets/css/buckets.css',
#             )
#         }
# 
#     def __init__(self, upload_to='', accepted_types=None, *args, **kwargs):
#         self.html = kwargs.pop('html', self.default_html)
#         self.upload_to = upload_to
#         self.accepted_types = accepted_types
# 
#         super(S3FileUploadWidget, self).__init__(*args, **kwargs)
# 
#     def render(self, name, value, attrs={}, renderer=None):
#         if isinstance(value, str):
#             file_url = value
#         else:
#             file_url = value.url if value else ''
# 
#         accepted_types = ''
#         if self.accepted_types:
#             accepted_types = 'data-accepted-types="{}"'.format(
#                 ','.join(self.accepted_types)
#             )
# 
#         mime_lookup = ''
#         if hasattr(settings, 'MIME_LOOKUPS'):
#             mime_lookup = ('<script>var MIME_LOOKUPS = ' +
#                            json.dumps(settings.MIME_LOOKUPS) + '</script>')
# 
#         output = self.html.format(
#             name=name,
#             file_url=file_url,
#             element_id=self.build_attrs(attrs).get('id'),
#             file_name=basename(file_url) if file_url else '',
#             uploaded_class=('uploaded' if file_url else ''),
#             upload_to=self.upload_to,
#             accepted_types=accepted_types,
#             mime_lookup=mime_lookup
#         )
# 
#         return mark_safe(output)


# Path: buckets/fields.py
import os
from django.utils.translation import ugettext as _
from django.core.files.storage import default_storage
from django.db import models
from .widgets import S3FileUploadWidget

            f = instance.__dict__.get(self.field.name)
            if f:
                o = f.url
            instance.__dict__[self.field.name] = S3File(value,
                                                        self.field,
                                                        original_url=o)


class S3FileField(models.Field):
    """ Stores and provides access to a file stored in an AWS S3 bucket. To set
        the value of an `S3FileField` you usually provide the URL of the file
        in the bucket. Internally this URL is converted to a `S3File`, which
        provides access to the actual file on S3."""
    attr_class = S3File
    descriptor_class = S3FileDescriptor

    description = _("A file stored in an AWS S3 buckets.")

    def __init__(self, upload_to='', storage=None, accepted_types=None,
                 *args, **kwargs):
        self.storage = storage or default_storage
        self.upload_to = upload_to
        self.accepted_types = accepted_types

        kwargs['max_length'] = kwargs.get('max_length', 200)
        super(S3FileField, self).__init__(*args, **kwargs)

    def get_internal_type(self):
        return 'CharField'


    def contribute_to_class(self, cls, name, **kwargs):