# Path: hyperadmin/resources/storages/endpoints.py
# class BoundFile(object):
#     def __init__(self, storage, name):
#         self.storage = storage
#         self.name = name
# 
#     @property
#     def pk(self):
#         return self.name
# 
#     @property
#     def url(self):
#         return self.storage.url(self.name)
# 
#     def delete(self):
#         return self.storage.delete(self.name)
# 
#     def exists(self):
#         return self.storage.exists(self.name)
# 
#     def __unicode__(self):
#         return self.name


import base64
        import os
from django import forms
from django.core.files.base import ContentFile
from django.middleware.csrf import get_token
from hyperadmin.resources.storages.endpoints import BoundFile
        if self.instance:
            self.initial['name'] = self.instance.name
            self.initial['overwrite'] = True

    def add_csrf_field(self, request):
        value = get_token(request)
        self.fields['csrfmiddlewaretoken'] = forms.CharField(initial=value, widget=forms.HiddenInput)

    def save(self, commit=True):
        if self.cleaned_data.get('overwrite', False): #TODO would be better if storage accepted an argument to overwrite
            if self.storage.exists(self.cleaned_data['name']):
                self.storage.delete(self.cleaned_data['name'])
        name = self.storage.save(self.cleaned_data['name'], self.cleaned_data['upload'])
        return BoundFile(self.storage, name)


class Base64UploadForm(UploadForm):
    upload = forms.CharField(widget=forms.Textarea)

    def clean_upload(self):
        django_file = None
        payload = self.cleaned_data.get('upload')
        if payload:
            try:
                django_file = ContentFile(base64.b64decode(payload))
            except TypeError as error:
                raise forms.ValidationError('Inproperly encoded file: %s' % error)
        return django_file



# Path: hyperadmin/resources/storages/endpoints.py
# class BoundFile(object):
#     def __init__(self, storage, name):
#         self.storage = storage
#         self.name = name
# 
#     @property
#     def pk(self):
#         return self.name
# 
#     @property
#     def url(self):
#         return self.storage.url(self.name)
# 
#     def delete(self):
#         return self.storage.delete(self.name)
# 
#     def exists(self):
#         return self.storage.exists(self.name)
# 
#     def __unicode__(self):
#         return self.name


# Path: hyperadmin/resources/storages/forms.py
import base64
        import os
from django import forms
from django.core.files.base import ContentFile
from django.middleware.csrf import get_token
from hyperadmin.resources.storages.endpoints import BoundFile

        if self.instance:
            self.initial['name'] = self.instance.name
            self.initial['overwrite'] = True

    def add_csrf_field(self, request):
        value = get_token(request)
        self.fields['csrfmiddlewaretoken'] = forms.CharField(initial=value, widget=forms.HiddenInput)

    def save(self, commit=True):
        if self.cleaned_data.get('overwrite', False): #TODO would be better if storage accepted an argument to overwrite
            if self.storage.exists(self.cleaned_data['name']):
                self.storage.delete(self.cleaned_data['name'])
        name = self.storage.save(self.cleaned_data['name'], self.cleaned_data['upload'])
        return BoundFile(self.storage, name)


class Base64UploadForm(UploadForm):
    upload = forms.CharField(widget=forms.Textarea)

    def clean_upload(self):
        django_file = None
        payload = self.cleaned_data.get('upload')
        if payload:
            try:
                django_file = ContentFile(base64.b64decode(payload))
            except TypeError as error:
                raise forms.ValidationError('Inproperly encoded file: %s' % error)
        return django_file



class UploadLinkForm(forms.Form):