From 411cf7c8a0062a8d32228f35b937680df27defe0 Mon Sep 17 00:00:00 2001
From: fadhlanhasyim <fadhlanhasyim54@gmail.com>
Date: Tue, 13 Dec 2022 15:15:16 +0700
Subject: [PATCH 1/4] Feat: Add Promo Restaurant

---
 .../templates/daftar_promo_restoran.html      |  2 +-
 trigger_6/templates/form_promo_restoran.html  | 84 ++++++++++++++-----
 trigger_6/urls.py                             |  2 +-
 trigger_6/views.py                            | 84 +++++++++++++++----
 4 files changed, 131 insertions(+), 41 deletions(-)

diff --git a/trigger_6/templates/daftar_promo_restoran.html b/trigger_6/templates/daftar_promo_restoran.html
index d7d5635..b44bb03 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 9603f17..2ba395c 100644
--- a/trigger_6/templates/form_promo_restoran.html
+++ b/trigger_6/templates/form_promo_restoran.html
@@ -1,17 +1,66 @@
 {% 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)
+        // $(document).ready(function () {
+        //     $('#name').click(function () {
+        //         console.log('nadya')
+        //         $.get(`/trigger_6/ubah-form-input`, function (data) {
+        //             console.log(data.foo)
+        //         });
+        //     })
+        // });
+
+        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 +70,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 +81,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 +97,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 fbdd658..eb03c4d 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 baeae37..71d3833 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.
 def show_riwayat(request):
@@ -211,41 +212,63 @@ def show_ubah_promo(request, jenis, id):
     return render(request, 'form_ubah_promosi.html', context)
 
 def show_daftar_promo_restoran(request, rname, 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],
+        '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}))
+
+        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)
 
-    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'),
+        'rname':rname,
+        'rbranch':rbranch,
         'record_promoname':record_promoname,
-
     }
-    print(record_promoname)
     return render(request, 'form_promo_restoran.html', context)
 
 def show_form_ubah_promo_restoran(request, id):
@@ -368,6 +391,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):
-    print('messi')
-    return JsonResponse({'foo':'bar'})
\ No newline at end of file
+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,
+        }
+
+    return JsonResponse(context)
\ No newline at end of file
-- 
GitLab


From 1fa3b11e2f67a70a195d3f9c0a32065a9937dadf Mon Sep 17 00:00:00 2001
From: fadhlanhasyim <fadhlanhasyim54@gmail.com>
Date: Tue, 13 Dec 2022 15:36:31 +0700
Subject: [PATCH 2/4] Fix: Delete All Cookie(s) When Logout

---
 account/views.py                             | 6 +++---
 trigger_6/templates/form_promo_restoran.html | 9 ---------
 2 files changed, 3 insertions(+), 12 deletions(-)

