From 8951bef502ac6d659af95c328b4303ed4d94e438 Mon Sep 17 00:00:00 2001 From: Kyrylo Kholodenko Date: Fri, 22 Dec 2023 15:24:48 +0200 Subject: [PATCH] feat: [ACI-139, ACI-145] new events for badge --- ...nedx+learning+badge+awarded+v1_schema.avsc | 100 ++++++++++++++++++ ...nedx+learning+badge+revoked+v1_schema.avsc | 100 ++++++++++++++++++ openedx_events/learning/data.py | 36 +++++++ openedx_events/learning/signals.py | 25 ++++- 4 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+awarded+v1_schema.avsc create mode 100644 openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+revoked+v1_schema.avsc diff --git a/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+awarded+v1_schema.avsc b/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+awarded+v1_schema.avsc new file mode 100644 index 00000000..5e374a35 --- /dev/null +++ b/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+awarded+v1_schema.avsc @@ -0,0 +1,100 @@ +{ + "name": "CloudEvent", + "type": "record", + "doc": "Avro Event Format for CloudEvents created with openedx_events/schema", + "fields": [ + { + "name": "badge", + "type": { + "name": "BadgeData", + "type": "record", + "fields": [ + { + "name": "uuid", + "type": "string" + }, + { + "name": "user", + "type": { + "name": "UserData", + "type": "record", + "fields": [ + { + "name": "id", + "type": "long" + }, + { + "name": "is_active", + "type": "boolean" + }, + { + "name": "pii", + "type": { + "name": "UserPersonalData", + "type": "record", + "fields": [ + { + "name": "username", + "type": "string" + }, + { + "name": "email", + "type": "string" + }, + { + "name": "name", + "type": "string" + } + ] + } + } + ] + } + }, + { + "name": "template", + "type": { + "name": "BadgeTemplateData", + "type": "record", + "fields": [ + { + "name": "uuid", + "type": "string" + }, + { + "name": "type", + "type": "string" + }, + { + "name": "name", + "type": [ + "null", + "string" + ], + "default": null + }, + { + "name": "description", + "type": [ + "null", + "string" + ], + "default": null + }, + { + "name": "image_url", + "type": [ + "null", + "string" + ], + "default": null + } + ] + } + } + ] + } + } + ], + "namespace": "org.openedx.learning.badge.awarded.v1" +} \ No newline at end of file diff --git a/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+revoked+v1_schema.avsc b/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+revoked+v1_schema.avsc new file mode 100644 index 00000000..eade1a70 --- /dev/null +++ b/openedx_events/event_bus/avro/tests/schemas/org+openedx+learning+badge+revoked+v1_schema.avsc @@ -0,0 +1,100 @@ +{ + "name": "CloudEvent", + "type": "record", + "doc": "Avro Event Format for CloudEvents created with openedx_events/schema", + "fields": [ + { + "name": "badge", + "type": { + "name": "BadgeData", + "type": "record", + "fields": [ + { + "name": "uuid", + "type": "string" + }, + { + "name": "user", + "type": { + "name": "UserData", + "type": "record", + "fields": [ + { + "name": "id", + "type": "long" + }, + { + "name": "is_active", + "type": "boolean" + }, + { + "name": "pii", + "type": { + "name": "UserPersonalData", + "type": "record", + "fields": [ + { + "name": "username", + "type": "string" + }, + { + "name": "email", + "type": "string" + }, + { + "name": "name", + "type": "string" + } + ] + } + } + ] + } + }, + { + "name": "template", + "type": { + "name": "BadgeTemplateData", + "type": "record", + "fields": [ + { + "name": "uuid", + "type": "string" + }, + { + "name": "type", + "type": "string" + }, + { + "name": "name", + "type": [ + "null", + "string" + ], + "default": null + }, + { + "name": "description", + "type": [ + "null", + "string" + ], + "default": null + }, + { + "name": "image_url", + "type": [ + "null", + "string" + ], + "default": null + } + ] + } + } + ] + } + } + ], + "namespace": "org.openedx.learning.badge.revoked.v1" +} \ No newline at end of file diff --git a/openedx_events/learning/data.py b/openedx_events/learning/data.py index 0e713650..5099fdf9 100644 --- a/openedx_events/learning/data.py +++ b/openedx_events/learning/data.py @@ -483,3 +483,39 @@ class UserCourseData: user = attr.ib(type=UserData) course = attr.ib(type=CourseData) + + +@attr.s(frozen=True) +class BadgeTemplateData: + """ + Attributes defined for Open edX badge template data object. + + Arguments: + uuid (str): UUID of the badge template + type (str): type of badge template + name (str): badge name + description (str): badge description + image_url (str): badge image url + """ + + uuid = attr.ib(type=str) + type = attr.ib(type=str) + name = attr.ib(type=str, default=None) + description = attr.ib(type=str, default=None) + image_url = attr.ib(type=str, default=None) + + +@attr.s(frozen=True) +class BadgeData: + """ + Attributes defined for the Open edX badge data object. + + Arguments: + uuid (str): the UUID of the badge + user (UserData): user associated with the badge + template (BadgeTemplateData): badge template data + """ + + uuid = attr.ib(type=str) + user = attr.ib(type=UserData) + template = attr.ib(type=BadgeTemplateData) diff --git a/openedx_events/learning/signals.py b/openedx_events/learning/signals.py index fc6fdd86..e2be5137 100644 --- a/openedx_events/learning/signals.py +++ b/openedx_events/learning/signals.py @@ -9,6 +9,7 @@ """ from openedx_events.learning.data import ( + BadgeData, CertificateData, CohortData, CourseDiscussionConfigurationData, @@ -23,7 +24,6 @@ UserData, UserNotificationData, XBlockSkillVerificationData, - UserCourseData, ) from openedx_events.tooling import OpenEdxPublicSignal @@ -351,3 +351,26 @@ "user_course_data": UserCourseData, } ) + +# .. event_type: org.openedx.learning.badge.awarded.v1 +# .. event_name: BADGE_AWARDED +# .. event_description: Emit when a badge is awarded to a learner +# .. event_data: BadgeData +BADGE_AWARDED = OpenEdxPublicSignal( + event_type="org.openedx.learning.badge.awarded.v1", + data={ + "badge": BadgeData, + } +) + + +# .. event_type: org.openedx.learning.badge.revoked.v1 +# .. event_name: BADGE_REVOKED +# .. event_description: Emit when a badge is revoked for a learner +# .. event_data: BadgeData +BADGE_REVOKED = OpenEdxPublicSignal( + event_type="org.openedx.learning.badge.revoked.v1", + data={ + "badge": BadgeData, + } +)