Skip to content

Commit

Permalink
refactor: [ACI-980] extract get_preconfigured_organozations & migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii committed May 17, 2024
1 parent 806f7f3 commit fa8e52e
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions credentials/apps/badges/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def get_fields(self, request, obj=None):
fields.append("api_key") if "api_key" not in fields else None
return fields

if str(obj.uuid) in settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {}).keys():
if str(obj.uuid) in CredlyOrganization.get_preconfigured_organizations():
fields = [field for field in fields if field != "api_key"]
fields.append("api_key_hidden")
else:
Expand All @@ -197,7 +197,7 @@ def get_readonly_fields(self, request, obj=None):
if not obj:
return readonly_fields

if str(obj.uuid) in settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {}).keys():
if str(obj.uuid) in CredlyOrganization.get_preconfigured_organizations():
readonly_fields.append("api_key_hidden")
return readonly_fields

Expand Down
2 changes: 1 addition & 1 deletion credentials/apps/badges/admin_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def clean(self):
uuid = cleaned_data.get("uuid")
api_key = cleaned_data.get("api_key")

if str(uuid) in settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {}).keys():
if str(uuid) in CredlyOrganization.get_preconfigured_organizations():
if api_key:
raise forms.ValidationError(_("You can't provide an API key for a configured organization."))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.2.20 on 2024-05-17 17:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('badges', '0020_remove_badgepenalty_description'),
]

operations = [
migrations.AlterField(
model_name='credlyorganization',
name='api_key',
field=models.CharField(blank=True, help_text='Credly API shared secret for Credly Organization.', max_length=255),
),
]
7 changes: 7 additions & 0 deletions credentials/apps/badges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def get_all_organization_ids(cls):
Get all organization IDs.
"""
return list(cls.objects.values_list("uuid", flat=True))

@classmethod
def get_preconfigured_organizations():
"""
Get preconfigured organizations.
"""
return settings.BADGES_CONFIG["credly"].get("ORGANIZATIONS", {})


class BadgeTemplate(AbstractCredential):
Expand Down

0 comments on commit fa8e52e

Please sign in to comment.