예제 #1
0
    def create_user_by_data(self, user_data):
        """
        Create the user by the Django model
        """
        from openedx.core.djangoapps.user_authn.views.registration_form import AccountCreationForm
        from common.djangoapps.student.helpers import do_create_account
        # Check and remove email if its already registered
        aux_pass = BaseUserManager().make_random_password(12)
        aux_pass = aux_pass.lower()
        user_pass = aux_pass if 'pass' not in user_data else user_data['pass']  # Temporary password
        if user_data['email'] == 'null':
            user_data['email'] = str(uuid.uuid4()) + '@invalid.invalid'
        form = AccountCreationForm(
            data={
                "username": self.generate_username(user_data),
                "email": user_data['email'],
                "password": user_pass,
                "name": user_data['nombreCompleto'],
            },
            tos_required=False,
            ignore_email_blacklist=True
        )
        user, _, reg = do_create_account(form)
        reg.activate()
        reg.save()
        #from common.djangoapps.student.models import create_comments_service_user
        #create_comments_service_user(user)

        return user
예제 #2
0
 def enroll_create_user(self, course_ids, mode, lista_data, enroll):
     """
         Create and enroll the user with/without UChile account
         if email or rut exists not saved them
     """
     lista_saved = []
     lista_not_saved = []
     # guarda el form
     with transaction.atomic():
         for dato in lista_data:
             dato = [d.strip() for d in dato]
             dato[1] = dato[1].lower()
             if len(dato) == 3:
                 dato[2] = dato[2].upper()
                 dato[2] = dato[2].replace("-", "")
                 dato[2] = dato[2].replace(".", "")
                 dato[2] = dato[2].strip()
             if len(dato) == 2:
                 dato.append("")
             while len(dato[2]) > 0 and len(dato[2]) < 10 and 'P' != dato[2][0] and 'CG' != dato[2][0:2]:
                 dato[2] = "0" + dato[2]
             aux_email = dato[1]
             aux_pass = BaseUserManager().make_random_password(12)
             aux_pass = aux_pass.lower()
             aux_user = False
             if User.objects.filter(email=dato[1]).exists():
                 dato[1] = 'null'
             if dato[2] != "":
                 aux_rut = ''
                 if EdxLoginUser.objects.filter(run=dato[2]).exists():
                     aux_rut = dato[2]
                     edxlogin_user = EdxLoginUser.objects.get(run=dato[2])
                     if not edxlogin_user.have_sso:
                         aux_user = True
                 else:
                     edxlogin_user = self.create_user_with_run(dato, aux_pass)
                 if edxlogin_user is None:
                     lista_not_saved.append([aux_email, dato[2]])
                 else:
                     for course_id in course_ids:
                         self.enroll_course(edxlogin_user, course_id, enroll, mode)
                     lista_saved.append({
                         'email_o': aux_email,
                         'email_d': edxlogin_user.user.email,
                         'nombreCompleto': edxlogin_user.user.profile.name.strip(),
                         'rut': dato[2],
                         'rut_aux': aux_rut,
                         'password': aux_pass,
                         'sso': edxlogin_user.have_sso,
                         'exists': aux_user
                     })
             else:
                 if dato[1] != 'null':
                     user_data = {
                         'email':dato[1],
                         'nombreCompleto':dato[0],
                         'pass': aux_pass
                     }
                     user = self.create_user_by_data(user_data)
                 else:
                     aux_user = True
                     user = User.objects.get(email=aux_email)
                 for course_id in course_ids:
                     self.enroll_course_user(user, course_id, enroll, mode)
                 lista_saved.append({
                     'email_o': aux_email,
                     'email_d': user.email,
                     'nombreCompleto': user.profile.name.strip(),
                     'rut': '',
                     'rut_aux': '',
                     'password': aux_pass,
                     'sso': False,
                     'exists': aux_user
                 })
     return lista_saved, lista_not_saved