Esempio n. 1
0
    def create_user(self, password=None, **kwargs):
        try:
            if kwargs.get('email'):
                # exp reg para saber si el nick corresponde al correo de la ugr (@correo.ugr.es)
                if not re.match(r'^[_a-z0-9]+(@correo\.ugr\.es)$', kwargs.get('email')):
                    raise NameError("El email no es correcto o no pertenece a la UGR")

                res = Alumno.objects.filter(email=kwargs.get('email'))
                if res.count() != 0:
                    raise NameError("El alumno ya existe")
            if kwargs.get('dni'):
                # exp reg para saber si el nick corresponde al correo de la ugr (@correo.ugr.es)
                if not re.match(r'(([X-Z]{1})([-]?)(\d{7})([-]?)([A-Z]{1}))|((\d{8})([-]?)([A-Z]{1}))', kwargs.get('dni')):
                    raise NameError("Error en el DNI del alumno")

            if kwargs.get('first_name') and not utils.is_string(kwargs.get('first_name')):
                raise NameError("Nombre incorrecto")

            if kwargs.get('last_name') and not utils.is_string(kwargs.get('last_name')):
                raise NameError("Apellidos incorrectos")

            usuario = self.model.objects.create(email=kwargs.get('email'), dni=kwargs.get('dni'),
                                                first_name=kwargs.get('first_name'), last_name=kwargs.get('last_name'))

            grupo_alumnos = Grupos.objects.get(name='Alumnos')
            usuario.set_password(password)
            usuario.save()
            grupo_alumnos.user_set.add(usuario)
            if usuario.email:
                utils.enviar_email_reset_password(usuario.email)
            return dict(status=True, data=usuario)

        except NameError as e:
            return dict(status=False, message=e.message)
Esempio n. 2
0
    def update(self, usuario, validated_data):
        try:
            # comprobando email
            if 'email' in validated_data.keys():
                new_email = validated_data.get('email')
                res = Usuario.objects.filter(email=new_email)
                if res.count() == 0:
                    if not utils.is_string(new_email) or not \
                            re.match(r'^[a-z][_a-z0-9]+(@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4}))$', new_email):
                        raise NameError("El email no es correcto")
                    else:
                        usuario.email = new_email
                else:
                    raise NameError("El usuario indicado ya existe")

            # comprobando dni
            if 'dni' in validated_data.keys() and not validated_data.get('creado'):
                new_dni = validated_data.get('dni')
                res = Usuario.objects.filter(dni=new_dni)
                if res.count() == 0:
                    if not utils.is_string(new_dni) or not \
                            re.match(r'(\d{8})([-]?)([A-Z]{1})', new_dni):
                        raise NameError("El dni no es correcto")
                    else:
                        usuario.email = new_dni
                else:
                    raise NameError("El usuario indicado ya existe")

            # comprobando nombre
            if 'first_name' in validated_data.keys():
                new_first_name = validated_data.get('first_name')
                if new_first_name == '' or not utils.is_string(new_first_name):
                    raise NameError("Nombre incorrecto")
                else:
                    usuario.first_name = new_first_name

            # comprobando apellidos
            if 'last_name' in validated_data.keys():
                new_last_name = validated_data.get('last_name')
                if new_last_name == '' or not utils.is_string(new_last_name):
                    raise NameError("Nombre incorrecto")
                else:
                    usuario.new_last_name = new_last_name

            # if 'password' in validated_data.keys() and 'confirm_password' in validated_data.keys():
            #     password = validated_data.get('password')
            #     confirm_password = validated_data.get('confirm_password')
            #     if password and confirm_password and password == confirm_password:
            #         alumno.set_password(password)

            usuario.save()
            if validated_data.get('creado'):
                utils.enviar_email_reset_password(usuario.email)

            return dict(status=True, data=usuario)

        except NameError as e:
            return dict(status=False, message=e.message)
        except:
            return dict(status=False, message="Error en los parametros")
Esempio n. 3
0
    def create_user(self, password=None, **kwargs):
        try:
            if kwargs.get('email'):
                if not (kwargs.get('email') or not (re.match(r'^[a-z][_a-z0-9]+(@ugr\.es)$', kwargs.get('email')))):
                    raise NameError("El correo no es correcto")
                res = Profesor.objects.filter(email=kwargs.get('email'))
                if res.count() != 0:
                    raise NameError("El profesor ya existe")
            if kwargs.get('dni') and kwargs.get('dni') is not '':
                # exp reg para saber si el nick corresponde al correo de la ugr (@correo.ugr.es)
                if not re.match(r'(\d{8})([-]?)([A-Z]{1})', kwargs.get('dni')):
                    raise NameError("Error en el DNI del profesor")

            if not kwargs.get('first_name') or not utils.is_string(kwargs.get('first_name')):
                raise NameError("Error en el nombre del profesor")

            if not kwargs.get('last_name') or not utils.is_string(kwargs.get('last_name')):
                raise NameError("Error en los apellidos del profesor")

            if not kwargs.get('departamento') or not isinstance(kwargs.get('departamento'), Departamento):
                raise NameError("Error en el departamento")

            usuario = self.model.objects.create(email=kwargs.get('email'), dni=kwargs.get('dni'),
                                                first_name=kwargs.get('first_name'), last_name=kwargs.get('last_name'),
                                                departamento=kwargs.get('departamento'))

            # comprobando si es Jefe de departamento
            if kwargs.get('jefe_departamento'):
                grupo_jefe_departamento = Grupos.objects.get(name='Jefe de Departamento')
                grupo_jefe_departamento.user_set.add(usuario)

            grupo_profesores = Grupos.objects.get(name='Profesores')
            usuario.set_password(password)
            usuario.save()
            grupo_profesores.user_set.add(usuario)
            if usuario.email:
                utils.enviar_email_reset_password(usuario.email)
            return dict(status=True, data=Profesor.objects.get(email=usuario.email))

        except NameError as e:
            return dict(status=False, message=e.message)