|
1 | | -"""API functions for the Open edX Certificates app.""" |
| 1 | +"""API functions for the Learning Credentials app.""" |
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
|
7 | 7 | from django.contrib.auth.models import User |
8 | 8 | from opaque_keys.edx.keys import CourseKey |
9 | 9 |
|
10 | | -from .models import ExternalCertificate, ExternalCertificateCourseConfiguration |
11 | | -from .tasks import generate_certificate_for_user_task |
| 10 | +from .models import Credential, CredentialConfiguration |
| 11 | +from .tasks import generate_credential_for_user_task |
12 | 12 |
|
13 | 13 | logger = logging.getLogger(__name__) |
14 | 14 |
|
15 | 15 |
|
16 | | -def get_eligible_users_by_certificate_type(course_id: CourseKey, user_id: int = None) -> dict[str, list[User]]: |
| 16 | +def get_eligible_users_by_credential_type(course_id: CourseKey, user_id: int | None = None) -> dict[str, list[User]]: |
17 | 17 | """ |
18 | | - Retrieve eligible users for each certificate type in the given course. |
| 18 | + Retrieve eligible users for each credential type in the given course. |
19 | 19 |
|
20 | 20 | :param course_id: The key of the course for which to check eligibility. |
21 | 21 | :param user_id: Optional. If provided, will check eligibility for the specific user. |
22 | | - :return: A dictionary with certificate type as the key and eligible users as the value. |
| 22 | + :return: A dictionary with credential type as the key and eligible users as the value. |
23 | 23 | """ |
24 | | - certificate_configs = ExternalCertificateCourseConfiguration.objects.filter(course_id=course_id) |
| 24 | + credential_configs = CredentialConfiguration.objects.filter(course_id=course_id) |
25 | 25 |
|
26 | | - if not certificate_configs: |
| 26 | + if not credential_configs: |
27 | 27 | return {} |
28 | 28 |
|
29 | 29 | eligible_users_by_type = {} |
30 | | - for certificate_config in certificate_configs: |
31 | | - user_ids = certificate_config.get_eligible_user_ids(user_id) |
32 | | - filtered_user_ids = certificate_config.filter_out_user_ids_with_certificates(user_ids) |
| 30 | + for credential_config in credential_configs: |
| 31 | + user_ids = credential_config.get_eligible_user_ids(user_id) |
| 32 | + filtered_user_ids = credential_config.filter_out_user_ids_with_credentials(user_ids) |
33 | 33 |
|
34 | 34 | if user_id: |
35 | | - eligible_users_by_type[certificate_config.certificate_type.name] = list(set(filtered_user_ids) & {user_id}) |
| 35 | + eligible_users_by_type[credential_config.credential_type.name] = list(set(filtered_user_ids) & {user_id}) |
36 | 36 | else: |
37 | | - eligible_users_by_type[certificate_config.certificate_type.name] = filtered_user_ids |
| 37 | + eligible_users_by_type[credential_config.credential_type.name] = filtered_user_ids |
38 | 38 |
|
39 | 39 | return eligible_users_by_type |
40 | 40 |
|
41 | 41 |
|
42 | | -def get_user_certificates_by_type(course_id: CourseKey, user_id: int) -> dict[str, dict[str, str]]: |
| 42 | +def get_user_credentials_by_type(course_id: CourseKey, user_id: int) -> dict[str, dict[str, str]]: |
43 | 43 | """ |
44 | | - Retrieve the available certificates for a given user in a course. |
| 44 | + Retrieve the available credentials for a given user in a course. |
45 | 45 |
|
46 | | - :param course_id: The course ID for which to retrieve certificates. |
47 | | - :param user_id: The ID of the user for whom certificates are being retrieved. |
48 | | - :return: A dict where keys are certificate types and values are dicts with the download link and status. |
| 46 | + :param course_id: The course ID for which to retrieve credentials. |
| 47 | + :param user_id: The ID of the user for whom credentials are being retrieved. |
| 48 | + :return: A dict where keys are credential types and values are dicts with the download link and status. |
49 | 49 | """ |
50 | | - certificates = ExternalCertificate.objects.filter(user_id=user_id, course_id=course_id) |
| 50 | + credentials = Credential.objects.filter(user_id=user_id, course_id=course_id) |
51 | 51 |
|
52 | | - return {cert.certificate_type: {'download_url': cert.download_url, 'status': cert.status} for cert in certificates} |
| 52 | + return {cred.credential_type: {'download_url': cred.download_url, 'status': cred.status} for cred in credentials} |
53 | 53 |
|
54 | 54 |
|
55 | | -def generate_certificate_for_user(course_id: CourseKey, certificate_type: str, user_id: int, force: bool = False): |
| 55 | +def generate_credential_for_user(course_id: CourseKey, credential_type: str, user_id: int, force: bool = False): |
56 | 56 | """ |
57 | | - Generate a certificate for a user in a course. |
| 57 | + Generate a credential for a user in a course. |
58 | 58 |
|
59 | | - :param course_id: The course ID for which to generate the certificate. |
60 | | - :param certificate_type: The type of certificate to generate. |
61 | | - :param user_id: The ID of the user for whom the certificate is being generated. |
62 | | - :param force: If True, will generate the certificate even if the user is not eligible. |
| 59 | + :param course_id: The course ID for which to generate the credential. |
| 60 | + :param credential_type: The type of credential to generate. |
| 61 | + :param user_id: The ID of the user for whom the credential is being generated. |
| 62 | + :param force: If True, will generate the credential even if the user is not eligible. |
63 | 63 | """ |
64 | | - certificate_config = ExternalCertificateCourseConfiguration.objects.get( |
65 | | - course_id=course_id, certificate_type__name=certificate_type |
| 64 | + credential_config = CredentialConfiguration.objects.get( |
| 65 | + course_id=course_id, credential_type__name=credential_type |
66 | 66 | ) |
67 | 67 |
|
68 | | - if not certificate_config: |
| 68 | + if not credential_config: |
69 | 69 | logger.error('No course configuration found for course %s', course_id) |
70 | 70 | return |
71 | 71 |
|
72 | | - if not force and not certificate_config.get_eligible_user_ids(user_id): |
73 | | - logger.error('User %s is not eligible for the certificate in course %s', user_id, course_id) |
74 | | - raise ValueError('User is not eligible for the certificate.') |
| 72 | + if not force and not credential_config.get_eligible_user_ids(user_id): |
| 73 | + logger.error('User %s is not eligible for the credential in course %s', user_id, course_id) |
| 74 | + raise ValueError('User is not eligible for the credential.') |
75 | 75 |
|
76 | | - generate_certificate_for_user_task.delay(certificate_config.id, user_id) |
| 76 | + generate_credential_for_user_task.delay(credential_config.id, user_id) |
0 commit comments