Skip to content

Commit

Permalink
update:tugas9
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsupilamieue committed Nov 15, 2023
1 parent ee5edb4 commit e408738
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 3 deletions.
Empty file added authentication/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions authentication/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions authentication/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthenticationConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'authentication'
Empty file.
3 changes: 3 additions & 0 deletions authentication/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions authentication/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
9 changes: 9 additions & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from authentication.views import *

app_name = 'authentication'

urlpatterns = [
path('login/', login, name='login'),
path('logout/', logout, name='logout'),
]
50 changes: 50 additions & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from django.shortcuts import render
from django.contrib.auth import authenticate, login as auth_login, logout as auth_logout
from django.contrib.auth import logout as auth_logout
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def login(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)
# Status login sukses.
return JsonResponse({
"username": user.username,
"status": True,
"message": "Login sukses!"
# Tambahkan data lainnya jika ingin mengirim data ke Flutter.
}, status=200)
else:
return JsonResponse({
"status": False,
"message": "Login gagal, akun dinonaktifkan."
}, status=401)

else:
return JsonResponse({
"status": False,
"message": "Login gagal, periksa kembali email atau kata sandi."
}, status=401)

@csrf_exempt
def logout(request):
username = request.user.username

try:
auth_logout(request)
return JsonResponse({
"username": username,
"status": True,
"message": "Logout berhasil!"
}, status=200)
except:
return JsonResponse({
"status": False,
"message": "Logout gagal."
}, status=401)

3 changes: 2 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.urls import path
from main.views import show_main, create_item, show_xml, show_json, show_json_by_id, show_xml_by_id, show_html, show_html_by_id, register, login_user, logout_user, add_amount, remove_item,delete_amount, create_ajax, get_item_json, delete_item_ajax
from main.views import *

app_name = 'main'

Expand All @@ -21,4 +21,5 @@
path('xml/<int:id>/', show_xml_by_id, name='show_xml_by_id'),
path('json/<int:id>/', show_json_by_id, name='show_json_by_id'),
path('html/<int:id>/', show_html_by_id, name='show_html_by_id'),
path('create-flutter/', create_item_flutter, name='create_item_flutter'),
]
24 changes: 22 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import json
from django.shortcuts import render
from django.http import HttpResponseNotFound, HttpResponseRedirect
from django.http import HttpResponseNotFound, HttpResponseRedirect, JsonResponse
from main.forms import ItemForm
from django.urls import reverse
from main.models import Item
Expand Down Expand Up @@ -143,4 +144,23 @@ def show_json_by_id(request, id):

def show_html_by_id(request, id):
data = Item.objects.filter(pk=id)
return render(request, 'items.html', {'items': data})
return render(request, 'items.html', {'items': data})

@csrf_exempt
def create_item_flutter(request):
if request.method == 'POST':

data = json.loads(request.body)

new_product = Item.objects.create(
user = request.user,
name = data["name"],
price = int(data["price"]),
description = data["description"]
)

new_product.save()

return JsonResponse({"status": "success"}, status=200)
else:
return JsonResponse({"status": "error"}, status=401)
10 changes: 10 additions & 0 deletions shopeeng/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
'main',
'tailwind',
'theme',
'authentication',
'corsheaders',
]

MIDDLEWARE = [
Expand All @@ -60,10 +62,18 @@
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
]

ROOT_URLCONF = 'shopeeng.urls'

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOW_CREDENTIALS = True
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SAMESITE = 'None'
SESSION_COOKIE_SAMESITE = 'None'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
Expand Down
1 change: 1 addition & 0 deletions shopeeng/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
path('auth/', include('authentication.urls')),
]

0 comments on commit e408738

Please sign in to comment.