test restore

This commit is contained in:
q
2016-07-04 15:17:36 +03:00
parent 1336f8e3cd
commit 488d0d8425
4 changed files with 32 additions and 3 deletions

26
shop.py
View File

@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# all the imports
import sqlite3, time, datetime, hashlib, os,re
from shutil import copyfile
from shutil import copyfile, move
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from revprox import ReverseProxied
@@ -200,6 +200,28 @@ def edit_md():
flash('Saved new file.')
return redirect(url_for('show_shop',shopid=shopid))
@app.route('/restore', methods=['POST'])
def restore_md():
if not session.get('logged_in'):
abort(401)
shopid=int(request.form['shopid'])
ownerid=g.db.execute('select owner from shops where id=?',request.form['shopid']).fetchall()[0][0]
shopname=g.db.execute('select shop from shops where id=?',request.form['shopid']).fetchall()[0][0]
ownername=get_username(ownerid)
data_dir=os.path.join(DATADIR, ownername)
data_file=os.path.join(data_dir, shopname+".md")
backup=data_file+".bkp"
backup_tmp=data_file+".tmp"
if not os.path.exists(backup):
flash('Backup does not exist')
return redirect(url_for('show_shop',shopid=shopid))
copyfile(data_file,backup_tmp)
copyfile(backup, data_file)
move(backup_tmp, backup)
flash('Backup restored')
return redirect(url_for('show_shop',shopid=shopid))
@app.route('/toggle', methods=['POST'])
def toggle_item():
@@ -247,6 +269,7 @@ def remove_toggled():
ownername=get_username(ownerid)
data_dir=os.path.join(DATADIR, ownername)
data_file=os.path.join(data_dir, shopname+".md")
backup=data_file+".bkp"
contents_file=open(data_file,'rt')
contents=[]
changed=False
@@ -257,6 +280,7 @@ def remove_toggled():
changed=True
contents_file.close()
if changed:
copyfile(data_file, backup)
contents_file=open(data_file,'wt')
contents_file.write("\n".join(contents).encode('utf-8'))
contents_file.close()

View File

@@ -17,7 +17,7 @@ td { height: 1.75em; }
.add-entry { font-size: 0.9em; }
.add-entry dl { font-weight: bold; }
.metanav { text-align: left; font-size: 1.2em;
background: #fafafa; }
background: #fafafa; font-weight: bold; }
.flash { background: #cee5F5; padding: 0.5em;
border: 1px solid #aacbe2; }
.error { background: #f0d6d6; padding: 0.5em; }

View File

@@ -14,7 +14,7 @@
<body>
<div class=page>
<div class=metanav>
<a href="{{ url_for('list_shops') }}">Shops</a>
<a href="{{ url_for('list_shops') }}" id="shopsList">Shops</a>
<div class="dropdown">
<p onclick="dropDown()" class="dropbtn">&sube;&supe;</p>
<div id="Dropdown" class="dropdown-content">

View File

@@ -44,6 +44,11 @@
</dl>
</form>
</div>
<h2 id="h2_restore" onclick="hidetoggle('disp_restore')" class=pointer>Restore backup:</h2>
<form id="disp_restore" class=hidden action="{{ url_for('restore_md') }}" method=post class=add-entry>
<dl>Shop state is backed up with "Edit items" and "Remove ticked" actions.</dl>
<dl><input type=hidden name=shopid value={{ shopid }}><input class=submit type=submit value=Restore onclick="return confirm('Do you really want to restore previous version?');">
</dl></form>
<h2 id="h2_delete" onclick="hidetoggle('disp_delete')" class=pointer>Delete shop:</h2>
<form id="disp_delete" class=hidden action="{{ url_for('remove_shop') }}" method=post class=add-entry>
<dl><input type=hidden name=shopid value={{ shopid }}><input class=submit type=submit value=Remove onclick="return confirm('Do you really want to remove shop {{ shop }}?');">