Refatoração do back-end de animais e lotes
O back-end dos animais e lotes agora possui uma estrutura mais organizada e visualmente clara, é desejável que as outras entidades sigam a mesma lógica.
This commit is contained in:
1 parent
9374bfd71b
commit
ae0483ddf1
18 files changed
+132
-128
No files matched your search
@@ -2,5 +2,5 @@
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
<button class="btn-primary" type="submit">Salvar</button>
|
||||
<a href="{{ return_url }}" class="btn-simple">Cancelar</a>
|
||||
<a href="{{ return_url }}" class="btn-primary">Cancelar</a>
|
||||
</form>
|
||||
@@ -28,7 +28,7 @@
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
{% if fail_login %}
|
||||
<p class="login-fail"><i class="fa-solid fa-circle-exclamation"></i> Login ou senha incorretos</p>
|
||||
<p class="login-fail"><i class="fa-solid fa-circle-exclamation"></i> Usuário ou senha incorretos</p>
|
||||
{% endif %}
|
||||
<button type="submit">Entrar <i class="fa-solid fa-right-to-bracket"></i></button>
|
||||
<p>Não possui cadastro? <a href="{% url 'entities:user:user_signin' %}">Solicite acesso</a>.</p>
|
||||
|
||||
@@ -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,39 +61,39 @@ def update_animal(request, animal_id):
|
||||
kwargs={'animal_id': animal.pk}
|
||||
)
|
||||
context['return_url'] = reverse('entities:animal:animal_list')
|
||||
context['form'] = form
|
||||
|
||||
if request.method == 'GET':
|
||||
form = AnimalEditForm(instance=animal)
|
||||
return context
|
||||
|
||||
elif request.method == 'POST':
|
||||
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 {'redirect': 'entities:animal:animal_list'}
|
||||
return True
|
||||
|
||||
context['form'] = form
|
||||
return False
|
||||
|
||||
return {'context': context}
|
||||
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':
|
||||
def get_batch_new_context(request):
|
||||
form = BatchForm()
|
||||
form.fields['animals'].queryset = Animal.objects.filter(
|
||||
farm=selected_farm,
|
||||
is_active=True
|
||||
)
|
||||
form.fields['animals'].queryset = _get_farms_active_animals(request)
|
||||
|
||||
context = {
|
||||
'title': 'Novo Lote',
|
||||
@@ -89,9 +102,9 @@ def create_batch(request):
|
||||
'return_url': reverse('entities:animal:batch_list')
|
||||
}
|
||||
|
||||
return {'context': context}
|
||||
return context
|
||||
|
||||
elif request.method == 'POST':
|
||||
def create_batch(request):
|
||||
form = BatchForm(request.POST)
|
||||
|
||||
if form.is_valid():
|
||||
@@ -99,76 +112,56 @@ def create_batch(request):
|
||||
batch.save()
|
||||
form.save_m2m()
|
||||
|
||||
return {'redirect': 'entities:animal:batch_list'}
|
||||
return True
|
||||
|
||||
form.fields['animals'].queryset = Animal.objects.filter(
|
||||
farm=selected_farm,
|
||||
is_active=True
|
||||
)
|
||||
return False
|
||||
|
||||
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_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')
|
||||
}
|
||||
|
||||
if request.method == 'GET':
|
||||
form = BatchForm(instance=batch_old)
|
||||
|
||||
form.fields['animals'].queryset = Animal.objects.filter(
|
||||
farm=selected_farm,
|
||||
is_active=True
|
||||
)
|
||||
|
||||
context['action_url'] = reverse(
|
||||
'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':
|
||||
return context
|
||||
|
||||
def update_batch(request, batch_id):
|
||||
form = BatchForm(request.POST)
|
||||
batch_old = _get_batch_by_id(request, batch_id)
|
||||
|
||||
if form.is_valid():
|
||||
batch_new = form.save(commit=False)
|
||||
batch_new.is_active = True
|
||||
|
||||
batch_old.is_active = False
|
||||
batch_old.save()
|
||||
|
||||
batch_new = form.save(commit=False)
|
||||
batch_new.is_active = True
|
||||
|
||||
batch_new.save()
|
||||
form.instance = batch_new
|
||||
form.save_m2m()
|
||||
|
||||
return {'redirect': 'entities:animal:batch_list'}
|
||||
return True
|
||||
|
||||
form.fields['animals'].queryset = Animal.objects.filter(
|
||||
farm=selected_farm,
|
||||
is_active=True
|
||||
)
|
||||
return False
|
||||
|
||||
context['action_url'] = reverse(
|
||||
'entities:animal:batch_update',
|
||||
kwargs={'batch_id': batch_old.pk}
|
||||
)
|
||||
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')
|
||||
|
||||
context['form'] = form
|
||||
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()
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -27,11 +27,7 @@
|
||||
<i class="fa-solid fa-magnifying-glass"></i>
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<!-- New -->
|
||||
<a href="{% url create_url %}">
|
||||
Novo <i class="fa-solid fa-plus"></i>
|
||||
</a>
|
||||
<a class="new-entity-button" hx-get="{% url create_url %}" hx-swap="beforeend" hx-target="body">Novo <i class="fa-solid fa-plus"></i></a>
|
||||
</div>
|
||||
|
||||
{% if not object_list %}
|
||||
@@ -57,19 +53,14 @@
|
||||
|
||||
<!-- Update -->
|
||||
<td>
|
||||
<a href="{% url edit_url obj.id %}">
|
||||
<a class="entity-update" hx-get="{% url edit_url obj.id %}" hx-swap="beforeend" hx-target="body">
|
||||
<i class="fa-solid fa-pen-to-square"></i>
|
||||
</a>
|
||||
</td>
|
||||
|
||||
<!-- Delete -->
|
||||
<td>
|
||||
<form method="POST" action="{% url delete_url obj.id %}">
|
||||
{% csrf_token %}
|
||||
<button type="submit" class="btn-icon-trash">
|
||||
<i class="fa-solid fa-trash"></i>
|
||||
</button>
|
||||
</form>
|
||||
<a class="entity-delete" hx-get="{% url delete_url obj.id %}" hx-swap="beforeend" hx-target="body"><i class="fa-solid fa-trash"></i></a>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
{% extends 'base.html' %}
|
||||
{% extends 'base_modal.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block extra_css %}
|
||||
|
||||
@@ -7,58 +7,76 @@ from ..services import animal as services
|
||||
# Animal Views
|
||||
@farm_required
|
||||
def animal_new(request):
|
||||
if request.method == 'GET':
|
||||
context = services.get_animal_new_context()
|
||||
if request.method == 'GET':
|
||||
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):
|
||||
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)
|
||||
context = services.get_batch_update_context(request, batch_id)
|
||||
if request.method == 'GET':
|
||||
return render(request, 'batch/batch_update.html', context)
|
||||
|
||||
if result.get('redirect'):
|
||||
return redirect(result['redirect'])
|
||||
|
||||
return render(request, 'batch/batch_update.html', result['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')
|
||||
Reference in new issue
Block a user