diff --git a/app/models.py b/app/models.py
index 3712687e..9f50bb3c 100644
--- a/app/models.py
+++ b/app/models.py
@@ -76,13 +76,21 @@ def save_client(cls, client_data):
def update_client(self, client_data):
""""update_client: Método para actualizar un cliente en la base de datos"""
+ errors = validate_client(client_data)
+
+ if len(errors.keys()) > 0:
+ return False, errors
+
self.name = client_data.get("name", "") or self.name
- self.email = client_data.get("email", "") or self.email
self.phone = client_data.get("phone", "") or self.phone
- self.address = client_data.get("address", "") or self.address
+ self.email = client_data.get("email", "") or self.email
+ # Actualizar la dirección, permitiendo que se establezca a None si no se proporciona
+ if "address" in client_data:
+ self.address = client_data["address"]
self.save()
+ return True, None
##---------medicine----------
diff --git a/app/templates/clients/form.html b/app/templates/clients/form.html
index beeb1a9e..6c18bf33 100644
--- a/app/templates/clients/form.html
+++ b/app/templates/clients/form.html
@@ -4,7 +4,11 @@
+ {% if client %}
+
Editar Cliente
+ {% else %}
Nuevo Cliente
+ {% endif %}
diff --git a/app/tests_integration.py b/app/tests_integration.py
index ad4c2c16..ebd400ef 100644
--- a/app/tests_integration.py
+++ b/app/tests_integration.py
@@ -85,10 +85,10 @@ def test_validate_email_ends_with_vetsoft_com(self):
def test_edit_user_with_valid_data(self):
client = Client.objects.create(
- name="Juan Sebastián Veron",
- address="13 y 44",
+ name="Juan Sebastian Veron",
phone="54221555232",
email="brujita75@vetsoft.com",
+ address="13 y 44",
)
response = self.client.post(
@@ -96,6 +96,9 @@ def test_edit_user_with_valid_data(self):
data={
"id": client.id,
"name": "Guido Carrillo",
+ "phone": client.phone,
+ "email": client.email,
+ "address": client.address,
},
)
@@ -103,10 +106,11 @@ def test_edit_user_with_valid_data(self):
self.assertEqual(response.status_code, 302)
editedClient = Client.objects.get(pk=client.id)
+
self.assertEqual(editedClient.name, "Guido Carrillo")
self.assertEqual(editedClient.phone, client.phone)
- self.assertEqual(editedClient.address, client.address)
self.assertEqual(editedClient.email, client.email)
+ self.assertEqual(editedClient.address, client.address)
def test_validation_valid_phone(self):
response = self.client.post(
diff --git a/app/tests_unit.py b/app/tests_unit.py
index cb8888fc..2efff2fb 100644
--- a/app/tests_unit.py
+++ b/app/tests_unit.py
@@ -11,8 +11,8 @@ def test_can_create_and_get_client(self):
{
"name": "Juan Sebastian Veron",
"phone": "54221555232",
- "address": "13 y 44",
"email": "brujita75@vetsoft.com",
+ "address": "13 y 44",
},
)
clients = Client.objects.all()
@@ -20,23 +20,30 @@ def test_can_create_and_get_client(self):
self.assertEqual(clients[0].name, "Juan Sebastian Veron")
self.assertEqual(clients[0].phone, "54221555232")
- self.assertEqual(clients[0].address, "13 y 44")
self.assertEqual(clients[0].email, "brujita75@vetsoft.com")
+ self.assertEqual(clients[0].address, "13 y 44")
def test_can_update_client(self):
Client.save_client(
{
"name": "Juan Sebastian Veron",
"phone": "54221555232",
- "address": "13 y 44",
"email": "brujita75@vetsoft.com",
+ "address": "13 y 44",
},
)
client = Client.objects.get(pk=1)
self.assertEqual(client.phone, "54221555232")
- client.update_client({"phone": "54221555233"})
+ client.update_client({
+ "id": client.id,
+ "name": client.name,
+ "phone": "54221555233",
+ "email": client.email,
+ "address": client.address,
+ })
+
client_updated = Client.objects.get(pk=1)
diff --git a/app/views.py b/app/views.py
index e9cf08d3..f00861a1 100644
--- a/app/views.py
+++ b/app/views.py
@@ -22,7 +22,7 @@ def clients_form(request, id=None):
saved, errors = Client.save_client(request.POST)
else:
client = get_object_or_404(Client, pk=client_id)
- client.update_client(request.POST)
+ saved, errors = client.update_client(request.POST)
if saved:
return redirect(reverse("clients_repo"))