learning-credentials 0.4.0__tar.gz → 0.4.0rc1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/CHANGELOG.rst +3 -27
  2. {learning_credentials-0.4.0/learning_credentials.egg-info → learning_credentials-0.4.0rc1}/PKG-INFO +4 -29
  3. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/admin.py +30 -3
  4. learning_credentials-0.4.0rc1/learning_credentials/api/v1/serializers.py +13 -0
  5. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/v1/urls.py +2 -1
  6. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/v1/views.py +61 -1
  7. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/compat.py +3 -6
  8. learning_credentials-0.4.0rc1/learning_credentials/generators.py +226 -0
  9. learning_credentials-0.4.0rc1/learning_credentials/migrations/0007_validation.py +94 -0
  10. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/models.py +63 -32
  11. learning_credentials-0.4.0rc1/learning_credentials/templates/learning_credentials/verify.html +83 -0
  12. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/urls.py +2 -0
  13. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1/learning_credentials.egg-info}/PKG-INFO +4 -29
  14. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials.egg-info/SOURCES.txt +3 -2
  15. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/pyproject.toml +9 -6
  16. learning_credentials-0.4.0rc1/tests/test_generators.py +299 -0
  17. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/tests/test_models.py +22 -274
  18. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/tests/test_processors.py +27 -2
  19. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/tests/test_views.py +106 -24
  20. learning_credentials-0.4.0/learning_credentials/generators.py +0 -416
  21. learning_credentials-0.4.0/learning_credentials/migrations/0007_migrate_to_text_elements_format.py +0 -138
  22. learning_credentials-0.4.0/tests/test_generators.py +0 -571
  23. learning_credentials-0.4.0/tests/test_migrations.py +0 -250
  24. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/LICENSE.txt +0 -0
  25. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/MANIFEST.in +0 -0
  26. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/README.rst +0 -0
  27. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/__init__.py +0 -0
  28. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/__init__.py +0 -0
  29. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/urls.py +0 -0
  30. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/v1/__init__.py +0 -0
  31. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/api/v1/permissions.py +0 -0
  32. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/apps.py +0 -0
  33. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/conf/locale/config.yaml +0 -0
  34. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/exceptions.py +0 -0
  35. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0001_initial.py +0 -0
  36. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0002_migrate_to_learning_credentials.py +0 -0
  37. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0003_rename_certificates_to_credentials.py +0 -0
  38. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0004_replace_course_keys_with_learning_context_keys.py +0 -0
  39. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0005_rename_processors_and_generators.py +0 -0
  40. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/0006_cleanup_openedx_certificates_tables.py +0 -0
  41. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/migrations/__init__.py +0 -0
  42. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/processors.py +0 -0
  43. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/settings/__init__.py +0 -0
  44. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/settings/common.py +0 -0
  45. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/settings/production.py +0 -0
  46. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/tasks.py +0 -0
  47. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/base.html +0 -0
  48. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/edx_ace/certificate_generated/email/body.html +0 -0
  49. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/edx_ace/certificate_generated/email/body.txt +0 -0
  50. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/edx_ace/certificate_generated/email/from_name.txt +0 -0
  51. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/edx_ace/certificate_generated/email/head.html +0 -0
  52. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials/templates/learning_credentials/edx_ace/certificate_generated/email/subject.txt +0 -0
  53. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials.egg-info/dependency_links.txt +0 -0
  54. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials.egg-info/entry_points.txt +0 -0
  55. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials.egg-info/requires.txt +0 -0
  56. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/learning_credentials.egg-info/top_level.txt +0 -0
  57. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/setup.cfg +0 -0
  58. {learning_credentials-0.4.0 → learning_credentials-0.4.0rc1}/tests/test_tasks.py +0 -0
@@ -16,38 +16,14 @@ Unreleased
16
16
 
17
17
  *
18
18
 
19
- 0.4.0 - 2026-01-28
19
+ 0.4.0 - 2025-11-03
20
20
  ******************
21
21
 
22
22
  Added
23
23
  =====
24
24
 
