Skip to content

Commit

Permalink
update:tugas9
Browse files Browse the repository at this point in the history
  • Loading branch information
Marsupilamieue committed Nov 16, 2023
1 parent 51eba40 commit e6b4dbf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
urlpatterns = [
path('login/', login, name='login'),
path('logout/', logout, name='logout'),
path('register/', register, name='register'),
]
22 changes: 22 additions & 0 deletions authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.contrib.auth import logout as auth_logout
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth.models import User

@csrf_exempt
def login(request):
Expand Down Expand Up @@ -31,6 +32,27 @@ def login(request):
"message": "Login gagal, periksa kembali email atau kata sandi."
}, status=401)

@csrf_exempt
def register(request):
username = request.POST.get('username')
password = request.POST.get('password')

if User.objects.filter(username=username).exists():
return JsonResponse({
"status": False,
"message": "Register gagal, username sudah digunakan."
}, status=400)

user = User.objects.create_user(username=username, password=password)
user.save()

return JsonResponse({
"username": user.username,
"status": True,
"message": "Register sukses!"
}, status=201)


@csrf_exempt
def logout(request):
username = request.user.username
Expand Down
7 changes: 4 additions & 3 deletions main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ def create_item_flutter(request):

data = json.loads(request.body)

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

new_product.save()
new_item.save()

return JsonResponse({"status": "success"}, status=200)
else:
Expand Down
13 changes: 13 additions & 0 deletions shopeeng/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@
'corsheaders.middleware.CorsMiddleware',
]

CORS_ALLOW_HEADERS = [
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'access-control-allow-origin',
]

ROOT_URLCONF = 'shopeeng.urls'

CORS_ALLOW_ALL_ORIGINS = True
Expand Down

0 comments on commit e6b4dbf

Please sign in to comment.