From dc96a5ff69a6f83a6aaccc6da1bedbccc613633d Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Mon, 19 Dec 2022 15:02:41 +0100 Subject: [PATCH] WIP --- changedetectionio/__init__.py | 15 ++++++++++++++- changedetectionio/forms.py | 9 +++++++++ changedetectionio/importer.py | 21 +++++++++++++++++++++ changedetectionio/templates/import.html | 10 +++++++++- 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/changedetectionio/__init__.py b/changedetectionio/__init__.py index fee7a8bc3c4..a2d938b4649 100644 --- a/changedetectionio/__init__.py +++ b/changedetectionio/__init__.py @@ -755,8 +755,11 @@ def settings_page(): @login_required def import_page(): remaining_urls = [] + from changedetectionio import forms + form = forms.importForm(request.form) + if request.method == 'POST': - from .importer import import_url_list, import_distill_io_json + from .importer import import_url_list, import_distill_io_json, import_changedetection_io_zip # URL List import if request.values.get('urls') and len(request.values.get('urls').strip()): @@ -779,10 +782,20 @@ def import_page(): for uuid in d_importer.new_uuids: update_q.put(queuedWatchMetaData.PrioritizedItem(priority=1, item={'uuid': uuid, 'skip_when_checksum_same': True})) + if request.files.get("backup_zip_file"): + if not form.validate(): + flash("An error occurred, please see below.", "error") + else: + d_importer = import_changedetection_io_zip() + d_importer.run(data=None, flash=flash, datastore=datastore) + for uuid in d_importer.new_uuids: + # Queue without priority, we will examine their own rule to find out if it should be checked + update_q.put(queuedWatchMetaData.PrioritizedItem(item={'uuid': uuid, 'skip_when_checksum_same': True})) # Could be some remaining, or we could be on GET output = render_template("import.html", + form=form, import_url_list_remaining="\n".join(remaining_urls), original_distill_json='' ) diff --git a/changedetectionio/forms.py b/changedetectionio/forms.py index 88c846ae5d3..86f946888f2 100644 --- a/changedetectionio/forms.py +++ b/changedetectionio/forms.py @@ -3,6 +3,7 @@ from wtforms import ( BooleanField, + FileField, Form, IntegerField, RadioField, @@ -425,6 +426,14 @@ def validate(self, **kwargs): result = False return result +# datastore.data['settings']['requests'].. +class importForm(Form): + #backup_zip_file = FileField("File", validators=[validators.regexp('\.zip$', flags=re.IGNORECASE)]) + backup_zip_file = FileField("File") + + def validate_backup_zip_file(form, field): + if field.data: + x=1 # datastore.data['settings']['requests'].. class globalSettingsRequestForm(Form): diff --git a/changedetectionio/importer.py b/changedetectionio/importer.py index 3668b356827..55fc5c03675 100644 --- a/changedetectionio/importer.py +++ b/changedetectionio/importer.py @@ -1,4 +1,5 @@ from abc import ABC, abstractmethod +from flask import request, url_for, current_app import time import validators @@ -20,6 +21,26 @@ def run(self, datastore): pass +class import_changedetection_io_zip(Importer): + + def run(self, + data, + flash, + datastore, + ): + # `data` should be none, we will hit up request directly + + + import zipfile + import io + + + with zipfile.ZipFile(io.BytesIO(request.files["backup_zip_file"].read()), 'r') as zf: + p =zf.namelist() + for file in zf.namelist(): + x=1 + + class import_url_list(Importer): """ diff --git a/changedetectionio/templates/import.html b/changedetectionio/templates/import.html index 951a8afa12c..2bed493f371 100644 --- a/changedetectionio/templates/import.html +++ b/changedetectionio/templates/import.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% block content %} +{% from '_helpers.jinja' import render_field %}
@@ -7,11 +8,12 @@
-
+
@@ -77,6 +79,12 @@ " rows="25">{{ original_distill_json }}
+
+ Upload your changedetection.io backup ZIP here
+
+ {{ render_field(form.backup_zip_file) }} +
+