Skip to content

Commit

Permalink
Merge pull request #2 from fathonidf/development
Browse files Browse the repository at this point in the history
tutorial 3 selesai
  • Loading branch information
fathonidf authored Sep 20, 2023
2 parents 96a9482 + bc1dc83 commit f778d3c
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 5 deletions.
Binary file modified db.sqlite3
Binary file not shown.
22 changes: 22 additions & 0 deletions main/migrations/0002_product_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 4.2.4 on 2023-09-20 07:13

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('main', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='product',
name='user',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
preserve_default=False,
),
]
2 changes: 2 additions & 0 deletions main/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django.db import models
from django.contrib.auth.models import User

class Product(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255)
date_added = models.DateField(auto_now_add=True)
price = models.IntegerField()
Expand Down
45 changes: 45 additions & 0 deletions main/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{% extends 'base.html' %}

{% block meta %}
<title>Login</title>
{% endblock meta %}

{% block content %}

<div class = "login">

<h1>Login</h1>

<form method="POST" action="">
{% csrf_token %}
<table>
<tr>
<td>Username: </td>
<td><input type="text" name="username" placeholder="Username" class="form-control"></td>
</tr>

<tr>
<td>Password: </td>
<td><input type="password" name="password" placeholder="Password" class="form-control"></td>
</tr>

<tr>
<td></td>
<td><input class="btn login_btn" type="submit" value="Login"></td>
</tr>
</table>
</form>

{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

Don't have an account yet? <a href="{% url 'main:register' %}">Register Now</a>

</div>

{% endblock content %}
8 changes: 8 additions & 0 deletions main/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,18 @@ <h5>Class:</h5>

<br />

<h5>Sesi terakhir login: {{ last_login }}</h5>

<a href="{% url 'main:create_product' %}">
<button>
Add New Product
</button>
</a>

<a href="{% url 'main:logout' %}">
<button>
Logout
</button>
</a>

{% endblock content %}
34 changes: 34 additions & 0 deletions main/templates/register.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends 'base.html' %}

{% block meta %}
<title>Register</title>
{% endblock meta %}

{% block content %}

<div class = "login">

<h1>Register</h1>

<form method="POST" >
{% csrf_token %}
<table>
{{ form.as_table }}
<tr>
<td></td>
<td><input type="submit" name="submit" value="Daftar"/></td>
</tr>
</table>
</form>

{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}

</div>

{% endblock content %}
5 changes: 4 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.urls import path
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id, register, login_user, logout_user

app_name = 'main'

urlpatterns = [
path('register/', register, name='register'),
path('login/', login_user, name='login'),
path('logout/', logout_user, name='logout'),
path('create-product', create_product, name='create_product'),
path('xml/', show_xml, name='show_xml'),
path('json/', show_json, name='show_json'),
Expand Down
51 changes: 47 additions & 4 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
import datetime
from django.shortcuts import render
from django.http import HttpResponseRedirect
from main.forms import ProductForm
from django.urls import reverse
from main.models import Product
from django.http import HttpResponse
from django.core import serializers
from django.shortcuts import redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

# Create your views here.
@login_required(login_url='/login')
def show_main(request):
products = Product.objects.all()
products = Product.objects.filter(user=request.user)

context = {
'name': 'Daffa Mohamad Fathoni',
'name': request.user.username,
'class': 'PBP E',
'products': products
'products': products,
'last_login': request.COOKIES['last_login']
}
return render(request, "main.html", context)

def create_product(request):
form = ProductForm(request.POST or None)

if form.is_valid() and request.method == "POST":
form.save()
product = form.save(commit=False)
product.user = request.user
product.save()
return HttpResponseRedirect(reverse('main:show_main'))

context = {'form': form}
return render(request, "create_product.html", context)

def register(request):
form = UserCreationForm()

if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Your account has been successfully created!')
return redirect('main:login')
context = {'form':form}
return render(request, 'register.html', context)

def login_user(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
response = HttpResponseRedirect(reverse("main:show_main"))
response.set_cookie('last_login', str(datetime.datetime.now()))
return response
else:
messages.info(request, 'Sorry, incorrect username or password. Please try again.')
context = {}
return render(request, 'login.html', context)

def logout_user(request):
logout(request)
response = HttpResponseRedirect(reverse('main:login'))
response.delete_cookie('last_login')
return response

def show_xml(request):
data = Product.objects.all()
return HttpResponse(serializers.serialize("xml", data), content_type="application/xml")
Expand Down
Binary file modified shopping_list/__pycache__/settings.cpython-311.pyc
Binary file not shown.

0 comments on commit f778d3c

Please sign in to comment.