9
9
10
10
from s3file .storages import storage
11
11
12
- logger = logging .getLogger (' s3file' )
12
+ logger = logging .getLogger (" s3file" )
13
13
14
14
15
15
class S3FileInputMixin :
16
16
"""FileInput that uses JavaScript to directly upload to Amazon S3."""
17
17
18
18
needs_multipart_form = False
19
- upload_path = str (getattr (
20
- settings , ' S3FILE_UPLOAD_PATH' , pathlib .PurePosixPath (' tmp' , ' s3file' )
21
- ))
19
+ upload_path = str (
20
+ getattr ( settings , " S3FILE_UPLOAD_PATH" , pathlib .PurePosixPath (" tmp" , " s3file" ) )
21
+ )
22
22
upload_path = safe_join (str (storage .location ), upload_path )
23
23
expires = settings .SESSION_COOKIE_AGE
24
24
@@ -33,25 +33,24 @@ def client(self):
33
33
def build_attrs (self , * args , ** kwargs ):
34
34
attrs = super ().build_attrs (* args , ** kwargs )
35
35
36
- accept = attrs .get (' accept' )
36
+ accept = attrs .get (" accept" )
37
37
response = self .client .generate_presigned_post (
38
38
self .bucket_name ,
39
- str (pathlib .PurePosixPath (self .upload_folder , ' ${filename}' )),
39
+ str (pathlib .PurePosixPath (self .upload_folder , " ${filename}" )),
40
40
Conditions = self .get_conditions (accept ),
41
41
ExpiresIn = self .expires ,
42
42
)
43
43
44
44
defaults = {
45
- 'data-fields-%s' % key : value
46
- for key , value in response ['fields' ].items ()
45
+ "data-fields-%s" % key : value for key , value in response ["fields" ].items ()
47
46
}
48
- defaults [' data-url' ] = response [' url' ]
47
+ defaults [" data-url" ] = response [" url" ]
49
48
defaults .update (attrs )
50
49
51
50
try :
52
- defaults [' class' ] += ' s3file'
51
+ defaults [" class" ] += " s3file"
53
52
except KeyError :
54
- defaults [' class' ] = ' s3file'
53
+ defaults [" class" ] = " s3file"
55
54
return defaults
56
55
57
56
def get_conditions (self , accept ):
@@ -60,9 +59,9 @@ def get_conditions(self, accept):
60
59
["starts-with" , "$key" , str (self .upload_folder )],
61
60
{"success_action_status" : "201" },
62
61
]
63
- if accept and ',' not in accept :
64
- top_type , sub_type = accept .split ('/' , 1 )
65
- if sub_type == '*' :
62
+ if accept and "," not in accept :
63
+ top_type , sub_type = accept .split ("/" , 1 )
64
+ if sub_type == "*" :
66
65
conditions .append (["starts-with" , "$Content-Type" , "%s/" % top_type ])
67
66
else :
68
67
conditions .append ({"Content-Type" : accept })
@@ -73,14 +72,14 @@ def get_conditions(self, accept):
73
72
74
73
@cached_property
75
74
def upload_folder (self ):
76
- return str (pathlib .PurePosixPath (
77
- self .upload_path ,
78
- base64 .urlsafe_b64encode (
79
- uuid .uuid4 ().bytes
80
- ).decode ('utf-8' ).rstrip ('=\n ' ),
81
- )) # S3 uses POSIX paths
75
+ return str (
76
+ pathlib .PurePosixPath (
77
+ self .upload_path ,
78
+ base64 .urlsafe_b64encode (uuid .uuid4 ().bytes )
79
+ .decode ("utf-8" )
80
+ .rstrip ("=\n " ),
81
+ )
82
+ ) # S3 uses POSIX paths
82
83
83
84
class Media :
84
- js = (
85
- 's3file/js/s3file.js' if settings .DEBUG else 's3file/js/s3file.min.js' ,
86
- )
85
+ js = ("s3file/js/s3file.js" if settings .DEBUG else "s3file/js/s3file.min.js" ,)
0 commit comments