Skip to content

Commit

Permalink
tugas 3 half
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinIW committed Sep 17, 2023
1 parent 8beb690 commit e875dea
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 14 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
15 changes: 13 additions & 2 deletions PBPTugas2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
"""

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-@mq56t!jgwm%qh=j62_p66fagoy=35%k-uojsl2hhs&uc1e#%='
PRODUCTION = env.bool('PRODUCTION', False)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down Expand Up @@ -56,7 +59,7 @@
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
Expand All @@ -82,6 +85,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 +128,7 @@
# https://docs.djangoproject.com/en/4.2/howto/static-files/

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
3 changes: 2 additions & 1 deletion PBPTugas2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from django.urls import path, include

urlpatterns = [
path('', include('main.urls')),
path('admin/', admin.site.urls),
path('main/', include('main.urls')),

]
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 PBPTugas2.wsgi
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 Item

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

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

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

{% endblock %}
48 changes: 41 additions & 7 deletions main/templates/main.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
<h1>Pengelolaan Stok Produk toko PBP</h1>

{% extends 'base.html' %}

<h5>Nama Aplikasi: </h5>
<p>{{ application }}</p>
<h5>Name: </h5>
<p>{{ name }}<p>
<h5>Class: </h5>
<p>{{ class }}<p>
{% block content %}


<h1>Pengelolaan Stok Produk toko PBP</h1>


<h5>Nama Aplikasi: </h5>
<p>{{ application }}</p>
<h5>Name: </h5>
<p>{{ name }}<p>
<h5>Class: </h5>
<p>{{ class }}<p>


<table>
<tr>
<th>Name</th>
<th>Ammount</th>
<th>Description</th>

</tr>

{% for item in items %}
<tr>
<td>{{item.name}}</td>
<td>{{item.amount}}</td>
<td>{{item.description}}</td>

</tr>
{% endfor %}
</table>

<br />

<a href="{% url 'main:create_item' %}">
<button>
Add New Item
</button>
</a>
{% endblock content %}
7 changes: 6 additions & 1 deletion main/urls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from django.urls import path
from main.views import show_main
from main.views import show_main, create_item, show_xml, show_json, show_xml_by_id, show_json_by_id

app_name = 'main'

urlpatterns = [
path('', show_main, name='show_main'),
path('create-item', create_item, name='create_item'),
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'),

]
37 changes: 35 additions & 2 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
from django.shortcuts import render

from django.http import HttpResponseRedirect
from main.forms import ItemForm
from django.urls import reverse
from main.models import Item
from django.http import HttpResponse
from django.core import serializers


def show_main(request):
items = Item.objects.all()
context = {
'application': 'Miracle',
'name': 'Kevin Ignatius Wijaya',
'class': 'PBP F'
'class': 'PBP F',
'items' : items
}

return render(request, "main.html", context)

def create_item(request):
form = ItemForm(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_item.html", context)

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

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

def show_json(request):
data = Item.objects.all()
return HttpResponse(serializers.serialize("json", data), content_type="application/json")
def show_json_by_id(request, id):
data = Item.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
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 e875dea

Please sign in to comment.