-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
upload images to local storage #182
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52c66e0
upload images to local storage
sinisaos 4acd67a
empty static directory
sinisaos 02a90b1
unnecessary console.log removed
sinisaos c42d59c
add docs
sinisaos 698ad56
Merge branch 'master' into image_upload
sinisaos 2aeadb0
fix upload test
sinisaos cc6b7b2
hide array add button for media columns
sinisaos 8ca71b3
rename static folder to media
sinisaos 1b77f8c
MediaHandler abstract class
sinisaos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
.. _Media Upload: | ||
|
||
Media Upload | ||
============ | ||
|
||
.. image:: ./images/media_upload.png | ||
|
||
Piccolo Admin has the option of uploading media files to local static folder | ||
(local storage). Multi file upload is enabled per record for all columns | ||
specified in ``media_columns``. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Piccolo admin uses the Piccolo ORM `Array <https://piccolo-orm.readthedocs.io/en/latest/piccolo/schema/column_types.html#array>`_ | ||
column for media uploads. You must specify the column in the table and then register | ||
the media column in the ``TableConfig`` ``media_columns`` | ||
so that the admin UI can display the file upload button for that field by | ||
inspecting the JSON schema. The final step is to write your own media handler that | ||
upload the files and register handler to ``TableConfig`` ``media_handler``. | ||
|
||
Full example: | ||
|
||
.. code-block:: python | ||
|
||
import shutil | ||
import typing as t | ||
import uuid | ||
from abc import ABC, abstractmethod | ||
from piccolo.columns import Array, Varchar | ||
from piccolo_admin.endpoints import ( | ||
MEDIA_PATH, | ||
TableConfig, | ||
create_admin | ||
) | ||
|
||
class Movie(Table): | ||
poster = Array(base_column=Varchar()) | ||
|
||
|
||
# An abstract class as a blueprint, allowing you to write your | ||
# own upload method to suit your needs. | ||
class MediaHandler(ABC): | ||
@abstractmethod | ||
def upload(self, request, file): | ||
pass | ||
|
||
|
||
class LocalMediaHandler(MediaHandler): | ||
def __init__(self, root_path: str) -> None: | ||
self.root_path = root_path | ||
|
||
def upload(self, request, file) -> t.Dict[str, str]: | ||
image = f"{self.root_path}/{uuid.uuid4()}.jpeg" | ||
image_path = "/".join(image.split("/")[-2:]) | ||
with open(image, "wb") as buffer: | ||
shutil.copyfileobj(file.file, buffer) | ||
url_path = dict(request.scope["headers"]).get(b"host", b"").decode() | ||
return {"image": f"{request.url.scheme}://{url_path}/{image_path}"} | ||
|
||
|
||
media_handler = LocalMediaHandler(root_path=MEDIA_PATH) | ||
|
||
movie_config = TableConfig( | ||
table_class=Movie, | ||
media_columns=[Movie.poster], | ||
media_handler=media_handler, | ||
) | ||
|
||
APP = create_admin([movie_config]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure about it having to be an
Array
column. I can imagine situations where someone wants an array, but I can also imagine situations where someone wants to use aVarchar
, as they only want a single image to be uploaded.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The input type file is set to upload only one file, so the user can easily upload only one image. For uploading multiple files
Array
column is ideal (we already have aArrayWidget
for adding and removing fields which is very useful), so this solves the uploading of multiple or single file in one solution.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, in some situations though you want to make sure the user can only upload one file.
Imagine you're building a user profile table, and you have a field called 'profile_image'. If there are multiple images stored, it will be confusing - which is the genuine one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made 2 videos to see what I mean.
Single file upload
single_file.mp4
Multiple files upload
multiple_file.mp4
But of course you can do it however you want, but I think this is a simple and efficient solution because we actually upload only one file each time.