"""
I use this to place a direct link to a file on Dropbox into a Charfield, which is then queued up to be downloaded.
You could also change the 'data-link-type' to 'preview' to link to the Dropbox preview page for the chosen file.

file = CharField(widget=DropboxChooser())

You can read more about the Dropbox Chooser dropin here: 
    https://www.dropbox.com/developers/dropins/chooser/
"""


from django import forms

class DropboxTextInput(DropboxInput, forms.TextInput):
    input_type = 'dropbox-chooser'


class DropboxChooser(DropboxTextInput):

    def __init__(self, attrs=None):
        default_attrs = {'data-link-type': 'direct', 'style': 'visibility: hidden;'}

        if attrs:
            default_attrs.update(attrs)

        super(DropboxChooser, self).__init__(default_attrs)

    def render(self, name, value, attrs=None):
        html = super(DropboxChooser, self).render(name, value, attrs)

        final_attrs = self.build_attrs(attrs, name=name)

        html += """
            <script type="text/javascript" src="https://www.dropbox.com/static/api/1/dropins.js" id="dropboxjs" data-app-key="123456789"></script>
        """

        return mark_safe(html)