diff --git a/multigranja/apps/core/templates/includes/_form.html b/multigranja/apps/core/templates/includes/_form.html index 8bd2c53..bddcab0 100644 --- a/multigranja/apps/core/templates/includes/_form.html +++ b/multigranja/apps/core/templates/includes/_form.html @@ -2,5 +2,5 @@ {% csrf_token %} {{ form }} - Cancelar + Cancelar \ No newline at end of file diff --git a/multigranja/apps/core/templates/login.html b/multigranja/apps/core/templates/login.html index 75f6e39..aaa8d38 100644 --- a/multigranja/apps/core/templates/login.html +++ b/multigranja/apps/core/templates/login.html @@ -28,7 +28,7 @@ {% csrf_token %} {{ form }} {% if fail_login %} -

Login ou senha incorretos

+

Usuário ou senha incorretos

{% endif %}

Não possui cadastro? Solicite acesso.

diff --git a/multigranja/apps/entities/services/animal.py b/multigranja/apps/entities/services/animal.py index 041a537..756edce 100644 --- a/multigranja/apps/entities/services/animal.py +++ b/multigranja/apps/entities/services/animal.py @@ -1,4 +1,4 @@ -from django.shortcuts import get_object_or_404 +from django.shortcuts import get_object_or_404, redirect from django.urls import reverse from django.db.models import Prefetch @@ -6,10 +6,24 @@ from ..models import Animal, Batch from ..forms import AnimalCreateForm, AnimalEditForm, BatchForm from entities.models import Farm +# Local utility functions +def _get_selected_farm_id(request): + return request.session['selected_farm_id'] -def get_selected_farm(request): - return Farm.objects.get(id=request.session['selected_farm_id']) +def _get_selected_farm(request): + return Farm.objects.get(id=_get_selected_farm_id(request)) +def _get_animal_by_id(request, animal_id): + return get_object_or_404(Animal, pk=animal_id, farm_id=_get_selected_farm_id(request)) + +def _get_farms_active_animals(request): + return Animal.objects.filter( + farm=_get_selected_farm_id(request), + is_active=True + ) + +def _get_batch_by_id(request, batch_id): + return get_object_or_404(Batch, pk=batch_id, is_active=True) # Animal Services def get_animal_new_context(): @@ -26,7 +40,7 @@ def get_animal_new_context(): def create_animal(request): form = AnimalCreateForm(request.POST) - selected_farm = get_selected_farm(request) + selected_farm = _get_selected_farm(request) if form.is_valid(): animal = form.save(commit=False) @@ -36,10 +50,9 @@ def create_animal(request): return False - -def update_animal(request, animal_id): - farm_id = request.session['selected_farm_id'] - animal = get_object_or_404(Animal, pk=animal_id, farm_id=farm_id) +def get_animal_update_context(request, animal_id): + animal = _get_animal_by_id(request, animal_id) + form = AnimalEditForm(instance=animal) context = {} context['title'] = 'Editar animal' @@ -48,127 +61,107 @@ def update_animal(request, animal_id): kwargs={'animal_id': animal.pk} ) context['return_url'] = reverse('entities:animal:animal_list') - - if request.method == 'GET': - form = AnimalEditForm(instance=animal) - - elif request.method == 'POST': - form = AnimalEditForm(request.POST, instance=animal) - - if form.is_valid(): - form.save() - return {'redirect': 'entities:animal:animal_list'} - context['form'] = form - return {'context': context} + return context +def update_animal(request, animal_id): + animal = _get_animal_by_id(request, animal_id) + form = AnimalEditForm(request.POST, instance=animal) + + if form.is_valid(): + form.save() + return True + + return False + +def get_animal_delete_context(request, animal_id): + animal = _get_animal_by_id(request, animal_id) + context = {} + context['description'] = f"Excluir registro de animal {animal.identifier}" + context['positive_url'] = reverse('entities:animal:animal_delete', kwargs={'animal_id': animal_id}) + context['negative_url'] = reverse('entities:animal:animal_list') + + return context def delete_animal(request, animal_id): - farm_id = request.session['selected_farm_id'] - animal = get_object_or_404(Animal, pk=animal_id, farm_id=farm_id) + animal = _get_animal_by_id(request, animal_id) animal.is_active = False animal.save() # Batch Services -def create_batch(request): - selected_farm = get_selected_farm(request) - - if request.method == 'GET': - form = BatchForm() - form.fields['animals'].queryset = Animal.objects.filter( - farm=selected_farm, - is_active=True - ) - - context = { - 'title': 'Novo Lote', - 'form': form, - 'action_url': reverse('entities:animal:batch_create'), - 'return_url': reverse('entities:animal:batch_list') - } - - return {'context': context} - - elif request.method == 'POST': - form = BatchForm(request.POST) - - if form.is_valid(): - batch = form.save(commit=False) - batch.save() - form.save_m2m() - - return {'redirect': 'entities:animal:batch_list'} - - form.fields['animals'].queryset = Animal.objects.filter( - farm=selected_farm, - is_active=True - ) - - context = { - 'title': 'Novo Lote', - 'form': form, - 'action_url': reverse('entities:animal:batch_create'), - 'return_url': reverse('entities:animal:batch_list') - } - - return {'context': context} - - -def update_batch(request, batch_id): - batch_old = get_object_or_404(Batch, pk=batch_id, is_active=True) - selected_farm = get_selected_farm(request) +def get_batch_new_context(request): + form = BatchForm() + form.fields['animals'].queryset = _get_farms_active_animals(request) context = { - 'title': f'Editar Lote {batch_old.identifier}', + 'title': 'Novo Lote', + 'form': form, + 'action_url': reverse('entities:animal:batch_create'), 'return_url': reverse('entities:animal:batch_list') } - if request.method == 'GET': - form = BatchForm(instance=batch_old) + return context - form.fields['animals'].queryset = Animal.objects.filter( - farm=selected_farm, - is_active=True - ) +def create_batch(request): + form = BatchForm(request.POST) - context['action_url'] = reverse( + if form.is_valid(): + batch = form.save(commit=False) + batch.save() + form.save_m2m() + + return True + + return False + +def get_batch_update_context(request, batch_id): + batch_old = _get_batch_by_id(request, batch_id) + form = BatchForm(instance=batch_old) + form.fields['animals'].queryset = _get_farms_active_animals(request) + + context = { + 'title': f'Editar Lote {batch_old.identifier}', + 'return_url': reverse('entities:animal:batch_list'), + 'action_url': reverse( 'entities:animal:batch_update', kwargs={'batch_id': batch_old.pk} - ) + ), + 'form': form + } - elif request.method == 'POST': - form = BatchForm(request.POST) + return context - if form.is_valid(): - batch_new = form.save(commit=False) - batch_new.is_active = True +def update_batch(request, batch_id): + form = BatchForm(request.POST) + batch_old = _get_batch_by_id(request, batch_id) - batch_old.is_active = False - batch_old.save() + if form.is_valid(): + batch_old.is_active = False + batch_old.save() - batch_new.save() - form.save_m2m() + batch_new = form.save(commit=False) + batch_new.is_active = True - return {'redirect': 'entities:animal:batch_list'} + batch_new.save() + form.instance = batch_new + form.save_m2m() - form.fields['animals'].queryset = Animal.objects.filter( - farm=selected_farm, - is_active=True - ) + return True - context['action_url'] = reverse( - 'entities:animal:batch_update', - kwargs={'batch_id': batch_old.pk} - ) + return False - context['form'] = form +def get_batch_delete_context(request, batch_id): + batch = _get_batch_by_id(request, batch_id) + context = {} + context['description'] = f"Excluir registro do lote {batch.identifier}" + context['positive_url'] = reverse('entities:animal:batch_delete', kwargs={'batch_id': batch_id}) + context['negative_url'] = reverse('entities:animal:batch_list') + + return context - return {'context': context} - - -def delete_batch(batch_id): - batch = get_object_or_404(Batch, pk=batch_id, is_active=True) +def delete_batch(request, batch_id): + batch = _get_batch_by_id(request, batch_id) batch.is_active = False batch.save() \ No newline at end of file diff --git a/multigranja/apps/entities/static/layout/entity_list.css b/multigranja/apps/entities/static/layout/entity_list.css index f750b65..a10ef1e 100644 --- a/multigranja/apps/entities/static/layout/entity_list.css +++ b/multigranja/apps/entities/static/layout/entity_list.css @@ -77,7 +77,9 @@ } /* NEW BUTTON */ -.entity_search > a { +.new-entity-button { + cursor: pointer; + user-select: none; height: 32px; padding: 0 var(--space-md); background: var(--color-success); @@ -91,7 +93,7 @@ transition: background var(--transition-speed); } -.entity_search > a:hover { +.new-entity-button:hover { background: var(--color-success); opacity: 0.85; } diff --git a/multigranja/apps/entities/templates/animal/animal_new.html b/multigranja/apps/entities/templates/animal/animal_new.html index 01ab5fc..7071e78 100644 --- a/multigranja/apps/entities/templates/animal/animal_new.html +++ b/multigranja/apps/entities/templates/animal/animal_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/animal/animal_update.html b/multigranja/apps/entities/templates/animal/animal_update.html index 91a13f3..29e1963 100644 --- a/multigranja/apps/entities/templates/animal/animal_update.html +++ b/multigranja/apps/entities/templates/animal/animal_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/batch/batch_new.html b/multigranja/apps/entities/templates/batch/batch_new.html index 72f0361..952fb8f 100644 --- a/multigranja/apps/entities/templates/batch/batch_new.html +++ b/multigranja/apps/entities/templates/batch/batch_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/batch/batch_update.html b/multigranja/apps/entities/templates/batch/batch_update.html index 09b7aed..b1c9c88 100644 --- a/multigranja/apps/entities/templates/batch/batch_update.html +++ b/multigranja/apps/entities/templates/batch/batch_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/entity_list.html b/multigranja/apps/entities/templates/entity_list.html index 860c291..e7c4549 100644 --- a/multigranja/apps/entities/templates/entity_list.html +++ b/multigranja/apps/entities/templates/entity_list.html @@ -9,7 +9,7 @@ {% block content %}

