Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add new admin action that allows to create external certificates #85

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eox_nelp/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""General admin module file.
Register all the nelp admin models.
"""
from eox_nelp.admin.certificates import * # noqa: F401
from eox_nelp.admin.course_creators import * # noqa: F401
from eox_nelp.course_experience.admin import * # noqa: F401
from eox_nelp.notifications.admin import * # noqa: F401
73 changes: 73 additions & 0 deletions eox_nelp/admin/certificates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""Certificates admin file.
Contains all the nelp admin models for certificates.

Classes:
NelpGeneratedCertificateAdmin: EoxNelp Certificates admin class.

Functions:
create_external_certificate_action: Allow to create external certificates.
"""
from django.contrib import admin
from django.utils import timezone
from eox_core.edxapp_wrapper.certificates import get_generated_certificate
from openedx_events.learning.data import CertificateData, CourseData, UserData, UserPersonalData

from eox_nelp.admin.register_admin_model import register_admin_model as register
from eox_nelp.edxapp_wrapper.certificates import GeneratedCertificateAdmin
from eox_nelp.signals.tasks import create_external_certificate
from eox_nelp.signals.utils import _generate_external_certificate_data

GeneratedCertificate = get_generated_certificate()


@admin.action(description="Create external certificates")
def create_external_certificate_action(modeladmin, request, queryset): # pylint: disable=unused-argument
"""
This creates CertificateData and runs the create_external_certificate task for every record
in the queryset, to allow to create certificates in the external NELP service.

Args:
modeladmin: Instance of NelpGeneratedCertificateAdmin
request: Current django request.
queryset: Selected records.
"""

for certificate in queryset:
certificate_data = CertificateData(
user=UserData(
pii=UserPersonalData(
username=certificate.user.username,
email=certificate.user.email,
name=certificate.user.profile.name,
),
id=certificate.user.id,
is_active=certificate.user.is_active,
),
course=CourseData(
course_key=certificate.course_id,
),
mode=certificate.mode,
grade=certificate.grade,
current_status=certificate.status,
download_url=certificate.download_url,
name=certificate.name,
)
time = certificate.modified_date.astimezone(timezone.utc)

create_external_certificate.delay(
external_certificate_data=_generate_external_certificate_data(
time=time,
certificate_data=certificate_data,
)
)


class NelpGeneratedCertificateAdmin(GeneratedCertificateAdmin):
"""
Nelp GeneratedCertificate admin class, this adds the NELP admin custom
behavior, for the GeneratedCertificate model.
"""
actions = [create_external_certificate_action]


register(GeneratedCertificate, NelpGeneratedCertificateAdmin)
15 changes: 15 additions & 0 deletions eox_nelp/edxapp_wrapper/backends/certificates_m_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Backend for certificates module.
This file contains all the necessary certificates dependencies from
https://github.com/eduNEXT/edunext-platform/tree/master/lms/djangoapps/certificates
"""
from lms.djangoapps.certificates import admin # pylint: disable=import-error


def get_generated_certificates_admin():
"""Allow to get the openedX GeneratedCertificateAdmin class.
https://github.com/eduNEXT/edunext-platform/tree/master/lms/djangoapps/certificates/admin.py

Returns:
GeneratedCertificateAdmin class.
"""
return admin.GeneratedCertificateAdmin
14 changes: 14 additions & 0 deletions eox_nelp/edxapp_wrapper/certificates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""Wrapper certificates module file.
This contains all the required dependencies from certificates.

Attributes:
backend:Imported module by using the plugin settings.
GeneratedCertificateAdmin: Wrapper GeneratedCertificateAdmin class.
"""
from importlib import import_module

from django.conf import settings

backend = import_module(settings.EOX_NELP_CERTIFICATES_BACKEND)

GeneratedCertificateAdmin = backend.get_generated_certificates_admin()
10 changes: 10 additions & 0 deletions eox_nelp/edxapp_wrapper/test_backends/certificates_m_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Test backend for certificates module."""
from mock import Mock


def get_generated_certificates_admin():
"""Return test admin class.
Returns:
Mock class.
"""
return Mock()
1 change: 1 addition & 0 deletions eox_nelp/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def plugin_settings(settings):
settings.EOX_NELP_STUDENT_BACKEND = 'eox_nelp.edxapp_wrapper.backends.student_m_v1'
settings.EOX_NELP_EDXMAKO_BACKEND = 'eox_nelp.edxapp_wrapper.backends.edxmako_m_v1'
settings.EOX_NELP_BRANDING_BACKEND = 'eox_nelp.edxapp_wrapper.backends.branding_m_v1'
settings.EOX_NELP_CERTIFICATES_BACKEND = 'eox_nelp.edxapp_wrapper.backends.certificates_m_v1'

settings.FUTUREX_API_URL = 'https://testing-site.com'
settings.FUTUREX_API_CLIENT_ID = 'my-test-client-id'
Expand Down
1 change: 1 addition & 0 deletions eox_nelp/settings/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def plugin_settings(settings): # pylint: disable=function-redefined
settings.EOX_NELP_STUDENT_BACKEND = 'eox_nelp.edxapp_wrapper.test_backends.student_m_v1'
settings.EOX_NELP_EDXMAKO_BACKEND = 'eox_nelp.edxapp_wrapper.test_backends.edxmako_m_v1'
settings.EOX_NELP_BRANDING_BACKEND = 'eox_nelp.edxapp_wrapper.test_backends.branding_m_v1'
settings.EOX_NELP_CERTIFICATES_BACKEND = 'eox_nelp.edxapp_wrapper.test_backends.certificates_m_v1'

settings.FUTUREX_API_URL = 'https://testing.com'
settings.FUTUREX_API_CLIENT_ID = 'my-test-client-id'
Expand Down
Loading