192 lines
6.1 KiB
Python
192 lines
6.1 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from .models import Feeding, Potty
|
|
from django.http import HttpResponse
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from .forms import FeedingForm, PottyForm
|
|
|
|
|
|
def index(request):
|
|
context = {}
|
|
|
|
feedings = Feeding.objects.all().order_by('-timestamp')
|
|
|
|
context['feedings'] = feedings
|
|
|
|
# Grab all objects sorted by timestamp (most recent first)
|
|
potties = Potty.objects.all().order_by('-timestamp')
|
|
|
|
|
|
context['potties'] = potties
|
|
|
|
return render(request, 'main/index.html', context)
|
|
|
|
|
|
def add_feed(request):
|
|
context = {}
|
|
if request.method == 'POST':
|
|
form = FeedingForm(request.POST)
|
|
if form.is_valid():
|
|
feeding_type = form.cleaned_data['feeding_type']
|
|
amount = form.cleaned_data['amount']
|
|
duration = form.cleaned_data['duration']
|
|
# Validate based on feeding type
|
|
if feeding_type == 'bottle' and not amount:
|
|
form.add_error('amount', 'Amount is required for bottle feeding.')
|
|
elif feeding_type == 'breast' and not duration:
|
|
form.add_error('duration', 'Duration is required for breast feeding.')
|
|
else:
|
|
form.save()
|
|
feedings = Feeding.objects.all().order_by('-timestamp')
|
|
context['feedings'] = feedings
|
|
return render(request, 'main/partials/feed_section.html', context)
|
|
|
|
else:
|
|
form = FeedingForm()
|
|
|
|
return render(request, 'main/partials/add_feed.html', {'form': form})
|
|
|
|
|
|
@csrf_exempt
|
|
def delete_feed(request, feed_id):
|
|
context = {}
|
|
if request.method == "DELETE":
|
|
feed = get_object_or_404(Feeding, id=int(feed_id))
|
|
feed.delete()
|
|
feedings = Feeding.objects.all().order_by('-timestamp')
|
|
context['feedings'] = feedings
|
|
return render(request, 'main/partials/feed_section.html', context) # Return a success response
|
|
return HttpResponse(status=405) # Method not allowed
|
|
|
|
from django.shortcuts import get_object_or_404, render
|
|
|
|
@csrf_exempt
|
|
def edit_feed(request, feed_id):
|
|
feed = get_object_or_404(Feeding, id=feed_id)
|
|
return render(request, 'main/partials/edit_feed_row.html', {'feeding': feed})
|
|
|
|
|
|
@csrf_exempt
|
|
def update_feed(request, feed_id):
|
|
|
|
if request.method == "POST":
|
|
feed = get_object_or_404(Feeding, id=feed_id)
|
|
feed.timestamp = request.POST['timestamp']
|
|
if feed.feeding_type == 'bottle':
|
|
feed.amount = float(request.POST['amount_or_duration'])
|
|
else:
|
|
feed.duration = int(float(request.POST['amount_or_duration']))
|
|
feed.notes = request.POST['notes']
|
|
feed.save()
|
|
return render(request, 'main/partials/feed_row.html', {'feeding': feed})
|
|
|
|
def cancel_edit_feed(request, feed_id):
|
|
feed = get_object_or_404(Feeding, id=feed_id)
|
|
return render(request, 'main/partials/feed_row.html', {'feeding': feed})
|
|
|
|
def add_potty(request):
|
|
context = {}
|
|
if request.method == 'POST':
|
|
form = PottyForm(request.POST)
|
|
if form.is_valid():
|
|
form.save()
|
|
potties = Potty.objects.all().order_by('-timestamp')
|
|
context['potties'] = potties
|
|
return render(request, 'main/partials/potty_section.html', context)
|
|
else:
|
|
form = PottyForm()
|
|
|
|
return render(request, 'main/partials/add_potty.html', {'form': form})
|
|
|
|
@csrf_exempt
|
|
def delete_potty(request, potty_id):
|
|
context = {}
|
|
if request.method == 'DELETE':
|
|
potty = get_object_or_404(Potty, id=int(potty_id))
|
|
potty.delete()
|
|
potties = Potty.objects.all().order_by('-timestamp')
|
|
context['potties'] = potties
|
|
return render(request, 'main/partials/potty_section.html', context)
|
|
return HttpResponse(status=405)
|
|
|
|
@csrf_exempt
|
|
def edit_potty(request, potty_id):
|
|
context = {}
|
|
potty = get_object_or_404(Potty, id=potty_id)
|
|
context['potty'] = potty
|
|
form = PottyForm(potty)
|
|
context['form'] = form
|
|
return render(request, 'main/partials/edit_potty_row.html', context)
|
|
|
|
|
|
@csrf_exempt
|
|
def update_potty(request, potty_id):
|
|
context = {}
|
|
if request.method == 'POST':
|
|
potty = get_object_or_404(Potty, id=potty_id)
|
|
potty.timestamp = request.POST['timestamp']
|
|
potty.consistency = request.POST['consistency']
|
|
potty.notes = request.POST['notes']
|
|
potty.save()
|
|
return render(request, 'main/partials/potty_row.html', {'potty': potty})
|
|
|
|
|
|
def cancel_edit_potty(request, potty_id):
|
|
potty = get_object_or_404(Potty, id=potty_id)
|
|
return render(request, 'main/partials/potty_row.html', {'potty': potty})
|
|
|
|
import csv
|
|
from django.http import HttpResponse
|
|
|
|
def export_feedings_csv(request):
|
|
# Create the HTTP response with CSV headers
|
|
response = HttpResponse(content_type='text/csv')
|
|
response['Content-Disposition'] = 'attachment; filename="feedings.csv"'
|
|
|
|
# Create a CSV writer
|
|
writer = csv.writer(response)
|
|
# Write the header row
|
|
writer.writerow(['Timestamp', 'Feeding Type', 'Amount', 'Duration', 'Notes'])
|
|
|
|
# Fetch the data from the database
|
|
feedings = Feeding.objects.all().order_by('-timestamp')
|
|
for feeding in feedings:
|
|
writer.writerow([
|
|
feeding.timestamp,
|
|
feeding.get_feeding_type_display(), # Use readable choices if applicable
|
|
feeding.amount if feeding.amount else "",
|
|
feeding.duration if feeding.duration else "",
|
|
feeding.notes,
|
|
])
|
|
|
|
return response
|
|
|
|
def export_potties_csv(request):
|
|
# Create the HTTP response with CSV headers
|
|
response = HttpResponse(content_type='text/csv')
|
|
response['Content-Disposition'] = 'attachment; filename="potties.csv"'
|
|
|
|
# Create a CSV writer
|
|
writer = csv.writer(response)
|
|
# Write the header row
|
|
writer.writerow(['Timestamp', 'Consistency', 'Notes'])
|
|
|
|
# Fetch the potty data from the database
|
|
potties = Potty.objects.all().order_by('-timestamp')
|
|
for potty in potties:
|
|
writer.writerow([
|
|
potty.timestamp,
|
|
potty.get_consistency_display() if potty.consistency else "",
|
|
potty.notes,
|
|
])
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|