Re #86 - Refactor scrub date limit code

pull/89/head
dgtlmoon 4 years ago
parent 17830de489
commit bae6641777

@ -267,29 +267,53 @@ def changedetection_app(conig=None, datastore_o=None):
@app.route("/scrub", methods=['GET', 'POST'])
@login_required
def scrub_page():
from pathlib import Path
global messages
import re
if request.method == 'POST':
confirmtext = request.form.get('confirmtext')
limit_timestamp = int(request.form.get('limit_date'))
limit_date = request.form.get('limit_date')
if confirmtext == 'scrub':
try:
limit_date = limit_date.replace('T', ' ')
# I noticed chrome will show '/' but actually submit '-'
limit_date = limit_date.replace('-', '/')
# In the case that :ss seconds are supplied
limit_date = re.sub('(\d\d:\d\d)(:\d\d)', '\\1', limit_date)
str_to_dt = datetime.datetime.strptime(limit_date, '%Y/%m/%d %H:%M')
limit_timestamp = int(str_to_dt.timestamp())
if limit_timestamp > time.time():
messages.append({'class': 'error',
'message': "Timestamp is in the future, cannot continue."})
return redirect(url_for('scrub_page'))
except ValueError:
messages.append({'class': 'ok', 'message': 'Incorrect date format, cannot continue.'})
return redirect(url_for('scrub_page'))
if confirmtext == 'scrub':
changes_removed = 0
for uuid, watch in datastore.data['watching'].items():
if len(str(limit_timestamp)) == 10:
datastore.scrub_watch(uuid, limit_timestamp = limit_timestamp)
if limit_timestamp:
changes_removed += datastore.scrub_watch(uuid, limit_timestamp=limit_timestamp)
else:
datastore.scrub_watch(uuid)
changes_removed += datastore.scrub_watch(uuid)
messages.append({'class': 'ok', 'message': 'Cleaned all version history.'})
messages.append({'class': 'ok',
'message': "Cleared snapshot history ({} snapshots removed)".format(
changes_removed)})
else:
messages.append({'class': 'error', 'message': 'Wrong confirm text.'})
messages.append({'class': 'error', 'message': 'Incorrect confirmation text.'})
return redirect(url_for('index'))
return render_template("scrub.html")
output = render_template("scrub.html", messages=messages)
messages = []
return output
# If they edited an existing watch, we need to know to reset the current/previous md5 to include
# the excluded text.

@ -241,18 +241,20 @@ class ChangeDetectionStore:
import hashlib
del_timestamps = []
changes_removed = 0
for timestamp, path in self.data['watching'][uuid]['history'].items():
if not limit_timestamp or (limit_timestamp is not False and int(timestamp) > limit_timestamp):
self.unlink_history_file(path)
del_timestamps.append(timestamp)
changes_removed += 1
if not limit_timestamp:
self.data['watching'][uuid]['last_checked'] = 0
self.data['watching'][uuid]['last_changed'] = 0
self.data['watching'][uuid]['previous_md5'] = 0
for timestamp in del_timestamps:
del self.data['watching'][uuid]['history'][str(timestamp)]
@ -272,9 +274,8 @@ class ChangeDetectionStore:
self.data['watching'][uuid]['previous_md5'] = False
pass
self.needs_write = True
return changes_removed
def add_watch(self, url, tag):
with self.lock:

@ -75,7 +75,6 @@
</div>
{% endif %}
{% block content %}
{% endblock %}

@ -2,34 +2,25 @@
{% block content %}
<div class="edit-form">
<form class="pure-form pure-form-stacked" action="/scrub" method="POST">
<fieldset>
<div class="pure-control-group">
This will remove all version snapshots/data, but keep your list of URLs. <br/>
You may like to use the <strong>BACKUP</strong> link first.<br/>
Type in the word <strong>scrub</strong> to confirm that you understand!
<br/>
</div>
<div class="pure-control-group">
<br/>
<label for="confirmtext">Confirm text</label><br/>
<div class="pure-control-group">
<label for="confirmtext">Confirmation text</label>
<input type="text" id="confirmtext" required="" name="confirmtext" value="" size="10"/>
<span class="pure-form-message-inline">Type in the word <strong>scrub</strong> to confirm that you understand!</span>
</div>
<div class="pure-control-group">
<br/>
<label for="confirmtext">Limit delete history including and after date</label><br/>
<input type="text" id="limit_date" required="" name="limit_date" value="" size="10"/>
<br/>
<div class="pure-control-group">
<label for="confirmtext">Optional: Limit deletion of of snapshots to snapshots <i>newer</i> than date/time</label>
<input type="datetime-local" id="limit_date" name="limit_date" />
<span class="pure-form-message-inline">dd/mm/yyyy hh:mm (24 hour format)</span>
</div>
<br/>
<div class="pure-control-group">
<button type="submit" class="pure-button pure-button-primary">Scrub!</button>
</div>
@ -37,12 +28,8 @@
<div class="pure-control-group">
<a href="/" class="pure-button button-small button-cancel">Cancel</a>
</div>
</fieldset>
</form>
</div>
{% endblock %}

@ -52,7 +52,7 @@ SMTPS - mailtos://user:pass@mail.domain.com?to=receivingAddress@example.com
<div class="pure-control-group">
<a href="/" class="pure-button button-small button-cancel">Back</a>
<a href="/scrub" class="pure-button button-small button-cancel">Delete history version data</a>
<a href="/scrub" class="pure-button button-small button-cancel">Delete History Snapshot Data</a>
</div>

Loading…
Cancel
Save