Skip to content
Closed
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
77 changes: 77 additions & 0 deletions eox_core/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import logging

from crum import get_current_request
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.db.models.signals import post_save
from openedx_filters import PipelineStep

from eox_core.edxapp_wrapper.configuration_helpers import get_configuration_helper
from eox_core.edxapp_wrapper.users import (
generate_password,
get_user_attribute,
Expand Down Expand Up @@ -219,3 +223,76 @@ def ensure_user_has_signup_source(user=None, *args, **kwargs):
"Created new singup source for user during the third party pipeline.",
**locals()
)


class AddCustomOptionsOnAccountSettings(PipelineStep):
""" Pipeline used to add custom option fields in account settings.

Example usage:

Add the following configurations to your configuration file:
"OPEN_EDX_FILTERS_CONFIG": {
"org.openedx.learning.student.settings.render.started.v1": {
"fail_silently": false,
"pipeline": [
"eox_core.pipeline.AddCustomOptionsOnAccountSettings"
]
}
}
"""

def run_filter(self, context): # pylint: disable=arguments-differ
""" Run the pipeline filter. """
extended_profile_fields = context.get("extended_profile_fields", [])

custom_options, field_labels_map = self._get_custom_context(extended_profile_fields)

conf_helper = get_configuration_helper()

extended_profile_field_options = conf_helper.get_value('EXTRA_FIELD_OPTIONS', custom_options)
extended_profile_field_option_tuples = {}
for field in extended_profile_field_options.keys():
field_options = extended_profile_field_options[field]
extended_profile_field_option_tuples[field] = [(option.lower(), option) for option in field_options]

for field in custom_options:
field_dict = {
"field_name": field,
"field_label": field_labels_map.get(field, field),
}

field_options = extended_profile_field_option_tuples.get(field)
if field_options:
field_dict["field_type"] = "ListField"
field_dict["field_options"] = field_options
else:
field_dict["field_type"] = "TextField"

field_index = next((index for (index, d) in enumerate(extended_profile_fields) if d["field_name"] == field_dict["field_name"]), None)
if field_index is not None:
context["extended_profile_fields"][field_index] = field_dict
return context

def _get_custom_context(self, extended_profile_fields):
""" Get custom context for the field. """
field_labels = {}
field_options = {}
custom_fields = getattr(settings, "EDNX_CUSTOM_REGISTRATION_FIELDS", [])

for field in custom_fields:
field_name = field.get("name")

if not field_name: # Required to identify the field.
msg = "Custom fields must have a `name` defined in their configuration."
raise ImproperlyConfigured(msg)

field_label = field.get("label")
if not any(extended_field['field_name'] == field_name for extended_field in extended_profile_fields) and field_label:
field_labels[field_name] = (field_label)

options = field.get("options")

if options:
field_options[field_name] = options

return field_options, field_labels
1 change: 1 addition & 0 deletions requirements/base.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ edx-opaque-keys[django]==2.3.0
openedx-events==0.13.0
django
click
openedx-filters==1.2.0
3 changes: 3 additions & 0 deletions requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ django==3.2.16
# event-tracking
# jsonfield
# openedx-events
# openedx-filters
django-crum==0.7.9
# via
# edx-django-utils
Expand Down Expand Up @@ -182,6 +183,8 @@ oauthlib==3.2.2
# via django-oauth-toolkit
openedx-events==0.13.0
# via -r requirements/base.in
openedx-filters==1.2.0
# via -r requirements/base.in
packaging==23.0
# via drf-yasg
pbr==5.11.1
Expand Down
1 change: 0 additions & 1 deletion requirements/pip-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ click==8.1.3
# via
# -c requirements/constraints.txt
# pip-tools
packaging==23.0
# via build
pip-tools==6.12.1
# via -r requirements/pip-tools.in
Expand Down
2 changes: 2 additions & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ oauthlib==3.2.2
# django-oauth-toolkit
openedx-events==0.13.0
# via -r requirements/base.txt
openedx-filters==1.2.0
# via -r requirements/base.txt
packaging==23.0
# via
# -r requirements/base.txt
Expand Down