diff --git a/account/views.py b/account/views.py
index 2e1a1ed..6622355 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/form_promo_restoran.html b/trigger_6/templates/form_promo_restoran.html
index 2ba395c..c3477b5 100644
--- a/trigger_6/templates/form_promo_restoran.html
+++ b/trigger_6/templates/form_promo_restoran.html
@@ -5,15 +5,6 @@
 <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-form-input`, function (data) {
-        //             console.log(data.foo)
-        //         });
-        //     })
-        // });
-
         function handleClick(value) {
             let csrfcookie = function () {
                 let cookieValue = null,
-- 
GitLab


From f4ac313f3359ec96cc2c2fda5e1d0134f3d3c391 Mon Sep 17 00:00:00 2001
From: fadhlanhasyim <fadhlanhasyim54@gmail.com>
Date: Tue, 13 Dec 2022 20:25:47 +0700
Subject: [PATCH 3/4] Finish Trigger 6

---
 account/views.py                              | 14 -------
 templates/navbar.html                         | 10 ++---
 trigger_6/templates/daftar_promo.html         |  2 +-
 .../templates/detail_promosi_restoran.html    |  2 +-
 .../templates/form_promo_hari_spesial.html    |  6 +--
 trigger_6/templates/form_promo_minimum.html   |  6 +--
 trigger_6/templates/form_promo_restoran.html  |  6 +--
 .../templates/form_ubah_promo_restoran.html   |  4 +-
 trigger_6/templates/form_ubah_promosi.html    | 10 ++---
 trigger_6/views.py                            | 37 ++++++++++++-------
 10 files changed, 46 insertions(+), 51 deletions(-)

diff --git a/account/views.py b/account/views.py
index 6622355..fcc1bd8 100644
--- a/account/views.py
+++ b/account/views.py
@@ -23,7 +23,6 @@ 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],
@@ -53,7 +52,6 @@ def show_main(request):
             response.set_cookie('rname', records[0][5])
             response.set_cookie('rbranch', records[0][6])
             response.set_cookie('adminid', records[0][20])
-            print('masuk resto')
             return response
 
         # Role Admin -> Dashboard Admin
@@ -90,7 +88,6 @@ def show_main(request):
                 elif (records_actor[i][0],) in list_restaurant:
                     records_actor[i] += ('Restoran', )
 
-            print(records_admin)
 
             context = {
                 'role': 'admin',
@@ -105,7 +102,6 @@ def show_main(request):
             response = render(request, 'dashboard_admin.html', context)
             response.set_cookie('role', 'admin')
             response.set_cookie('email', records_admin[0][0])
-            print('masuk admin')
             return response
 
         # Role Customer -> Dashboard Customer
@@ -132,7 +128,6 @@ def show_main(request):
             response.set_cookie('role', 'customer')
             response.set_cookie('email', records[0][0])
             response.set_cookie('adminid', records[0][13])
-            print('masuk pelanggan')
             return response
 
         # Role Courier -> Dashboard Courier
@@ -161,7 +156,6 @@ def show_main(request):
             response.set_cookie('role', 'courier')
             response.set_cookie('email', records[0][0])
             response.set_cookie('adminid', records[0][15])
-            print('masuk kurir')
             return response
 
     # Jika tidak ada cookie role, redirect ke halaman login
@@ -229,7 +223,6 @@ def login(request):
                 response = render(request, 'dashboard_admin.html', context)
                 response.set_cookie('role', 'admin')
                 response.set_cookie('email', records_admin[0][0])
-                print('masuk admin')
                 return response
 
             cursor.execute(
@@ -272,7 +265,6 @@ def login(request):
                 response.set_cookie('rname', records[0][5])
                 response.set_cookie('rbranch', records[0][6])
                 response.set_cookie('adminid', records[0][20])
-                print('masuk resto')
                 return response
 
             cursor.execute(
@@ -296,19 +288,16 @@ def login(request):
                     'adminid': records[0][15],
                     'role': 'courier'
                 }
-                # print(context)
                 response = render(request, 'dashboard_pengguna.html', context)
                 response.set_cookie('role', 'courier')
                 response.set_cookie('email', records[0][0])
                 response.set_cookie('adminid', records[0][15])
-                print('masuk kurir')
                 return response
 
             cursor.execute(
                 f'select * from user_acc u, customer c, transaction_actor ta where u.email = c.email and u.email = \'{email}\' and c.email = ta.email')
             records = cursor.fetchmany()
             if (len(records) == 1):
-                # print(records)
                 context = {
                     'email': records[0][0],
                     'password': records[0][1],
@@ -324,12 +313,10 @@ def login(request):
                     'adminid': records[0][13],
                     'role': 'customer'
                 }
-                # print(context)
                 response = render(request, 'dashboard_pengguna.html', context)
                 response.set_cookie('role', 'customer')
                 response.set_cookie('email', records[0][0])
                 response.set_cookie('adminid', records[0][13])
-                print('masuk pelanggan')
                 return response
 
             # response = HttpResponseRedirect(reverse('account:profile_pelanggan'))
@@ -753,7 +740,6 @@ def profile_restoran(request, email):
         'jadwal': records_hours,
         'promo': records_promo,
     }
-    print(record[0])
     return render(request, 'profile_restoran.html', context)
 
 
diff --git a/templates/navbar.html b/templates/navbar.html
index e688395..02b0740 100644
--- a/templates/navbar.html
+++ b/templates/navbar.html
@@ -13,10 +13,10 @@
         <a href="{% url 'account:show_main' %}">
             <div class="text-2xl font-bold text-[#fff] flex items-center cursor-pointer hover:text-cream-tua">
                 {% comment %}  if adminid is not none {% endcomment %}
-                {% if adminid is not none %}
-                    SIREST {{role}} {{rname}} {{rbranch}}
+                {% if adminid is not none or role == 'admin' %}
+                    SIREST {{role}}
                 {% else %}
-                    SIREST {{role}} {{rname}} {{rbranch}} <h3 class="text-xs"> Not Verified</h3>
+                    SIREST {{role}}  <h3 class="text-xs"> Not Verified</h3>
                 {% endif %}
             </div>
         </a>
@@ -26,7 +26,7 @@
     </div>
     <div id="list-navbar" class="text-base text-white hidden md:show md:flex md:flex-row md:items-center md:justify-end">
         <div class="md:mx-5 my-3 md:my-0">
-            <a href="" class="hover:text-[#DBC8AC] hover:text-lg duration-500">Dashboard</a>
+            <a href="{% url 'account:show_main' %}" class="hover:text-[#DBC8AC] hover:text-lg duration-500">Dashboard</a>
         </div>
 
         <!-- {admin start} -->
@@ -149,7 +149,7 @@
             <div x-show="open" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="transform opacity-0 scale-95" x-transition:enter-end="transform opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="transform opacity-100 scale-100" x-transition:leave-end="transform opacity-0 scale-95" class="absolute right-0 w-full mt-2 origin-top-right rounded-md shadow-lg md:w-48">
                 <div class="px-2 py-2 bg-white text-gray-900 rounded-md shadow ">
                     <a class="block px-4 py-2 mt-2 rounded-lg hover:bg-gray-200 focus:bg-gray-200 focus:outline-none focus:shadow-outline" href="{% url 'trigger_6:show_daftar_promo' %}">Daftar Promo Tersedia</a>
-                    <a class="block px-4 py-2 mt-2  rounded-lg hover:bg-gray-200 focus:bg-gray-200 focus:outline-none focus:shadow-outline" href="#">Daftar Promo Restoran</a>
+                    <a class="block px-4 py-2 mt-2  rounded-lg hover:bg-gray-200 focus:bg-gray-200 focus:outline-none focus:shadow-outline" href="{% url 'trigger_6:show_daftar_promo_restoran' rname rbranch %}">Daftar Promo Restoran</a>
                 </div>
             </div>
         </div> 
diff --git a/trigger_6/templates/daftar_promo.html b/trigger_6/templates/daftar_promo.html
index a13feb5..c60d025 100644
--- a/trigger_6/templates/daftar_promo.html
+++ b/trigger_6/templates/daftar_promo.html
@@ -1,5 +1,5 @@
 {% extends 'base.html' %} {% block meta %}
-<title>Riwayat</title>
+<title>Detail Promo</title>
 {% endblock meta %} {% block content %}
 <div class="m-20">
     {% if role == 'admin' %}
diff --git a/trigger_6/templates/detail_promosi_restoran.html b/trigger_6/templates/detail_promosi_restoran.html
index 47d91aa..23be314 100644
--- a/trigger_6/templates/detail_promosi_restoran.html
+++ b/trigger_6/templates/detail_promosi_restoran.html
@@ -1,5 +1,5 @@
 {% extends 'base.html' %} {% block meta %}
-<title>Detail Promo</title>
+<title>Detail Promo Restoran</title>
 {% endblock meta %} {% block content %}
 <div class="border-black m-20">
     <div class="flex justify-center items-center p-2 bg-red-300 rounded-t-lg mb-3">
diff --git a/trigger_6/templates/form_promo_hari_spesial.html b/trigger_6/templates/form_promo_hari_spesial.html
index b1e576c..a3ee8ee 100644
--- a/trigger_6/templates/form_promo_hari_spesial.html
+++ b/trigger_6/templates/form_promo_hari_spesial.html
@@ -15,15 +15,15 @@
                         <div class="flex flex-col justify-around gap-2 w-full">
                             <div class="flex flex-col text-sm font-medium justify-around">
                                 <p>Nama Promo:</p>
-                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black">
+                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Diskon:</p>
-                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black">
+                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Tanggal Berlangsung:</p>
-                                <input type="date" name="date" id="date" class="form-control rounded-lg text-black">
+                                <input type="date" name="date" id="date" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex justify-center">
                                 <input type="submit" name="submit" value="SIMPAN" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"/> 
diff --git a/trigger_6/templates/form_promo_minimum.html b/trigger_6/templates/form_promo_minimum.html
index a3f5985..4c90b16 100644
--- a/trigger_6/templates/form_promo_minimum.html
+++ b/trigger_6/templates/form_promo_minimum.html
@@ -15,15 +15,15 @@
                         <div class="flex flex-col justify-around gap-2 w-full">
                             <div class="flex flex-col text-sm font-medium justify-around">
                                 <p>Nama Promo:</p>
-                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black">
+                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Diskon:</p>
-                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black">
+                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Minimum Transaksi:</p>
-                                <input type="text" name="mintransaction" id="mintransaction" class="form-control rounded-lg text-black">
+                                <input type="text" name="mintransaction" id="mintransaction" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex justify-center">
                                 <input type="submit" name="submit" value="SIMPAN" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"/> 
diff --git a/trigger_6/templates/form_promo_restoran.html b/trigger_6/templates/form_promo_restoran.html
index c3477b5..5679218 100644
--- a/trigger_6/templates/form_promo_restoran.html
+++ b/trigger_6/templates/form_promo_restoran.html
@@ -69,12 +69,12 @@
                         <div class="flex flex-col justify-around gap-2 w-full">
                             <div class="flex flex-col text-sm font-medium justify-around">
                                 <label for="name">Nama Promo:</label>
-                                <select name="name" id="name"
+                                <select name="name" id="name" required oninput="setCustomValidity('')" oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')"
                                     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 required>
                                     {% for promoname in record_promoname %}
-                                    <option value="{{ promoname.1 }}">{{ promoname.0 }}</option>
+                                    <option value="{{ promoname.1 }}">{{ promoname.0 }}</option required>
                                     {% endfor %}
                                 </select>
                             </div>
diff --git a/trigger_6/templates/form_ubah_promo_restoran.html b/trigger_6/templates/form_ubah_promo_restoran.html
index e353fab..d3de107 100644
--- a/trigger_6/templates/form_ubah_promo_restoran.html
+++ b/trigger_6/templates/form_ubah_promo_restoran.html
@@ -27,11 +27,11 @@
                             </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">
+                                <input type="date" name="startdate" id="startdate" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </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">
+                                <input type="date" name="enddate" id="enddate" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             <div class="flex justify-center">
                                 <input type="submit" name="submit" value="SIMPAN" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"/> 
diff --git a/trigger_6/templates/form_ubah_promosi.html b/trigger_6/templates/form_ubah_promosi.html
index f21d9b9..0eb40d7 100644
--- a/trigger_6/templates/form_ubah_promosi.html
+++ b/trigger_6/templates/form_ubah_promosi.html
@@ -15,24 +15,24 @@
                         <div class="flex flex-col justify-around gap-2">
                             <div class="flex flex-col text-sm font-medium justify-around">
                                 <p>Jenis Promosi:</p>
-                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" disabled placeholder='{{jenis_promo}}'>
+                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" disabled placeholder='{{jenis_promo}}' required>
                             </div>
                             <div class="flex flex-col text-sm font-medium justify-around">
                                 <p>Nama Promosi:</p>
-                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" disabled placeholder='{{nama_promo}}''>
+                                <input type="text" name="name" id="name" class="form-control rounded-lg text-black" disabled placeholder='{{nama_promo}}' required>
                             </div>
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Diskon (%):</p>
-                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black">
+                                <input type="text" name="discount" id="discount" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             {% if jenis_promo == 'Promo Minimum Transaksi' %}
                             <div class="flex flex-col text-sm font-medium mb-2 justify-around">
                                 <p>Minimum Transaksi:</p>
-                                <input type="text" name="mintransaction" id="mintransaction" class="form-control rounded-lg text-black">
+                                <input type="text" name="mintransaction" id="mintransaction" class="form-control rounded-lg text-black" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')">
                             </div>
                             {% endif %}
                             <div class="flex justify-center">
-                                <input type="submit" name="submit" value="SIMPAN" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"/> 
+                                <input type="submit" name="submit" value="SIMPAN" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800" required oninvalid="this.setCustomValidity('Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu')" oninput="setCustomValidity('')"/> 
                             </div>
                         </div>
                     </div>
diff --git a/trigger_6/views.py b/trigger_6/views.py
index 77a49b3..b2745e2 100644
--- a/trigger_6/views.py
+++ b/trigger_6/views.py
@@ -43,6 +43,7 @@ def show_riwayat(request):
         'rname': request.COOKIES.get('rname'),
         'rbranch': request.COOKIES.get('rbranch'),
         'record': record,
+        'adminid':request.COOKIES.get('adminid')
     }
     print(record)
     return render(request, 'riwayat.html', context)
@@ -78,11 +79,9 @@ def show_detail_riwayat(request, email, datetime):
         'role': request.COOKIES.get('role'),
         'rname': request.COOKIES.get('rname'),
         'rbranch': request.COOKIES.get('rbranch'),
+        'adminid':request.COOKIES.get('adminid')
     }
-    print('masuk')
-    print(record_riwayat)
-    print(ordered_food)
-    print(transaction_status)
+
     return render(request, 'detail_riwayat.html', context)
 
 
@@ -98,11 +97,12 @@ def show_form_penilaian(request, email, datetime):
             context = {
                 'status': 'error',
                 'message': 'Data yang diisikan belum lengkap, silahkan lengkapi data terlebih dahulu',
-                'role': request.COOKIES.get('role')
+                'role': request.COOKIES.get('role'),
+                'adminid':request.COOKIES.get('adminid')
             }
             return render(request, 'form_penilaian.html', context)
 
-    return render(request, 'form_penilaian.html', {'role': request.COOKIES.get('role')})
+    return render(request, 'form_penilaian.html', {'role': request.COOKIES.get('role'), 'adminid':request.COOKIES.get('adminid')})
 
 
 def show_buat_promo(request):
@@ -171,7 +171,6 @@ def show_form_promo_hari_spesial(request):
 
 def show_daftar_promo(request):
     cursor.execute('select * from promo')
-    # TODO: GANTI FETCH ALL
     records_promo = cursor.fetchall()
     records_promo = sorted(records_promo, key=lambda x: x[1].lower())
     print(records_promo)
@@ -195,6 +194,7 @@ def show_daftar_promo(request):
         'records_promo': records_promo,
         'role': request.COOKIES.get('role'),
         'rname': request.COOKIES.get('rname'),
+        'adminid':request.COOKIES.get('adminid'),
         'rbranch': request.COOKIES.get('rbranch'),
     }
 
@@ -256,7 +256,8 @@ def show_daftar_promo_restoran(request, rname, rbranch):
         'role':request.COOKIES.get('role'),
         'rname':rname,
         'rbranch':rbranch,
-        'empty':len(records_promo_resto)
+        'empty':len(records_promo_resto),
+        'adminid':request.COOKIES.get('adminid')
     }
 
     return render(request, 'daftar_promo_restoran.html', context)
@@ -284,11 +285,12 @@ def show_form_promo_restoran(request):
         except Exception as e:
             connection.rollback()
             context = {
-                'message':e.args[0:40],
+                'message':e.args[0][0:40],
                 'role':request.COOKIES.get('role'),
                 'rname':rname,
                 'rbranch':rbranch,
                 'record_promoname':record_promoname,
+                'adminid':request.COOKIES.get('adminid')
             }
             return render(request, 'form_promo_restoran.html', context)
 
@@ -297,6 +299,7 @@ def show_form_promo_restoran(request):
         'rname':rname,
         'rbranch':rbranch,
         'record_promoname':record_promoname,
+        'adminid':request.COOKIES.get('adminid')
     }
     return render(request, 'form_promo_restoran.html', context)
 
@@ -330,6 +333,7 @@ def show_form_ubah_promo_restoran(request, id):
                 'rname': request.COOKIES.get('rname'),
                 'rbranch': request.COOKIES.get('rbranch'),
                 'discount': record_pname[0][1],
+                'adminid':request.COOKIES.get('adminid')
             }
             return render(request, 'form_ubah_promo_restoran.html', context)
         else:
@@ -349,7 +353,8 @@ def show_form_ubah_promo_restoran(request, id):
                     'role': request.COOKIES.get('role'),
                     'rname': request.COOKIES.get('rname'),
                     'rbranch': request.COOKIES.get('rbranch'),
-                    'discount': record_pname[0][1],
+                    'discount': record_pname[0][1],    
+                    'adminid':request.COOKIES.get('adminid'),
                 }
                 return render(request, 'form_ubah_promo_restoran.html', context)
 
@@ -360,6 +365,7 @@ def show_form_ubah_promo_restoran(request, id):
         'role': request.COOKIES.get('role'),
         'rname': request.COOKIES.get('rname'),
         'rbranch': request.COOKIES.get('rbranch'),
+        'adminid':request.COOKIES.get('adminid')
     }
 
     return render(request, 'form_ubah_promo_restoran.html', context)
@@ -385,6 +391,7 @@ def show_detail_promo(request, id):
         'records_promo': records_promo[0],
         'role': request.COOKIES.get('role'),
         'rname': request.COOKIES.get('rname'),
+        'adminid':request.COOKIES.get('adminid'),
         'rbranch': request.COOKIES.get('rbranch'),
     }
 
@@ -409,14 +416,16 @@ def show_detail_promo_restoran(request, rname, rbranch, id):
     record_promo = list(record_promo[0])
     record_promo[6] = record_promo[6].date()
     record_promo[7] = record_promo[7].date()
+
     context = {
         'record_promo': record_promo,
         'id': id,
-        'rname': rname,
-        'rbranch': rbranch,
+        'rname': request.COOKIES.get('rname'),
+        'rbranch': request.COOKIES.get('rbranch'),
+        'adminid':request.COOKIES.get('adminid'),
+        'role': request.COOKIES.get('role'),
     }
 
-    print(record_promo)
     return render(request, 'detail_promosi_restoran.html', context)
 
 
@@ -430,7 +439,7 @@ def delete_promo_restoran(request, rname, rbranch, id):
     cursor.execute(
         f'delete from restaurant_promo where pid = \'{id}\' and rname = \'{rname}\' and rbranch = \'{rbranch}\'')
     connection.commit()
-    return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo_restoran'))
+    return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo_restoran', kwargs={'rname':rname, 'rbranch':rbranch}))
 
 def ubah_form_input(request, id):
     cursor.execute(f'select discount from sirest.promo where id = \'{id}\'')
-- 
GitLab


From d5dd618150d69c9b261fb2f92cc32d306b82d988 Mon Sep 17 00:00:00 2001
From: fadhlanhasyim <fadhlanhasyim54@gmail.com>
Date: Tue, 13 Dec 2022 20:35:51 +0700
Subject: [PATCH 4/4] Fix: Login Required

---
 trigger_6/views.py | 135 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 134 insertions(+), 1 deletion(-)

diff --git a/trigger_6/views.py b/trigger_6/views.py
index b2745e2..70bc5a9 100644
--- a/trigger_6/views.py
+++ b/trigger_6/views.py
@@ -12,6 +12,13 @@ def show_riwayat(request):
     role = request.COOKIES.get('role')
     email = request.COOKIES.get('email')
 
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     if role == 'restaurant':
         rname = request.COOKIES.get('rname')
         rbranch = request.COOKIES.get('rbranch')
@@ -45,11 +52,18 @@ def show_riwayat(request):
         'record': record,
         'adminid':request.COOKIES.get('adminid')
     }
-    print(record)
+
     return render(request, 'riwayat.html', context)
 
 
 def show_detail_riwayat(request, email, datetime):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+    cursor.execute('set search_path to sirest')
     # query riwayat by id
     sql = f'''select u.fname, u.lname, t.street, t.district, t.city, t.province, r.rname, r.rbranch, t.datetime, t.rating, foo.fname, foo.lname, co.platenum, co.vehicletype, co.vehiclebrand, 
     r.street, r.district, r.city, r.province, t.totalfood, t.totaldiscount, t.deliveryfee, t.totalprice, pm.name, ps.name from transaction t, courier co, transaction_food tf,  
@@ -86,6 +100,15 @@ def show_detail_riwayat(request, email, datetime):
 
 
 def show_form_penilaian(request, email, datetime):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     if request.method == 'POST':
         rating = request.POST.get('nilai')
         if rating != '0':
@@ -106,10 +129,25 @@ def show_form_penilaian(request, email, datetime):
 
 
 def show_buat_promo(request):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
     return render(request, 'buat_promo.html', {'role': request.COOKIES.get('role')})
 
 
 def show_form_promo_minimum(request):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     if request.method == "POST":
         name = request.POST.get('name')
         discount = request.POST.get('discount')
@@ -143,6 +181,15 @@ def show_form_promo_minimum(request):
 
 
 def show_form_promo_hari_spesial(request):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     if request.method == "POST":
         name = request.POST.get('name')
         discount = request.POST.get('discount')
@@ -170,6 +217,15 @@ def show_form_promo_hari_spesial(request):
 
 
 def show_daftar_promo(request):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute('select * from promo')
     records_promo = cursor.fetchall()
     records_promo = sorted(records_promo, key=lambda x: x[1].lower())
@@ -202,6 +258,15 @@ def show_daftar_promo(request):
 
 
 def show_ubah_promo(request, jenis, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(f'select promoname from promo where id = \'{id}\'')
     record = cursor.fetchall()
     if request.method == 'POST':
@@ -240,6 +305,13 @@ def show_ubah_promo(request, jenis, id):
 
 
 def show_daftar_promo_restoran(request, rname, rbranch):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
     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()
@@ -264,6 +336,13 @@ def show_daftar_promo_restoran(request, rname, rbranch):
 
 
 def show_form_promo_restoran(request):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
     cursor.execute('set search_path to sirest')
     rname = request.COOKIES.get('rname')
     rbranch = request.COOKIES.get('rbranch')
@@ -305,6 +384,15 @@ def show_form_promo_restoran(request):
 
 
 def show_form_ubah_promo_restoran(request, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(
         f'select promoname, discount from promo where id = \'{id}\'')
     record_pname = cursor.fetchall()
@@ -372,6 +460,15 @@ def show_form_ubah_promo_restoran(request, id):
 
 
 def show_detail_promo(request, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(f'select * from promo where id = \'{id}\'')
     records_promo = cursor.fetchmany()
 
@@ -399,6 +496,15 @@ def show_detail_promo(request, id):
 
 
 def show_detail_promo_restoran(request, rname, rbranch, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(
         f'select * from promo p, restaurant_promo r where p.id = r.pid and r.pid = \'{id}\' and r.rname = \'{rname}\' and r.rbranch = \'{rbranch}\'')
     record_promo = cursor.fetchall()
@@ -430,18 +536,45 @@ def show_detail_promo_restoran(request, rname, rbranch, id):
 
 
 def delete_promo(request, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(f'delete from promo where id = \'{id}\'')
     connection.commit()
     return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo'))
 
 
 def delete_promo_restoran(request, rname, rbranch, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(
         f'delete from restaurant_promo where pid = \'{id}\' and rname = \'{rname}\' and rbranch = \'{rbranch}\'')
     connection.commit()
     return HttpResponseRedirect(reverse('trigger_6:show_daftar_promo_restoran', kwargs={'rname':rname, 'rbranch':rbranch}))
 
 def ubah_form_input(request, id):
+    role = request.COOKIES.get('role')
+
+    if role == None:
+        return redirect("/login")
+    if role != 'restaurant':
+        return redirect("/")
+
+    cursor.execute('set search_path to sirest')
+
     cursor.execute(f'select discount from sirest.promo where id = \'{id}\'')
     discount = cursor.fetchone()
     
-- 
GitLab