diff --git a/changedetectionio/blueprint/tags/__init__.py b/changedetectionio/blueprint/tags/__init__.py index e826aea0..65117bf2 100644 --- a/changedetectionio/blueprint/tags/__init__.py +++ b/changedetectionio/blueprint/tags/__init__.py @@ -96,22 +96,41 @@ def construct_blueprint(datastore: ChangeDetectionStore): @tags_blueprint.route("/edit/", methods=['GET']) @login_optionally_required def form_tag_edit(uuid): - from changedetectionio import forms + + from changedetectionio.processors.restock_diff.forms import processor_settings_form if uuid == 'first': uuid = list(datastore.data['settings']['application']['tags'].keys()).pop() default = datastore.data['settings']['application']['tags'].get(uuid) - form = forms.processor_text_json_diff_form(formdata=request.form if request.method == 'POST' else None, - data=default, - ) - form.datastore=datastore # needed? + form = processor_settings_form(formdata=request.form if request.method == 'POST' else None, + data=default, + ) + + template_args = { + 'data': default, + 'form': form, + 'watch': default + } + + included_content = {} + if form.extra_form_content(): + # So that the extra panels can access _helpers.html etc, we set the environment to load from templates/ + # And then render the code from the module + from jinja2 import Environment, FileSystemLoader + import importlib.resources + templates_dir = str(importlib.resources.files("changedetectionio").joinpath('templates')) + env = Environment(loader=FileSystemLoader(templates_dir)) + template = env.from_string(form.extra_form_content()) + included_content = template.render(**template_args) output = render_template("edit-tag.html", - data=default, - form=form, + settings_application=datastore.data['settings']['application'], + extra_tab_content=form.extra_tab_content() if form.extra_tab_content() else None, + extra_form_content=included_content, + **template_args ) return output @@ -120,13 +139,13 @@ def construct_blueprint(datastore: ChangeDetectionStore): @tags_blueprint.route("/edit/", methods=['POST']) @login_optionally_required def form_tag_edit_submit(uuid): - from changedetectionio import forms + from changedetectionio.processors.restock_diff.forms import processor_settings_form if uuid == 'first': uuid = list(datastore.data['settings']['application']['tags'].keys()).pop() default = datastore.data['settings']['application']['tags'].get(uuid) - form = forms.processor_text_json_diff_form(formdata=request.form if request.method == 'POST' else None, + form = processor_settings_form(formdata=request.form if request.method == 'POST' else None, data=default, ) # @todo subclass form so validation works @@ -136,6 +155,7 @@ def construct_blueprint(datastore: ChangeDetectionStore): # return redirect(url_for('tags.form_tag_edit_submit', uuid=uuid)) datastore.data['settings']['application']['tags'][uuid].update(form.data) + datastore.data['settings']['application']['tags'][uuid]['processor'] = 'restock_diff' datastore.needs_write_urgent = True flash("Updated") diff --git a/changedetectionio/blueprint/tags/templates/edit-tag.html b/changedetectionio/blueprint/tags/templates/edit-tag.html index 9f316c55..7e84fc1e 100644 --- a/changedetectionio/blueprint/tags/templates/edit-tag.html +++ b/changedetectionio/blueprint/tags/templates/edit-tag.html @@ -26,6 +26,9 @@ @@ -97,6 +100,12 @@ nav + {# rendered sub Template #} + {% if extra_form_content %} +
+ {{ extra_form_content|safe }} +
+ {% endif %}
diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index f07bbfcb..eae4f85e 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -1550,7 +1550,7 @@ def changedetection_app(config=None, datastore_o=None): if datastore.data['watching'].get(uuid): datastore.data['watching'][uuid]['tags'].append(tag_uuid) - flash("{} watches assigned tag".format(len(uuids))) + flash(f"{len(uuids)} watches were tagged") return redirect(url_for('index')) diff --git a/changedetectionio/store.py b/changedetectionio/store.py index c9c7ad10..d42e7503 100644 --- a/changedetectionio/store.py +++ b/changedetectionio/store.py @@ -83,16 +83,14 @@ class ChangeDetectionStore: self.__data['settings']['application'].update(from_disk['settings']['application']) # Convert each existing watch back to the Watch.model object - for uuid, watch in self.__data['watching'].items(): - watch['uuid'] = uuid - watch_class = get_custom_watch_obj_for_processor(watch.get('processor')) - if watch.get('uuid') != 'text_json_diff': - logger.trace(f"Loading Watch object '{watch_class.__module__}.{watch_class.__name__}' for UUID {uuid}") - - self.__data['watching'][uuid] = watch_class(datastore_path=self.datastore_path, default=watch) + self.__data['watching'][uuid] = self.rehydrate_entity(uuid, watch) + logger.info(f"Watching: {uuid} {watch['url']}") - logger.info(f"Watching: {uuid} {self.__data['watching'][uuid]['url']}") + # And for Tags also, should be Restock type because it has extra settings + for uuid, tag in self.__data['settings']['application']['tags'].items(): + self.__data['settings']['application']['tags'][uuid] = self.rehydrate_entity(uuid, tag, processor_override='restock_diff') + logger.info(f"Tag: {uuid} {tag['title']}") # First time ran, Create the datastore. except (FileNotFoundError): @@ -147,6 +145,22 @@ class ChangeDetectionStore: # Finally start the thread that will manage periodic data saves to JSON save_data_thread = threading.Thread(target=self.save_datastore).start() + def rehydrate_entity(self, uuid, entity, processor_override=None): + """Set the dict back to the dict Watch object""" + entity['uuid'] = uuid + + if processor_override: + watch_class = get_custom_watch_obj_for_processor(processor_override) + entity['processor']=processor_override + else: + watch_class = get_custom_watch_obj_for_processor(entity.get('processor')) + + if entity.get('uuid') != 'text_json_diff': + logger.trace(f"Loading Watch object '{watch_class.__module__}.{watch_class.__name__}' for UUID {uuid}") + + entity = watch_class(datastore_path=self.datastore_path, default=entity) + return entity + def set_last_viewed(self, uuid, timestamp): logger.debug(f"Setting watch UUID: {uuid} last viewed to {int(timestamp)}") self.data['watching'][uuid].update({'last_viewed': int(timestamp)}) @@ -185,6 +199,9 @@ class ChangeDetectionStore: @property def has_unviewed(self): + if not self.__data.get('watching'): + return None + for uuid, watch in self.__data['watching'].items(): if watch.history_n >= 2 and watch.viewed == False: return True