Skip to content

Commit

Permalink
Merge pull request #1 from fathonidf/development
Browse files Browse the repository at this point in the history
selesai tutorial 2 dan merge ke main (15 september)
  • Loading branch information
fathonidf authored Sep 15, 2023
2 parents a7daff9 + a5f482d commit 96a9482
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 11 deletions.
32 changes: 32 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
**/*.pyc
**/*.pyo
**/*.mo
**/*.db
**/*.css.map
**/*.egg-info
**/*.sql.gz
**/__pycache__/
.cache
.project
.idea
.pydevproject
.idea/workspace.xml
.DS_Store
.git/
.sass-cache
.vagrant/
dist
docs
env
logs
src/{{ project_name }}/settings/local.py
src/node_modules
web/media
web/static/CACHE
stats
Dockerfile
.gitignore
Dockerfile
db.sqlite3
**/*.md
logs/
24 changes: 24 additions & 0 deletions .github/workflows/pbp-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Deploy

on:
push:
branches:
- main
- master

jobs:
Deployment:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Cloning repo
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Push to Dokku server
uses: dokku/github-action@master
with:
branch: 'main'
git_remote_url: ssh://dokku@${{ secrets.DOKKU_SERVER_IP }}/${{ secrets.DOKKU_APP_NAME }}
ssh_private_key: ${{ secrets.DOKKU_SSH_PRIVATE_KEY }}
32 changes: 32 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
FROM python:3.10-slim-buster

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
PYTHONPATH=/app \
DJANGO_SETTINGS_MODULE=shopping_list.settings \
PORT=8000 \
WEB_CONCURRENCY=2

# Install system packages required Django.
RUN apt-get update --yes --quiet && apt-get install --yes --quiet --no-install-recommends \
&& rm -rf /var/lib/apt/lists/*

RUN addgroup --system django \
&& adduser --system --ingroup django django

# Requirements are installed here to ensure they will be cached.
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt

# Copy project code
COPY . .

RUN python manage.py collectstatic --noinput --clear

# Run as non-root user
RUN chown -R django:django /app
USER django

# Run application
# CMD gunicorn shopping_list.wsgi:application
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
release: django-admin migrate --noinput
web: gunicorn shopping_list.wsgi
Binary file modified db.sqlite3
Binary file not shown.
7 changes: 7 additions & 0 deletions main/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.forms import ModelForm
from main.models import Product

class ProductForm(ModelForm):
class Meta:
model = Product
fields = ["name", "price", "description"]
19 changes: 19 additions & 0 deletions main/templates/create_product.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{% extends 'base.html' %}

{% block content %}
<h1>Add New Product</h1>

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

{% endblock %}
44 changes: 39 additions & 5 deletions main/templates/main.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
<h1>Shopping List Page</h1>
{% extends 'base.html' %}

<h5>Name: </h5>
<p>{{ name }}</p> <!-- Ubahlah sesuai dengan nama kamu -->
<h5>Class: </h5>
<p>{{ class }}</p> <!-- Ubahlah sesuai dengan kelas kamu -->
{% block content %}
<h1>Shopping List Page</h1>

<h5>Name:</h5>
<p>{{name}}</p>

<h5>Class:</h5>
<p>{{class}}</p>

<table>
<tr>
<th>Name</th>
<th>Price</th>
<th>Description</th>
<th>Date Added</th>
</tr>

{% comment %} Berikut cara memperlihatkan data produk di bawah baris ini {% endcomment %}

{% for product in products %}
<tr>
<td>{{product.name}}</td>
<td>{{product.price}}</td>
<td>{{product.description}}</td>
<td>{{product.date_added}}</td>
</tr>
{% endfor %}
</table>

<br />

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

{% endblock content %}
7 changes: 6 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.urls import path
from main.views import show_main
from main.views import show_main, create_product, show_xml, show_json, show_xml_by_id, show_json_by_id

app_name = 'main'

urlpatterns = [
path('create-product', create_product, name='create_product'),
path('xml/', show_xml, name='show_xml'),
path('json/', show_json, name='show_json'),
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('', show_main, name='show_main')
]
39 changes: 37 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
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

# Create your views here.
def show_main(request):
products = Product.objects.all()

context = {
'name': 'Daffa Mohamad Fathoni',
'class': 'PBP C'
'class': 'PBP E',
'products': products
}
return render(request, "main.html", context)
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()
return HttpResponseRedirect(reverse('main:show_main'))

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

def show_xml(request):
data = Product.objects.all()
return HttpResponse(serializers.serialize("xml", data), content_type="application/xml")

def show_json(request):
data = Product.objects.all()
return HttpResponse(serializers.serialize("json", data), content_type="application/json")

def show_xml_by_id(request, id):
data = Product.objects.filter(pk=id)
return HttpResponse(serializers.serialize("xml", data), content_type="application/xml")

def show_json_by_id(request, id):
data = Product.objects.filter(pk=id)
return HttpResponse(serializers.serialize("json", data), content_type="application/json")
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ gunicorn
whitenoise
psycopg2-binary
requests
urllib3
urllib3
django-environ
Binary file modified shopping_list/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file modified shopping_list/__pycache__/urls.cpython-311.pyc
Binary file not shown.
17 changes: 16 additions & 1 deletion shopping_list/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@
"""

from pathlib import Path
import environ
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env()


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-cd3(&!&vsrd2x45ta@-l+2dg@@x*odbp979$(4966_l9g+4ao0'
# Automatically determine environment by detecting if DATABASE_URL variable.
# DATABASE_URL is provided by Heroku if a database add-on is added (e.g. Heroku Postgres).
PRODUCTION = env.bool('PRODUCTION', False)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down Expand Up @@ -55,7 +61,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand All @@ -81,6 +87,13 @@
}
}

# Set database settings automatically using DATABASE_URL.
if PRODUCTION:
DATABASES = {
'default': env.db('DATABASE_URL')
}
DATABASES["default"]["ATOMIC_REQUESTS"] = True


# Password validation
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -118,6 +131,8 @@

STATIC_URL = 'static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

Expand Down
2 changes: 1 addition & 1 deletion shopping_list/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

urlpatterns = [
path('admin/', admin.site.urls),
path('main/', include('main.urls'))
path('', include('main.urls'))
]
18 changes: 18 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0"
/>
{% block meta %}
{% endblock meta %}
</head>

<body>
{% block content %}
{% endblock content %}
</body>
</html>

0 comments on commit 96a9482

Please sign in to comment.