summaryrefslogtreecommitdiff
path: root/ishtar_common/admin.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2021-10-19 23:02:09 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:20:59 +0100
commitfede10bc3ed5a9df9325d3c9d671a167760a9381 (patch)
treec6a83be154a7e11b114939c2f7fd0b978795fdbd /ishtar_common/admin.py
parent66ba898ce0797fd51a826f6ea654b87a3487e8bb (diff)
downloadIshtar-fede10bc3ed5a9df9325d3c9d671a167760a9381.tar.bz2
Ishtar-fede10bc3ed5a9df9325d3c9d671a167760a9381.zip
Syndication - update association from match document WIP
Diffstat (limited to 'ishtar_common/admin.py')
-rw-r--r--ishtar_common/admin.py80
1 files changed, 61 insertions, 19 deletions
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py
index 761d07b4d..a974823a5 100644
--- a/ishtar_common/admin.py
+++ b/ishtar_common/admin.py
@@ -2162,7 +2162,7 @@ def update_types_from_source(modeladmin, request, queryset):
return send_error_message(
request,
str(_("Timeout: failed to join {}.")).format(source.url),
- return_url
+ return_url,
)
if response.status_code != 200:
return send_error_message(
@@ -2172,7 +2172,7 @@ def update_types_from_source(modeladmin, request, queryset):
"Bad response for {}. Response status code {} != 200. Check your key?"
)
).format(source.name, response.status_code),
- return_url
+ return_url,
)
try:
content = response.json()
@@ -2180,7 +2180,7 @@ def update_types_from_source(modeladmin, request, queryset):
return send_error_message(
request,
str(_("Response of {} is not a valid JSON message.")).format(source.url),
- return_url
+ return_url,
)
result = source.update_matches(content)
if result.get("created", None):
@@ -2202,32 +2202,41 @@ def update_types_from_source(modeladmin, request, queryset):
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'])
+ 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?")),
+ 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'])
+ 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?")),
+ str(
+ _(
+ f"Theses types have not been found: {missing_types}. Not the same Ishtar version?"
+ )
+ ),
)
- return response
+ return HttpResponseRedirect(return_url)
+
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)
- )
- + "?"
- + urllib.parse.urlencode(request.GET)
+ reverse(
+ "admin:%s_%s_changelist"
+ % (modeladmin.model._meta.app_label, modeladmin.model._meta.model_name)
+ )
+ + "?"
+ + urllib.parse.urlencode(request.GET)
)
if queryset.count() != 1:
return send_error_message(
@@ -2253,12 +2262,38 @@ def generate_match_document(modeladmin, request, queryset):
return response
-generate_match_document.short_description = _("Generate match document")
+def update_association_from_match_document(modeladmin, request, queryset):
+ return_url = (
+ reverse(
+ "admin:%s_%s_changelist"
+ % (modeladmin.model._meta.app_label, modeladmin.model._meta.model_name)
+ )
+ + "?"
+ + urllib.parse.urlencode(request.GET)
+ )
+ if queryset.count() != 1:
+ return send_error_message(
+ request,
+ str(_("Select only one source.")),
+ return_url,
+ message_type=messages.WARNING,
+ )
+ result = queryset.all()[0].update_from_match_document()
+ return HttpResponseRedirect(return_url)
+
+
+update_association_from_match_document.short_description = _(
+ "Update association from match document"
+)
class ApiExternalSourceAdmin(admin.ModelAdmin):
model = models_rest.ApiExternalSource
- actions = [update_types_from_source, generate_match_document]
+ actions = [
+ update_types_from_source,
+ generate_match_document,
+ update_association_from_match_document,
+ ]
list_display = ("name", "url", "key")
@@ -2267,9 +2302,16 @@ admin_site.register(models_rest.ApiExternalSource, ApiExternalSourceAdmin)
class ApiKeyMatchAdmin(admin.ModelAdmin):
model = models_rest.ApiKeyMatch
- list_display = ["source", "search_model", "associated_type",
- "distant_slug", "distant_label", "local_slug",
- "local_label", "do_not_match"]
+ list_display = [
+ "source",
+ "search_model",
+ "associated_type",
+ "distant_slug",
+ "distant_label",
+ "local_slug",
+ "local_label",
+ "do_not_match",
+ ]
list_filter = ["source", "do_not_match"]
search_fields = ["associated_type__model", "distant_slug", "distant_label"]
actions = [