+class NotableBookInline(OrderableAdmin, admin.TabularInline):
+ model = models.NotableBook
+ autocomplete_fields = ['book']
+ ordering_field_hide_input = True
+
+
+class WoblinkCatalogueWidget(forms.Select):
+ class Media:
+ js = (
+ "admin/js/vendor/jquery/jquery.min.js",
+ "admin/js/vendor/select2/select2.full.min.js",
+ "admin/js/vendor/select2/i18n/pl.js",
+ "catalogue/woblink_admin.js",
+ "admin/js/jquery.init.js",
+ "admin/js/autocomplete.js",
+ )
+ css = {
+ "screen": (
+ "admin/css/vendor/select2/select2.min.css",
+ "admin/css/autocomplete.css",
+ ),
+ }
+
+ def __init__(self):
+ self.attrs = {}
+ self.choices = []
+ self.field = None
+
+ def get_url(self):
+ return reverse('catalogue_woblink_autocomplete', args=[self.category])
+
+ def build_attrs(self, base_attrs, extra_attrs=None):
+ attrs = super().build_attrs(base_attrs, extra_attrs=extra_attrs)
+ attrs.setdefault("class", "")
+ attrs.update(
+ {
+ "data-ajax--cache": "true",
+ "data-ajax--delay": 250,
+ "data-ajax--type": "GET",
+ "data-ajax--url": self.get_url(),
+ "data-app-label": '',
+ "data-model-name": '',
+ "data-field-name": '',
+ "data-theme": "admin-autocomplete",
+ "data-allow-clear": json.dumps(not self.is_required),
+
+ "data-placeholder": "", # Chyba że znaleziony?
+ "lang": "pl",
+ "class": attrs["class"]
+ + (" " if attrs["class"] else "")
+ + "admin-autocomplete admin-woblink",
+ }
+ )
+ return attrs
+
+ def optgroups(self, name, value, attrs=None):
+ """ Add synthetic option for keeping the current value. """
+ return [(None, [
+ self.create_option(
+ name,
+ v,
+ '(bez zmian)',
+ selected=True,
+ index=index,
+ attrs=attrs,
+ )
+ for index, v in enumerate(value)
+ ], 0)]
+
+class WoblinkAuthorWidget(WoblinkCatalogueWidget):
+ category = 'author'
+
+class AuthorForm(forms.ModelForm):
+ class Meta:
+ model = models.Author
+ fields = '__all__'
+ widgets = {
+ 'woblink': WoblinkAuthorWidget,
+ }
+
+class AuthorAdmin(WikidataAdminMixin, TabbedTranslationAdmin):
+ form = AuthorForm