From 4505819546512aa65d05263c51da6d0bdc4c55dd Mon Sep 17 00:00:00 2001 From: Alexander Huddleston Date: Sat, 7 Mar 2020 19:12:24 -0600 Subject: [PATCH] Asynchronous counter implemented. Home page updated to show current value periodically. --- uwume/lib/classUser.py | 13 +++++++---- uwume/lib/classUwuCounter.py | 21 ++++++++++++++++++ uwume/static/js/home/index.js | 27 +++++++++++++++++++++++ uwume/static/js/user/index.js | 31 ++++++++++++++++++++++++++ uwume/templates/home/index.html | 11 ++++++++-- uwume/templates/user/index.html | 22 ++++++++++++++----- uwume/views.py | 39 ++++++++++++++++++++++++++++++--- 7 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 uwume/lib/classUwuCounter.py 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 %} +
+
+

+ {{ user_curval }} +

+
+
{% for item in content_text %}

@@ -16,8 +23,8 @@

{% endblock %} diff --git a/uwume/templates/user/index.html b/uwume/templates/user/index.html index d474e48..435694b 100644 --- a/uwume/templates/user/index.html +++ b/uwume/templates/user/index.html @@ -7,11 +7,23 @@ {% endblock %} {% block content %}
-
- -
+
+

+ {{ user_curval }} +

+
+
+
+ +
+
+
{% for item in content_text %}

{{ 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/', methods=['GET']) +@app.route('/user//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/', 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('/')