Skip to content

Commit

Permalink
Merge pull request #203 from xjodoin/patch-1
Browse files Browse the repository at this point in the history
Use built-in 'open' method for file object
  • Loading branch information
KaratasFurkan authored Jul 26, 2023
2 parents b772fa4 + a6c248d commit b99be64
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drf_extra_fields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def to_representation(self, file):
return ""

try:
with open(file.path, "rb") as f:
with file.open() as f:
return base64.b64encode(f.read()).decode()
except Exception:
raise OSError("Error encoding file")
Expand Down
6 changes: 6 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class ImageFieldFile:
def __init__(self, path):
self.path = path

def open(self):
return open(self.path, "rb")

def __init__(self, image_path):
self.image = self.ImageFieldFile(path=image_path)

Expand All @@ -53,6 +56,9 @@ class FieldFile:
def __init__(self, path):
self.path = path

def open(self):
return open(self.path, "rb")

def __init__(self, file_path):
self.file = self.FieldFile(path=file_path)

Expand Down

0 comments on commit b99be64

Please sign in to comment.