{{ title }}

- {% block before_table %} {% endblock %} + {% block before_table %}{% endblock %} {% if not object_list %} @@ -57,19 +53,14 @@ - + -
- {% csrf_token %} - -
+ {% endfor %} @@ -101,6 +92,6 @@ {% endif %} {% endif %} - {% block after_table %} {% endblock %} + {% block after_table %}{% endblock %} {% endblock %} \ No newline at end of file diff --git a/multigranja/apps/entities/templates/farm/farm_new.html b/multigranja/apps/entities/templates/farm/farm_new.html index 1a6a2a6..697f85b 100644 --- a/multigranja/apps/entities/templates/farm/farm_new.html +++ b/multigranja/apps/entities/templates/farm/farm_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/farm/farm_update.html b/multigranja/apps/entities/templates/farm/farm_update.html index dc68d98..0d24c10 100644 --- a/multigranja/apps/entities/templates/farm/farm_update.html +++ b/multigranja/apps/entities/templates/farm/farm_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/paddock/paddock_new.html b/multigranja/apps/entities/templates/paddock/paddock_new.html index ae3602e..25902fa 100644 --- a/multigranja/apps/entities/templates/paddock/paddock_new.html +++ b/multigranja/apps/entities/templates/paddock/paddock_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/paddock/paddock_update.html b/multigranja/apps/entities/templates/paddock/paddock_update.html index ea1f89e..4feb2cf 100644 --- a/multigranja/apps/entities/templates/paddock/paddock_update.html +++ b/multigranja/apps/entities/templates/paddock/paddock_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/paddock_group/paddock_group_new.html b/multigranja/apps/entities/templates/paddock_group/paddock_group_new.html index 206e97e..18c2b3f 100644 --- a/multigranja/apps/entities/templates/paddock_group/paddock_group_new.html +++ b/multigranja/apps/entities/templates/paddock_group/paddock_group_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/paddock_group/paddock_group_update.html b/multigranja/apps/entities/templates/paddock_group/paddock_group_update.html index ea70fdc..f3de954 100644 --- a/multigranja/apps/entities/templates/paddock_group/paddock_group_update.html +++ b/multigranja/apps/entities/templates/paddock_group/paddock_group_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/rotation/rotation_new.html b/multigranja/apps/entities/templates/rotation/rotation_new.html index 81aaf58..63f4915 100644 --- a/multigranja/apps/entities/templates/rotation/rotation_new.html +++ b/multigranja/apps/entities/templates/rotation/rotation_new.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/templates/rotation/rotation_update.html b/multigranja/apps/entities/templates/rotation/rotation_update.html index af0450d..1875fc9 100644 --- a/multigranja/apps/entities/templates/rotation/rotation_update.html +++ b/multigranja/apps/entities/templates/rotation/rotation_update.html @@ -1,4 +1,4 @@ -{% extends 'base.html' %} +{% extends 'base_modal.html' %} {% load static %} {% block extra_css %} diff --git a/multigranja/apps/entities/views/animal.py b/multigranja/apps/entities/views/animal.py index 02ad90a..da72356 100644 --- a/multigranja/apps/entities/views/animal.py +++ b/multigranja/apps/entities/views/animal.py @@ -7,58 +7,76 @@ from ..services import animal as services # Animal Views @farm_required def animal_new(request): + context = services.get_animal_new_context() if request.method == 'GET': - context = services.get_animal_new_context() return render(request, 'animal/animal_new.html', context) - elif request.method == 'POST': - success = services.create_animal(request) - if success: + if request.method == 'POST': + if services.create_animal(request): return redirect('entities:animal:animal_list') - - return redirect('entities:animal:animal_create') + context['error'] = "Não foi possível registrar o animal" + return render(request, 'animal/animal_new.html', context) @farm_required def animal_update(request, animal_id): - result = services.update_animal(request, animal_id) + context = services.get_animal_update_context(request, animal_id) + if request.method == 'GET': + return render(request, 'animal/animal_update.html', context) - if result.get('redirect'): - return redirect(result['redirect']) - - return render(request, 'animal/animal_update.html', result['context']) + if request.method == 'POST': + if services.update_animal(request, animal_id): + return redirect('entities:animal:animal_list') + context['error'] = "Não foi possível editar o animal" + return render(request, 'animal/animal_update.html', context) @farm_required -@require_http_methods(["POST"]) def animal_delete(request, animal_id): - services.delete_animal(request, animal_id) + if request.method == 'GET': + context = services.get_animal_delete_context(request, animal_id) + return render(request, 'confirmation_popup.html', context) + + if request.method == 'POST': + services.delete_animal(request, animal_id) + return redirect('entities:animal:animal_list') # Batch Views @farm_required def batch_new(request): - result = services.create_batch(request) + context = services.get_batch_new_context(request) + if request.method == 'GET': + return render(request, 'batch/batch_new.html', context) - if result.get('redirect'): - return redirect(result['redirect']) - - return render(request, 'batch/batch_new.html', result['context']) + if request.method == 'POST': + if services.create_batch(request): + return redirect('entities:animal:batch_list') + context['error'] = "Não foi possível criar o lote" + return render(request, 'batch/batch_new.html', context) @farm_required def batch_update(request, batch_id): - result = services.update_batch(request, batch_id) - - if result.get('redirect'): - return redirect(result['redirect']) - - return render(request, 'batch/batch_update.html', result['context']) + context = services.get_batch_update_context(request, batch_id) + if request.method == 'GET': + return render(request, 'batch/batch_update.html', context) + + if request.method == 'POST': + if services.update_batch(request, batch_id): + return redirect('entities:animal:batch_list') + context['error'] = "Não foi possível editar o lote" + return render(request, 'batch/batch_update.html', context) @farm_required -@require_http_methods(["POST"]) def batch_delete(request, batch_id): - services.delete_batch(batch_id) + if request.method == 'GET': + context = services.get_batch_delete_context(request, batch_id) + return render(request, 'confirmation_popup.html', context) + + if request.method == 'POST': + services.delete_batch(request, batch_id) + return redirect('entities:animal:batch_list') \ No newline at end of file