-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from radinceorc/main
Add the new feat name svg field
- Loading branch information
Showing
12 changed files
with
107 additions
and
66 deletions.
There are no files selected for viewing
Empty file.
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,53 @@ | ||
import os | ||
|
||
from django.contrib.admin.widgets import AdminFileWidget | ||
from django.core.validators import FileExtensionValidator | ||
from django.db import models | ||
|
||
from sage_tools.widgets import SVGWidget | ||
|
||
|
||
class SVGField(models.FileField): | ||
"""Custom field for handling SVG files.""" | ||
|
||
def __init__(self, *args, **kwargs): | ||
# Add the SVG file extension validator by default | ||
svg_validator = FileExtensionValidator(allowed_extensions=["svg"]) | ||
validators = kwargs.pop("validators", []) | ||
validators.append(svg_validator) | ||
self.url = None | ||
super().__init__(*args, validators=validators, **kwargs) | ||
|
||
def deconstruct(self): | ||
name, path, args, kwargs = super().deconstruct() | ||
if "validators" in kwargs: | ||
validators = kwargs["validators"] | ||
if validators and isinstance(validators[-1], FileExtensionValidator): | ||
validators = validators[:-1] | ||
kwargs["validators"] = validators | ||
return name, path, args, kwargs | ||
|
||
def from_db_value(self, value, expression, connection): | ||
if value and os.path.exists(value): | ||
self.url = value | ||
with open(value, "r") as svg_file: | ||
return svg_file.read() | ||
return "" | ||
|
||
def to_python(self, value): | ||
"""Ensure the value is converted to the SVG content.""" | ||
if isinstance(value, str) and os.path.exists(value): | ||
return value | ||
return value | ||
|
||
def formfield(self, **kwargs): | ||
"""Override formfield to manipulate the value and ensure it behaves like a FileField.""" | ||
kwargs["widget"] = SVGWidget(url=self.url) | ||
return super().formfield(**kwargs) | ||
|
||
def value_to_string(self, obj): | ||
"""Ensure serialization returns the file path.""" | ||
value = self.value_from_object(obj) | ||
if value: | ||
return self.url | ||
return "" |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .svg import SVGWidget |
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,13 @@ | ||
from django.contrib.admin.widgets import AdminFileWidget | ||
|
||
|
||
class SVGWidget(AdminFileWidget): | ||
def __init__(self, url=None, attrs=None): | ||
self.svg_field_url = url | ||
super().__init__(attrs) | ||
|
||
def format_value(self, value): | ||
"""Return the file URL if available.""" | ||
if self.svg_field_url: | ||
return self.svg_field_url | ||
return super().format_value(value) |