Skip to content

Commit

Permalink
lint
Browse files Browse the repository at this point in the history
  • Loading branch information
joegasewicz committed Jan 23, 2023
1 parent 7606ff5 commit 6606637
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ confidence=
# --enable=similarities". If you want to run only the classes checker, but have
# no Warning level messages displayed, use "--disable=all --enable=classes
# --disable=W".
disable=too-few-public-methods,
disable=
too-few-public-methods,
invalid-name,
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Bobtail Upload will attach the an Upload API to the request object
There are 2 methods now available:

- `add(self, *, file_name: str, data: bytes, mimetype: str) -> None`
- `save(self, *, table_name: str = None, id: Union[int, str] = None) -> None`
- `save(self, *, table_name: str = None, pk: Union[int, str] = None) -> None`

```python
def post(self, req: Request, res: Response):
Expand Down Expand Up @@ -54,7 +54,7 @@ def post(self, req: Request, res: Response):
)

# Use your ORM to save the file to your db & obtain the returned primary key (pk)
req.upload.save(table_name="images", id=pk)
req.upload.save(table_name="images", pk=pk)
```

### Options
Expand Down
5 changes: 4 additions & 1 deletion bobtail_upload/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from .upload import BobtailUpload
"""
Bobtail Upload
"""
from .upload import BobtailUpload
35 changes: 24 additions & 11 deletions bobtail_upload/upload.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
"""
Upload
"""
import os
from typing import Union

from bobtail import AbstractMiddleware, Request, Response, Tail


class BobtailUploadError(Exception):
pass
"""
BobtailUploadError
"""


class Upload:
"""
Upload
"""

file_name: str
data: bytes
mimetype: str

def __init__(self, options):
self.options = options
Expand All @@ -25,24 +37,23 @@ def add(self, *, file_name: str, data: bytes, mimetype: str) -> None:
self.data = data
self.mimetype = mimetype

def _create_path(self, table_name: str, id: Union[int, str]) -> str:
def _create_path(self, table_name: str, pk: Union[int, str]) -> str:
base_path = self.options['UPLOAD_DIR']
if table_name and id:
dir_path = f"{base_path}/{table_name}/{id}/"
if table_name and pk:
dir_path = f"{base_path}/{table_name}/{pk}/"
os.makedirs(os.path.dirname(dir_path), exist_ok=True)
return f"{dir_path}{self.file_name}"
else:
return f"{base_path}/{self.file_name}"
return f"{base_path}/{self.file_name}"

def save(self, *, table_name: str = None, id: Union[int, str] = None) -> None:
def save(self, *, table_name: str = None, pk: Union[int, str] = None) -> None:
"""
Saves the file with optional table name & id path
Saves the file with optional table name & pk path
:param table_name:
:param id:
:param pk:
:return:
"""
try:
with open(self._create_path(table_name, id), "wb") as f:
with open(self._create_path(table_name, pk), "wb") as f:
f.write(self.data)
self._clean_up()
except Exception as exc:
Expand All @@ -56,7 +67,9 @@ def _clean_up(self):


class BobtailUpload(AbstractMiddleware):

"""
BobtailUpload
"""
options = {
"UPLOAD_DIR": "uploads",
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def post(self, req, res):
data=data,
mimetype="image/png"
)
req.upload.save(table_name="images", id=1)
req.upload.save(table_name="images", pk=1)
res.set_body(None)
res.set_status(202)

Expand Down

0 comments on commit 6606637

Please sign in to comment.