diff options
Diffstat (limited to 'ishtar_common')
| -rw-r--r-- | ishtar_common/admin.py | 4 | ||||
| -rw-r--r-- | ishtar_common/models_rest.py | 60 | ||||
| -rw-r--r-- | ishtar_common/rest.py | 22 | 
3 files changed, 50 insertions, 36 deletions
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 94306f6b6..daee5d521 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -2247,8 +2247,7 @@ update_types_from_source.short_description = _("Update types from source")  def generate_match_document(modeladmin, request, queryset):      return_url = (          reverse( -            "admin:%s_%s_changelist" -            % (modeladmin.model._meta.app_label, modeladmin.model._meta.model_name) +            f"admin:{modeladmin.model._meta.app_label}_{modeladmin.model._meta.model_name}_changelist"          )          + "?"          + urllib.parse.urlencode(request.GET) @@ -2257,7 +2256,6 @@ def generate_match_document(modeladmin, request, queryset):          send_error_message(              request,              str(_("Select only one source.")), -            return_url,              message_type=messages.WARNING,          )          return HttpResponseRedirect(return_url) diff --git a/ishtar_common/models_rest.py b/ishtar_common/models_rest.py index f2b060bef..40398bca7 100644 --- a/ishtar_common/models_rest.py +++ b/ishtar_common/models_rest.py @@ -187,9 +187,7 @@ class ApiExternalSource(models.Model):          if not q.count():              return          sm = q.all()[0].search_model -        sheet.Name = ( -            f"{sm.app_label}.{sm.model}-{model._meta.app_label}.{model.__name__.lower()}" -        ) +        sheet.Name = f"{sm.app_label}.{sm.model}-{model._meta.app_label}.{model.__name__.lower()}"          for col_number, column in enumerate(              (_("Distant key"), _("Distant label"), _("Local"))          ): @@ -198,8 +196,12 @@ class ApiExternalSource(models.Model):              cell.CharWeight = 150              cell.setString(str(column)) +        current_list = []          for idx, match in enumerate(q.all()):              cell = sheet.getCellByPosition(0, idx + 1) +            if match.distant_slug in current_list: +                continue +            current_list.append(match.distant_slug)              cell.setString(match.distant_slug)              cell = sheet.getCellByPosition(1, idx + 1)              cell.setString(match.distant_label) @@ -252,9 +254,7 @@ class ApiExternalSource(models.Model):                  errors.append(str(_(f"{sheet_name} is not a correct sheet name.")))                  continue              data = uno.sheet_get_data(sheet) -            base_q = ApiKeyMatch.objects.filter( -                source=self, search_model=search_model, associated_type=ct -            ) +            base_q = ApiKeyMatch.objects.filter(source=self, associated_type=ct)              model = ct.model_class()              for idx_line, line in enumerate(data):                  if not idx_line:  # header @@ -262,36 +262,38 @@ class ApiExternalSource(models.Model):                  distant_key, distant_label, local_name = line                  q = base_q.filter(distant_slug=distant_key, distant_label=distant_label)                  if not q.count(): -                    errors.append( -                        str( -                            _( -                                f"{ctype} - {distant_key}, {distant_label} not referenced in the database." +                    if distant_key: +                        errors.append( +                            str( +                                _( +                                    f"{ctype} - {distant_key}, {distant_label} not referenced in the database." +                                )                              )                          ) -                    )                      continue                  if q.filter(local_label=local_name).count():                      # no change                      continue -                api_key = q.all()[0] -                key_name = "slug" -                if hasattr(model, "txt_idx"): -                    key_name = "txt_idx" -                q = model.objects.filter(label=local_name) -                if not q.count(): -                    errors.append( -                        str( -                            _( -                                f"{ctype} - {local_name} do not exists in local database." +                for api_key in q.all(): +                    key_name = "slug" +                    if hasattr(model, "txt_idx"): +                        key_name = "txt_idx" +                    q = model.objects.filter(label=local_name) +                    if not q.count(): +                        if local_name: +                            errors.append( +                                str( +                                    _( +                                        f"{ctype} - {local_name} do not exists in local database." +                                    ) +                                )                              ) -                        ) -                    ) -                    continue -                tpe = q.all()[0] -                api_key.local_slug = getattr(tpe, key_name) -                api_key.local_label = local_name -                api_key.save() -                updated += 1 +                        continue +                    tpe = q.all()[0] +                    api_key.local_slug = getattr(tpe, key_name) +                    api_key.local_label = local_name +                    api_key.save() +                    updated += 1          return {"errors": errors, "updated": updated} diff --git a/ishtar_common/rest.py b/ishtar_common/rest.py index 90f61f2aa..1ca8824b0 100644 --- a/ishtar_common/rest.py +++ b/ishtar_common/rest.py @@ -9,6 +9,7 @@ from rest_framework.response import Response  from rest_framework.views import APIView  from ishtar_common import models_rest +from ishtar_common.models_common import GeneralType  from ishtar_common.views_item import get_item  from ishtar_common.serializers_utils import generic_get_results @@ -88,12 +89,25 @@ class FacetAPIView(APIView):                  continue              ct_model = f"{model._meta.app_label}.{model._meta.model_name}"              values[ct_model] = [] -            for type in select_form.TYPES: +            search_types = [ +                (search_type.key, search_type.model) +                for search_type in select_form.TYPES +            ] +            for key in select_form.base_fields: +                field = select_form.base_fields[key] +                associated_model = getattr(field.widget, "associated_model", None) +                if ( +                    associated_model +                    and issubclass(associated_model, GeneralType) +                    and (key, associated_model) not in search_types +                ): +                    search_types.append((key, associated_model)) +            for associated_key, associated_model in search_types:                  values_ct = [] -                for item in type.model.objects.filter(available=True).all(): +                for item in associated_model.objects.filter(available=True).all():                      key = item.slug if hasattr(item, "slug") else item.txt_idx                      values_ct.append((key, str(item))) -                search_key = model.ALT_NAMES[type.key].search_key +                search_key = model.ALT_NAMES[associated_key].search_key                  search_keys = []                  for language_code, language_lbl in settings.LANGUAGES:                      activate(language_code) @@ -101,7 +115,7 @@ class FacetAPIView(APIView):                      deactivate()                  values[ct_model].append(                      [ -                        f"{type.model._meta.app_label}.{type.model._meta.model_name}", +                        f"{associated_model._meta.app_label}.{associated_model._meta.model_name}",                          search_keys,                          values_ct,                      ]  | 
