<s> import base64 <EOL> from django import forms <EOL> from django . core . files . base import ContentFile <EOL> from django . middleware . csrf import get_token <EOL> from hyperadmin . resources . storages . endpoints import BoundFile <EOL> class UploadForm ( forms . Form ) : <EOL> name = forms . CharField ( ) <EOL> upload = forms . FileField ( ) <EOL> overwrite = forms . BooleanField ( required = False ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . instance = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . storage = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( UploadForm , self ) . __init__ ( ** kwargs ) <EOL> if self . instance : <EOL> self . initial [ '<STR_LIT:name>' ] = self . instance . name <EOL> self . initial [ '<STR_LIT>' ] = True <EOL> def add_csrf_field ( self , request ) : <EOL> value = get_token ( request ) <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( initial = value , widget = forms . HiddenInput ) <EOL> def save ( self , commit = True ) : <EOL> if self . cleaned_data . get ( '<STR_LIT>' , False ) : <EOL> if self . storage . exists ( self . cleaned_data [ '<STR_LIT:name>' ] ) : <EOL> self . storage . delete ( self . cleaned_data [ '<STR_LIT:name>' ] ) <EOL> name = self . storage . save ( self . cleaned_data [ '<STR_LIT:name>' ] , self . cleaned_data [ '<STR_LIT>' ] ) <EOL> return BoundFile ( self . storage , name ) <EOL> class Base64UploadForm ( UploadForm ) : <EOL> upload = forms . CharField ( widget = forms . Textarea ) <EOL> def clean_upload ( self ) : <EOL> django_file = None <EOL> payload = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> if payload : <EOL> try : <EOL> django_file = ContentFile ( base64 . b64decode ( payload ) ) <EOL> except TypeError as error : <EOL> raise forms . ValidationError ( '<STR_LIT>' % error ) <EOL> return django_file <EOL> class UploadLinkForm ( forms . Form ) : <EOL> name = forms . CharField ( ) <EOL> upload_to = forms . CharField ( required = False ) <EOL> overwrite = forms . BooleanField ( required = False ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> self . storage = kwargs . pop ( '<STR_LIT>' ) <EOL> self . resource = kwargs . pop ( '<STR_LIT>' ) <EOL> self . request = kwargs . pop ( '<STR_LIT>' ) <EOL> super ( UploadLinkForm , self ) . __init__ ( ** kwargs ) <EOL> def save ( self , commit = True ) : <EOL> import os <EOL> file_name = self . storage . get_valid_name ( self . cleaned_data [ '<STR_LIT:name>' ] ) <EOL> upload_to = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if upload_to : <EOL> path = os . path . join ( upload_to , file_name ) <EOL> else : <EOL> path = file_name <EOL> overwrite = self . cleaned_data . get ( '<STR_LIT>' , False ) <EOL> if overwrite : <EOL> name = path <EOL> else : <EOL> name = self . storage . get_available_name ( path ) <EOL> form_kwargs = { '<STR_LIT>' : { '<STR_LIT:name>' : name , '<STR_LIT>' : overwrite } } <EOL> link = self . resource . link_prototypes [ '<STR_LIT>' ] . get_link ( form_kwargs = form_kwargs , rel = '<STR_LIT>' ) <EOL> link . form . add_csrf_field ( self . request ) <EOL> response_type = self . request . META . get ( '<STR_LIT>' , None ) <EOL> if response_type : <EOL> link . state [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' + response_type <EOL> return link </s>
