diff --git a/delivery_fee/models.py b/delivery_fee/models.py index 4f5a576ba60be7f2913c00d448c9a91224611af3..bba36bdf6fc13e5881ede9eee356e6168aa822a6 100644 --- a/delivery_fee/models.py +++ b/delivery_fee/models.py @@ -1,9 +1,3 @@ from django.db import models import datetime -from django.contrib.auth.models import User - -# class Task(models.Model): -# user = models.ForeignKey(User,on_delete=models.CASCADE,) -# province = models.CharField(max_length=255) -# motorate = models.CharField(max_length=255) -# carrate = models.CharField(max_length=255) \ No newline at end of file +from django.contrib.auth.models import User \ No newline at end of file diff --git a/delivery_fee/templates/C_delivery_fee.html b/delivery_fee/templates/C_delivery_fee.html index 25304e8d3db0f3ed4649dce473d80e1f743decc9..f88769fc2ee75136720a76e0d0b1cc9272aeff83 100644 --- a/delivery_fee/templates/C_delivery_fee.html +++ b/delivery_fee/templates/C_delivery_fee.html @@ -7,13 +7,13 @@ <body> <h1> <h5>Create Delivery Fee per KM</h5> - <form action="/action_page.php"> + <form> <label for="province">Province:</label> - <input type="text" id="province" name="province"><br><br> + <input type="text" name="province"><br><br> <label for="motorate">Motorcycle Delivery Rate:</label> - <input type="text" id="motorate" name="motorate"><br><br> + <input type="text" name="motorate"><br><br> <label for="carrate">Car Delivery Rate:</label> - <input type="text" id="carrate" name="carrate"><br><br> + <input type="text" name="carrate"><br><br> <button class="btn btn-success my-3" type="submitbutton" id="add_fee">Save</button> </form> </h1> diff --git a/delivery_fee/templates/R_delivery_fee.html b/delivery_fee/templates/R_delivery_fee.html index 6c08745cd6f06c93fc8ee6bc43b24d6154321242..45aa2e26ef28e457706c622023f78d2021f34e06 100644 --- a/delivery_fee/templates/R_delivery_fee.html +++ b/delivery_fee/templates/R_delivery_fee.html @@ -20,11 +20,11 @@ <th>Actions</th> </tr> <tr> - {% for item in list_item %} - <td>{{item.pk}}</td> + {% for item in delivery_fee %} + <td>{{item.index}}</td> <td>{{item.province}}</td> - <td>{{item.motorate}}</td> - <td>{{item.carrate}}</td> + <td>{{item.motorfee}}</td> + <td>{{item.carfee}}</td> {%endfor%} <td> <button type="submit" id="editbutton"><a href="/delivery_fee/change_fee">Edit</a></button><br> diff --git a/delivery_fee/templates/U_delivery_fee.html b/delivery_fee/templates/U_delivery_fee.html index 53b3a73e9668f282fb2dc34694c138d74b9c1ab4..b40c34d9d0572734313a03b18dfb8a7e71466a6f 100644 --- a/delivery_fee/templates/U_delivery_fee.html +++ b/delivery_fee/templates/U_delivery_fee.html @@ -12,7 +12,7 @@ <input type="text" id="motorate" name="motorate"><br><br> <label for="carrate">Car Delivery Rate:</label> <input type="text" id="carrate" name="carrate"><br><br> - <button class="btn btn-success my-3" type="submitbutton" id="change_fee">Save</button> + <button class="btn btn-success my-3" type="submit" id="change_fee">Save</button> </form> </h1> </body> diff --git a/delivery_fee/views.py b/delivery_fee/views.py index 3a3e63de669ebcdef420e93ffbd8339c62ba361f..a13f909ddb369b98b0f6cc04869f834b1dca5852 100644 --- a/delivery_fee/views.py +++ b/delivery_fee/views.py @@ -1,3 +1,5 @@ +import random +import string from django.shortcuts import render, redirect from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.urls import reverse @@ -7,72 +9,146 @@ from django.contrib.auth import authenticate, login, logout from django.contrib.auth.decorators import login_required from django.core import serializers import datetime +from django.db import connection # from delivery_fee.models import Task def show_fee(request): + errors = [] cursor = connection.cursor() - cursor.execute("SET search_path to SIREST;") + cursor.execute("SET search_path to PUBLIC") + email = request.session.get('user_email') + cursor.execute("SET search_path to SIREST") SQL = f""" - SELECT * FROM DELIVERY_FEE_PER_KM + SELECT province, motorfee, carfee + FROM DELIVERY_FEE_PER_KM """ cursor.execute(SQL) - alldeliveryfee = cursor.fetchall() + delivery_fee_per_km_tuple = cursor.fetchall() - context = {'deliveryfee': alldeliveryfee} - return render(request, 'R_delivery_fee.html', context) + index = 0 + delivery_fee_per_km_tuple_edit = [] + for stuff in delivery_fee_per_km_tuple: + index += 1 + delivery_fee_per_km_tuple_edit.append((index, stuff[0], stuff[1], stuff[2])) + + context = { + 'errors': errors, + 'delivery_fee': delivery_fee_per_km_tuple, + } + + return render(request, "R_delivery_fee.html", context) def add_fee(request): - cursor = connection.cursor() - cursor.execute("SET search_path to SIREST;") + if not request.session.get("isLoggedIn"): + return redirect('sirest:logout') + if not request.session.get("role") == 'admin': + return redirect('sirest:logout') - if request.method == 'POST': - id = request.POST.get('id') + def varcharRandomizer(): + characters = string.ascii_letters + string.digits + varchar = ''.join(random.choice(characters) for i in range(random.randint(6,9))) + return varchar + errors = [] + cursor = connection.cursor() + cursor.execute("SET search_path to SIREST") + + if request.method == "POST": province = request.POST.get('province') - motorfee = request.POST.get('motorfee') - carfee = request.POST.get('carfee') - - if not id or not province or not motorfee or not carfee: - return () - else: - newdeliveryfeeperkm = (id, province, motorfee, carfee) + motorcycle_delivery_rate = request.POST.get('motorate') + car_delivery_rate = request.POST.get('carrate') SQL = f""" - INSERT INTO DELIVERY_FEE_PER_KM VALUES {newdeliveryfeeperkm} + SELECT id + FROM DELIVERY_FEE_PER_KM """ + cursor.execute(SQL) - return redirect('delivery_fee:show_fee') + id_tuple = [i[0].strip() for i in cursor.fetchall()] + + id = varcharRandomizer() -def change_fee(request): -# if request.method == "PUT": -# task = Task.objects.get(user=request.user, id=id) -# task.save() -# return JsonResponse( -# { -# "pk": task.id, -# "fields": { -# "province": task.province, -# "motorate": task.motorate, -# "carrate": task.carrate, -# }, -# }, -# status=200, -# ) - return render(request, "U_delivery_fee.html") - -def delete_fee(request, id): + while id in id_tuple: + id = varcharRandomizer() + + if province and motorcycle_delivery_rate and car_delivery_rate: + SQL = f""" + INSERT INTO DELIVERY_FEE_PER_KM + VALUES + ('{id}', '{province}', '{motorcycle_delivery_rate}', '{car_delivery_rate}') + """ + cursor.execute(SQL) + return redirect('delivery_fee:show_fee') + + else: + errors.append("Please fill out all fields.") + + return render(request, "C_delivery_fee.html", {'errors': errors}) + +def change_fee(request, province, motorfee, carfee): + if not request.session.get("isLoggedIn"): + return redirect('sirest:logout') + if not request.session.get("role") == 'admin': + return redirect('sirest:logout') + + errors = [] cursor = connection.cursor() - cursor.execute("SET search_path to SIREST;") + cursor.execute("SET search_path to PUBLIC") + email = request.session.get('user_email') + cursor.execute("SET search_path to SIREST") - SQL = f""" - SELECT EXISTS (SELECT * FROM DELIVERY_FEE_PER_KM WHERE id = '{id}') - """ - cursor.execute(SQL) + if request.method == "POST": + new_motorfee = request.POST.get('motorate') + new_carfee = request.POST.get('carrate') - isitthere = cursor.fetchone()[0] - if isitthere: - SQL = f""" - DELETE FROM DELIVERY_FEE_PER_KM WHERE id = '{id}' + if new_motorfee and new_carfee: + SQL = f""" + SELECT * + FROM DELIVERY_FEE_PER_KM + WHERE province = '{province}' AND motorfee = '{motorfee}' AND carfee = '{carfee}' + """ + cursor.execute(SQL) + + id = cursor.fetchone()[0] + print(id) + + try: + SQL = f""" + UPDATE DELIVERY_FEE_PER_KM + SET motorfee = '{new_motorfee}', carfee = '{new_carfee}' + WHERE id = '{id}' AND province = '{province}' AND motorfee = '{motorfee}' AND carfee = '{carfee}' + """ + cursor.execute(SQL) + + return redirect('delivery_fee:show_fee') + + except: + errors.append("Delivery fee to-be-edited does not exist.") + + else: + errors.append("Fill all the fields.") + + context = { + 'errors': errors, + 'province': province, + 'motorfee': motorfee, + 'carfee': carfee, + } + + return render(request, "U_delivery_fee.html", context) + +def delete_fee(request, province, motorfee, carfee): + if not request.session.get("isLoggedIn"): + return redirect('sirest:logout') + if not request.session.get("role") == 'admin': + return redirect('sirest:logout') + cursor = connection.cursor() + + cursor.execute("SET search_path to SIREST") + + SQL = f""" + DELETE FROM DELIVERY_FEE_PER_KM + WHERE province = '{province}' AND motorfee = '{motorfee}' AND carfee = '{carfee}' """ - cursor.execute(SQL) + cursor.execute(SQL) return redirect('delivery_fee:show_fee') diff --git a/food/views.py b/food/views.py index 7b3ed5274cdfa24350174342ab1f9516e1b96f15..d666af614177116ca2261dda248a3d1d210a2270 100644 --- a/food/views.py +++ b/food/views.py @@ -1,3 +1,6 @@ +import random +import string +from django.db import connection from django.shortcuts import render, redirect from django.http import HttpResponse, HttpResponseRedirect, JsonResponse from django.urls import reverse @@ -9,35 +12,80 @@ from django.core import serializers import datetime def show_resto_list(request): + SQL = f""" + SELECT rname,rbranch,rating + FROM RESTAURANT + """ return render(request, "R_resto_list.html") def show_resto_detail(request): + SQL = f""" + SELECT * + FROM RESTAURANT + """ return render(request, "R_resto_detail.html") def show_food(request): + SQL = f""" + SELECT * + FROM FOOD + """ return render(request, "R_food_data.html") def add_food(request): - # if request.method == "POST": - # fname = request.POST.get("fname") - # description = request.POST.get("desc") - # stock = request.POST.get("stock") - # price = request.POST.get("price") - # fatcat = request.POST.get("fatcat") - # yngvi = request.POST.get("yngvi") - # Task.objects.create( - # user=request.user, - # fname = fname, - # description = description, - # stock = stock, - # price = price, - # fatcat = fatcat, - # yngvi=yngvi, - # ) - # return HttpResponseRedirect(reverse("delivery_fee:show_food")) - return render(request, "C_food_data.html") + if not request.session.get("isLoggedIn"): + return redirect('sirest:logout') + if not request.session.get("role") == 'admin': + return redirect('sirest:logout') + + def varcharRandomizer(): + characters = string.ascii_letters + string.digits + varchar = ''.join(random.choice(characters) for i in range(random.randint(6,9))) + return varchar + errors = [] + cursor = connection.cursor() + cursor.execute("SET search_path to SIREST") + + if request.method == "POST": + fname = request.POST.get('fname') + desc = request.POST.get('desc') + stock = request.POST.get('stock') + price = request.POST.get('price') + fatcat = request.POST.get('fatcat') + yngvi = request.POST.get('yngvi') + + SQL = f""" + SELECT foodname + FROM DELIVERY_FEE_PER_KM + """ + + cursor.execute(SQL) + id_tuple = [i[0].strip() for i in cursor.fetchall()] + + id = varcharRandomizer() + + while id in id_tuple: + id = varcharRandomizer() + + if fname and desc and stock and price and fatcat and yngvi: + SQL = f""" + INSERT food + VALUES + ('{id}', '{fname}', '{desc}', '{stock}','{price}','{fatcat}','{yngvi}') + """ + cursor.execute(SQL) + return redirect('food:show_food') + + else: + errors.append("Please fill out all fields.") + + return render(request,"C_food_data.html", {'errors': errors}) def change_food(request): + if not request.session.get("isLoggedIn"): + return redirect('sirest:logout') + if not request.session.get("role") == 'admin': + return redirect('sirest:logout') # if request.method == "PUT": # task = Task.objects.get(user=request.user, id=id) # task.save() diff --git a/project_django/settings.py b/project_django/settings.py index 8f3b09d60d073a6aef25e89c82bbe960edac98aa..054c4059619532aa6cc646d32b03a57f4faf2973 100644 --- a/project_django/settings.py +++ b/project_django/settings.py @@ -100,9 +100,9 @@ DATABASES = { DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', - 'NAME': 'nafis.azizi', - 'USER': 'nafisaziziriza', - 'PASSWORD': '', + 'NAME': 'postgres', + 'USER': 'postgres', + 'PASSWORD': 'choki001', 'HOST': 'localhost', 'PORT': '5432', } diff --git a/templates/admin_base.html b/templates/admin_base.html index c771ef9247628beea6059ae463cf72d788f0bcb7..57547294b4e2138a26826817c705c0a09c3d7393 100644 --- a/templates/admin_base.html +++ b/templates/admin_base.html @@ -33,7 +33,7 @@ <li><a class="dropdown-item" href="#">Create Food Category</a></li> <li><a class="dropdown-item" href="food_ingredient/templates/r_food_ingredient.html/">Food Ingredient List</a></li> <li><a class="dropdown-item" href="food_ingredient/templates/c_food_ingredient.html/">Create Food Ingredient</a></li> - <li><a class="dropdown-item" href="#">Restaurant and Food List</a></li> + <li><a class="dropdown-item" href="{% url 'food:show_resto_list' %}">Restaurant and Food List</a></li> </ul> </li> <li class="nav-item dropdown">