Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Agrego validación en el editar de cliente #40

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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----------


Expand Down
4 changes: 4 additions & 0 deletions app/templates/clients/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<div class="container">
<div class="row">
<div class="col-lg-6 offset-lg-3">
{% if client %}
<h1>Editar Cliente</h1>
{% else %}
<h1>Nuevo Cliente</h1>
{% endif %}
</div>
</div>

Expand Down
10 changes: 7 additions & 3 deletions app/tests_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,32 @@ 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="[email protected]",
address="13 y 44",
)

response = self.client.post(
reverse("clients_form"),
data={
"id": client.id,
"name": "Guido Carrillo",
"phone": client.phone,
"email": client.email,
"address": client.address,
},
)

# redirect after post
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(
Expand Down
15 changes: 11 additions & 4 deletions app/tests_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,39 @@ def test_can_create_and_get_client(self):
{
"name": "Juan Sebastian Veron",
"phone": "54221555232",
"address": "13 y 44",
"email": "[email protected]",
"address": "13 y 44",
},
)
clients = Client.objects.all()
self.assertEqual(len(clients), 1)

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, "[email protected]")
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": "[email protected]",
"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)

Expand Down
2 changes: 1 addition & 1 deletion app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down