+
+ def wikidata_populate_field(self, client, entity, attname, wd, save, lang):
+ 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 = None
+ if wd == "description":
+ wdvalue = entity.description.get(lang, str(entity.description))
+ elif wd == "label":
+ wdvalue = entity.label.get(lang, str(entity.label))
+ else:
+ try:
+ # TODO: lang?
+ wdvalue = entity.get(client.get(wd))
+ except DatavalueError:
+ pass
+
+ self.set_field_from_wikidata(attname, wdvalue, save=save)
+
+ def wikidata_populate(self, save=True):
+ 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)
+ if hasattr(Wikidata, '_supplement'):
+ for attname, wd in Wikidata._supplement(self):
+ self.wikidata_populate_attribute(client, entity, attname, wd, save=save)
+
+ def wikidata_fields_for_attribute(self, attname):
+ field = getattr(type(self), attname)
+ if type(self) in translator._registry:
+ opts = translator.get_options_for_model(type(self))
+ if attname in opts.fields:
+ tfields = opts.fields[attname]
+ for tf in tfields:
+ yield tf.name, tf.language
+ return