diff --git a/uwume/lib/classUser.py b/uwume/lib/classUser.py index 0e5bef0..061c2e7 100644 --- a/uwume/lib/classUser.py +++ b/uwume/lib/classUser.py @@ -1,16 +1,21 @@ +try: + from classUwuCounter import UwuCounter +except: + from .classUwuCounter import UwuCounter from flask import Flask, Response from flask_login import LoginManager, UserMixin, login_required class User(UserMixin): # proxy for a database of users - user_database = {"JohnDoe": ("JohnDoe", "John"), - "JaneDoe": ("JaneDoe", "Jane"), - "admin": ("admin", "admin")} + user_database = {"JohnDoe": ("JohnDoe", "John", UwuCounter(0)), + "JaneDoe": ("JaneDoe", "Jane", UwuCounter(10)), + "admin": ("admin", "admin", UwuCounter(100))} - def __init__(self, username, password): + def __init__(self, username, password, initialCount=UwuCounter(0)): self.id = username self.password = password + self.count = UwuCounter(initialCount) @classmethod def get(cls, id): diff --git a/uwume/lib/classUwuCounter.py b/uwume/lib/classUwuCounter.py new file mode 100644 index 0000000..3288106 --- /dev/null +++ b/uwume/lib/classUwuCounter.py @@ -0,0 +1,21 @@ +#!/bin/python +from threading import Lock + +class UwuCounter: + ''' + Class defined to be a threadsafe Atomic Counter for UwUs. + + Original concept and code for this idea was borrowed from + a GitHub Gist by user benhoyt + https://gist.github.com/benhoyt/8c8a8d62debe8e5aa5340373f9c509c7 + ''' + + def __init__(self, initial=0): + super().__init__() + self.curval = initial + self._lock = Lock() + + def increment(self, num=1): + with self._lock: + self.curval += num + return self.curval \ No newline at end of file diff --git a/uwume/static/js/home/index.js b/uwume/static/js/home/index.js index e69de29..1658729 100644 --- a/uwume/static/js/home/index.js +++ b/uwume/static/js/home/index.js @@ -0,0 +1,27 @@ +const getCurval = function() { + const xhttp = new XMLHttpRequest(); + xhttp.withCredentials = true; + xhttp.onreadystatechange = updateUwuCounter; + xhttp.open("GET", window.location.href + "/getCurval", true); + xhttp.send(); +}; + +const updateUwuCounter = function() { + const uwu_counter = document.getElementById("uwu-counter"); + if (!this.response) { + uwu_counter.textContent = uwu_counter.textContent; + } else { + try { + uwu_counter.textContent = this.response; + } catch (error) { + console.log(error); + console.log(response); + } + } +}; + +window.addEventListener("load", function() { + console.log("View specific JS initialized."); + this.setInterval(getCurval, 5000); +}); + \ No newline at end of file diff --git a/uwume/static/js/user/index.js b/uwume/static/js/user/index.js index e69de29..5a3a32f 100644 --- a/uwume/static/js/user/index.js +++ b/uwume/static/js/user/index.js @@ -0,0 +1,31 @@ +const getCurval = function() { + const xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = updateUwuCounter; + xhttp.open("GET", window.location.href + "/getCurval", true); + xhttp.send(); +}; + +const incCurval = function() { + console.log("TEst"); + const xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = updateUwuCounter; + xhttp.open("POST", window.location.href, true); + xhttp.send(); + console.log("anotehrone"); +}; + +const updateUwuCounter = function() { + const uwu_counter = document.getElementById("uwu-counter"); + if (!this.response) { + uwu_counter.textContent = uwu_counter.textContent; + } else { + uwu_counter.textContent = this.response; + } +}; + +window.addEventListener("load", function() { + console.log("View specific JS initialized."); + uwu_button = this.document.getElementById("uwu-button"); + uwu_button.onclick = incCurval; + this.setInterval(getCurval, 5000); +}); diff --git a/uwume/templates/home/index.html b/uwume/templates/home/index.html index da53915..84105f7 100644 --- a/uwume/templates/home/index.html +++ b/uwume/templates/home/index.html @@ -6,6 +6,13 @@ Hello, {{ current_user.id }} {% endblock %} {% block content %} +
@@ -16,8 +23,8 @@
{{ item }}
diff --git a/uwume/views.py b/uwume/views.py
index 2d5d079..ab4d9c7 100644
--- a/uwume/views.py
+++ b/uwume/views.py
@@ -37,18 +37,50 @@ def load_user(request):
return User(request, User.user_database[request][1])
-@app.route('/user/