forked from openedx/credentials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [ACI-331, ACI-357, ACI-363] badge templates creation (#58)
- Loading branch information
1 parent
466953f
commit 5f6a392
Showing
10 changed files
with
202 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
credentials/apps/badges/distribution/credly/credly_badges/forms.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Django forms for the credly badges | ||
""" | ||
|
||
from django import forms | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from .models import CredlyOrganization | ||
from .rest_api import CredlyAPIClient | ||
from .exceptions import CredlyAPIError | ||
|
||
class CredlyOrganizationAdminForm(forms.ModelForm): | ||
class Meta: | ||
model = CredlyOrganization | ||
fields = "__all__" | ||
|
||
def clean(self): | ||
""" | ||
Validate that organization is existing on Credly services. | ||
""" | ||
cleaned_data = super().clean() | ||
|
||
uuid = cleaned_data.get("uuid") | ||
api_key = cleaned_data.get("api_key") | ||
|
||
try: | ||
credly_api_client = CredlyAPIClient(uuid, api_key) | ||
credly_api_client.fetch_organization() | ||
except CredlyAPIError: | ||
raise forms.ValidationError(_('Invalid organization ID or API key. Organization not found on Credly services.')) | ||
|
||
return cleaned_data |
Empty file.
Empty file.
35 changes: 35 additions & 0 deletions
35
...istribution/credly/credly_badges/management/commands/sync_organization_badge_templates.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import logging | ||
from django.core.management.base import BaseCommand | ||
from credly_badges.utils import sync_badge_templates_for_organization | ||
from credly_badges.models import CredlyOrganization | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Sync badge templates for a specific organization or all organizations' | ||
|
||
def add_arguments(self, parser): | ||
parser.add_argument('--organization_id', type=str, help='UUID of the organization.') | ||
|
||
def handle(self, *args, **options): | ||
""" | ||
Sync badge templates for a specific organization or all organizations. | ||
Usage: | ||
./manage.py sync_organization_badge_templates | ||
./manage.py sync_organization_badge_templates --organization_id c117c179-81b1-4f7e-a3a1-e6ae30568c13 | ||
""" | ||
organization_id = options.get('organization_id') | ||
|
||
if organization_id: | ||
logger.info(f'Syncing badge templates for single organization: {organization_id}') | ||
sync_badge_templates_for_organization(organization_id) | ||
else: | ||
all_organization_ids = CredlyOrganization.get_all_organization_ids() | ||
logger.info(f'Organization id was not provided. Syncing badge templates for all organizations: {all_organization_ids}') | ||
for organization_id in all_organization_ids: | ||
sync_badge_templates_for_organization(organization_id) | ||
|
||
logger.info('Done.') |
39 changes: 39 additions & 0 deletions
39
...ibution/credly/credly_badges/migrations/0002_alter_credlyorganization_api_key_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Generated by Django 4.2.7 on 2024-01-26 11:47 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('credly_badges', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='credlyorganization', | ||
name='api_key', | ||
field=models.CharField(help_text='Credly API shared secret for organization.', max_length=255), | ||
), | ||
migrations.AlterField( | ||
model_name='credlyorganization', | ||
name='name', | ||
field=models.CharField(help_text='Organization display name.', max_length=255), | ||
), | ||
migrations.AlterField( | ||
model_name='credlyorganization', | ||
name='uuid', | ||
field=models.UUIDField(help_text='Unique organization ID.', unique=True), | ||
), | ||
migrations.CreateModel( | ||
name='BadgeTemplate', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('uuid', models.UUIDField(help_text='Unique badge template ID.', unique=True)), | ||
('name', models.CharField(help_text='Badge template name.', max_length=255)), | ||
('state', models.CharField(choices=[('active', 'Active'), ('archived', 'Archived'), ('draft', 'Draft'), ('inactive', 'Inactive')], default='inactive', help_text='State of the badge template.', max_length=255)), | ||
('organization', models.ForeignKey(help_text='Organization of the badge template.', on_delete=django.db.models.deletion.CASCADE, to='credly_badges.credlyorganization')), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
credentials/apps/badges/distribution/credly/credly_badges/utils.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from django.shortcuts import get_object_or_404 | ||
|
||
from .rest_api import CredlyAPIClient | ||
from .models import CredlyOrganization, BadgeTemplate | ||
|
||
|
||
def sync_badge_templates_for_organization(organization_id): | ||
""" | ||
Sync badge templates for a specific organization and create records in the database. | ||
Args: | ||
organization_id (str): UUID of the organization. | ||
Raises: | ||
Http404: If organization is not found. | ||
""" | ||
organization = get_object_or_404(CredlyOrganization, uuid=organization_id) | ||
|
||
credly_api_client = CredlyAPIClient(organization_id, organization.api_key) | ||
badge_templates_data = credly_api_client.fetch_badge_templates() | ||
|
||
for badge_template_data in badge_templates_data.get('data', []): | ||
BadgeTemplate.objects.update_or_create( | ||
uuid=badge_template_data.get('id'), | ||
defaults={ | ||
'name': badge_template_data.get('name'), | ||
'organization': organization, | ||
} | ||
) |