diff --git a/.gitignore b/.gitignore index 4abad92d..ef0e2d73 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .idea *.pyc +datastore/url-watches.json diff --git a/backend/backend.py b/backend/backend.py index 18dab7b9..898c0b76 100644 --- a/backend/backend.py +++ b/backend/backend.py @@ -9,14 +9,13 @@ import os import getopt import sys import datetime -from flask import Flask, render_template, request, send_file, send_from_directory, safe_join, abort +from flask import Flask, render_template, request, send_file, send_from_directory, safe_join, abort, redirect, url_for # Local import store - datastore = store.ChangeDetectionStore() - +messages = [] app = Flask(__name__, static_url_path='/static') app.config['STATIC_RESOURCES'] = "/app/static" @@ -28,7 +27,12 @@ app.config['TEMPLATES_AUTO_RELOAD'] = True @app.route("/", methods=['GET']) def main_page(): - return render_template("watch-overview.html", watches=datastore.data['watching']) + global messages + + # Show messages but once. + output = render_template("watch-overview.html", watches=datastore.data['watching'], messages=messages) + messages = [] + return output @app.route("/static//", methods=['GET']) @@ -39,6 +43,19 @@ def static_content(group, filename): abort(404) +@app.route("/api/add", methods=['POST']) +def api_watch_add(): + global messages + + #@todo add_watch should throw a custom Exception for validation etc + datastore.add_watch(url=request.form.get('url'), tag=request.form.get('tag')) + messages.append({'class':'ok', 'message': 'Saved'}) + + return redirect(url_for('main_page')) + # datastore.add_watch + + + def main(argv): ssl_mode = False diff --git a/backend/requirements.txt b/backend/requirements.txt index d902f050..9d50a5f5 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -6,19 +6,13 @@ python-engineio six==1.10.0 yarl flask -gevent-websocket + eventlet requests +validators -# Actual connecting to services -pytz -phpserialize==1.3.0 -redis>=2.6.2 -pymysql==0.8 bleach==3.2.1 html5lib==0.9999999 # via bleach -flask_socketio~=5.0 - # @notes # - Dont install socketio, it interferes with flask_socketio diff --git a/backend/static/css/styles.css b/backend/static/css/styles.css index ac6320f9..381bbd02 100644 --- a/backend/static/css/styles.css +++ b/backend/static/css/styles.css @@ -17,7 +17,9 @@ a.github-link { } section.content { + padding-top: 3em; +flex-direction: column; display: flex; align-items: center; justify-content: center; diff --git a/backend/store.py b/backend/store.py index b2ed0237..5ff89424 100644 --- a/backend/store.py +++ b/backend/store.py @@ -1,8 +1,10 @@ import json import uuid +import validators + # Is there an existing library to ensure some data store (JSON etc) is in sync with CRUD methods? # Open a github issue if you know something :) - +# https://stackoverflow.com/questions/6190468/how-to-trigger-function-on-value-change class ChangeDetectionStore: def __init__(self): @@ -22,6 +24,7 @@ class ChangeDetectionStore: self.data['watching'].append({ 'url': 'https://changedetection.io', 'tag': 'general', + 'last_checked': 0, 'uuid': str(uuid.uuid4()) }) @@ -29,6 +32,16 @@ class ChangeDetectionStore: json.dump(self.data, json_file) + def add_watch(self, url, tag): + validators.url(url) + + self.data['watching'].append({ + 'url': url, + 'tag': tag, + 'uuid': str(uuid.uuid4()) + }) + self.sync_to_json() + # @todo throw custom exception def sync_to_json(self): with open('/datastore/url-watches.json', 'w') as json_file: diff --git a/backend/templates/base.html b/backend/templates/base.html index 3c231127..79a0ba1b 100644 --- a/backend/templates/base.html +++ b/backend/templates/base.html @@ -34,12 +34,17 @@
{% block header %}{% endblock %}
- - {% block content %}{% endblock %} + +
+ {% for message in messages %} +
{{ message['message'] }}
+ {% endfor %} +
+ + + {% block content %} + + {% endblock %} diff --git a/backend/templates/watch-overview.html b/backend/templates/watch-overview.html index 1b9d7ac2..ff71239c 100644 --- a/backend/templates/watch-overview.html +++ b/backend/templates/watch-overview.html @@ -3,16 +3,16 @@ {% block content %}
-
+
Add new change detection watch - - + +
- +
@@ -26,8 +26,8 @@ {% for watch in watches %} - - + + diff --git a/datastore/url-watches.json b/datastore/url-watches.json deleted file mode 100644 index 9551a880..00000000 --- a/datastore/url-watches.json +++ /dev/null @@ -1 +0,0 @@ -{"watching": [{"url": "https://changedetection.io", "tag": "general", "uuid": "3ba44e22-af8b-4dfd-8227-f9be55bd5788"}]} \ No newline at end of file
#
1
{{ loop.index }} {{ watch.url }} 2021/2/2 14:00:00 No Change