Asynchronous counter implemented. Home page updated to show current value periodically.
This commit is contained in:
parent
abe474487a
commit
4505819546
7 changed files with 150 additions and 14 deletions
|
@ -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):
|
||||
|
|
21
uwume/lib/classUwuCounter.py
Normal file
21
uwume/lib/classUwuCounter.py
Normal file
|
@ -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
|
|
@ -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);
|
||||
});
|
||||
|
|
@ -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);
|
||||
});
|
|
@ -6,6 +6,13 @@
|
|||
Hello, {{ current_user.id }}
|
||||
</h5>
|
||||
{% endblock %} {% block content %}
|
||||
<div class="card-panel grey darken-3">
|
||||
<div class="center">
|
||||
<h2 class="blue-grey-text text-lighten-5" id="uwu-counter">
|
||||
{{ user_curval }}
|
||||
</h2>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-panel grey darken-3">
|
||||
{% for item in content_text %}
|
||||
<p class="blue-grey-text text-lighten-5">
|
||||
|
@ -16,8 +23,8 @@
|
|||
<div class="card-panel grey darken-3">
|
||||
<div class="center">
|
||||
<a class="waves-effect waves-light btn blue darken-2" href="/logout">
|
||||
Logout
|
||||
Logout
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
|
@ -7,11 +7,23 @@
|
|||
</h5>
|
||||
{% endblock %} {% block content %}
|
||||
<div class="card-panel grey darken-3">
|
||||
<div class="center">
|
||||
<button class="waves-effect waves-light btn blue darken-2" href="/">
|
||||
UwU
|
||||
</button>
|
||||
</div>
|
||||
<div class="center">
|
||||
<h2 class="blue-grey-text text-lighten-5" id="uwu-counter">
|
||||
{{ user_curval }}
|
||||
</h2>
|
||||
</div>
|
||||
<div class="row"></div>
|
||||
<div class="center">
|
||||
<button
|
||||
class="waves-effect waves-light btn blue darken-2"
|
||||
href="/"
|
||||
id="uwu-button"
|
||||
>
|
||||
UwU
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-panel grey darken-3">
|
||||
{% for item in content_text %}
|
||||
<p class="blue-grey-text text-lighten-5">
|
||||
{{ item }}
|
||||
|
|
|
@ -37,18 +37,50 @@ def load_user(request):
|
|||
return User(request, User.user_database[request][1])
|
||||
|
||||
|
||||
@app.route('/user/<username>', methods=['GET'])
|
||||
@app.route('/user/<username>/getCurval', methods=['GET'])
|
||||
def get_curval(username):
|
||||
error = ''
|
||||
if(request.method == 'GET'):
|
||||
try:
|
||||
return f'{User.user_database[username][2].curval}'
|
||||
except Exception as e:
|
||||
error = f'{e}'
|
||||
return f'ERROR: {error}'
|
||||
|
||||
|
||||
@app.route('/user/<username>', methods=['POST', 'GET'])
|
||||
def user_page(username):
|
||||
error = ''
|
||||
if(request.method == 'GET'):
|
||||
return render_template('user/index.html', this_user=username, static_paths=get_static_paths(), content_text=get_content_text())
|
||||
if(username in User.user_database.keys()):
|
||||
return render_template('user/index.html', this_user=username, user_curval=str(User.user_database[username][2].curval), static_paths=get_static_paths(), content_text=get_content_text())
|
||||
else:
|
||||
error = 'User doesn\'t exist.'
|
||||
elif(request.method == 'POST'):
|
||||
try:
|
||||
User.user_database[username][2].increment()
|
||||
print('test')
|
||||
return f'{User.user_database[username][2].curval}'
|
||||
except Exception as e:
|
||||
error = f'{e}'
|
||||
return f'ERROR: {error}'
|
||||
|
||||
@app.route('/home/getCurval', methods=['GET'])
|
||||
@login_required
|
||||
def home_get_curval():
|
||||
error = ''
|
||||
if(request.method == 'GET'):
|
||||
try:
|
||||
return f'{User.user_database[current_user.id][2].curval}'
|
||||
except Exception as e:
|
||||
error = f'{e}'
|
||||
return f'ERROR: {error}'
|
||||
|
||||
|
||||
@app.route('/home', methods=['GET'])
|
||||
@login_required
|
||||
def home():
|
||||
return render_template('home/index.html', static_paths=get_static_paths(), content_text=get_content_text())
|
||||
return render_template('home/index.html', user_curval=str(User.user_database[current_user.id][2].curval), static_paths=get_static_paths(), content_text=get_content_text())
|
||||
|
||||
|
||||
@app.route('/signup', methods=['GET', 'POST'])
|
||||
|
@ -58,6 +90,7 @@ def signup():
|
|||
username = request.form['username']
|
||||
password = request.form['password']
|
||||
if(not username in User.user_database.keys()):
|
||||
|
||||
userClass = User(username, password)
|
||||
User.user_database[username] = (username, password)
|
||||
return redirect('/')
|
||||
|
|
Reference in a new issue