Просмотр исходного кода

Set update routes to check for differences, and return True or False to the templates

deadtom 1 месяц назад
Родитель
Сommit
56484098cc
1 измененных файлов с 45 добавлено и 14 удалено
  1. 45 14
      ownchatbot/web_panels.py

+ 45 - 14
ownchatbot/web_panels.py

@@ -1,4 +1,4 @@
-from flask import flash, render_template, Blueprint, current_app, redirect, request, url_for, session, g, send_from_directory
+from flask import flash, render_template, Blueprint, current_app, redirect, request, url_for, session, g, send_from_directory, jsonify
 from datetime import timezone
 from ownchatbot.db import get_db, reread_goals, reread_votes, rem_vote, reset_vote, reset_goal, clear_fulfilled_rewards, clear_reward_queue, rem_cool, rem_from_queue
 from ownchatbot.reward_handlers import all_active_votes, all_active_goals, all_active_rewards, get_queue, fulfill_reward, save_rewards, activate_category, deactivate_category, refund_reward, reread_categories, save_config, save_alerts, del_alert_file
@@ -13,11 +13,14 @@ import pkce
 import requests
 from functools import wraps
 import os
+import copy
 
 ocb = Blueprint('web_panels', __name__)
 
 state_value = ''
-
+current_goals = {}
+current_votes = {}
+current_todos = {}
 
 def requires_login(f):
     @wraps(f)
@@ -676,43 +679,71 @@ def ocb_alert(alert_type):
                                alert_name=alert_name)
 
 
-@ocb.route('/goals')  # Goals overlay
+@ocb.route('/goals', methods=['GET'])  # Goals overlay
 def goals():
     db = get_db()
+    goals = all_active_goals(db)
+    global current_goals
+    current_goals = goals
+    rewards = all_active_rewards()
     return render_template('goals.html',
-                           goals=all_active_goals(db),
-                           rewards=all_active_rewards())
+                           goals=goals,
+                           rewards=rewards)
 
 
 @ocb.route('/updateGoals', methods=['GET'])  # Polled by goals.html for updated goals
 def update_goals():
     db = get_db()
-    goals_data = all_active_goals(db)
-    return jsonify({'goals': [{'name': goal[3], 'current': goal[1], 'target': goal[2]} for goal in goals_data]})
+    goals = all_active_goals(db)
+    global current_goals
+    if current_goals == goals:
+        return jsonify({"updated": False})
+    else:
+        current_goals = goals
+        return jsonify({"updated": True})
 
 
-@ocb.route('/votes')  # Votes overlay
+@ocb.route('/votes', methods=['GET'])  # Votes overlay
 def votes():
     db = get_db()
+    votes = all_active_votes(db)
+    global current_votes
+    current_votes = votes
     return render_template('votes.html',
-                           votes=all_active_votes(db))
+                           votes=votes)
 
 
 @ocb.route('/updateVotes', methods=['GET'])  # Polled by votes.html for updated votes
 def update_votes():
     db = get_db()
-    votes_data = all_active_votes(db)
-    return jsonify({'votes': [{'item': vote[2], 'count': vote[1]} for vote in votes_data]})
+    global current_votes
+    votes = all_active_votes(db)
+    if current_votes == votes:
+        return jsonify({"updated": False})
+    else:
+        current_votes = votes
+        return jsonify({"updated": True})
 
 
-@ocb.route('/todo')  # Todo overlay
+@ocb.route('/todo', methods=['GET'])  # Todo overlay
 def todo():
+    global current_todos
     todolist_items = current_app.config['LIST']
+    current_todos = copy.deepcopy(todolist_items)  # deepcopy because current_todos was getting updated with LIST
+    current_app.logger.info(f'/todo: {current_todos}')  # TESTING
     return render_template('list.html', items=todolist_items)
 
 
-@ocb.route('/checkTodo')  # Polled by list.html to check for changes in the to-do list
+@ocb.route('/updateTodo', methods=['GET'])  # Polled by list.html to check for changes in the to-do list
 def check_todo():
+    global current_todos
     todolist_items = current_app.config['LIST']
-    return jsonify({'items': [{'name': item.name, 'crossed': item.crossed} for item in todolist_items]})
+    current_app.logger.info(f'\n/updateTodo:\n{current_todos}\n{todolist_items}')  # TESTING
+    if current_todos == todolist_items:
+        current_app.logger.info('FALSE')  # TESTING
+        return jsonify({"updated": False})
+    else:
+        current_todos = copy.deepcopy(todolist_items)  # deepcopy because current_todos was getting updated with LIST
+        current_app.logger.info('TRUE')  # TESTING
+        return jsonify({"updated": True})