From a76661cfad8d5cb09ba032f8a4f3a446ee470134 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Fri, 8 Mar 2024 19:11:50 -0600 Subject: [PATCH] You can set where the backend server is now. --- requirements.txt | 37 ++---------------- tunictracker/requirements.txt | 27 ------------- tunictracker/tracker/forms.py | 14 +++++++ .../tracker/static/tracker/assets/refresh.js | 28 +++++++++++-- .../static/tracker/data/listen_address.json | 1 + .../templates/tracker/address/index.html | 13 +++++++ .../tracker/templates/tracker/index.html | 1 + tunictracker/tracker/urls.py | 1 + tunictracker/tracker/views.py | 39 ++++++++++++++++--- tunictracker/tunictracker/settings.py | 15 +++++-- 10 files changed, 104 insertions(+), 72 deletions(-) delete mode 100644 tunictracker/requirements.txt create mode 100644 tunictracker/tracker/forms.py create mode 100644 tunictracker/tracker/static/tracker/data/listen_address.json create mode 100644 tunictracker/tracker/templates/tracker/address/index.html diff --git a/requirements.txt b/requirements.txt index b963363..54df976 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,46 +1,15 @@ -annotated-types==0.6.0 -anyio==4.3.0 -arrow==1.3.0 asgiref==3.7.2 autopep8==2.0.4 -binaryornot==0.4.4 certifi==2024.2.2 -chardet==5.2.0 charset-normalizer==3.3.2 -click==8.1.7 -cookiecutter==2.6.0 -Django==5.0.2 +Django==5.0.3 django-browser-reload==1.12.1 django-tailwind==3.8.0 -fastapi==0.110.0 -h11==0.14.0 -httptools==0.6.1 idna==3.6 -Jinja2==3.1.3 -markdown-it-py==3.0.0 -MarkupSafe==2.1.5 -mdurl==0.1.2 numpy==1.26.4 -pyarrow==15.0.0 +pyarrow==15.0.1 pycodestyle==2.11.1 -pydantic==2.6.3 -pydantic_core==2.16.3 -Pygments==2.17.2 -python-dateutil==2.8.2 -python-dotenv==1.0.1 -python-slugify==8.0.4 -PyYAML==6.0.1 +pylance==0.10.2 requests==2.31.0 -rich==13.7.1 -six==1.16.0 -sniffio==1.3.1 sqlparse==0.4.4 -starlette==0.36.3 -text-unidecode==1.3 -types-python-dateutil==2.8.19.20240106 -typing_extensions==4.10.0 urllib3==2.2.1 -uvicorn==0.27.1 -uvloop==0.19.0 -watchfiles==0.21.0 -websockets==12.0 diff --git a/tunictracker/requirements.txt b/tunictracker/requirements.txt deleted file mode 100644 index 9f836be..0000000 --- a/tunictracker/requirements.txt +++ /dev/null @@ -1,27 +0,0 @@ -annotated-types==0.6.0 -anyio==4.3.0 -asgiref==3.7.2 -autopep8==2.0.4 -click==8.1.7 -Django==5.0.2 -django-browser-reload==1.12.1 -django-tailwind==3.8.0 -fastapi==0.110.0 -h11==0.14.0 -httptools==0.6.1 -idna==3.6 -numpy==1.26.4 -pyarrow==15.0.0 -pycodestyle==2.11.1 -pydantic==2.6.3 -pydantic_core==2.16.3 -python-dotenv==1.0.1 -PyYAML==6.0.1 -sniffio==1.3.1 -sqlparse==0.4.4 -starlette==0.36.3 -typing_extensions==4.10.0 -uvicorn==0.27.1 -uvloop==0.19.0 -watchfiles==0.21.0 -websockets==12.0 diff --git a/tunictracker/tracker/forms.py b/tunictracker/tracker/forms.py new file mode 100644 index 0000000..786103a --- /dev/null +++ b/tunictracker/tracker/forms.py @@ -0,0 +1,14 @@ +from django import forms +from json import loads + + +class ServerAddressForm(forms.Form): + with open('tracker/static/tracker/data/listen_address.json', 'r') as address: + read_address = loads(address.read()) + if (read_address['address']['user'] != ""): + server_address = read_address['address']['user'] + else: + server_address = read_address['address']['default'] + server_address_form = forms.URLField( + max_length=1312, initial=server_address, empty_value="http://localhost:8000/", label="") + server_address_form.widget.attrs["class"] = "w-full text-sm rounded-md border-2 border-bluelight-dark bg-[#242424]/10" diff --git a/tunictracker/tracker/static/tracker/assets/refresh.js b/tunictracker/tracker/static/tracker/assets/refresh.js index b8143da..e147546 100644 --- a/tunictracker/tracker/static/tracker/assets/refresh.js +++ b/tunictracker/tracker/static/tracker/assets/refresh.js @@ -1,7 +1,9 @@ var current_hash = ""; +var server_address = ""; window.onload = () => { - fetch("http://localhost:8080/static/tracker/data/holy_cross_codes.json") + get_updated_server_address(); + fetch(`${document.URL}static/tracker/data/holy_cross_codes.json`) .then((response) => response.json()) .then( (data) => { @@ -19,8 +21,26 @@ window.onload = () => { ); }; +async function get_updated_server_address() { + fetch(`${document.URL}static/tracker/data/listen_address.json`) + .then((response) => response.json()) + .then( + (data) => { + server_address_object = JSON.parse(JSON.stringify(data)); + if (server_address_object.user != "") { + server_address = server_address_object.address.user; + } else { + server_address = server_address_object.address.default; + } + document.getElementById("id_server_address_form").value = + server_address; + }, + (error) => console.log(error) + ); +} + async function refresh_elements(cross_codes) { - fetch("http://localhost:8000/spoiler") + fetch(server_address) .then((response) => response.json()) .then( (data) => { @@ -31,6 +51,7 @@ async function refresh_elements(cross_codes) { return; } else { current_hash = response_object.Debug.Hash; + get_updated_server_address(); } // Parse out data from the back-end into variables. @@ -319,7 +340,7 @@ async function refresh_elements(cross_codes) { scene_entrances_undiscovered <= 0 ) { summary_block.classList.add("hidden"); - breakdown_block.classList.add("hidden"); + // breakdown_block.classList.add("hidden"); } new_summary_list.firstElementChild.appendChild( summary_block.cloneNode(true) @@ -346,6 +367,7 @@ async function refresh_elements(cross_codes) { }, (error) => { document.getElementById("status-block").classList.remove("hidden"); + // get_updated_server_address(); return error; } ); diff --git a/tunictracker/tracker/static/tracker/data/listen_address.json b/tunictracker/tracker/static/tracker/data/listen_address.json new file mode 100644 index 0000000..192b9c2 --- /dev/null +++ b/tunictracker/tracker/static/tracker/data/listen_address.json @@ -0,0 +1 @@ +{"address": {"default": "http://localhost:8000/spoiler", "user": "http://localhost:8000/spoiler"}} \ No newline at end of file diff --git a/tunictracker/tracker/templates/tracker/address/index.html b/tunictracker/tracker/templates/tracker/address/index.html new file mode 100644 index 0000000..398f3e9 --- /dev/null +++ b/tunictracker/tracker/templates/tracker/address/index.html @@ -0,0 +1,13 @@ +
+ + {% csrf_token %} + + {% for field in form %}{{ field }}{% endfor %} + +
diff --git a/tunictracker/tracker/templates/tracker/index.html b/tunictracker/tracker/templates/tracker/index.html index 71a99f6..48aa444 100644 --- a/tunictracker/tracker/templates/tracker/index.html +++ b/tunictracker/tracker/templates/tracker/index.html @@ -15,6 +15,7 @@  Tunic Transition Tracker
+ {% include "tracker/address/index.html" %} {% include "tracker/status/index.html" %}
diff --git a/tunictracker/tracker/urls.py b/tunictracker/tracker/urls.py index a9d7f56..1579878 100644 --- a/tunictracker/tracker/urls.py +++ b/tunictracker/tracker/urls.py @@ -4,4 +4,5 @@ from . import views urlpatterns = [ path("", views.index, name="index"), + path("set/address/", views.set_address, name="set_address"), ] \ No newline at end of file diff --git a/tunictracker/tracker/views.py b/tunictracker/tracker/views.py index 137fdde..5ecdddd 100644 --- a/tunictracker/tracker/views.py +++ b/tunictracker/tracker/views.py @@ -1,19 +1,28 @@ from django.shortcuts import render -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.template import loader -from json import loads +from json import loads, dumps from math import floor import requests +from .forms import ServerAddressForm # Create your views here. def index(request): request_data = None - server_address = 'http://localhost:8000/spoiler' + + with open('tracker/static/tracker/data/listen_address.json', 'r') as address: + read_address = loads(address.read()) + if (read_address['address']['user'] != ""): + server_address = read_address['address']['user'] + else: + server_address = read_address['address']['default'] + # server_address = 'http://localhost:8000/spoiler' is_hidden = True try: - request_data = requests.get(server_address).text + request_data = requests.get( + server_address, timeout=5, verify=True).text except: with open('empty_spoiler.json', 'r') as t: try: @@ -52,6 +61,8 @@ def index(request): current_cross_codes = {} print(e) template = loader.get_template("tracker/index.html") + form = ServerAddressForm() + form.fields["server_address_form"].initial = server_address context = { "server_address": server_address, "is_hidden": is_hidden, @@ -63,6 +74,24 @@ def index(request): "current_scene": { "title": tracker_current_scene, "data": tracker_current_scene_data - } + }, + "form": form } return HttpResponse(template.render(context, request)) + + +def set_address(request): + if request.method == "POST": + form = ServerAddressForm(request.POST) + form.label_classes = ("p-2") + if form.is_valid(): + with open("tracker/static/tracker/data/listen_address.json", "r") as la: + listen_address = loads(la.read()) + listen_address["address"]["user"] = form.cleaned_data["server_address_form"] + + with open("tracker/static/tracker/data/listen_address.json", "w") as la: + la.write(dumps(listen_address)) + return HttpResponseRedirect("/") + else: + form = ServerAddressForm() + return render(request, "tracker/index.html", {"form": form}) diff --git a/tunictracker/tunictracker/settings.py b/tunictracker/tunictracker/settings.py index b9278c3..8425418 100644 --- a/tunictracker/tunictracker/settings.py +++ b/tunictracker/tunictracker/settings.py @@ -23,9 +23,12 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = 'django-insecure-ae-fwu3z$wexeiac4ggt0l@x1*uq5v9-q&$y#frf85xli)bo8f' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = False -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = [ + "localhost", + "127.0.0.1" +] # Application definition @@ -118,7 +121,13 @@ USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.0/howto/static-files/ -STATIC_URL = 'static/' +STATIC_URL = '/static/' + +STATIC_ROOT = "tracker/static/" + +STATICFILES_DIRS = [ + BASE_DIR / "static/" +] # Default primary key field type # https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field