Skip to content

Commit

Permalink
fix: fix accredible models redirect and handle errors on sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrylo-kh committed Jan 14, 2025
1 parent c366335 commit 2c042b4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 11 deletions.
38 changes: 29 additions & 9 deletions credentials/apps/badges/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
PenaltyDataRuleForm,
PenaltyDataRuleFormSet,
)
from credentials.apps.badges.exceptions import BadgeProviderError
from credentials.apps.badges.models import (
AccredibleAPIConfig,
AccredibleBadge,
Expand All @@ -38,6 +39,12 @@
from credentials.apps.badges.toggles import is_badges_enabled


ADMIN_CHANGE_VIEW_REVERSE_NAMES = {
CredlyBadgeTemplate.ORIGIN: "admin:badges_credlybadgetemplate_change",
AccredibleGroup.ORIGIN: "admin:badges_accrediblegroup_change",
}


class BadgeRequirementInline(admin.TabularInline):
"""
Badge template requirement inline setup.
Expand Down Expand Up @@ -388,14 +395,19 @@ def template_link(self, instance):
"""
Interactive link to parent (badge template).
"""
url = reverse("admin:badges_credlybadgetemplate_change", args=[instance.template.pk])
reverse_name = ADMIN_CHANGE_VIEW_REVERSE_NAMES.get(instance.template.origin, "admin:index")
reverse_args = [] if reverse_name == "admin:index" else [instance.template.pk]

url = reverse(reverse_name, args=reverse_args)
return format_html('<a href="{}">{}</a>', url, instance.template)

template_link.short_description = _("badge template")

def response_change(self, request, obj):
if "_save" in request.POST:
return HttpResponseRedirect(reverse("admin:badges_credlybadgetemplate_change", args=[obj.template.pk]))
reverse_name = ADMIN_CHANGE_VIEW_REVERSE_NAMES.get(obj.template.origin, "admin:index")
reverse_args = [] if reverse_name == "admin:index" else [obj.template.pk]
return HttpResponseRedirect(reverse(reverse_name, args=reverse_args))
return super().response_change(request, obj)


Expand Down Expand Up @@ -445,7 +457,9 @@ def template_link(self, instance):
"""
Interactive link to parent (badge template).
"""
url = reverse("admin:badges_credlybadgetemplate_change", args=[instance.template.pk])
reverse_name = ADMIN_CHANGE_VIEW_REVERSE_NAMES.get(instance.template.origin, "admin:index")
reverse_args = [] if reverse_name == "admin:index" else [instance.template.pk]
url = reverse(reverse_name, args=reverse_args)
return format_html('<a href="{}">{}</a>', url, instance.template)

template_link.short_description = _("badge template")
Expand All @@ -460,7 +474,9 @@ def formfield_for_manytomany(self, db_field, request, **kwargs):

def response_change(self, request, obj):
if "_save" in request.POST:
return HttpResponseRedirect(reverse("admin:badges_credlybadgetemplate_change", args=[obj.template.pk]))
reverse_name = ADMIN_CHANGE_VIEW_REVERSE_NAMES.get(obj.template.origin, "admin:index")
reverse_args = [] if reverse_name == "admin:index" else [obj.template.pk]
return HttpResponseRedirect(reverse(reverse_name, args=reverse_args))
return super().response_change(request, obj)


Expand Down Expand Up @@ -562,11 +578,15 @@ def sync_groups(self, request, queryset):
"""
site = get_current_site(request)
for api_config in queryset:
call_command(
"sync_accredible_groups",
api_config_id=api_config.id,
site_id=site.id,
)
try:
call_command(
"sync_accredible_groups",
api_config_id=api_config.id,
site_id=site.id,
)
except BadgeProviderError as exc:
messages.set_level(request, messages.ERROR)
messages.error(request, _("Failed to sync groups for API config: {}. {}").format(api_config.name, exc))

messages.success(request, _("Accredible groups were successfully updated."))

Expand Down
6 changes: 4 additions & 2 deletions credentials/apps/badges/base_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ def _raise_for_error(self, response):
try:
response.raise_for_status()
except HTTPError:
logger.error(f"Error while processing request: {response.status_code} - {response.text}")
raise BadgeProviderError(f"{self.PROVIDER_NAME} API:{response.text}({response.status_code})")
logger.error(
f"Error while processing {self.PROVIDER_NAME} request: {response.status_code} - {response.text}"
)
raise BadgeProviderError(f"{response.text} Status({response.status_code})")

@property
def base_api_url(self):
Expand Down
Binary file modified credentials/conf/locale/eo/LC_MESSAGES/django.mo
Binary file not shown.
6 changes: 6 additions & 0 deletions credentials/conf/locale/eo/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ msgstr ""
msgid "badge template"
msgstr "ßädgé témpläté Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт#"

#: apps/badges/admin.py
msgid "Failed to sync groups for API config: {}. {}"
msgstr ""
"Fäïléd tö sýnç gröüps för ÀPÌ çönfïg: {}. {} Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, "
"¢σηѕє¢тєтυя #"

#: apps/badges/admin.py
msgid "Accredible groups were successfully updated."
msgstr ""
Expand Down
Binary file modified credentials/conf/locale/rtl/LC_MESSAGES/django.mo
Binary file not shown.
4 changes: 4 additions & 0 deletions credentials/conf/locale/rtl/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ msgstr "Ⱥɔʇᴉʌǝ bɐdƃǝ ʇǝɯdlɐʇǝ ɯnsʇ ɥɐʌǝ ɐʇ lǝɐsʇ øn
msgid "badge template"
msgstr "bɐdƃǝ ʇǝɯdlɐʇǝ"

#: apps/badges/admin.py
msgid "Failed to sync groups for API config: {}. {}"
msgstr "Fɐᴉlǝd ʇø sʎnɔ ƃɹønds ɟøɹ ȺⱣƗ ɔønɟᴉƃ: {}. {}"

#: apps/badges/admin.py
msgid "Accredible groups were successfully updated."
msgstr "Ⱥɔɔɹǝdᴉblǝ ƃɹønds ʍǝɹǝ snɔɔǝssɟnllʎ nddɐʇǝd."
Expand Down

0 comments on commit 2c042b4

Please sign in to comment.