111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
from django.shortcuts import render
|
|
from django.http import JsonResponse
|
|
from django.apps import apps
|
|
from datetime import date
|
|
|
|
# View to render the table page
|
|
def excel_table_view(request):
|
|
# Fetch existing entries from both tables
|
|
entries_table1 = apps.get_model('sheets', 'ExcelEntry').objects.all()
|
|
entries_table2 = apps.get_model('sheets', 'SecondTableEntry').objects.all()
|
|
return render(request, 'excel_table.html', {
|
|
'entries_table1': entries_table1,
|
|
'entries_table2': entries_table2,
|
|
})
|
|
|
|
# Generic view to add a new row to any table
|
|
def add_entry(request, model_name):
|
|
if request.method == 'POST':
|
|
try:
|
|
# Dynamically get the model
|
|
model = apps.get_model('sheets', model_name)
|
|
|
|
# Get data from the request
|
|
name = request.POST.get('name', 'New Name')
|
|
age = int(request.POST.get('age', 0))
|
|
email = request.POST.get('email', 'example@email.com')
|
|
|
|
# Create a new entry
|
|
entry = model.objects.create(
|
|
name=name,
|
|
age=age,
|
|
email=email,
|
|
date_joined=date.today()
|
|
)
|
|
|
|
# Return the new entry as JSON response
|
|
return JsonResponse({
|
|
'id': entry.id,
|
|
'name': entry.name,
|
|
'age': entry.age,
|
|
'email': entry.email,
|
|
'date_joined': entry.date_joined.strftime('%Y-%m-%d'),
|
|
})
|
|
|
|
except Exception as e:
|
|
return JsonResponse({'status': 'error', 'message': str(e)}, status=400)
|
|
|
|
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)
|
|
|
|
# Generic view to update an entry in any table
|
|
def update_entry(request, model_name):
|
|
if request.method == 'POST':
|
|
try:
|
|
# Dynamically get the model
|
|
model = apps.get_model('sheets', model_name)
|
|
|
|
# Get data from the request
|
|
entry_id = int(request.POST.get('id'))
|
|
name = request.POST.get('name')
|
|
age = int(request.POST.get('age'))
|
|
email = request.POST.get('email')
|
|
|
|
# Fetch the entry to be updated
|
|
entry = model.objects.get(id=entry_id)
|
|
|
|
# Update the entry with new data
|
|
entry.name = name
|
|
entry.age = age
|
|
entry.email = email
|
|
entry.save()
|
|
|
|
# Return the updated entry as a JSON response
|
|
return JsonResponse({
|
|
'status': 'success',
|
|
'id': entry.id,
|
|
'name': entry.name,
|
|
'age': entry.age,
|
|
'email': entry.email,
|
|
'date_joined': entry.date_joined.strftime('%Y-%m-%d'),
|
|
})
|
|
|
|
except model.DoesNotExist:
|
|
return JsonResponse({'status': 'error', 'message': 'Entry not found'}, status=404)
|
|
except Exception as e:
|
|
return JsonResponse({'status': 'error', 'message': str(e)}, status=400)
|
|
|
|
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400)
|
|
|
|
# Generic view to delete an entry from any table
|
|
def delete_entry(request, model_name):
|
|
if request.method == 'POST':
|
|
try:
|
|
# Dynamically get the model
|
|
model = apps.get_model('sheets', model_name)
|
|
|
|
# Get the entry ID from the request
|
|
entry_id = request.POST.get('id')
|
|
|
|
# Find the entry by its ID and delete it
|
|
entry = model.objects.get(id=entry_id)
|
|
entry.delete()
|
|
|
|
# Return success response
|
|
return JsonResponse({'status': 'success', 'message': 'Entry deleted'})
|
|
|
|
except model.DoesNotExist:
|
|
return JsonResponse({'status': 'error', 'message': 'Entry not found'}, status=404)
|
|
except Exception as e:
|
|
return JsonResponse({'status': 'error', 'message': str(e)}, status=400)
|
|
|
|
return JsonResponse({'status': 'error', 'message': 'Invalid request'}, status=400) |