diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-10-18 17:44:54 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:20:59 +0100 | 
| commit | d15d5ec54c73e657370cc8d4d7e1c1e7e6f40af4 (patch) | |
| tree | 24c9a3bc567b906549ac66a96e66b99e60d002d5 /ishtar_common | |
| parent | 3221ede6e709fc680e1bebebcfc334cc46e889ca (diff) | |
| download | Ishtar-d15d5ec54c73e657370cc8d4d7e1c1e7e6f40af4.tar.bz2 Ishtar-d15d5ec54c73e657370cc8d4d7e1c1e7e6f40af4.zip | |
Syndication - delete unused matches - display match result - add a field for associated type in match
Diffstat (limited to 'ishtar_common')
| -rw-r--r-- | ishtar_common/admin.py | 33 | ||||
| -rw-r--r-- | ishtar_common/migrations/0217_auto_20211018_1741.py (renamed from ishtar_common/migrations/0217_auto_20211015_1728.py) | 7 | ||||
| -rw-r--r-- | ishtar_common/models_rest.py | 75 | 
3 files changed, 86 insertions, 29 deletions
| diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 39d7de021..59dc99f15 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -2183,7 +2183,38 @@ def update_types_from_source(modeladmin, request, queryset):              return_url          )      result = source.update_matches(content) -    print(result) +    if result.get("created", None): +        messages.add_message( +            request, +            messages.INFO, +            str(_(f"{result['created']} matches created")), +        ) +    if result.get("updated", None): +        messages.add_message( +            request, +            messages.INFO, +            str(_(f"{result['updated']} matches updated")), +        ) +    if result.get("deleted", None): +        messages.add_message( +            request, +            messages.INFO, +            str(_(f"{result['deleted']} matches deleted")), +        ) +    if result.get("search_model do not exist", None): +        missing_search_models = ", ".join(result['search_model do not exist']) +        messages.add_message( +            request, +            messages.INFO, +            str(_(f"Theses search models have not been found: {missing_search_models}. Not the same Ishtar version?")), +        ) +    if result.get("type do not exist", None): +        missing_types = ", ".join(result['type do not exist']) +        messages.add_message( +            request, +            messages.INFO, +            str(_(f"Theses types have not been found: {missing_types}. Not the same Ishtar version?")), +        )      return response diff --git a/ishtar_common/migrations/0217_auto_20211015_1728.py b/ishtar_common/migrations/0217_auto_20211018_1741.py index 47bfa7c6e..d79224b5a 100644 --- a/ishtar_common/migrations/0217_auto_20211015_1728.py +++ b/ishtar_common/migrations/0217_auto_20211018_1741.py @@ -1,4 +1,4 @@ -# Generated by Django 2.2.24 on 2021-10-15 17:28 +# Generated by Django 2.2.24 on 2021-10-18 17:41  from django.conf import settings  import django.contrib.postgres.fields @@ -10,8 +10,8 @@ import django.db.models.deletion  class Migration(migrations.Migration):      dependencies = [ -        ('contenttypes', '0002_remove_content_type_name'),          ('auth', '0011_update_proxy_permissions'), +        ('contenttypes', '0002_remove_content_type_name'),          ('ishtar_common', '0216_auto_20210805_1703'),      ] @@ -198,7 +198,8 @@ class Migration(migrations.Migration):                  ('local_slug', models.SlugField(allow_unicode=True, max_length=200, verbose_name='Local key')),                  ('local_label', models.TextField(blank=True, default='', verbose_name='Local value')),                  ('do_not_match', models.BooleanField(default=False, verbose_name='Disable match for this search')), -                ('search_model', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType', verbose_name='Search model')), +                ('associated_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='key_match_types', to='contenttypes.ContentType', verbose_name='Associated type')), +                ('search_model', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='key_match_search_models', to='contenttypes.ContentType', verbose_name='Search model')),                  ('source', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.ApiExternalSource')),              ],              options={ diff --git a/ishtar_common/models_rest.py b/ishtar_common/models_rest.py index a74df2c8a..566a73127 100644 --- a/ishtar_common/models_rest.py +++ b/ishtar_common/models_rest.py @@ -6,9 +6,7 @@ from django.contrib.postgres.fields import ArrayField  from ishtar_common.utils import ugettext_lazy as _ -MAIN_CONTENT_TYPES = ( -    ("archaeological_operations", "operation"), -) +MAIN_CONTENT_TYPES = (("archaeological_operations", "operation"),)  class ApiUser(models.Model): @@ -29,8 +27,10 @@ class ApiSearchModel(models.Model):      user = models.ForeignKey(ApiUser, on_delete=models.CASCADE)      content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)      limit_query = models.TextField( -        verbose_name=_("Limit query"), blank=True, null=True, -        help_text=_("Search query add to each request") +        verbose_name=_("Limit query"), +        blank=True, +        null=True, +        help_text=_("Search query add to each request"),      )      class Meta: @@ -55,7 +55,6 @@ class ApiExternalSource(models.Model):              "search_model do not exist": [],              "type do not exist": [],          } -        updated = []          for search_model in content:              app, model_name = search_model.split(".")              try: @@ -66,17 +65,22 @@ class ApiExternalSource(models.Model):              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 -                    ) +                    ct_type = ContentType.objects.get(app_label=tapp, model=tmodel_name)                  except ContentType.DoesNotExist: -                    result["type do not exist"].append(search_model) +                    result["type do not exist"].append(ct_type)                      continue                  t_model = ct_type.model_class() +                current_matches = []                  for slug, label in values: +                    current_matches.append(slug)                      m, created = ApiKeyMatch.objects.get_or_create( -                        source=self, search_model=ct, search_keys=keys, -                        distant_slug=slug, defaults={"distant_label": label}) +                        source=self, +                        search_model=ct, +                        associated_type=ct_type, +                        search_keys=keys, +                        distant_slug=slug, +                        defaults={"distant_label": label}, +                    )                      updated = False                      if not created and m.distant_label != label:                          updated = True @@ -97,25 +101,46 @@ class ApiExternalSource(models.Model):                              result["updated"] += 1                      if created:                          result["created"] += 1 +                # delete removed keys +                q = ApiKeyMatch.objects.filter( +                    source=self, search_model=ct, associated_type=ct_type, +                ).exclude(distant_slug__in=current_matches) +                result["deleted"] += q.count() +                q.delete()          return result +    def generate_match_csv(self): +        pass +  class ApiKeyMatch(models.Model):      source = models.ForeignKey(ApiExternalSource, on_delete=models.CASCADE) -    search_model = models.ForeignKey(ContentType, on_delete=models.CASCADE, -                                     verbose_name=_("Search model")) -    search_keys = ArrayField(models.CharField(max_length=200), -                             verbose_name=_("Search keys"), blank=True) -    distant_slug = models.SlugField(verbose_name=_("Distant key"), max_length=200, -                                    allow_unicode=True) -    distant_label = models.TextField(verbose_name=_("Distant value"), blank=True, -                                     default="") -    local_slug = models.SlugField(verbose_name=_("Local key"), max_length=200, -                                  allow_unicode=True) -    local_label = models.TextField(verbose_name=_("Local value"), blank=True, -                                   default="") +    search_model = models.ForeignKey( +        ContentType, on_delete=models.CASCADE, verbose_name=_("Search model"), +        related_name="key_match_search_models" +    ) +    associated_type = models.ForeignKey( +        ContentType, on_delete=models.CASCADE, verbose_name=_("Associated type"), +        related_name="key_match_types" +    ) +    search_keys = ArrayField( +        models.CharField(max_length=200), verbose_name=_("Search keys"), blank=True +    ) +    distant_slug = models.SlugField( +        verbose_name=_("Distant key"), max_length=200, allow_unicode=True +    ) +    distant_label = models.TextField( +        verbose_name=_("Distant value"), blank=True, default="" +    ) +    local_slug = models.SlugField( +        verbose_name=_("Local key"), max_length=200, 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) +        verbose_name=_("Disable match for this search"), default=False +    )      class Meta:          verbose_name = _("API - Key match") | 
