add button!
This commit is contained in:
@@ -3,6 +3,7 @@ FROM python:3.8.2
|
||||
WORKDIR /app
|
||||
COPY config config
|
||||
COPY api api
|
||||
COPY ui ui
|
||||
COPY manage.py manage.py
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
|
||||
@@ -32,6 +32,7 @@ ALLOWED_HOSTS = ["localhost", "test.ducoterra.net"]
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'api',
|
||||
'ui',
|
||||
'rest_framework',
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
|
||||
@@ -19,5 +19,6 @@ from django.http import JsonResponse
|
||||
|
||||
urlpatterns = [
|
||||
path('', include('api.urls')),
|
||||
path('', include('ui.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
|
||||
0
ui/__init__.py
Normal file
0
ui/__init__.py
Normal file
3
ui/admin.py
Normal file
3
ui/admin.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
5
ui/apps.py
Normal file
5
ui/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class UiConfig(AppConfig):
|
||||
name = 'ui'
|
||||
0
ui/migrations/__init__.py
Normal file
0
ui/migrations/__init__.py
Normal file
3
ui/models.py
Normal file
3
ui/models.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
35
ui/static/ui/button.js
Normal file
35
ui/static/ui/button.js
Normal file
@@ -0,0 +1,35 @@
|
||||
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;
|
||||
}
|
||||
|
||||
var csrftoken = getCookie('csrftoken');
|
||||
var button = document.getElementById("BUTTON");
|
||||
var count = document.getElementById("COUNT");
|
||||
|
||||
button.addEventListener("click", event => {
|
||||
fetch('/button', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrftoken
|
||||
}
|
||||
})
|
||||
.then((response) => {
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
count.innerText = data.pressed;
|
||||
});
|
||||
})
|
||||
33
ui/templates/ui/button.html
Normal file
33
ui/templates/ui/button.html
Normal file
@@ -0,0 +1,33 @@
|
||||
{% load static %}
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Hello Bulma!</title>
|
||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.8.2/css/bulma.min.css">
|
||||
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
{% csrf_token %}
|
||||
<section class="section">
|
||||
<div class="container">
|
||||
<div>
|
||||
<h1 class="title">
|
||||
The Button
|
||||
</h1>
|
||||
<button class="button" id="BUTTON">Press</button>
|
||||
</div>
|
||||
<div><br></div>
|
||||
<div>
|
||||
<h1 class="title" id="COUNT">{{ pressed }}</h1>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
<script src="{% static 'ui/button.js' %}"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
33
ui/tests.py
Normal file
33
ui/tests.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from django.contrib.auth.models import AnonymousUser, User
|
||||
from django.test import RequestFactory, TestCase
|
||||
|
||||
from .views import button
|
||||
|
||||
class SimpleTest(TestCase):
|
||||
def setUp(self):
|
||||
# Every test needs access to the request factory.
|
||||
self.factory = RequestFactory()
|
||||
self.user = User.objects.create_user(
|
||||
username='testuser', email='test@test.test', password='testpass')
|
||||
|
||||
def test_button(self):
|
||||
# Create an instance of a GET request.
|
||||
request = self.factory.get('/snippets')
|
||||
request.user = self.user
|
||||
request.session = self.client.session
|
||||
response = button(request)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
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)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(request.session.get('pressed'), 2)
|
||||
6
ui/urls.py
Normal file
6
ui/urls.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('button', views.button, name = 'button'),
|
||||
]
|
||||
14
ui/views.py
Normal file
14
ui/views.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
|
||||
def button(request):
|
||||
PRESSED = 'pressed'
|
||||
try:
|
||||
request.session[PRESSED]
|
||||
except KeyError:
|
||||
request.session[PRESSED] = 0
|
||||
|
||||
if request.method == "POST":
|
||||
request.session[PRESSED] += 1
|
||||
return JsonResponse({PRESSED: request.session[PRESSED]})
|
||||
return render(request, "ui/button.html", {PRESSED: request.session[PRESSED]})
|
||||
Reference in New Issue
Block a user