export data
This commit is contained in:
parent
3e92c644fd
commit
bdcef0911d
@ -144,8 +144,6 @@ def changeListContent():
|
||||
except:
|
||||
return str('error changing content'), 400
|
||||
|
||||
|
||||
|
||||
@app.route('/app/datahistory',methods = ['GET'])
|
||||
def data_history_request():
|
||||
if request.method == 'GET':
|
||||
@ -191,6 +189,9 @@ def data_list_request():
|
||||
details = get_list(list_uuid)
|
||||
return jsonify(details), 200
|
||||
|
||||
@app.route('/app/data-all', methods = ['GET'])
|
||||
def data_request_all():
|
||||
return jsonify(fetch_all()), 200
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug = True)
|
||||
|
14
src/db.py
14
src/db.py
@ -369,6 +369,20 @@ def change_list_description(uuid, description):
|
||||
connection.commit()
|
||||
connection.close()
|
||||
|
||||
def fetch_all():
|
||||
tables = ["item", "itemlist", "listcontent", "history"]
|
||||
results = []
|
||||
connection = connect_db()
|
||||
cursor = connection.cursor()
|
||||
for table in tables:
|
||||
query = f'SELECT * FROM {str(table)};'
|
||||
cursor.execute(query)
|
||||
results.append(cursor.fetchall())
|
||||
cursor.close()
|
||||
connection.commit()
|
||||
connection.close()
|
||||
return results
|
||||
|
||||
def export_csv():
|
||||
'''join item and history data from database and export it in ./output.csv'''
|
||||
connection = connect_db()
|
||||
|
@ -39,6 +39,9 @@
|
||||
<div class="btn-group mr-2" role="group" aria-label="Toggle show group">
|
||||
<button id="global_toggleshow" class="btn btn-secondary" type="button">show hidden</button>
|
||||
</div>
|
||||
<div class="btn-group mr-2" role="group" aria-label="export">
|
||||
<button id="export_data_button" class="btn btn-secondary" type="button">export</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -112,4 +115,5 @@
|
||||
<script src="{{ url_for('static', filename='popups.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='rendergraphs.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='list.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='csv.js') }}"></script>
|
||||
<script src="{{ url_for('static', filename='app.js') }}"></script>
|
||||
|
24
web/app.js
24
web/app.js
@ -227,3 +227,27 @@ function getCheckedCheckboxIds(classname) {
|
||||
});
|
||||
return checkedIds;
|
||||
}
|
||||
|
||||
|
||||
// export data
|
||||
async function downloadCSV() {
|
||||
const all_data = await fetch_all();
|
||||
var export_data = '';
|
||||
for (const [key, value] of Object.entries(all_data)) {
|
||||
const csvContent = objectToCsv(value);
|
||||
export_data += `#####${key}\n`;
|
||||
export_data += csvContent;
|
||||
export_data += '\n';
|
||||
}
|
||||
export_blob = new Blob([export_data], {type: "text/plain;charset=utf-8"});
|
||||
const url = window.URL.createObjectURL(export_blob);
|
||||
const a = document.createElement('a');
|
||||
a.setAttribute('href', url);
|
||||
a.setAttribute('download', 'data.csv');
|
||||
a.style.display = 'none';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
}
|
||||
|
||||
document.getElementById('export_data_button').addEventListener('click', downloadCSV);
|
||||
|
43
web/fetch.js
43
web/fetch.js
@ -218,3 +218,46 @@ async function get_list_details(list_uuid) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async function fetch_all() {
|
||||
try {
|
||||
const response = await fetch(`${currentUrl}app/data-all`);
|
||||
const rawData = await response.json();
|
||||
// tables = ['item', 'itemlist', 'listcontent', 'history']
|
||||
let data = Object();
|
||||
// uuid, itemid, skuid, choice, attributes, image, show
|
||||
data.item = rawData[0].map(d => ({
|
||||
uuid: d[0],
|
||||
itemid: d[1],
|
||||
skuid: d[2],
|
||||
choice: d[3],
|
||||
attributes: d[4],
|
||||
image: d[5],
|
||||
show: d[6],
|
||||
}));
|
||||
// uuid, name, description
|
||||
data.itemlist = rawData[1].map(d => ({
|
||||
uuid: d[0],
|
||||
name: d[1],
|
||||
description: d[2],
|
||||
}));
|
||||
// list_uuid, item_uuid
|
||||
data.listcontent = rawData[2].map(d => ({
|
||||
list_uuid: d[0],
|
||||
item_uuid: d[1],
|
||||
}));
|
||||
// uuid, quantity, discount_percentage, price, currency, h_timestamp
|
||||
data.history = rawData[3].map(d => ({
|
||||
uuid: d[0],
|
||||
quantity: d[1],
|
||||
discount_percentage: d[2],
|
||||
price: d[3],
|
||||
currency: d[4],
|
||||
h_timestamp: d[5],
|
||||
}));
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('Error fetching data history: ', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
function filters_checkboxes() {
|
||||
console.log("refresh filters");
|
||||
fetch_list().then(function(data){
|
||||
{
|
||||
const node = document.getElementById("filters_checkboxes");
|
||||
|
Loading…
x
Reference in New Issue
Block a user