Skip to content

Commit

Permalink
Add custom form fields
Browse files Browse the repository at this point in the history
  • Loading branch information
knifecake committed Dec 22, 2024
1 parent e6e3287 commit c5d8c48
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions anchor/forms/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import fields, widgets # noqa
7 changes: 7 additions & 0 deletions anchor/forms/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django import forms

from .widgets import ClearableBlobInput


class BlobField(forms.FileField):
widget = ClearableBlobInput
10 changes: 10 additions & 0 deletions anchor/forms/widgets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.forms.widgets import ClearableFileInput


class ClearableBlobInput(ClearableFileInput):
# make this widget work with the Django admin
choices = []


class AdminBlobInput(ClearableBlobInput):
template_name = "anchor/widgets/admin_blob_input.html"
9 changes: 5 additions & 4 deletions anchor/models/fields/single_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.db import models
from django.db.models import Model
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor
from django.utils.text import capfirst

from anchor.models import Attachment, Blob

Expand Down Expand Up @@ -151,17 +152,17 @@ def contribute_to_class(self, cls: type[Model], name: str, **kwargs) -> None:
)

def formfield(self, **kwargs):
from django.forms import ClearableFileInput, FileField
from anchor.forms.fields import BlobField

super().formfield(**kwargs)
defaults = {
"required": not self.blank,
"widget": ClearableFileInput,
"label": self.verbose_name.title() if self.verbose_name else None,
"label": capfirst(self.verbose_name),
"help_text": self.help_text,
}
defaults.update(kwargs)

return FileField(**defaults)
return BlobField(**defaults)

def get_forward_related_filter(self, obj):
return {
Expand Down
25 changes: 25 additions & 0 deletions anchor/templates/anchor/widgets/admin_blob_input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{% load anchor %}
<div style="display: flex; flex-wrap: wrap; gap: 0.5rem">
<div>
{% if widget.is_initial %}
{% if widget.value.is_image %}
<img style="max-width: 200px; max-height: 200px; object-fit: contain;"
src="{% variant_url widget.value resize_to_fit='200x200' %}">
{% else %}
{{ widget.initial_text }}: <a href="{{ widget.value.url }}">{{ widget.value }}</a>
{% endif %}
{% endif %}
</div>
<div>
{% if not widget.required and widget.is_initial %}
<div>
<div style="display: flex; align-items: center; gap: 0.25em">
<input type="checkbox" {% if widget.attrs.disabled %} disabled{% endif %}
name="{{ widget.checkbox_name }}" id="{{ widget.checkbox_id }}">
<label for="{{ widget.checkbox_id }}">{{ widget.clear_checkbox_label }}</label>
</div>
</div>
{% endif %}
<input type="{{ widget.type }}" name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %}>
</div>
</div>
9 changes: 9 additions & 0 deletions demo/movies/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
from django.contrib import admin

from anchor.forms.widgets import AdminBlobInput
from anchor.models.fields import SingleAttachmentField

from .models import Movie


@admin.register(Movie)
class MovieAdmin(admin.ModelAdmin):
list_display = ["title"]
search_fields = ["title"]

formfield_overrides = {
SingleAttachmentField: {
"widget": AdminBlobInput,
},
}

0 comments on commit c5d8c48

Please sign in to comment.