25
- * New ``text_elements`` format for PDF credential generation with flexible text positioning and placeholder support.
26
- * Support for custom text elements with ``{name}``, ``{context_name}``, and ``{issue_date}`` placeholders.
27
- * Global ``defaults`` configuration for font, color, and character spacing.
28
-
29
- Modified
30
- ========
31
-
32
- * Migrated generator options from flat format (``name_y``, ``context_name_color``, etc.) to structured ``text_elements`` format.
33
-
34
- 0.3.1 - 2025-12-15
35
- ******************
36
-
37
- Added
38
- =====
39
-
40
- * Support for defining the course name using the ``cert_name_long`` field (in Studio's Advanced Settings).
41
- * Support for specifying individual fonts for PDF text elements.
42
- * Support for \n in learning context names in PDF certificates.
43
- * Options for uppercase name and issue date in PDF certificates.
44
- * Option for defining character spacing for issue date in PDF certificates.
45
- * Option for defining the horizontal offset of the issue date from its centered position (``issue_date_x``).
46
-
47
- Modified
48
- ========
49
-
50
- * Replaced ``template_two_lines`` with ``template_multiline``.
25
+ * Frontend form and backend API endpoint for verifying credentials.
26
+ * Option to invalidate issued credentials.
51
27
 
52
28
  0.3.0 - 2025-09-17
53
29
  ******************
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: learning-credentials
3
- Version: 0.4.0
3
+ Version: 0.4.0rc1
4
4
  Summary: A pluggable service for preparing Open edX credentials.
5
5
  Author-email: OpenCraft <help@opencraft.com>
6
6
  License-Expression: AGPL-3.0-or-later
@@ -11,7 +11,6 @@ Keywords: Python,edx,credentials,django
11
11
  Classifier: Development Status :: 5 - Production/Stable
12
12
  Classifier: Framework :: Django
13
13
  Classifier: Framework :: Django :: 4.2
14
- Classifier: Framework :: Django :: 5.2
15
14
  Classifier: Intended Audience :: Developers
16
15
  Classifier: Natural Language :: English
17
16
  Classifier: Programming Language :: Python :: 3
@@ -177,38 +176,14 @@ Unreleased
177
176
 
178
177
  *
179
178
 
180
- 0.4.0 - 2026-01-28
179
+ 0.4.0 - 2025-11-03
181
180
  ******************
182
181
 
183
182
  Added
184
183
  =====
185
184
 
186
- * New ``text_elements`` format for PDF credential generation with flexible text positioning and placeholder support.
187
- * Support for custom text elements with ``{name}``, ``{context_name}``, and ``{issue_date}`` placeholders.
188
- * Global ``defaults`` configuration for font, color, and character spacing.
189
-
190
- Modified
191
- ========
192
-
193
- * Migrated generator options from flat format (``name_y``, ``context_name_color``, etc.) to structured ``text_elements`` format.
194
-
195
- 0.3.1 - 2025-12-15
196
- ******************
197
-
198
- Added
199
- =====
200
-
201
- * Support for defining the course name using the ``cert_name_long`` field (in Studio's Advanced Settings).
202
- * Support for specifying individual fonts for PDF text elements.
203
- * Support for \n in learning context names in PDF certificates.
204
- * Options for uppercase name and issue date in PDF certificates.
205
- * Option for defining character spacing for issue date in PDF certificates.
206
- * Option for defining the horizontal offset of the issue date from its centered position (``issue_date_x``).
207
-
208
- Modified
209
- ========
210
-
211
- * Replaced ``template_two_lines`` with ``template_multiline``.
185
+ * Frontend form and backend API endpoint for verifying credentials.
186
+ * Option to invalidate issued credentials.
212
187
 
213
188
  0.3.0 - 2025-09-17
214
189
  ******************
@@ -7,8 +7,9 @@ import inspect
7
7
  from typing import TYPE_CHECKING
8
8
 
9
9
  from django import forms
10
- from django.contrib import admin
10
+ from django.contrib import admin, messages
11
11
  from django.core.exceptions import ValidationError
12
+ from django.urls import reverse
12
13
  from django.utils.html import format_html
13
14
  from django_object_actions import DjangoObjectActions, action
14
15
  from django_reverse_admin import ReverseModelAdmin
@@ -225,7 +226,7 @@ class CredentialConfigurationAdmin(DjangoObjectActions, ReverseModelAdmin):
225
226
 
226
227
 
227
228
  @admin.register(Credential)
228
- class CredentialAdmin(admin.ModelAdmin): # noqa: D101
229
+ class CredentialAdmin(DjangoObjectActions, admin.ModelAdmin): # noqa: D101
229
230
  list_display = (
230
231
  'user_id',
231
232
  'user_full_name',
@@ -237,19 +238,32 @@ class CredentialAdmin(admin.ModelAdmin): # noqa: D101
237
238
  'modified',
238
239
  )
239
240
  readonly_fields = (
241
+ 'uuid',
242
+ 'verify_uuid',
240
243
  'user_id',
241
244
  'created',
242
245
  'modified',
243
246
  'user_full_name',
244
247
  'learning_context_key',
248
+ 'learning_context_name',
245
249
  'credential_type',
246
250
  'status',
247
251
  'url',
248
252
  'legacy_id',
249
253
  'generation_task_id',
250
254
  )
251
- search_fields = ("learning_context_key", "user_id", "user_full_name")
255
+ search_fields = ("learning_context_key", "user_id", "user_full_name", "uuid")
252
256
  list_filter = ("learning_context_key", "credential_type", "status")
257
+ change_actions = ('reissue_credential',)
258
+
259
+ def save_model(self, request: HttpRequest, obj: Credential, _form: forms.ModelForm, _change: bool): # noqa: FBT001
260
+ """Display validation errors as messages in the admin interface."""
261
+ try:
262
+ obj.save()
263
+ except ValidationError as e:
264
+ self.message_user(request, e.message or "Invalid data", level=messages.ERROR)
265
+ # Optionally, redirect to the change form with the error message
266
+ return
253
267
 
254
268
  def get_form(self, request: HttpRequest, obj: Credential | None = None, **kwargs) -> forms.ModelForm:
255
269
  """Hide the download_url field."""
@@ -263,3 +277,16 @@ class CredentialAdmin(admin.ModelAdmin): # noqa: D101
263
277
  if obj.download_url:
264
278
  return format_html("<a href='{url}'>{url}</a>", url=obj.download_url)
265
279
  return "-"
280
+
281
+ @action(label="Reissue credential", description="Reissue the credential for the user.")
282
+ def reissue_credential(self, request: HttpRequest, obj: Credential):
283
+ """Reissue the credential for the user."""
284
+ try:
285
+ new_credential = obj.reissue()
286
+ admin_url = reverse('admin:learning_credentials_credential_change', args=[new_credential.pk])
287
+ message = format_html(
288
+ 'The credential has been reissued as <a href="{}">{}</a>.', admin_url, new_credential.uuid
289
+ )
290
+ messages.success(request, message)
291
+ except CredentialConfiguration.DoesNotExist:
292
+ messages.error(request, "The configuration does not exist.")
@@ -0,0 +1,13 @@
1
+ """API serializers for learning credentials."""
2
+
3
+ from rest_framework import serializers
4
+
5
+ from learning_credentials.models import Credential
6
+
7
+
8
+ class CredentialSerializer(serializers.ModelSerializer):
9
+ """Serializer that returns credential metadata."""
10
+
11
+ class Meta: # noqa: D106
12
+ model = Credential
13
+ fields = ('user_full_name', 'created', 'learning_context_name', 'status', 'invalidation_reason')
@@ -2,7 +2,7 @@
2
2
 
3
3
  from django.urls import path
4
4
 
5
- from .views import CredentialConfigurationCheckView
5
+ from .views import CredentialConfigurationCheckView, CredentialMetadataView
6
6
 
7
7
  urlpatterns = [
8
8
  path(
@@ -10,4 +10,5 @@ urlpatterns = [
10
10
  CredentialConfigurationCheckView.as_view(),
11
11
  name='credential_configuration_check',
12
12
  ),
13
+ path('metadata/<uuid:uuid>/', CredentialMetadataView.as_view(), name='credential-metadata'),
13
14
  ]
@@ -9,9 +9,10 @@ from rest_framework.permissions import IsAuthenticated
9
9
  from rest_framework.response import Response
10
10
  from rest_framework.views import APIView
11
11
 
12
- from learning_credentials.models import CredentialConfiguration
12
+ from learning_credentials.models import Credential, CredentialConfiguration
13
13
 
14
14
  from .permissions import CanAccessLearningContext
15
+ from .serializers import CredentialSerializer
15
16
 
16
17
  if TYPE_CHECKING:
17
18
  from rest_framework.request import Request
@@ -81,3 +82,62 @@ class CredentialConfigurationCheckView(APIView):
81
82
  }
82
83
 
83
84
  return Response(response_data, status=status.HTTP_200_OK)
85
+
86
+
87
+ class CredentialMetadataView(APIView):
88
+ """API view to retrieve credential metadata by UUID."""
89
+
90
+ @apidocs.schema(
91
+ parameters=[
92
+ apidocs.string_parameter(
93
+ "uuid",
94
+ ParameterLocation.PATH,
95
+ description="The UUID of the credential to retrieve.",
96
+ ),
97
+ ],
98
+ responses={
99
+ 200: "Successfully retrieved the credential metadata.",
100
+ 404: "Credential not found or not valid.",
101
+ },
102
+ )
103
+ def get(self, _request: "Request", uuid: str) -> Response:
104
+ """
105
+ Retrieve credential metadata by its UUID.
106
+
107
+ **Example Request**
108
+
109
+ ``GET /api/learning_credentials/v1/metadata/123e4567-e89b-12d3-a456-426614174000/``
110
+
111
+ **Response Values**
112
+
113
+ - **200 OK**: Successfully retrieved the credential metadata.
114
+ - **404 Not Found**: Credential not found or not valid.
115
+
116
+ **Example Response**
117
+
118
+ .. code-block:: json
119
+
120
+ {
121
+ "user_full_name": "John Doe",
122
+ "created": "2023-01-01",
123
+ "learning_context_name": "Demo Course",
124
+ "status": "available",
125
+ "invalidation_reason": ""
126
+ }
127
+
128
+
129
+ {
130
+ "user_full_name": "John Doe",
131
+ "created": "2023-01-01",
132
+ "learning_context_name": "Demo Course",
133
+ "status": "invalidated",
134
+ "invalidation_reason": "Reissued due to name change."
135
+ }
136
+ """
137
+ try:
138
+ credential = Credential.objects.get(verify_uuid=uuid)
139
+ except Credential.DoesNotExist:
140
+ return Response({'error': 'Credential not found.'}, status=status.HTTP_404_NOT_FOUND)
141
+
142
+ serializer = CredentialSerializer(credential)
143
+ return Response(serializer.data, status=status.HTTP_200_OK)
@@ -52,13 +52,10 @@ def get_course_grading_policy(course_id: CourseKey) -> dict:
52
52
  def _get_course_name(course_id: CourseKey) -> str:
53
53
  """Get the course name from Open edX."""
54
54
  # noinspection PyUnresolvedReferences,PyPackageRequirements
55
- from openedx.core.djangoapps.content.course_overviews.api import get_course_overview_or_none
55
+ from openedx.core.djangoapps.content.learning_sequences.api import get_course_outline
56
56
 
57
- name = str(course_id)
58
- if course_overview := get_course_overview_or_none(course_id):
59
- name = course_overview.cert_name_long or course_overview.display_name or name
60
-
61
- return name
57
+ course_outline = get_course_outline(course_id)
58
+ return (course_outline and course_outline.title) or str(course_id)
62
59
 
63
60
 
64
61
  def _get_learning_path_name(learning_path_key: LearningPathKey) -> str:
@@ -0,0 +1,226 @@
1
+ """
2
+ This module provides functions to generate credentials.
3
+
4
+ The functions prefixed with `generate_` are automatically detected by the admin page and are used to generate the
5
+ credentials for the users.
6
+
7
+ We will move this module to an external repository (a plugin).
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import io
13
+ import logging
14
+ import secrets
15
+ from typing import TYPE_CHECKING, Any
16
+
17
+ from django.conf import settings
18
+ from django.core.files.base import ContentFile
19
+ from django.core.files.storage import FileSystemStorage, default_storage
20
+ from pypdf import PdfReader, PdfWriter
21
+ from pypdf.constants import UserAccessPermissions
22
+ from reportlab.pdfbase import pdfmetrics
23
+ from reportlab.pdfbase.ttfonts import TTFont
24
+ from reportlab.pdfgen import canvas
25
+
26
+ from .compat import get_default_storage_url, get_learning_context_name, get_localized_credential_date
27
+ from .models import CredentialAsset
28
+
29
+ log = logging.getLogger(__name__)
30
+
31
+ if TYPE_CHECKING: # pragma: no cover
32
+ from uuid import UUID
33
+
34
+ from django.contrib.auth.models import User
35
+ from opaque_keys.edx.keys import CourseKey
36
+
37
+
38
+ def _get_user_name(user: User) -> str:
39
+ """
40
+ Retrieve the user's name.
41
+
42
+ :param user: The user to generate the credential for.
43
+ :return: Username.
44
+ """
45
+ return user.profile.name or f"{user.first_name} {user.last_name}"
46
+
47
+
48
+ def _register_font(options: dict[str, Any]) -> str:
49
+ """
50
+ Register a custom font if specified in options. If not specified, use the default font (Helvetica).
51
+
52
+ :param options: A dictionary containing the font.
53
+ :returns: The font name.
54
+ """
55
+ if font := options.get('font'):
56
+ pdfmetrics.registerFont(TTFont(font, CredentialAsset.get_asset_by_slug(font)))
57
+
58
+ return font or 'Helvetica'
59
+
60
+
61
+ def _write_text_on_template(template: any, font: str, username: str, context_name: str, options: dict[str, Any]) -> any:
62
+ """
63
+ Prepare a new canvas and write the user and course name onto it.
64
+
65
+ :param template: Pdf template.
66
+ :param font: Font name.
67
+ :param username: The name of the user to generate the credential for.
68
+ :param context_name: The name of the learning context.
69
+ :param options: A dictionary documented in the `generate_pdf_credential` function.
70
+ :returns: A canvas with written data.
71
+ """
72
+
73
+ def hex_to_rgb(hex_color: str) -> tuple[float, float, float]:
74
+ """
75
+ Convert a hexadecimal color code to an RGB tuple with floating-point values.
76
+
77
+ :param hex_color: A hexadecimal color string, which can start with '#' and be either 3 or 6 characters long.
78
+ :returns: A tuple representing the RGB color as (red, green, blue), with each value ranging from 0.0 to 1.0.
79
+ """
80
+ hex_color = hex_color.lstrip('#')
81
+ # Expand shorthand form (e.g. "158" to "115588")
82
+ if len(hex_color) == 3:
83
+ hex_color = ''.join([c * 2 for c in hex_color])
84
+
85
+ # noinspection PyTypeChecker
86
+ return tuple(int(hex_color[i : i + 2], 16) / 255 for i in range(0, 6, 2))
87
+
88
+ template_width, template_height = template.mediabox[2:]
89
+ pdf_canvas = canvas.Canvas(io.BytesIO(), pagesize=(template_width, template_height))
90
+
91
+ # Write the learner name.
92
+ pdf_canvas.setFont(font, options.get('name_size', 32))
93
+ name_color = options.get('name_color', '#000')
94
+ pdf_canvas.setFillColorRGB(*hex_to_rgb(name_color))
95
+
96
+ name_x = (template_width - pdf_canvas.stringWidth(username)) / 2
97
+ name_y = options.get('name_y', 290)
98
+ pdf_canvas.drawString(name_x, name_y, username)
99
+
100
+ # Write the learning context name.
101
+ pdf_canvas.setFont(font, options.get('context_name_size', 28))
102
+ context_name_color = options.get('context_name_color', '#000')
103
+ pdf_canvas.setFillColorRGB(*hex_to_rgb(context_name_color))
104
+
105
+ context_name_y = options.get('context_name_y', 220)
106
+ context_name_line_height = 28 * 1.1
107
+
108
+ # Split the learning context name into lines and write each of them in the center of the template.
109
+ for line_number, line in enumerate(context_name.split('\n')):
110
+ line_x = (template_width - pdf_canvas.stringWidth(line)) / 2
111
+ line_y = context_name_y - (line_number * context_name_line_height)
112
+ pdf_canvas.drawString(line_x, line_y, line)
113
+
114
+ # Write the issue date.
115
+ issue_date = get_localized_credential_date()
116
+ pdf_canvas.setFont(font, 12)
117
+ issue_date_color = options.get('issue_date_color', '#000')
118
+ pdf_canvas.setFillColorRGB(*hex_to_rgb(issue_date_color))
119
+
120
+ issue_date_x = (template_width - pdf_canvas.stringWidth(issue_date)) / 2
121
+ issue_date_y = options.get('issue_date_y', 120)
122
+ pdf_canvas.drawString(issue_date_x, issue_date_y, issue_date)
123
+
124
+ return pdf_canvas
125
+
126
+
127
+ def _save_credential(credential: PdfWriter, credential_uuid: UUID) -> str:
128
+ """
129
+ Save the final PDF file to BytesIO and upload it using Django default storage.
130
+
131
+ :param credential: Pdf credential.
132
+ :param credential_uuid: The UUID of the credential.
133
+ :returns: The URL of the saved credential.
134
+ """
135
+ # Save the final PDF file to BytesIO.
136
+ output_path = f'external_certificates/{credential_uuid}.pdf'
137
+
138
+ view_print_extract_permission = (
139
+ UserAccessPermissions.PRINT
140
+ | UserAccessPermissions.PRINT_TO_REPRESENTATION
141
+ | UserAccessPermissions.EXTRACT_TEXT_AND_GRAPHICS
142
+ )
143
+ credential.encrypt('', secrets.token_hex(32), permissions_flag=view_print_extract_permission, algorithm='AES-256')
144
+
145
+ pdf_bytes = io.BytesIO()
146
+ credential.write(pdf_bytes)
147
+ pdf_bytes.seek(0) # Rewind to start.
148
+ # Upload with Django default storage.
149
+ credential_file = ContentFile(pdf_bytes.read())
150
+ # Delete the file if it already exists.
151
+ if default_storage.exists(output_path):
152
+ default_storage.delete(output_path)
153
+ default_storage.save(output_path, credential_file)
154
+ if isinstance(default_storage, FileSystemStorage):
155
+ url = f"{get_default_storage_url()}{output_path}"
156
+ else:
157
+ url = default_storage.url(output_path)
158
+
159
+ if custom_domain := getattr(settings, 'LEARNING_CREDENTIALS_CUSTOM_DOMAIN', None):
160
+ url = f"{custom_domain}/{credential_uuid}.pdf"
161
+
162
+ return url
163
+
164
+
165
+ def generate_pdf_credential(
166
+ learning_context_key: CourseKey,
167
+ user: User,
168
+ credential_uuid: UUID,
169
+ options: dict[str, Any],
170
+ ) -> str:
171
+ """
172
+ Generate a PDF credential.
173
+
174
+ :param learning_context_key: The ID of the course or learning path the credential is for.
175
+ :param user: The user to generate the credential for.
176
+ :param credential_uuid: The UUID of the credential to generate.
177
+ :param options: The custom options for the credential.
178
+ :returns: The URL of the saved credential.
179
+
180
+ Options:
181
+ - template: The path to the PDF template file.
182
+ - template_two_lines: The path to the PDF template file for two-line context names.
183
+ A two-line context name is specified by using a semicolon as a separator.
184
+ - font: The name of the font to use.
185
+ - name_y: The Y coordinate of the name on the credential (vertical position on the template).
186
+ - name_color: The color of the name on the credential (hexadecimal color code).
187
+ - name_size: The font size of the name on the credential. The default value is 32.
188
+ - context_name: Specify the custom course or Learning Path name.
189
+ - context_name_y: The Y coordinate of the context name on the credential (vertical position on the template).
190
+ - context_name_color: The color of the context name on the credential (hexadecimal color code).
191
+ - context_name_size: The font size of the context name on the credential. The default value is 28.
192
+ - issue_date_y: The Y coordinate of the issue date on the credential (vertical position on the template).
193
+ - issue_date_color: The color of the issue date on the credential (hexadecimal color code).
194
+ """
195
+ log.info("Starting credential generation for user %s", user.id)
196
+
197
+ username = _get_user_name(user)
198
+ context_name = options.get('context_name') or get_learning_context_name(learning_context_key)
199
+
200
+ # Get template from the CredentialAsset.
201
+ # HACK: We support two-line strings by using a semicolon as a separator.
202
+ if ';' in context_name and (template_path := options.get('template_two_lines')):
203
+ template_file = CredentialAsset.get_asset_by_slug(template_path)
204
+ context_name = context_name.replace(';', '\n')
205
+ else:
206
+ template_file = CredentialAsset.get_asset_by_slug(options['template'])
207
+
208
+ font = _register_font(options)
209
+
210
+ # Load the PDF template.
211
+ with template_file.open('rb') as template_file:
212
+ template = PdfReader(template_file).pages[0]
213
+
214
+ credential = PdfWriter()
215
+
216
+ # Create a new canvas, prepare the page and write the data
217
+ pdf_canvas = _write_text_on_template(template, font, username, context_name, options)
218
+
219
+ overlay_pdf = PdfReader(io.BytesIO(pdf_canvas.getpdfdata()))
220
+ template.merge_page(overlay_pdf.pages[0])
221
+ credential.add_page(template)
222
+
223
+ url = _save_credential(credential, credential_uuid)
224
+
225
+ log.info("Credential saved to %s", url)
226
+ return url
@@ -0,0 +1,94 @@
1
+ # Generated by Django 4.2.25 on 2025-10-31 17:43
2
+
3
+ from django.db import migrations, models
4
+ import uuid
5
+
6
+
7
+ def backfill_credential_fields(apps, schema_editor):
8
+ """Generate verification UUIDs and backfill learning_context_name for all existing credentials."""
9
+ from learning_credentials.compat import get_learning_context_name
10
+
11
+ Credential = apps.get_model("learning_credentials", "Credential")
12
+ for credential in Credential.objects.all():
13
+ credential.verify_uuid = uuid.uuid4()
14
+ credential.learning_context_name = get_learning_context_name(credential.learning_context_key)
15
+ credential.save(update_fields=["verify_uuid", "learning_context_name"])
16
+
17
+
18
+ class Migration(migrations.Migration):
19
+ dependencies = [
20
+ ("learning_credentials", "0006_cleanup_openedx_certificates_tables"),
21
+ ]
22
+
23
+ operations = [
24
+ migrations.AlterUniqueTogether(
25
+ name="credential",
26
+ unique_together=set(),
27
+ ),
28
+ migrations.AlterField(
29
+ model_name="credential",
30
+ name="user_full_name",
31
+ field=models.CharField(
32
+ editable=False,
33
+ help_text="User receiving the credential. This field is used for validation purposes.",
34
+ max_length=255,
35
+ ),
36
+ ),
37
+ migrations.AddField(
38
+ model_name="credential",
39
+ name="invalidated_at",
40
+ field=models.DateTimeField(
41
+ editable=False,
42
+ help_text="Timestamp when the credential was invalidated",
43
+ null=True,
44
+ ),
45
+ ),
46
+ migrations.AddField(
47
+ model_name="credential",
48
+ name="invalidation_reason",
49
+ field=models.CharField(
50
+ blank=True,
51
+ help_text="Reason for invalidating the credential",
52
+ max_length=255,
53
+ ),
54
+ ),
55
+ migrations.AddField(
56
+ model_name="credential",
57
+ name="learning_context_name",
58
+ field=models.CharField(
59
+ editable=False,
60
+ help_text="Name of the learning context for which the credential was issued. This field is used for validation purposes.",
61
+ max_length=255,
62
+ null=True,
63
+ ),
64
+ ),
65
+ migrations.AddField(
66
+ model_name="credential",
67
+ name="verify_uuid",
68
+ field=models.UUIDField(
69
+ default=uuid.uuid4,
70
+ editable=False,
71
+ help_text="UUID used for verifying the credential",
72
+ null=True,
73
+ ),
74
+ ),
75
+ migrations.RunPython(backfill_credential_fields, reverse_code=migrations.RunPython.noop),
76
+ migrations.AlterField(
77
+ model_name="credential",
78
+ name="learning_context_name",
79
+ field=models.CharField(
80
+ editable=False,
81
+ help_text="Name of the learning context for which the credential was issued. This field is used for validation purposes.",
82
+ max_length=255,
83
+ ),
84
+ ),
85
+ migrations.AlterField(
86
+ model_name="credential",
87
+ name="verify_uuid",
88
+ field=models.UUIDField(
89
+ default=uuid.uuid4,
90
+ editable=False,
91
+ help_text="UUID used for verifying the credential",
92
+ ),
93
+ ),
94
+ ]