Compare commits
36 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f45e289ed8 | ||
|
|
43c48d5216 | ||
|
|
0cb3896e4b | ||
|
|
e102db9f38 | ||
|
|
15c5f5293a | ||
|
|
f11f9a0d97 | ||
|
|
c80ef7441d | ||
|
|
a2e7a92280 | ||
|
|
b4ef050e1f | ||
|
|
c09558c0ff | ||
|
|
c3666783e4 | ||
|
|
54c6336e22 | ||
|
|
7da888aa09 | ||
|
|
07d98bf11d | ||
|
|
4584ba0143 | ||
|
|
6feac7ef2e | ||
|
|
5efb93ea68 | ||
|
|
1ee6b890ef | ||
|
|
18aab648c6 | ||
|
|
57328a7fc8 | ||
|
|
948569a659 | ||
|
|
50cdfd8180 | ||
|
|
8393efa3a6 | ||
|
|
b3db1816bb | ||
|
|
7123f4c389 | ||
|
|
477ddfe165 | ||
|
|
6bc472f7fe | ||
|
|
41f4549ffd | ||
|
|
7dd45cc2e8 | ||
|
|
8ec174b2c3 | ||
|
|
7868867908 | ||
|
|
d2bf889e1f | ||
|
|
a70aa8840f | ||
|
|
8404d43222 | ||
|
|
f31106dd29 | ||
|
|
fbd8b0e2a7 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
venv/
|
venv/
|
||||||
__pycache__/
|
__pycache__/
|
||||||
db/
|
db/
|
||||||
|
staticfiles/
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
variables:
|
variables:
|
||||||
CI_PROJECT_DIR: "."
|
CI_PROJECT_DIR: "."
|
||||||
CI_REGISTRY_IMAGE: hub.ducoterra.net/ducoterra/mysite
|
CI_REGISTRY_IMAGE: hub.ducoterra.net/ducoterra/mysite
|
||||||
DEPLOY: test
|
|
||||||
|
|
||||||
stages:
|
stages:
|
||||||
- build
|
- build
|
||||||
@@ -17,34 +16,61 @@ build:
|
|||||||
name: gcr.io/kaniko-project/executor:debug
|
name: gcr.io/kaniko-project/executor:debug
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
script:
|
script:
|
||||||
|
- echo $DEPLOY
|
||||||
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
- /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
||||||
|
|
||||||
test:
|
test:
|
||||||
|
stage: test
|
||||||
only:
|
only:
|
||||||
variables:
|
variables:
|
||||||
- $CI_COMMIT_TAG
|
- $CI_COMMIT_TAG
|
||||||
stage: test
|
|
||||||
image:
|
image:
|
||||||
name: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
name: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
script:
|
script:
|
||||||
- python manage.py test
|
- python manage.py test
|
||||||
|
|
||||||
deploy:
|
deploy_to_test:
|
||||||
|
variables:
|
||||||
|
DEPLOY: test
|
||||||
|
stage: deploy
|
||||||
only:
|
only:
|
||||||
variables:
|
variables:
|
||||||
- $CI_COMMIT_TAG
|
- $CI_COMMIT_TAG
|
||||||
stage: deploy
|
|
||||||
image:
|
image:
|
||||||
name: debian:latest
|
name: debian:10
|
||||||
entrypoint: [""]
|
entrypoint: [""]
|
||||||
script:
|
script:
|
||||||
- echo $CI_REGISTRY_IMAGE
|
|
||||||
- apt -qq update >> /dev/null && apt -qq install -y curl gettext >> /dev/null
|
- apt -qq update >> /dev/null && apt -qq install -y curl gettext >> /dev/null
|
||||||
- curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
- curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
||||||
- chmod +x ./kubectl
|
- chmod +x ./kubectl
|
||||||
- envsubst < k8s/deploy.yaml > out.yaml
|
- mkdir /deploy
|
||||||
- mv out.yaml k8s/deploy.yaml
|
- for f in $(find k8s -regex '.*\.ya*ml'); do envsubst < $f > "/deploy/$(basename $f)"; done
|
||||||
- ./kubectl apply -f k8s
|
- for f in $(find k8s/test -regex '.*\.ya*ml'); do envsubst < $f > "/deploy/$(basename $f)"; done
|
||||||
|
- ./kubectl apply -f /deploy
|
||||||
- ./kubectl rollout status deploy $DEPLOY
|
- ./kubectl rollout status deploy $DEPLOY
|
||||||
- ./kubectl exec $(./kubectl get pods --selector=app=$DEPLOY --output=jsonpath='{.items[*].metadata.name}') -- python manage.py migrate
|
- POD=$(./kubectl get pods --selector=app=$DEPLOY --output=jsonpath='{.items[*].metadata.name}')
|
||||||
|
- ./kubectl exec $POD -- python manage.py migrate
|
||||||
|
|
||||||
|
deploy_to_prod:
|
||||||
|
variables:
|
||||||
|
DEPLOY: prod
|
||||||
|
stage: deploy
|
||||||
|
only:
|
||||||
|
variables:
|
||||||
|
- $CI_COMMIT_TAG
|
||||||
|
when: manual
|
||||||
|
image:
|
||||||
|
name: debian:10
|
||||||
|
entrypoint: [""]
|
||||||
|
script:
|
||||||
|
- apt -qq update >> /dev/null && apt -qq install -y curl gettext >> /dev/null
|
||||||
|
- curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
|
||||||
|
- chmod +x ./kubectl
|
||||||
|
- mkdir /deploy
|
||||||
|
- for f in $(find k8s -regex '.*\.ya*ml'); do envsubst < $f > "/deploy/$(basename $f)"; done
|
||||||
|
- for f in $(find k8s/prod -regex '.*\.ya*ml'); do envsubst < $f > "/deploy/$(basename $f)"; done
|
||||||
|
- ./kubectl apply -f /deploy
|
||||||
|
- ./kubectl rollout status deploy $DEPLOY
|
||||||
|
- POD=$(./kubectl get pods --selector=app=$DEPLOY --output=jsonpath='{.items[*].metadata.name}')
|
||||||
|
- ./kubectl exec $POD -- python manage.py migrate
|
||||||
17
.vscode/launch.json
vendored
17
.vscode/launch.json
vendored
@@ -5,14 +5,27 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "Python: Django",
|
"name": "Test",
|
||||||
"type": "python",
|
"type": "python",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/manage.py",
|
"program": "${workspaceFolder}/manage.py",
|
||||||
"args": [
|
"args": [
|
||||||
"test",
|
"test",
|
||||||
],
|
],
|
||||||
"django": true
|
"django": true,
|
||||||
|
"preLaunchTask": "Migrate"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Run Server",
|
||||||
|
"type": "python",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/manage.py",
|
||||||
|
"args": [
|
||||||
|
"runserver",
|
||||||
|
"--noreload"
|
||||||
|
],
|
||||||
|
"django": true,
|
||||||
|
"preLaunchTask": "Migrate"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
24
.vscode/tasks.json
vendored
Normal file
24
.vscode/tasks.json
vendored
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"label": "Collect Static",
|
||||||
|
"command": "venv/bin/python manage.py collectstatic --no-input",
|
||||||
|
"type": "shell",
|
||||||
|
"presentation": {
|
||||||
|
"reveal": "always"
|
||||||
|
},
|
||||||
|
"group": "build"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"dependsOn": "Collect Static",
|
||||||
|
"label": "Migrate",
|
||||||
|
"command": "venv/bin/python manage.py migrate",
|
||||||
|
"type": "shell",
|
||||||
|
"presentation": {
|
||||||
|
"reveal": "always"
|
||||||
|
},
|
||||||
|
"group": "build"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -6,12 +6,12 @@ COPY api api
|
|||||||
COPY ui ui
|
COPY ui ui
|
||||||
COPY manage.py manage.py
|
COPY manage.py manage.py
|
||||||
COPY requirements.txt requirements.txt
|
COPY requirements.txt requirements.txt
|
||||||
|
RUN pip install -r requirements.txt
|
||||||
|
|
||||||
RUN useradd -ms /bin/bash django
|
RUN useradd -ms /bin/bash django
|
||||||
RUN chown -R django .
|
RUN chown -R django .
|
||||||
|
|
||||||
USER django
|
USER django
|
||||||
RUN pip install -r requirements.txt --user
|
|
||||||
RUN python manage.py collectstatic
|
RUN python manage.py collectstatic
|
||||||
|
|
||||||
CMD ["gunicorn","-b",":8000", "-w", "4", "config.wsgi"]
|
CMD ["gunicorn","-b",":8000", "-w", "4", "config.wsgi"]
|
||||||
21
README.md
21
README.md
@@ -2,4 +2,23 @@
|
|||||||
|
|
||||||
My CI testing pipeline for a django project.
|
My CI testing pipeline for a django project.
|
||||||
|
|
||||||
[](http://gitlab.ducoterra.net/ducoterra/ci_builder/-/commits/master)
|
[](http://gitlab.ducoterra.net/ducoterra/ci_builder/-/commits/master)
|
||||||
|
|
||||||
|
## Django Environment Variables
|
||||||
|
|
||||||
|
### Django Secret
|
||||||
|
|
||||||
|
```bash
|
||||||
|
kubectl create secret generic django-secrets --from-literal=SECRET_KEY=$(python -c "import secrets ; print(secrets.token_urlsafe(32))")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Django Allowed Hosts
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: test
|
||||||
|
data:
|
||||||
|
ALLOWED_HOSTS: localhost,test.ducoterra.net
|
||||||
|
```
|
||||||
|
|||||||
@@ -20,13 +20,12 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|||||||
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
|
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
|
||||||
|
|
||||||
# SECURITY WARNING: keep the secret key used in production secret!
|
# SECURITY WARNING: keep the secret key used in production secret!
|
||||||
SECRET_KEY = 'b8fi9=f-qj=@-#1iru34-f@a6pzfysgrf(1n_&d=ur%!1w$q*w'
|
SECRET_KEY = os.getenv("SECRET_KEY")
|
||||||
|
|
||||||
# SECURITY WARNING: don't run with debug turned on in production!
|
# SECURITY WARNING: don't run with debug turned on in production!
|
||||||
DEBUG = True if os.getenv("DEBUG") == "True" else False
|
DEBUG = True if os.getenv("DEBUG") == "True" else False
|
||||||
|
|
||||||
ALLOWED_HOSTS = ["localhost", "test.ducoterra.net"]
|
ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "localhost").split(",")
|
||||||
|
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ from django.urls import path, include
|
|||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include('api.urls')),
|
# path('api/', include('api.urls')),
|
||||||
path('', include('ui.urls')),
|
path('', include('ui.urls')),
|
||||||
path('admin/', admin.site.urls),
|
# path('admin/', admin.site.urls),
|
||||||
]
|
]
|
||||||
|
|||||||
6
k8s/configmap.yaml
Normal file
6
k8s/configmap.yaml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
apiVersion: v1
|
||||||
|
kind: ConfigMap
|
||||||
|
metadata:
|
||||||
|
name: $DEPLOY
|
||||||
|
data:
|
||||||
|
ALLOWED_HOSTS: localhost,$DEPLOY.ducoterra.net
|
||||||
@@ -1,22 +1,27 @@
|
|||||||
apiVersion: apps/v1
|
apiVersion: apps/v1
|
||||||
kind: Deployment
|
kind: Deployment
|
||||||
metadata:
|
metadata:
|
||||||
name: test
|
name: $DEPLOY
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
matchLabels:
|
matchLabels:
|
||||||
app: test
|
app: $DEPLOY
|
||||||
template:
|
template:
|
||||||
metadata:
|
metadata:
|
||||||
labels:
|
labels:
|
||||||
app: test
|
app: $DEPLOY
|
||||||
spec:
|
spec:
|
||||||
containers:
|
containers:
|
||||||
- name: test
|
- name: $DEPLOY
|
||||||
image: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
image: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
|
||||||
|
envFrom:
|
||||||
|
- configMapRef:
|
||||||
|
name: $DEPLOY
|
||||||
|
- secretRef:
|
||||||
|
name: django-secrets
|
||||||
volumeMounts:
|
volumeMounts:
|
||||||
- mountPath: /app/db
|
- mountPath: /app/db
|
||||||
name: test
|
name: $DEPLOY
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
memory: "256Mi"
|
memory: "256Mi"
|
||||||
@@ -27,6 +32,6 @@ spec:
|
|||||||
ports:
|
ports:
|
||||||
- containerPort: 8000
|
- containerPort: 8000
|
||||||
volumes:
|
volumes:
|
||||||
- name: test
|
- name: $DEPLOY
|
||||||
persistentVolumeClaim:
|
persistentVolumeClaim:
|
||||||
claimName: test
|
claimName: $DEPLOY
|
||||||
@@ -3,18 +3,18 @@ kind: Ingress
|
|||||||
metadata:
|
metadata:
|
||||||
annotations:
|
annotations:
|
||||||
ingress.kubernetes.io/ssl-redirect: "true"
|
ingress.kubernetes.io/ssl-redirect: "true"
|
||||||
name: test
|
name: $DEPLOY
|
||||||
spec:
|
spec:
|
||||||
tls:
|
tls:
|
||||||
- hosts:
|
- hosts:
|
||||||
- test.ducoterra.net
|
- $DEPLOY.ducoterra.net
|
||||||
secretName: letsencrypt
|
secretName: letsencrypt
|
||||||
rules:
|
rules:
|
||||||
- host: test.ducoterra.net
|
- host: $DEPLOY.ducoterra.net
|
||||||
http:
|
http:
|
||||||
paths:
|
paths:
|
||||||
- backend:
|
- backend:
|
||||||
serviceName: test
|
serviceName: $DEPLOY
|
||||||
servicePort: 8000
|
servicePort: 8000
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -22,17 +22,17 @@ spec:
|
|||||||
apiVersion: traefik.containo.us/v1alpha1
|
apiVersion: traefik.containo.us/v1alpha1
|
||||||
kind: IngressRoute
|
kind: IngressRoute
|
||||||
metadata:
|
metadata:
|
||||||
name: test-external-tls
|
name: $DEPLOY-external-tls
|
||||||
spec:
|
spec:
|
||||||
entryPoints:
|
entryPoints:
|
||||||
- websecure
|
- websecure
|
||||||
tls:
|
tls:
|
||||||
secretName: letsencrypt
|
secretName: letsencrypt
|
||||||
routes:
|
routes:
|
||||||
- match: Host(`test.ducoterra.net`)
|
- match: Host(`$DEPLOY.ducoterra.net`)
|
||||||
kind: Rule
|
kind: Rule
|
||||||
services:
|
services:
|
||||||
- name: test
|
- name: $DEPLOY
|
||||||
port: 8000
|
port: 8000
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -40,15 +40,15 @@ spec:
|
|||||||
apiVersion: traefik.containo.us/v1alpha1
|
apiVersion: traefik.containo.us/v1alpha1
|
||||||
kind: IngressRoute
|
kind: IngressRoute
|
||||||
metadata:
|
metadata:
|
||||||
name: test-external-web
|
name: $DEPLOY-external-web
|
||||||
spec:
|
spec:
|
||||||
entryPoints:
|
entryPoints:
|
||||||
- web
|
- web
|
||||||
routes:
|
routes:
|
||||||
- match: Host(`test.ducoterra.net`)
|
- match: Host(`$DEPLOY.ducoterra.net`)
|
||||||
kind: Rule
|
kind: Rule
|
||||||
services:
|
services:
|
||||||
- name: test
|
- name: $DEPLOY
|
||||||
port: 8000
|
port: 8000
|
||||||
middlewares:
|
middlewares:
|
||||||
- name: httpsredirect
|
- name: httpsredirect
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: PersistentVolumeClaim
|
kind: PersistentVolumeClaim
|
||||||
metadata:
|
metadata:
|
||||||
name: test
|
name: $DEPLOY
|
||||||
spec:
|
spec:
|
||||||
storageClassName: nfs-encrypted
|
storageClassName: nfs-encrypted
|
||||||
accessModes:
|
accessModes:
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
apiVersion: v1
|
apiVersion: v1
|
||||||
kind: Service
|
kind: Service
|
||||||
metadata:
|
metadata:
|
||||||
name: test
|
name: $DEPLOY
|
||||||
spec:
|
spec:
|
||||||
selector:
|
selector:
|
||||||
app: test
|
app: $DEPLOY
|
||||||
ports:
|
ports:
|
||||||
- port: 8000
|
- port: 8000
|
||||||
targetPort: 8000
|
targetPort: 8000
|
||||||
18
k8s/test/ingress.yaml
Normal file
18
k8s/test/ingress.yaml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
apiVersion: networking.k8s.io/v1beta1
|
||||||
|
kind: Ingress
|
||||||
|
metadata:
|
||||||
|
annotations:
|
||||||
|
ingress.kubernetes.io/ssl-redirect: "true"
|
||||||
|
name: $DEPLOY
|
||||||
|
spec:
|
||||||
|
tls:
|
||||||
|
- hosts:
|
||||||
|
- $DEPLOY.ducoterra.net
|
||||||
|
secretName: letsencrypt
|
||||||
|
rules:
|
||||||
|
- host: $DEPLOY.ducoterra.net
|
||||||
|
http:
|
||||||
|
paths:
|
||||||
|
- backend:
|
||||||
|
serviceName: $DEPLOY
|
||||||
|
servicePort: 8000
|
||||||
@@ -6,6 +6,7 @@ import sys
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
os.environ.setdefault('DEBUG', 'True')
|
os.environ.setdefault('DEBUG', 'True')
|
||||||
|
os.environ.setdefault('SECRET_KEY', 'SeVOOxOHISQZv82RfCPds0B2l8M6jGju4G8F-GcuSrc')
|
||||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
|
||||||
try:
|
try:
|
||||||
from django.core.management import execute_from_command_line
|
from django.core.management import execute_from_command_line
|
||||||
|
|||||||
41
ui/static/ui/achievement.css
Normal file
41
ui/static/ui/achievement.css
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
.achievement {
|
||||||
|
position: absolute;
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
z-index: -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.achievement-animate {
|
||||||
|
animation-name: moveup;
|
||||||
|
animation-duration: 2s;
|
||||||
|
animation-fill-mode: forwards;
|
||||||
|
}
|
||||||
|
|
||||||
|
.achievment-column {
|
||||||
|
height: 100%;
|
||||||
|
overflow: auto;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.achievement-text {
|
||||||
|
font-size: 4rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 768px) {
|
||||||
|
.achievment-column {
|
||||||
|
height: 50%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes moveup {
|
||||||
|
from {bottom: 0px;}
|
||||||
|
to {bottom: 200px; color: white;}
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes fadeout {
|
||||||
|
from {}
|
||||||
|
to {color: transparent;}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
html, body {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.section, .container {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.columns {
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-column {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
text-align: center;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media only screen and (max-width: 768px) {
|
||||||
|
.button-column {
|
||||||
|
height: 50%;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.button-container {
|
||||||
|
position: absolute;
|
||||||
|
}
|
||||||
@@ -1,26 +1,31 @@
|
|||||||
function getCookie(name) {
|
const csrftoken = getCookie('csrftoken');
|
||||||
var cookieValue = null;
|
const button = document.getElementById("BUTTON");
|
||||||
if (document.cookie && document.cookie !== '') {
|
const count = document.getElementById("COUNT");
|
||||||
var cookies = document.cookie.split(';');
|
const button_container = document.getElementById("button-container");
|
||||||
for (var i = 0; i < cookies.length; i++) {
|
const achievement = document.getElementById("achievement");
|
||||||
var cookie = cookies[i].trim();
|
const achievement_list = document.getElementById("achievement-list");
|
||||||
// Does this cookie string begin with the name we want?
|
const achievement_column = document.getElementById("achievement-column");
|
||||||
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
|
||||||
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
function add_achievement(text) {
|
||||||
break;
|
if (text != undefined) {
|
||||||
}
|
achievement.querySelector(".achievement-text").innerText = text;
|
||||||
}
|
achievement.classList.remove("achievement-animate");
|
||||||
|
void achievement.offsetWidth;
|
||||||
|
achievement.classList.add("achievement-animate");
|
||||||
|
|
||||||
|
var elem = document.createElement("div");
|
||||||
|
elem.innerText = text;
|
||||||
|
achievement_list.appendChild(elem);
|
||||||
|
|
||||||
|
achievement_column.scrollTo(0, achievement_list.scrollHeight);
|
||||||
}
|
}
|
||||||
return cookieValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var csrftoken = getCookie('csrftoken');
|
// when button is clicked submit an empty post request
|
||||||
var button = document.getElementById("BUTTON");
|
|
||||||
var count = document.getElementById("COUNT");
|
|
||||||
|
|
||||||
button.addEventListener("click", event => {
|
button.addEventListener("click", event => {
|
||||||
button.disabled = true;
|
button.disabled = true;
|
||||||
fetch('/button', {
|
button.classList.add("is-loading");
|
||||||
|
fetch('/button/', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -32,9 +37,12 @@ button.addEventListener("click", event => {
|
|||||||
})
|
})
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
count.innerText = data.pressed;
|
count.innerText = data.pressed;
|
||||||
|
add_achievement(data.achievement);
|
||||||
}).finally(() => {
|
}).finally(() => {
|
||||||
button.disabled = false;
|
button.disabled = false;
|
||||||
|
button.classList.remove("is-loading");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// when the page is loaded automatically select the button
|
||||||
button.focus();
|
button.focus();
|
||||||
16
ui/static/ui/helper.js
Normal file
16
ui/static/ui/helper.js
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// get cookies when fetching with django
|
||||||
|
function getCookie(name) {
|
||||||
|
var cookieValue = null;
|
||||||
|
if (document.cookie && document.cookie !== '') {
|
||||||
|
var cookies = document.cookie.split(';');
|
||||||
|
for (var i = 0; i < cookies.length; i++) {
|
||||||
|
var cookie = cookies[i].trim();
|
||||||
|
// Does this cookie string begin with the name we want?
|
||||||
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||||
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return cookieValue;
|
||||||
|
}
|
||||||
@@ -4,25 +4,43 @@
|
|||||||
|
|
||||||
{% block css %}
|
{% block css %}
|
||||||
<link rel="stylesheet" href="{% static 'ui/button.css' %}">
|
<link rel="stylesheet" href="{% static 'ui/button.css' %}">
|
||||||
|
<link rel="stylesheet" href="{% static 'ui/achievement.css' %}">
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block js %}
|
{% block js %}
|
||||||
|
<script src="{% static 'ui/helper.js' %}"></script>
|
||||||
<script src="{% static 'ui/button.js' %}"></script>
|
<script src="{% static 'ui/button.js' %}"></script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
<section class="section">
|
<section class="section">
|
||||||
<div class="container">
|
<div class="columns">
|
||||||
<div>
|
<div class="column">
|
||||||
<h1 class="title">
|
|
||||||
The Button
|
|
||||||
</h1>
|
|
||||||
<button class="button is-primary" id="BUTTON">Press</button>
|
|
||||||
</div>
|
</div>
|
||||||
<div><br></div>
|
<div class="button-column column">
|
||||||
<div>
|
<div>
|
||||||
<h1 class="title" id="COUNT">{{ pressed }}</h1>
|
<h1 class="title">
|
||||||
|
The Button
|
||||||
|
</h1>
|
||||||
|
<button class="button is-primary" id="BUTTON">Press</button>
|
||||||
|
</div>
|
||||||
|
<div><br></div>
|
||||||
|
<div>
|
||||||
|
<h1 class="title" id="COUNT">{{ pressed }}</h1>
|
||||||
|
</div>
|
||||||
|
<div id="achievement" class="achievement">
|
||||||
|
<div>
|
||||||
|
<div class="achievement-text"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="column achievment-column" id = "achievement-column">
|
||||||
|
<div class="achievements-list" id="achievement-list">
|
||||||
|
<h1 class="title">Achievements</h1>
|
||||||
|
{% for key,value in achievement.items %}
|
||||||
|
<div>{{ value }}</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
44
ui/tests.py
44
ui/tests.py
@@ -1,5 +1,5 @@
|
|||||||
from django.contrib.auth.models import AnonymousUser, User
|
from django.contrib.auth.models import AnonymousUser, User
|
||||||
from django.test import RequestFactory, TestCase
|
from django.test import RequestFactory, TestCase, Client
|
||||||
|
|
||||||
from .views import button
|
from .views import button
|
||||||
|
|
||||||
@@ -11,23 +11,31 @@ class SimpleTest(TestCase):
|
|||||||
username='testuser', email='test@test.test', password='testpass')
|
username='testuser', email='test@test.test', password='testpass')
|
||||||
|
|
||||||
def test_button(self):
|
def test_button(self):
|
||||||
# Create an instance of a GET request.
|
# Test initial load
|
||||||
request = self.factory.get('/snippets')
|
c = Client()
|
||||||
request.user = self.user
|
response = c.get('/button')
|
||||||
request.session = self.client.session
|
self.assertEqual(response.status_code, 301)
|
||||||
response = button(request)
|
response = c.get('/button/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.context.get("achievement"), {})
|
||||||
request = self.factory.post(
|
|
||||||
'/button',
|
|
||||||
data={},
|
|
||||||
content_type='application/json'
|
|
||||||
)
|
|
||||||
request.session = self.client.session
|
|
||||||
response = button(request)
|
|
||||||
self.assertEqual(response.status_code, 200)
|
|
||||||
self.assertEqual(request.session.get('pressed'), 1)
|
|
||||||
|
|
||||||
response = button(request)
|
# Test first achievement
|
||||||
|
response = c.post('/button/', {})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(request.session.get('pressed'), 2)
|
self.assertEqual(response.json().get("pressed"), 1)
|
||||||
|
self.assertEqual(response.json().get("achievement"), "Clicked!")
|
||||||
|
self.assertEqual(c.session.get('pressed'), 1)
|
||||||
|
|
||||||
|
# Test second achievement
|
||||||
|
response = c.post('/button/', {})
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.json().get("pressed"), 2)
|
||||||
|
self.assertEqual(response.json().get("achievement"), "Clicked Twice!")
|
||||||
|
self.assertEqual(c.session.get('pressed'), 2)
|
||||||
|
|
||||||
|
# Test no achievement
|
||||||
|
response = c.post('/button/', {})
|
||||||
|
self.assertEqual(response.status_code, 200)
|
||||||
|
self.assertEqual(response.json().get("pressed"), 3)
|
||||||
|
self.assertEqual(response.json().get("achievement"), None)
|
||||||
|
self.assertEqual(c.session.get('pressed'), 3)
|
||||||
@@ -2,5 +2,5 @@ from django.urls import path
|
|||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('button', views.button, name = 'button'),
|
path('button/', views.button, name = 'button'),
|
||||||
]
|
]
|
||||||
66
ui/views.py
66
ui/views.py
@@ -1,8 +1,61 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
|
|
||||||
|
achievements = {
|
||||||
|
1: "Clicked!",
|
||||||
|
2: "Clicked Twice!",
|
||||||
|
4: "2^2",
|
||||||
|
8: "2^3",
|
||||||
|
16: "2^4",
|
||||||
|
24: "I'm that old",
|
||||||
|
32: "2^5",
|
||||||
|
64: "2^6",
|
||||||
|
69: "Nice",
|
||||||
|
100: "one hundred",
|
||||||
|
128: "2^7",
|
||||||
|
200: "two hundred",
|
||||||
|
250: "quarter thousand",
|
||||||
|
256: "2^8",
|
||||||
|
300: "three hundred",
|
||||||
|
400: "four hundred",
|
||||||
|
420: "Blaze it",
|
||||||
|
500: "half thousand",
|
||||||
|
512: "2^9",
|
||||||
|
600: "six hundred",
|
||||||
|
700: "seven hundred",
|
||||||
|
800: "eight hundred",
|
||||||
|
900: "nine hundred",
|
||||||
|
1000: "full thousand",
|
||||||
|
1024: "2^10",
|
||||||
|
1776: "America",
|
||||||
|
1914: "Some War here",
|
||||||
|
1938: "Some more war here",
|
||||||
|
1950: "Lots of war in here",
|
||||||
|
2000: "Computers die",
|
||||||
|
2008: "Houses die",
|
||||||
|
2019: "People die",
|
||||||
|
2048: "2048!",
|
||||||
|
2500: "Keep going!",
|
||||||
|
3000: "three thousand",
|
||||||
|
4000: "four thousand",
|
||||||
|
4096: "2^11",
|
||||||
|
5000: "halfway to ten thousand",
|
||||||
|
10001: "ten thousand one",
|
||||||
|
100000: "one hundred thousand",
|
||||||
|
1000000: "one million?",
|
||||||
|
10000000: "ten millions???",
|
||||||
|
100000000: "one hundo billion",
|
||||||
|
1000000000: "JK this is actually a billion though",
|
||||||
|
10000000000: "I'm not going to create another achievement",
|
||||||
|
100000000000: "one hundred billion",
|
||||||
|
1000000000000: "It's physically impossible to click this high"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def button(request):
|
def button(request):
|
||||||
PRESSED = 'pressed'
|
PRESSED = 'pressed'
|
||||||
|
ACHIEVE = 'achievement'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
request.session[PRESSED]
|
request.session[PRESSED]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@@ -10,5 +63,14 @@ def button(request):
|
|||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
request.session[PRESSED] += 1
|
request.session[PRESSED] += 1
|
||||||
return JsonResponse({PRESSED: request.session[PRESSED]})
|
response = {
|
||||||
return render(request, "ui/button.html", {PRESSED: request.session[PRESSED]})
|
PRESSED: request.session[PRESSED],
|
||||||
|
ACHIEVE: achievements.get(request.session[PRESSED])
|
||||||
|
}
|
||||||
|
return JsonResponse(response)
|
||||||
|
|
||||||
|
pressed = request.session[PRESSED]
|
||||||
|
response = {PRESSED: pressed}
|
||||||
|
achieved = {k:v for k,v in achievements.items() if k <= pressed}
|
||||||
|
response.update({ACHIEVE: achieved})
|
||||||
|
return render(request, "ui/button.html", response)
|
||||||
Reference in New Issue
Block a user