71 lines
2.4 KiB
Python
71 lines
2.4 KiB
Python
import csv
|
|
from io import StringIO
|
|
from flask import Blueprint, render_template, redirect, request, send_file, url_for, flash
|
|
from app.models import Reading, db
|
|
from flask_login import login_required, current_user
|
|
from datetime import datetime
|
|
|
|
data = Blueprint('data', __name__)
|
|
|
|
@data.route('/', methods=['GET', 'POST'])
|
|
@login_required
|
|
def manage_data():
|
|
if request.method == 'POST':
|
|
# Handle CSV file upload
|
|
file = request.files.get('file')
|
|
if file and file.filename.endswith('.csv'):
|
|
try:
|
|
csv_data = csv.reader(StringIO(file.read().decode('utf-8')))
|
|
next(csv_data) # Skip the header row
|
|
for row in csv_data:
|
|
timestamp, systolic, diastolic, heart_rate = row
|
|
reading = Reading(
|
|
user_id=current_user.id,
|
|
timestamp=datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S'),
|
|
systolic=int(systolic),
|
|
diastolic=int(diastolic),
|
|
heart_rate=int(heart_rate),
|
|
)
|
|
db.session.add(reading)
|
|
db.session.commit()
|
|
flash('Data imported successfully!', 'success')
|
|
except Exception as e:
|
|
flash(f'Error importing data: {str(e)}', 'danger')
|
|
else:
|
|
flash('Please upload a valid CSV file.', 'danger')
|
|
return redirect(url_for('data.manage_data'))
|
|
|
|
return render_template('data.html')
|
|
|
|
@data.route('/export', methods=['GET'])
|
|
@login_required
|
|
def export_data():
|
|
import io
|
|
|
|
output = io.StringIO()
|
|
writer = csv.writer(output)
|
|
|
|
# Write CSV header
|
|
writer.writerow(['Timestamp', 'Systolic', 'Diastolic', 'Heart Rate'])
|
|
|
|
# Write user readings to the CSV
|
|
readings = Reading.query.filter_by(user_id=current_user.id).all()
|
|
for reading in readings:
|
|
writer.writerow([
|
|
reading.timestamp.strftime('%Y-%m-%d %H:%M:%S'),
|
|
reading.systolic,
|
|
reading.diastolic,
|
|
reading.heart_rate,
|
|
])
|
|
|
|
# Convert text to bytes for `send_file`
|
|
output.seek(0)
|
|
response = io.BytesIO(output.getvalue().encode('utf-8'))
|
|
output.close()
|
|
|
|
return send_file(
|
|
response,
|
|
mimetype='text/csv',
|
|
as_attachment=True,
|
|
download_name='readings.csv'
|
|
) |