Enhancements for wikidata imports.
authorRadek Czajka <rczajka@rczajka.pl>
Thu, 10 Nov 2022 09:54:09 +0000 (10:54 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Thu, 10 Nov 2022 09:54:09 +0000 (10:54 +0100)
src/catalogue/models.py
src/catalogue/static/catalogue/wikidata_admin.css
src/catalogue/static/catalogue/wikidata_admin.js
src/catalogue/views.py
src/catalogue/wikidata.py
src/catalogue/wikimedia.py

index d3766d5..92b2765 100644 (file)
@@ -83,7 +83,7 @@ class Author(WikidataModel):
         year_of_death = WIKIDATA.DATE_OF_DEATH
         place_of_death = WIKIDATA.PLACE_OF_DEATH
         gender = WIKIDATA.GENDER
-        notes = "description"
+        notes = WikiMedia.append("description")
         plwiki = "plwiki"
         photo = WikiMedia.download(WIKIDATA.IMAGE)
         photo_source = WikiMedia.descriptionurl(WIKIDATA.IMAGE)
index 24e27de..b205523 100644 (file)
@@ -30,7 +30,7 @@
     background-repeat: no-repeat;
     background-position: -64px 50%;
 }
-#id_wikidata.wikidata-processing {
+#id_wikidata.wikidata-processing, .wikidata-hint.wikidata-processing {
     background-position: 100% 50%;
     transition: 10s background-position;
 }
index f13510b..d2af5f7 100644 (file)
@@ -4,6 +4,67 @@
         let model = $('body').attr('class').match(/model-([^\s]*)/)[1];
         $("#id_wikidata").each(show_wikidata_hints).on('change', show_wikidata_hints);
 
+        function add_wikidata_hint($input, val) {
+            if (val && val != $input.val()) {
+                let already_set = false;
+                let el = $('<span class="wikidata-hint">');
+
+                if (val.wd) {
+                    let iv = $input.val();
+                    if (val.id) {
+                        if (Array.isArray(iv)) {
+                            if (iv.indexOf(val.id.toString()) != -1) {
+                                already_set = true;
+                            }
+                        } else if (val.id == iv) {
+                            already_set = true;
+                        }
+                    }
+
+                    if (!already_set) {
+                        // A representation of a WD Entity.
+                        el.on('click', function() {
+                            $(this).addClass('wikidata-processing');
+                            set_value_from_wikidata_id(
+                                $input, val.model, val.wd,
+                                () => {$(this).remove();}
+                            );
+                        });
+                        el.text(val.label);
+                    }
+                } else if (val.img) {
+                    // A downloadable remote image.
+                    let img = $('<img height="32">');
+                    img.attr('src', val.img);
+                    el.append(img);
+                    el.on('click', function() {
+                        set_file_from_url(
+                            $input, val.download,
+                            () => {$(this).remove();}
+                        );
+                    });
+                } else if (val.action == 'append') {
+                    el.on('click', function() {
+                        $input.val(
+                            $input.val() + '\n' + val.value
+                        );
+                        $(this).remove();
+                    });
+                    el.text('+ ' + val.value);
+                } else {
+                    // A plain literal.
+                    el.on('click', function() {
+                        $input.val(val);
+                        $(this).remove();
+                    });
+                    el.text(val);
+                }
+                if (!already_set) {
+                    $input.parent().append(el);
+                }
+            }
+        }
+        
         function show_wikidata_hints() {
             $(".wikidata-hint").remove();
             $wdinput = $(this);
                         for (att in result) {
                             let val = result[att];
                             let $input = $("#id_" + att);
-                            if (val && val != $input.val()) {
-                                let already_set = false;
-                                let el = $('<span class="wikidata-hint">');
 
-                                if (val.wd) {
-                                    if (val.id && val.id == $input.val()) {
-                                        already_set = true;
-                                    } else {
-                                        // A representation of a WD Entity.
-                                        el.on('click', function() {
-                                            set_value_from_wikidata_id(
-                                                $input, val.model, val.wd,
-                                                () => {$(this).remove();}
-                                            );
-                                        });
-                                        el.text(val.label);
-                                    }
-                                } else if (val.img) {
-                                    // A downloadable remote image.
-                                    let img = $('<img height="32">');
-                                    img.attr('src', val.img);
-                                    el.append(img);
-                                    el.on('click', function() {
-                                        set_file_from_url(
-                                            $input, val.download,
-                                            () => {$(this).remove();}
-                                        );
-                                    });
-                                } else {
-                                    // A plain literal.
-                                    el.on('click', function() {
-                                        $input.val(val);
-                                        $(this).remove();
-                                    });
-                                    el.text(val);
-                                }
-                                if (!already_set) {
-                                    $input.parent().append(el);
+                            if (Array.isArray(val)) {
+                                for (singleValue of val) {
+                                    add_wikidata_hint($input, singleValue);
                                 }
+                            } else {
+                                add_wikidata_hint($input, val);
                             }
                         };
 
index 95f33fd..889a832 100644 (file)
@@ -178,6 +178,7 @@ class WikidataView(APIView):
                     d[fieldname] = [
                         {
                             "model": type(item)._meta.model_name,
+                            "id": item.pk,
                             "wd": item.wikidata,
                             "label": str(item) or item._wikidata_label
                         } for item in d[fieldname].all()
index 90f1a5c..b5ce07b 100644 (file)
@@ -159,7 +159,12 @@ class WikidataModel(models.Model):
                 wdvalue = wdvalue.label.get(language, str(wdvalue.label))
 
             if not skip_set:
-                setattr(self, attname, wdvalue)
+                try:
+                    wdvalue = model_field.to_python(wdvalue)
+                except:
+                    pass
+                else:
+                    setattr(self, attname, wdvalue)
 
     def wikidata_link(self):
         if self.wikidata:
index 534ca17..fe6b1c6 100644 (file)
@@ -50,7 +50,21 @@ class WikiMedia:
             return Downloadable(download_url)
         return transform
 
+    @classmethod
+    def append(cls, arg):
+        def transform(get_value):
+            value = get_value(arg)
+            return Appendable(value)
+        return transform
 
+
+class Appendable(str):
+    def as_hint_json(self):
+        return {
+            'value': self,
+            'action': 'append',
+        }
+    
 class Downloadable:
     def __init__(self, url):
         self.url = url