+
+ def wikidata_populate_field(self, client, entity, attname, wd, save, lang, force=False):
+ if not force:
+ model_field = self._meta.get_field(attname)
+ if isinstance(model_field, models.ManyToManyField):
+ if getattr(self, attname).all().exists():
+ return
+ else:
+ if getattr(self, attname):
+ return
+
+ wdvalue = self.get_wikidata_property(client, entity, wd, lang)
+
+ self.set_field_from_wikidata(attname, wdvalue, save=save)
+
+ def wikidata_populate(self, save=True, force=False):
+ Wikidata = type(self).Wikidata
+ client = Client()
+ # Probably should getlist
+ entity = client.get(self.wikidata)
+ for attname in dir(Wikidata):
+ if attname.startswith("_"):
+ continue
+ wd = getattr(Wikidata, attname)
+
+ self.wikidata_populate_attribute(client, entity, attname, wd, save=save, force=force)
+ if hasattr(Wikidata, '_supplement'):
+ for attname, wd in Wikidata._supplement(self):
+ self.wikidata_populate_attribute(client, entity, attname, wd, save=save, force=force)
+
+ def wikidata_fields_for_attribute(self, attname):
+ field = getattr(type(self), attname)
+ if type(self) in translator._registry:
+ try:
+ opts = translator.get_options_for_model(type(self))
+ except:
+ pass
+ else:
+ if attname in opts.fields:
+ tfields = opts.fields[attname]
+ for tf in tfields:
+ yield tf.name, tf.language
+ return
+
+ yield attname, settings.LANGUAGE_CODE
+
+ def wikidata_populate_attribute(self, client, entity, attname, wd, save, force=False):
+ for fieldname, lang in self.wikidata_fields_for_attribute(attname):
+ self.wikidata_populate_field(client, entity, fieldname, wd, save, lang, force=force)
+