Skip to content

Commit

Permalink
S3SignView: cleanup method signatures and use tz-aware datetime
Browse files Browse the repository at this point in the history
A number of these methods are actually static methods, and don't rely on
the object instance. I'm cleaning up some of the code here, and also
updating the datetime.now() to call timezone.now(), as recommended by
Django and which we use everywhere else.
  • Loading branch information
nikolas committed Jan 13, 2025
1 parent 3114c3b commit 0472d4e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
==================
* Refactored upload logic out of view code
* Added .avif image format.
* Clean up SignS3View method signatures
* Use timezone-aware datetime object in SignS3View

0.4.1 (2025-01-10)
==================
Expand Down
37 changes: 21 additions & 16 deletions s3sign/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@

import boto3

from datetime import datetime

from django.conf import settings
from django.http import HttpResponse
from django.views.generic import View
from django.utils import timezone

from s3sign.utils import s3_config, upload_file

Expand Down Expand Up @@ -59,34 +58,37 @@ def dispatch(self, request, *args, **kwargs):
)
return super().dispatch(request, *args, **kwargs)

def get_name_field(self):
def get_name_field(self) -> str:
return self.name_field

def get_type_field(self):
def get_type_field(self) -> str:
return self.type_field

def get_expiration_time(self):
return self.expiration_time

def get_mime_type_extensions(self):
def get_mime_type_extensions(self) -> list:
return self.mime_type_extensions

def get_default_extension(self):
def get_default_extension(self) -> str:
return self.default_extension

def get_root(self):
def get_root(self) -> str:
return self.root

def get_path_string(self):
def get_path_string(self) -> str:
return self.path_string

def get_aws_access_key(self):
@staticmethod
def get_aws_access_key() -> str:
return settings.AWS_ACCESS_KEY

def get_aws_secret_key(self):
@staticmethod
def get_aws_secret_key() -> str:
return settings.AWS_SECRET_KEY

def get_bucket(self):
@staticmethod
def get_bucket() -> str:
return settings.AWS_UPLOAD_BUCKET

def get_mimetype(self, request):
Expand All @@ -98,21 +100,24 @@ def extension_from_mimetype(self, mime_type):
return ext
return self.get_default_extension()

def now(self):
return datetime.now()
@staticmethod
def now():
return timezone.now()

def now_time(self):
@staticmethod
def now_time():
return time.time()

def basename(self, request):
@staticmethod
def basename():
return str(uuid.uuid4())

def extension(self, request):
return self.extension_from_mimetype(self.get_mimetype(request))

def get_object_name(self, request):
now = self.now()
basename = self.basename(request)
basename = self.basename()
extension = self.extension(request)
return self.get_path_string().format(
now=now, basename=basename, extension=extension,
Expand Down

0 comments on commit 0472d4e

Please sign in to comment.