diff --git a/backend/backend.py b/backend/backend.py index ce398c6a..88e18968 100644 --- a/backend/backend.py +++ b/backend/backend.py @@ -121,6 +121,30 @@ def edit_page(): output = render_template("edit.html", uuid=uuid, watch=datastore.data['watching'][uuid], messages=messages) return output + +@app.route("/settings", methods=['GET', "POST"]) +def settings_page(): + global messages + if request.method == 'POST': + try: + minutes = int(request.values.get('minutes').strip()) + except ValueError: + messages.append({'class': 'error', 'message': "Invalid value given, use an integer."}) + + else: + if minutes >= 5 and minutes <= 600: + datastore.data['settings']['requests']['minutes_between_check'] = minutes + datastore.needs_write = True + + messages.append({'class': 'ok', 'message': "Updated"}) + else: + messages.append({'class': 'error', 'message': "Must be equal to or greater than 5 and less than 600 minutes"}) + + output = render_template("settings.html", messages=messages, minutes=datastore.data['settings']['requests']['minutes_between_check']) + messages =[] + + return output + @app.route("/import", methods=['GET', "POST"]) def import_page(): import validators @@ -275,8 +299,12 @@ def launch_checks(): import fetch_site_status global running_update_threads - for uuid,watch in datastore.data['watching'].items(): - if watch['last_checked'] <= time.time() - 3 * 60 * 60: + + minutes = datastore.data['settings']['requests']['minutes_between_check'] + for uuid, watch in datastore.data['watching'].items(): + + + if watch['last_checked'] <= time.time() - (minutes * 60): running_update_threads[watch['uuid']] = fetch_site_status.perform_site_check(uuid=uuid, datastore=datastore) running_update_threads[watch['uuid']].start() diff --git a/backend/store.py b/backend/store.py index 5dc79422..2702602f 100644 --- a/backend/store.py +++ b/backend/store.py @@ -23,7 +23,7 @@ class ChangeDetectionStore: }, 'requests': { 'timeout': 15, # Default 15 seconds - 'max_seconds_from_last_check': 3 * 60 * 60 # Default 3 hours + 'minutes_between_check': 3 * 60 # Default 3 hours } } } @@ -47,7 +47,18 @@ class ChangeDetectionStore: with open('/datastore/url-watches.json') as json_file: from_disk = json.load(json_file) - self.__data.update(from_disk) + # @todo isnt there a way todo this dict.update recursively? + # Problem here is if the one on the disk is missing a sub-struct, it wont be present anymore. + if 'watching' in from_disk: + self.__data['watching'].update(from_disk['watching']) + + if 'settings' in from_disk: + if 'headers' in from_disk['settings']: + self.__data['settings']['headers'].update(from_disk['settings']['headers']) + + if 'requests' in from_disk['settings']: + self.__data['settings']['requests'].update(from_disk['settings']['requests']) + # Reinitialise each `watching` with our generic_definition in the case that we add a new var in the future. # @todo pretty sure theres a python we todo this with an abstracted(?) object! diff --git a/backend/templates/settings.html b/backend/templates/settings.html index 54ebaeb4..cc7008dd 100644 --- a/backend/templates/settings.html +++ b/backend/templates/settings.html @@ -4,34 +4,16 @@