-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3983f71
commit ef5d5c5
Showing
15 changed files
with
156 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"""FileField definition""" | ||
from typing import Callable, Union, TYPE_CHECKING | ||
from masonite.facades.Hash import Hash | ||
from .Field import Field | ||
|
||
if TYPE_CHECKING: | ||
from masoniteorm.models.Model import Model | ||
|
||
|
||
class File(Field): | ||
"""FileField definition""" | ||
|
||
type = "file" | ||
|
||
def __init__( | ||
self, name: str, attribute: Union[str, Callable] = None, resolve_callback: Callable = None | ||
): | ||
# Field's component | ||
self.component = "FileField" | ||
# Field's suggestions callback | ||
self.suggestions = None | ||
|
||
super().__init__(name, attribute, resolve_callback) | ||
|
||
def fill(self, request, model: "Model"): | ||
"""Fill the field""" | ||
|
||
storage = request.app.make("storage") | ||
path = storage.disk("local").put_file("collapsar/storage/", request.input(self.attribute)) | ||
setattr(model, self.attribute, path) | ||
|
||
return None | ||
|
||
def json_serialize(self): | ||
""" | ||
Prepare the element for JSON serialization. | ||
:return: dict | ||
""" | ||
serialized = super().json_serialize() | ||
return serialized |
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,41 @@ | ||
import { Input } from "@/components/ui/input"; | ||
import { AspectRatio } from "@radix-ui/react-aspect-ratio"; | ||
import { useState } from "react"; | ||
|
||
export function FileField(props: any) { | ||
const [replaceImage, setReplaceImage] = useState(true); | ||
|
||
const displayRender = () => { | ||
return ( | ||
<div className="flex items-center space-x-2 max-h-[200px]"> | ||
<img className="h-full" src={`/${props.value}`} alt="" /> | ||
</div> | ||
); | ||
}; | ||
|
||
if (props.renderForDisplay || !props.fieldConfig) { | ||
return displayRender(); | ||
} | ||
|
||
const handleOnChange = (e: any) => { | ||
props.onChange(e.target.files ? e.target.files[0] : null) | ||
} | ||
|
||
return ( | ||
<> | ||
{props.value && !replaceImage ? ( | ||
<AspectRatio ratio={5 / 3} className="bg-muted"> | ||
<img className="min-h-full min-w-full" src={props.value} alt="" onClick={() => setReplaceImage(true)} /> | ||
</AspectRatio> | ||
) : ( | ||
<Input | ||
type="file" | ||
readOnly={props.fieldConfig.readonly} | ||
onChange={handleOnChange} | ||
|
||
name={props.name} | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as AspectRatioPrimitive from "@radix-ui/react-aspect-ratio" | ||
|
||
const AspectRatio = AspectRatioPrimitive.Root | ||
|
||
export { AspectRatio } |
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
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
19 changes: 19 additions & 0 deletions
19
tests/integrations/databases/migrations/2023_10_26_214109_articles_add_image.py
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,19 @@ | ||
"""ArticlesAddImage Migration.""" | ||
|
||
from masoniteorm.migrations import Migration | ||
|
||
|
||
class ArticlesAddImage(Migration): | ||
def up(self): | ||
""" | ||
Run the migrations. | ||
""" | ||
with self.schema.table("articles") as table: | ||
table.string("image").nullable() | ||
|
||
def down(self): | ||
""" | ||
Revert the migrations. | ||
""" | ||
with self.schema.table("articles") as table: | ||
table.drop_column("image") |
Binary file added
BIN
+1.9 MB
...framework/filesystem/collapsar/storage/27a53723-0ab5-4dd4-af92-b86ba63b2d02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+1.9 MB
...framework/filesystem/collapsar/storage/f5e64ec6-e148-4f4a-bb4b-8c1bbdf1def9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.