diff --git a/account/views.py b/account/views.py index 2e1a1ed7fcf15fbd97aac97cb0689471db799ce6..66223558ada06399b856c2da9b64f83dc085b44b 100644 --- a/account/views.py +++ b/account/views.py @@ -23,7 +23,7 @@ def show_main(request): f'select day, starthours, endhours from restaurant r, restaurant_operating_hours roh where name = rname and branch = rbranch and email = \'{email}\'' ) records_hours = cursor.fetchall() - + print(records) context = { 'email': records[0][0], 'password': records[0][1], @@ -170,8 +170,8 @@ def show_main(request): def logout_user(request): response = HttpResponseRedirect(reverse('account:show_main')) - response.delete_cookie('email') - response.delete_cookie('role') + for cookie in request.COOKIES: + response.delete_cookie(cookie) return response diff --git a/trigger_6/templates/daftar_promo_restoran.html b/trigger_6/templates/daftar_promo_restoran.html index d7d5635d01392e5c9030d0f152a11c316861818c..b44bb03c19743b02b0631ad3fe94daeb13b22dce 100644 --- a/trigger_6/templates/daftar_promo_restoran.html +++ b/trigger_6/templates/daftar_promo_restoran.html @@ -38,7 +38,7 @@ {% for record in records_promo_resto %} <tr class="bg-white border-b"> <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap"> - {{record.8}} + {{forloop.counter}} </th> <td class="py-4 px-6"> {{record.1}} diff --git a/trigger_6/templates/form_promo_restoran.html b/trigger_6/templates/form_promo_restoran.html index 9603f174874e52d538b84d2bb7c859b8d3e7c556..c3477b5197db9270aaa9e9dc3ec8c274000edc5b 100644 --- a/trigger_6/templates/form_promo_restoran.html +++ b/trigger_6/templates/form_promo_restoran.html @@ -1,17 +1,57 @@ {% extends 'base.html' %} {% block meta %} <title>Riwayat</title> {% endblock meta %} {% block content %} + <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script type="text/javascript"> - $(document).ready(function () { - $('#name').click(function () { - console.log('nadya') - $.get(`/trigger_6/ubah`, function (data) { - console.log(data) + function handleClick(value) { + let csrfcookie = function () { + let cookieValue = null, + name = "csrftoken"; + if (document.cookie && document.cookie !== "") { + let cookies = document.cookie.split(";"); + for (let i = 0; i < cookies.length; i++) { + let cookie = cookies[i].trim(); + if (cookie.substring(0, name.length + 1) == (name + "=")) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + }; + + $.ajax({ + url: `/trigger_6/ubah-form-input/${value}`, + type: 'POST', + beforeSend: function (request, settings) { + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { + request.setRequestHeader("X-CSRFToken", csrfcookie()); + } + }, + success: function (data) { + console.log(data); + $('#jenis').val(data.jenis_promo) + $('#discount').val(data.discount) + var label = '' + if (data.jenis_promo == 'Promo Hari Spesial'){ + label = 'Tanggal Spesial:' + input = data.special_date + } else { + label = 'Minimum Transaksi:' + input = data.min_transaction + } + $('#add').empty() + $('#add').append( + `<p id="add-label">${label}</p> + <input type="" name="add-input" id="add-input" class="form-control rounded-lg text-black" + required disabled>` + ) + $('#add-input').val(input) + }, }); - }) - }); + } </script> </head> @@ -21,6 +61,7 @@ <div class="font-extrabold text-center"> FORM PROMO RESTORAN </div> + <h1 class="text-red-500 font-bold my-2">{{message}}</h1> <div> <form action="" method="post"> {% csrf_token %} @@ -31,15 +72,15 @@ <select name="name" id="name" onchange="handleClick(this.value); this.selectedindex = -1" class="border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 mt-2"> - <option style="display: none" ></option> + <option style="display: none"></option> {% for promoname in record_promoname %} - <option value="{{ promoname.0 }}">{{ promoname.0 }}</option> + <option value="{{ promoname.1 }}">{{ promoname.0 }}</option> {% endfor %} </select> </div> <div class="flex flex-col text-sm font-medium justify-around"> <p>Jenis Promo:</p> - <input type="text" name="name" id="name" class="form-control rounded-lg text-black" + <input type="text" name="jenis" id="jenis" class="form-control rounded-lg text-black" required disabled> </div> <div class="flex flex-col text-sm font-medium mb-2 justify-around"> @@ -47,24 +88,16 @@ <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black" required disabled> </div> - <!-- <div class="flex flex-col text-sm font-medium mb-2 justify-around"> - <p>Minimum Transaksi:</p> - <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black" required disabled> - </div> --> - <div class="flex flex-col text-sm font-medium mb-2 justify-around"> - <p>Tanggal Spesial:</p> - <input type="date" name="date" id="date" class="form-control rounded-lg text-black" - required disabled> - </div> + <div class="flex flex-col text-sm font-medium mb-2 justify-around" id="add"></div> <div class="flex flex-col text-sm font-medium mb-2 justify-around"> <p>Tanggal Mulai:</p> <input type="date" name="startdate" id="startdate" - class="form-control rounded-lg text-black" required> + class="form-control rounded-lg text-black" required oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')"> </div> <div class="flex flex-col text-sm font-medium mb-2 justify-around"> <p>Tanggal Berakhir:</p> <input type="date" name="enddate" id="enddate" - class="form-control rounded-lg text-black" required> + class="form-control rounded-lg text-black" required oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')"> </div> <div class="flex justify-center"> <input type="submit" name="submit" value="SIMPAN" diff --git a/trigger_6/urls.py b/trigger_6/urls.py index fbdd65835893adf905d60cbd19b6b41d54bf7cd7..eb03c4d513790d1e240a38d88c302b71801beaef 100644 --- a/trigger_6/urls.py +++ b/trigger_6/urls.py @@ -20,5 +20,5 @@ urlpatterns = [ path('detail-promo-restoran/<rname>/<rbranch>/<id>', show_detail_promo_restoran, name='show_detail_promo_restoran'), path('delete-promo/<id>', delete_promo, name='delete_promo'), path('delete-promo-restoran/<rname>/<rbranch>/<id>', delete_promo_restoran, name='delete_promo_restoran'), - path('ubah-form-input/', ubah_form_input, name='ubah_form_input') + path('ubah-form-input/<id>', ubah_form_input, name='ubah_form_input') ] \ No newline at end of file diff --git a/trigger_6/views.py b/trigger_6/views.py index a69cd4b9ce1d1e91053828c54b779db8e981e573..77a49b3e9dc26452f3a40b6ace6d818d517a9594 100644 --- a/trigger_6/views.py +++ b/trigger_6/views.py @@ -3,6 +3,7 @@ from django.http import HttpResponseRedirect, JsonResponse from django.shortcuts import redirect, render from django.urls import reverse from utils.query import * +from django.views.decorators.csrf import csrf_exempt # Create your views here. @@ -239,43 +240,64 @@ def show_ubah_promo(request, jenis, id): def show_daftar_promo_restoran(request, rname, rbranch): - cursor.execute( - f'select * from promo p, restaurant_promo r where p.id = r.pid and r.rname = \'{rname}\' and r.rbranch = \'{rbranch}\'') + cursor.execute('set search_path to sirest') + cursor.execute(f'select * from promo p, restaurant_promo r where p.id = r.pid and r.rname = \'{rname}\' and r.rbranch = \'{rbranch}\'') records_promo_resto = cursor.fetchall() + print(len(records_promo_resto)) for i in range(len(records_promo_resto)): - records_promo_resto[i] += (i+1,) records_promo_resto_list = list(records_promo_resto[i]) - records_promo_resto_list[6] = records_promo_resto[i][6].date() - records_promo_resto_list[7] = records_promo_resto[i][7].date() - - records_promo_resto = tuple(records_promo_resto_list) + records_promo_resto_list[6] = records_promo_resto_list[6].date() + records_promo_resto_list[7] = records_promo_resto_list[7].date() + records_promo_resto[i] = records_promo_resto_list context = { - 'records_promo_resto': [records_promo_resto], - 'role': request.COOKIES.get('role'), - 'rname': rname, - 'rbranch': rbranch, - 'empty': len(records_promo_resto) + 'records_promo_resto':records_promo_resto, + 'role':request.COOKIES.get('role'), + 'rname':rname, + 'rbranch':rbranch, + 'empty':len(records_promo_resto) } - print([records_promo_resto]) + return render(request, 'daftar_promo_restoran.html', context) def show_form_promo_restoran(request): + cursor.execute('set search_path to sirest') + rname = request.COOKIES.get('rname') + rbranch = request.COOKIES.get('rbranch') + cursor.execute('select promoname, id from promo') + record_promoname = cursor.fetchall() + if request.method == 'POST': - print(request.POST) + try: + if len(request.POST) != 5: + raise Exception('Harap isi semua field yang ada.') + + pid = request.POST.get('name') + startdate = request.POST.get('startdate') + enddate = request.POST.get('enddate') + cursor.execute(f'insert into restaurant_promo values (\'{rname}\', \'{rbranch}\', \'{pid}\', \'{startdate}\', \'{enddate}\')') + connection.commit() + return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo_restoran', kwargs={'rname':rname, 'rbranch':rbranch})) - cursor.execute('select promoname from promo') - record_promoname = cursor.fetchall() - context = { - 'role': request.COOKIES.get('role'), - 'rname': request.COOKIES.get('rname'), - 'rbranch': request.COOKIES.get('rbranch'), - 'record_promoname': record_promoname, + except Exception as e: + connection.rollback() + context = { + 'message':e.args[0:40], + 'role':request.COOKIES.get('role'), + 'rname':rname, + 'rbranch':rbranch, + 'record_promoname':record_promoname, + } + return render(request, 'form_promo_restoran.html', context) + context = { + 'role':request.COOKIES.get('role'), + 'rname':rname, + 'rbranch':rbranch, + 'record_promoname':record_promoname, } - print(record_promoname) return render(request, 'form_promo_restoran.html', context) @@ -410,7 +432,31 @@ def delete_promo_restoran(request, rname, rbranch, id): connection.commit() return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo_restoran')) +def ubah_form_input(request, id): + cursor.execute(f'select discount from sirest.promo where id = \'{id}\'') + discount = cursor.fetchone() + + cursor.execute(f'select id from sirest.special_day_promo') + special_day_promo_id = cursor.fetchall() + + if (str(id),) in special_day_promo_id: + jenis_promo = 'Promo Hari Spesial' + cursor.execute(f'select date from sirest.special_day_promo where id = \'{id}\'') + special_date = cursor.fetchone() + context = { + 'discount':discount[0], + 'jenis_promo':jenis_promo, + 'special_date':special_date[0], + } + else: + jenis_promo = 'Promo Minimum Transaksi' + cursor.execute(f'select minimumtransactionnum from sirest.min_transaction_promo where id = \'{id}\'') + min_transaction = cursor.fetchone() + + context = { + 'discount':discount[0], + 'jenis_promo':jenis_promo, + 'min_transaction':min_transaction, + } -def ubah_form_input(request): - print('messi') - return JsonResponse({'foo': 'bar'}) + return JsonResponse(context) \ No newline at end of file