Commit e4801b6b authored by Matthew Dizon's avatar Matthew Dizon

messages with CRUD functionality for customer

parent 645ee339
......@@ -28,8 +28,6 @@
<input type="datetime-local" class="form-control" id="date" name="date" required>
</div>
<input type="hidden" name="message" value="Food created successfully">
<a href="{% url 'add_food' %}"><button type="submit" class="mt-3 btn btn-primary" >Add Food</button></a>
</form>
......
......@@ -16,4 +16,7 @@
<a href="{% url 'update_customer' customer.pk %}"><button type="submit" class="btn btn-primary">Update Customer</button></a>
<a href="{% url 'delete_customer' customer.pk %}"><button class="btn btn-danger my-4">Delete Customer</button></a>
<a href="{% url 'view_customers' %}"><button class="btn btn-secondary">Back</button></a>
{% if message %}
<p class="mt-3 alert alert-success" style="width: 75%"> {{message}}</p>
{% endif %}
{% endblock %}
\ No newline at end of file
......@@ -9,6 +9,10 @@
</div>
</section>
{% if message %}
<p class="mt-3 alert alert-success"> {{message}}</p>
{% endif %}
<div class="col-12">
<table class="table table-striped">
<thead>
......
......@@ -151,6 +151,7 @@ def update_food(request, pk):
try:
Food.objects.filter(pk=pk).update(name=name, description=description, price=price, created_at=created_at)
message = "Food updated successfully"
return redirect(f'/view_food_details/{pk}?message={message}', pk=pk)
except Exception as e:
......@@ -195,25 +196,42 @@ def add_food(request):
### Views for Customer Model
def view_customers(request):
customers = Customer.objects.all()
context = {"customers":customers}
if request.GET.get('message') == None:
context = {"customers":customers}
else:
context = {"customers":customers, 'message':request.GET.get('message')}
return render(request, 'Kiosk/view_customers.html', context)
def view_customer_details(request, pk):
customer = get_object_or_404(Customer, pk=pk)
context = {"customer":customer}
if request.GET.get('message') == None:
context = {"customer":customer}
else:
context = {"customer":customer, 'message':request.GET.get('message')}
return render(request, 'Kiosk/view_customer_details.html', context)
def delete_customer(request, pk):
Customer.objects.filter(pk=pk).delete()
return redirect('view_customers')
message = "Customer deleted successfully"
return redirect(f'/customers?message={message}')
def update_customer(request, pk):
if(request.method=="POST"):
customer_name = request.POST.get('customer_name')
address = request.POST.get('address')
city = request.POST.get('city')
Customer.objects.filter(pk=pk).update(name=customer_name, address=address, city=city)
return redirect('view_customer_details', pk=pk)
try:
Customer.objects.filter(pk=pk).update(name=customer_name, address=address, city=city)
message = "Customer updated successfully"
return redirect(f'/view_customer_details/{pk}?message={message}', pk=pk)
except Exception as e:
message = f"Customer with name '{customer_name}' already exists"
customer = get_object_or_404(Customer, pk=pk)
context = {"customer": customer, "message":message}
return render(request, 'Kiosk/update_customer.html', context)
else:
customer = get_object_or_404(Customer, pk=pk)
context = {"customer": customer}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment