コード例 #1
0
ファイル: user.py プロジェクト: OscarM3615/biteapi
    def put(self, user_id: int):
        """
		Actualizar los datos del usuario. Permitir solo si es el mismo usuario quien lo solicita.
		"""
        if current_identity.id != user_id:
            return {
                "message":
                "No tiene permiso para modificar los datos de esta cuenta."
            }, 401

        data = User.parser.parse_args()
        user = UserModel.find_by_id(user_id)

        if identityRegex.match(
                data['first_name']) is None or identityRegex.match(
                    data['last_name']) is None:
            return {
                "message": "El formato del nombre o apellido no es correcto."
            }, 400

        if emailRegex.match(data['email']) is None:
            return {
                "message":
                "El correo proporcionado no parece ser un correo realmente."
            }, 400

        if UserModel.find_by_email(
                data['email']) and user.email != data['email']:
            return {
                "message":
                "El correo proporcionado ya pertenece a una cuenta registrada."
            }, 400

        if user.user_type == user_types['vendor'] and data.get(
                'user_type') == user_types['normal']:
            ProductModel.delete_user_products(user_id)
        if data.get('user_type'):
            user.user_type = data['user_type']

        user.first_name = data['first_name']
        user.last_name = data['last_name']
        user.email = data['email']
        user.is_active = data['is_active']
        user.save_to_db()

        return user.json()
コード例 #2
0
ファイル: user.py プロジェクト: OscarM3615/biteapi
    def post(self):
        """
		Registrar a un usuario en la base de datos proporcionando su nombre completo, correo y contraseña.
		"""
        data = UserRegistration.parser.parse_args()

        if UserModel.find_by_email(data['email']):
            return {
                "message": "Ya existe una cuenta registrada con ese correo."
            }, 400

        if identityRegex.match(
                data['first_name']) is None or identityRegex.match(
                    data['last_name']) is None:
            return {
                "message": "El formato del nombre o apellido no es correcto."
            }, 400

        if emailRegex.match(data['email']) is None:
            return {
                "message":
                "El correo proporcionado no parece ser un correo realmente."
            }, 400

        if passwordRegex.match(data['password']) is None:
            return {
                "message":
                "La contraseña indicada es muy corta (mínimo 4 caracteres)."
            }, 400

        hashed_password = bcrypt.hashpw(data['password'].encode('utf-8'),
                                        bcrypt.gensalt()).decode('utf-8')
        new_user = UserModel(data['first_name'], data['last_name'],
                             data['email'], hashed_password)
        new_user.save_to_db()

        return new_user.json(), 201
コード例 #3
0
ファイル: tests.py プロジェクト: OscarM3615/biteapi
 def test_nombre_con_simbolos(self):
     """No debe aceptar nombres con símbolos."""
     self.assertIsNone(identityRegex.match('André.'))
コード例 #4
0
ファイル: tests.py プロジェクト: OscarM3615/biteapi
 def test_nombre_con_numeros(self):
     """No debe aceptar nombres con números."""
     self.assertIsNone(identityRegex.match('OscarM3615'))
コード例 #5
0
ファイル: tests.py プロジェクト: OscarM3615/biteapi
 def test_tres_nombres(self):
     """No debe aceptar más de dos nombres."""
     self.assertIsNone(identityRegex.match('Óscar Antonio Miranda'))
コード例 #6
0
ファイル: tests.py プロジェクト: OscarM3615/biteapi
 def test_dos_nombres(self):
     """Debe aceptar si se incluyen los dos nombres."""
     self.assertIsNotNone(identityRegex.match('Óscar Antonio'))
コード例 #7
0
ファイル: tests.py プロジェクト: OscarM3615/biteapi
 def test_un_nombre(self):
     """Debe aceptar un solo nombre, incluso con acento."""
     self.assertIsNotNone(identityRegex.match('Óscar'))