2121 get_retired_email_by_email ,
2222 username_exists_or_retired ,
2323)
24+ from django import forms
2425from django .conf import settings
2526from django .contrib .auth import get_user_model
2627from django .db import transaction
3233 _set_unusable_password # pylint: disable=import-error,unused-import
3334from openedx .core .djangoapps .user_api .models import UserRetirementStatus # pylint: disable=import-error
3435from openedx .core .djangoapps .user_api .preferences import api as preferences_api # pylint: disable=import-error
36+ from openedx .core .djangoapps .user_authn .views .register import REGISTER_USER as post_register # pylint: disable=import-error
3537from openedx .core .djangoapps .user_authn .views .registration_form import ( # pylint: disable=import-error
3638 AccountCreationForm ,
3739)
@@ -67,6 +69,34 @@ def check_edxapp_account_conflicts(email, username):
6769 return conflicts
6870
6971
72+ class EdnxAccountCreationForm (AccountCreationForm ):
73+ """
74+ A form to extend the behaviour of the AccountCreationForm.
75+ For now the purpose of this form is to allow to make the
76+ password optional if the flag 'skip_password' is True.
77+ This form it's currently only used for validation, not rendering.
78+ """
79+
80+ def __init__ ( # pylint:disable=too-many-arguments
81+ self ,
82+ data = None ,
83+ extra_fields = None ,
84+ extended_profile_fields = None ,
85+ do_third_party_auth = True ,
86+ tos_required = True ,
87+ ):
88+ super ().__init__ (
89+ data = data ,
90+ extra_fields = extra_fields ,
91+ extended_profile_fields = extended_profile_fields ,
92+ do_third_party_auth = do_third_party_auth ,
93+ tos_required = tos_required ,
94+ )
95+
96+ if data .pop ("skip_password" , False ):
97+ self .fields ['password' ] = forms .CharField (required = False )
98+
99+
70100def create_edxapp_user (* args , ** kwargs ):
71101 """
72102 Creates a user on the open edx django site using calls to
@@ -88,30 +118,26 @@ def create_edxapp_user(*args, **kwargs):
88118 """
89119 errors = []
90120
91- email = kwargs .pop ("email" )
92- username = kwargs .pop ("username" )
121+ extra_fields = getattr (settings , "REGISTRATION_EXTRA_FIELDS" , {})
122+ extended_profile_fields = getattr (settings , "extended_profile_fields" , [])
123+ kwargs ["name" ] = kwargs .pop ("fullname" , None )
124+ email = kwargs .get ("email" )
125+ username = kwargs .get ("username" )
93126 conflicts = check_edxapp_account_conflicts (email = email , username = username )
94127 if conflicts :
95128 return None , ["Fatal: account collition with the provided: {}" .format (", " .join (conflicts ))]
96129
97- data = {
98- 'username' : username ,
99- 'email' : email ,
100- 'password' : kwargs .pop ("password" ),
101- 'name' : kwargs .pop ("fullname" ),
102- }
103130 # Go ahead and create the new user
104131 with transaction .atomic ():
105132 # In theory is possible to extend the registration form with a custom app
106133 # An example form app for this can be found at http://github.com/open-craft/custom-form-app
107134 # form = get_registration_extension_form(data=params)
108135 # if not form:
109- form = AccountCreationForm (
110- data = data ,
136+ form = EdnxAccountCreationForm (
137+ data = kwargs ,
111138 tos_required = False ,
112- # TODO: we need to support the extra profile fields as defined in the django.settings
113- # extra_fields=extra_fields,
114- # extended_profile_fields=extended_profile_fields,
139+ extra_fields = extra_fields ,
140+ extended_profile_fields = extended_profile_fields ,
115141 # enforce_password_policy=enforce_password_policy,
116142 )
117143 (user , profile , registration ) = do_create_account (form ) # pylint: disable=unused-variable
@@ -129,6 +155,9 @@ def create_edxapp_user(*args, **kwargs):
129155
130156 # TODO: link account with third party auth
131157
158+ # Announce registration through API call
159+ post_register .send_robust (sender = None , user = user ) # pylint: disable=no-member
160+
132161 lang_pref = kwargs .pop ("language_preference" , False )
133162 if lang_pref :
134163 try :
0 commit comments