102 lines
4.1 KiB
Python
102 lines
4.1 KiB
Python
from collections import defaultdict
|
|
from flask import Blueprint, render_template, redirect, request, url_for, flash
|
|
import humanize
|
|
from pytz import timezone, utc
|
|
from sqlalchemy import func
|
|
from app.models import Reading, db
|
|
from app.forms import DeleteForm, ReadingForm
|
|
from flask_login import login_required, current_user
|
|
from datetime import date, datetime, timedelta
|
|
reading = Blueprint('reading', __name__)
|
|
|
|
@reading.route('/add', methods=['GET', 'POST'])
|
|
@login_required
|
|
def add_reading():
|
|
form = ReadingForm()
|
|
if form.validate_on_submit():
|
|
new_reading = Reading(
|
|
user_id=current_user.id,
|
|
timestamp=form.timestamp.data,
|
|
systolic=form.systolic.data,
|
|
diastolic=form.diastolic.data,
|
|
heart_rate=form.heart_rate.data
|
|
)
|
|
db.session.add(new_reading)
|
|
db.session.commit()
|
|
flash("Reading added successfully.", "success")
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
# Fetch the user's timezone (default to 'UTC' if none is set)
|
|
user_timezone = current_user.profile.timezone if current_user.profile and current_user.profile.timezone else 'UTC'
|
|
local_tz = timezone(user_timezone)
|
|
|
|
form.timestamp.data = utc.localize(datetime.utcnow()).astimezone(local_tz)
|
|
return render_template('reading/add_reading.html', form=form)
|
|
|
|
@reading.route('/<int:reading_id>/edit', methods=['GET', 'POST'])
|
|
@login_required
|
|
def edit_reading(reading_id):
|
|
reading = Reading.query.get_or_404(reading_id)
|
|
|
|
# Ensure the reading belongs to the logged-in user
|
|
if reading.user_id != current_user.id:
|
|
flash('You are not authorized to edit this reading.', 'danger')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
# Fetch the user's timezone (default to 'UTC' if none is set)
|
|
user_timezone = current_user.profile.timezone if current_user.profile and current_user.profile.timezone else 'UTC'
|
|
local_tz = timezone(user_timezone)
|
|
|
|
reading.local_timestamp = utc.localize(reading.timestamp).astimezone(local_tz)
|
|
|
|
form = ReadingForm(obj=reading) # Populate form with existing reading data
|
|
form.timestamp.data = reading.local_timestamp
|
|
if form.validate_on_submit():
|
|
# Convert the local timestamp back to UTC for saving
|
|
local_timestamp = form.timestamp.data
|
|
# Ensure the local timestamp is naive before localizing
|
|
if local_timestamp.tzinfo is not None:
|
|
local_timestamp = local_timestamp.replace(tzinfo=None)
|
|
|
|
reading.timestamp = local_tz.localize(local_timestamp).astimezone(utc)
|
|
|
|
reading.systolic = form.systolic.data
|
|
reading.diastolic = form.diastolic.data
|
|
reading.heart_rate = form.heart_rate.data
|
|
db.session.commit()
|
|
flash('Reading updated successfully!', 'success')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
return render_template('reading/edit_reading.html', form=form, reading=reading)
|
|
|
|
@reading.route('/<int:reading_id>/confirm_delete', methods=['GET', 'POST'])
|
|
@login_required
|
|
def confirm_delete(reading_id):
|
|
# Fetch the reading to confirm deletion
|
|
reading = Reading.query.filter_by(id=reading_id, user_id=current_user.id).first_or_404()
|
|
|
|
if request.method == 'POST':
|
|
# Handle deletion
|
|
db.session.delete(reading)
|
|
db.session.commit()
|
|
flash('Reading deleted successfully!', 'success')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
return render_template('reading/confirm_delete.html', reading=reading)
|
|
|
|
|
|
@reading.route('/<int:reading_id>/delete', methods=['POST'])
|
|
@login_required
|
|
def delete_reading(reading_id):
|
|
reading = Reading.query.get_or_404(reading_id)
|
|
|
|
# Ensure the reading belongs to the logged-in user
|
|
if reading.user_id != current_user.id:
|
|
flash('You are not authorized to delete this reading.', 'danger')
|
|
return redirect(url_for('main.dashboard'))
|
|
|
|
db.session.delete(reading)
|
|
db.session.commit()
|
|
flash('Reading deleted successfully!', 'success')
|
|
return redirect(url_for('main.dashboard'))
|