Skip to content

Commit

Permalink
feat: django-money support (#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav committed Sep 14, 2023
1 parent 8e2830c commit a990cf1
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
15 changes: 15 additions & 0 deletions src/unfold/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
UnfoldAdminImageSmallFieldWidget,
UnfoldAdminIntegerFieldWidget,
UnfoldAdminIntegerRangeWidget,
UnfoldAdminMoneyWidget,
UnfoldAdminNullBooleanSelectWidget,
UnfoldAdminSingleDateWidget,
UnfoldAdminSingleTimeWidget,
Expand All @@ -75,6 +76,13 @@
except ImportError:
HAS_PSYCOPG = False

try:
from djmoney.models.fields import MoneyField

HAS_MONEY = True
except ImportError:
HAS_MONEY = False

checkbox = forms.CheckboxInput({"class": "action-select"}, lambda value: False)

FORMFIELD_OVERRIDES = {
Expand Down Expand Up @@ -108,6 +116,13 @@
}
)

if HAS_MONEY:
FORMFIELD_OVERRIDES.update(
{
MoneyField: {"widget": UnfoldAdminMoneyWidget},
}
)

FORMFIELD_OVERRIDES_INLINE = copy.deepcopy(FORMFIELD_OVERRIDES)

FORMFIELD_OVERRIDES_INLINE.update(
Expand Down
2 changes: 1 addition & 1 deletion src/unfold/static/unfold/css/styles.css

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/unfold/templates/unfold/widgets/split_money.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="flex flex-row gap-2 max-w-2xl">
<div class="w-2/3">
{% with widget=widget.subwidgets.0 %}
{% include widget.template_name %}
{% endwith %}
</div>

<div class="w-1/3">
{% with widget=widget.subwidgets.1 %}
{% include widget.template_name %}
{% endwith %}
</div>
</div>
41 changes: 39 additions & 2 deletions src/unfold/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
AdminTimeWidget,
AdminUUIDInputWidget,
)
from django.forms import MultiWidget, NullBooleanSelect, NumberInput
from django.forms import MultiWidget, NullBooleanSelect, NumberInput, Select
from django.utils.translation import gettext_lazy as _

from .exceptions import UnfoldException

LABEL_CLASSES = [
"block",
"font-medium",
Expand Down Expand Up @@ -84,7 +86,13 @@
"h-full",
]

SELECT_CLASSES = [*BASE_INPUT_CLASSES, "pr-8", "max-w-2xl", "appearance-none"]
SELECT_CLASSES = [
*BASE_INPUT_CLASSES,
"pr-8",
"max-w-2xl",
"appearance-none",
"truncate",
]

PROSE_CLASSES = [
"font-normal",
Expand Down Expand Up @@ -287,3 +295,32 @@ def __init__(self, attrs: Optional[Dict[str, Any]] = None) -> None:

class UnfoldAdminNullBooleanSelectWidget(NullBooleanSelect):
pass


class UnfoldAdminSelect(Select):
def __init__(self, attrs=None, choices=()):
if attrs is None:
attrs = {}

attrs["class"] = " ".join(SELECT_CLASSES)
super().__init__(attrs, choices)


try:
from djmoney.forms.widgets import MoneyWidget
from djmoney.settings import CURRENCY_CHOICES

class UnfoldAdminMoneyWidget(MoneyWidget):
template_name = "unfold/widgets/split_money.html"

def __init__(self, *args, **kwargs):
super().__init__(
amount_widget=UnfoldAdminTextInputWidget,
currency_widget=UnfoldAdminSelect(choices=CURRENCY_CHOICES),
)

except ImportError:

class UnfoldAdminMoneyWidget:
def __init__(self, *args, **kwargs):
raise UnfoldException("django-money not installed")

0 comments on commit a990cf1

Please sign in to comment.