summaryrefslogtreecommitdiff
path: root/ishtar_common/models_rest.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-10-15 17:54:03 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:20:59 +0100
commit3221ede6e709fc680e1bebebcfc334cc46e889ca (patch)
tree88ad3ba1c0bc80e648cf994794652a0bb53673f1 /ishtar_common/models_rest.py
parent6fc6efad0ee57e430903b0d24c6e925652ff3714 (diff)
downloadIshtar-3221ede6e709fc680e1bebebcfc334cc46e889ca.tar.bz2
Ishtar-3221ede6e709fc680e1bebebcfc334cc46e889ca.zip
Syndication - initialize type match with a source
Diffstat (limited to 'ishtar_common/models_rest.py')
-rw-r--r--ishtar_common/models_rest.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/ishtar_common/models_rest.py b/ishtar_common/models_rest.py
index db7d21834..a74df2c8a 100644
--- a/ishtar_common/models_rest.py
+++ b/ishtar_common/models_rest.py
@@ -47,6 +47,58 @@ class ApiExternalSource(models.Model):
verbose_name = _("API - External source")
verbose_name_plural = _("API - External sources")
+ def update_matches(self, content):
+ result = {
+ "created": 0,
+ "updated": 0,
+ "deleted": 0,
+ "search_model do not exist": [],
+ "type do not exist": [],
+ }
+ updated = []
+ for search_model in content:
+ app, model_name = search_model.split(".")
+ try:
+ ct = ContentType.objects.get(app_label=app, model=model_name)
+ except ContentType.DoesNotExist:
+ result["search_model do not exist"].append(search_model)
+ continue
+ for ct_type, keys, values in content[search_model]:
+ tapp, tmodel_name = ct_type.split(".")
+ try:
+ ct_type = ContentType.objects.get(
+ app_label=tapp, model=tmodel_name
+ )
+ except ContentType.DoesNotExist:
+ result["type do not exist"].append(search_model)
+ continue
+ t_model = ct_type.model_class()
+ for slug, label in values:
+ m, created = ApiKeyMatch.objects.get_or_create(
+ source=self, search_model=ct, search_keys=keys,
+ distant_slug=slug, defaults={"distant_label": label})
+ updated = False
+ if not created and m.distant_label != label:
+ updated = True
+ m.distant_label = label
+ if not m.do_not_match and not m.local_slug:
+ slug_key = "txt_idx"
+ if hasattr(t_model, "slug"):
+ slug_key = "slug"
+ q = t_model.objects.filter(**{slug_key: m.distant_slug})
+ if q.count():
+ local_value = q.all()[0]
+ setattr(m, "local_slug", getattr(local_value, slug_key))
+ m.local_value = str(local_value)
+ updated = True
+ if updated:
+ m.save()
+ if not created:
+ result["updated"] += 1
+ if created:
+ result["created"] += 1
+ return result
+
class ApiKeyMatch(models.Model):
source = models.ForeignKey(ApiExternalSource, on_delete=models.CASCADE)
@@ -62,6 +114,8 @@ class ApiKeyMatch(models.Model):
allow_unicode=True)
local_label = models.TextField(verbose_name=_("Local value"), blank=True,
default="")
+ do_not_match = models.BooleanField(
+ verbose_name=_("Disable match for this search"), default=False)
class Meta:
verbose_name = _("API - Key match")