Commit 1538bc6a authored by Tarsvini's avatar Tarsvini

commit lab_9

parent dcb4c85a
Pipeline #14866822 failed with stages
in 4 minutes and 4 seconds
......@@ -9,3 +9,10 @@ def get_drones():
return drones
# lengkapi pemanggilan utk SOUNDCARD_API dan OPTICAL_API untuk mengerjakan CHALLENGE
def get_soundcards():
soundcards = requests.get(SOUNDCARD_API)
return soundcard
def get_opticals():
opticals = requests.get(OPTICAL_API)
return optical
\ No newline at end of file
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.contrib import messages
#catatan: tidak bisa menampilkan messages jika bukan menggunakan method 'render'
from .api_enterkomputer import get_drones
response = {}
# NOTE : untuk membantu dalam memahami tujuan dari suatu fungsi (def)
# Silahkan jelaskan menggunakan bahasa kalian masing-masing, di bagian atas
# sebelum fungsi tersebut.
# ======================================================================== #
# User Func
# Apa yang dilakukan fungsi INI? #silahkan ganti ini dengan penjelasan kalian
def index(request):
print ("#==> masuk index")
if 'user_login' in request.session:
return HttpResponseRedirect(reverse('lab-9:profile'))
else:
html = 'lab_9/session/login.html'
return render(request, html, response)
def set_data_for_session(res, request):
response['author'] = request.session['user_login']
response['access_token'] = request.session['access_token']
response['kode_identitas'] = request.session['kode_identitas']
response['role'] = request.session['role']
response['drones'] = get_drones().json()
# print ("#drones = ", get_drones().json(), " - response = ", response['drones'])
## handling agar tidak error saat pertama kali login (session kosong)
if 'drones' in request.session.keys():
response['fav_drones'] = request.session['drones']
# jika tidak ditambahkan else, cache akan tetap menyimpan data
# sebelumnya yang ada pada response, sehingga data tidak up-to-date
else:
response['fav_drones'] = []
def profile(request):
print ("#==> profile")
## sol : bagaimana cara mencegah error, jika url profile langsung diakses
if 'user_login' not in request.session.keys():
return HttpResponseRedirect(reverse('lab-9:index'))
## end of sol
set_data_for_session(response, request)
html = 'lab_9/session/profile.html'
return render(request, html, response)
# ======================================================================== #
### Drones
def add_session_drones(request, id):
ssn_key = request.session.keys()
if not 'drones' in ssn_key:
print ("# init drones ")
request.session['drones'] = [id]
else:
drones = request.session['drones']
print ("# existing drones => ", drones)
if id not in drones:
print ('# add new item, then save to session')
drones.append(id)
request.session['drones'] = drones
messages.success(request, "Berhasil tambah drone favorite")
return HttpResponseRedirect(reverse('lab-9:profile'))
def del_session_drones(request, id):
print ("# DEL drones")
drones = request.session['drones']
print ("before = ", drones)
drones.remove(id) #untuk remove id tertentu dari list
request.session['drones'] = drones
print ("after = ", drones)
messages.error(request, "Berhasil hapus dari favorite")
return HttpResponseRedirect(reverse('lab-9:profile'))
def clear_session_drones(request):
print ("# CLEAR session drones")
print ("before 1 = ", request.session['drones'])
del request.session['drones']
messages.error(request, "Berhasil reset favorite drones")
return HttpResponseRedirect(reverse('lab-9:profile'))
# ======================================================================== #
# COOKIES
# Apa yang dilakukan fungsi INI? #silahkan ganti ini dengan penjelasan kalian
def cookie_login(request):
print ("#==> masuk login")
if is_login(request):
return HttpResponseRedirect(reverse('lab-9:cookie_profile'))
else:
html = 'lab_9/cookie/login.html'
return render(request, html, response)
def cookie_auth_login(request):
print ("# Auth login")
if request.method == "POST":
user_login = request.POST['username']
user_password = request.POST['password']
if my_cookie_auth(user_login, user_password):
print ("#SET cookies")
res = HttpResponseRedirect(reverse('lab-9:cookie_login'))
res.set_cookie('user_login', user_login)
res.set_cookie('user_password', user_password)
return res
else:
msg = "Username atau Password Salah"
messages.error(request, msg)
return HttpResponseRedirect(reverse('lab-9:cookie_login'))
else:
return HttpResponseRedirect(reverse('lab-9:cookie_login'))
def cookie_profile(request):
print ("# cookie profile ")
# method ini untuk mencegah error ketika akses URL secara langsung
if not is_login(request):
print ("belum login")
return HttpResponseRedirect(reverse('lab-9:cookie_login'))
else:
# print ("cookies => ", request.COOKIES)
in_uname = request.COOKIES['user_login']
in_pwd= request.COOKIES['user_password']
# jika cookie diset secara manual (usaha hacking), distop dengan cara berikut
# agar bisa masuk kembali, maka hapus secara manual cookies yang sudah diset
if my_cookie_auth(in_uname, in_pwd):
html = "lab_9/cookie/profile.html"
res = render(request, html, response)
return res
else:
print ("#login dulu")
msg = "Kamu tidak punya akses :P "
messages.error(request, msg)
html = "lab_9/cookie/login.html"
return render(request, html, response)
def cookie_clear(request):
res = HttpResponseRedirect('/lab-9/cookie/login')
res.delete_cookie('lang')
res.delete_cookie('user_login')
msg = "Anda berhasil logout. Cookies direset"
messages.info(request, msg)
return res
# Apa yang dilakukan fungsi ini?
def my_cookie_auth(in_uname, in_pwd):
my_uname = "utest" #SILAHKAN ganti dengan USERNAME yang kalian inginkan
my_pwd = "ptest" #SILAHKAN ganti dengan PASSWORD yang kalian inginkan
return in_uname == my_uname and in_pwd == my_pwd
#Apa yang dilakukan fungsi ini?
def is_login(request):
return 'user_login' in request.COOKIES and 'user_password' in request.COOKIES
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="LAB 9">
<meta name="author" content="{{author}}">
<!-- bootstrap csss -->
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<style>
.rata-tengah {
text-align: center;
margin : 20px;
}
.judul {
text-transform:uppercase;
margin-bottom: 50px;
margin-top: 50px;
}
</style>
<title>
{% block title %} Lab 9 By {{author}} {% endblock %}
</title>
</head>
<body>
<header>
<h1 style="text-align:center">
<small><em> Hello! </em></small>
</h1>
<!-- Your Header Here -->
</header>
<content>
<div class="container">
{% for message in messages %}
<div class="alert {{ message.tags }} alert-dismissible" role="alert" id="django-messages">
<button type="button" class="close" data-dismiss="alert" aria-label="Close" style="margin-right: 15px;">
<span aria-hidden="true">&times;</span>
</button>
{{ message }}
</div>
{% endfor %}
{% block content %}
<!-- Your Content Here -->
{% endblock %}
</div>
</content>
<footer>
<hr>
{% block footer %}
<h1 style="text-align:center">
<small><em> Made by Tarsvini Ravinther </em></small>
</h1>
<!-- Your Footer Here -->
{% endblock %}
</footer>
<!-- Jquery n Bootstrap Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="application/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
from django.conf.urls import urls
from .views import index,profile,
\add_session_drones,del_ssession_drones,clear_session_drones,
\cookie_login,cookie_profile,cookie_clear
from django.conf.urls import url
from .views import index, profile, \
add_session_drones, del_session_drones, clear_session_drones, \
cookie_login, cookie_auth_login, cookie_profile, cookie_clear
#sol to challenge
from .views import add_session_item, del_session_item, clear_session_item
# /sol
......
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