From a8b0095da3186be3dfcf54c0d0afdff3979289fc Mon Sep 17 00:00:00 2001 From: Alexander Hosking Date: Sat, 23 Apr 2022 14:22:44 -0400 Subject: [PATCH] Add Update Function and view --- period/cycles.py | 72 +++++++++++++++++++++++++++--- period/templates/cycle/index.html | 1 + period/templates/cycle/update.html | 15 +++++++ 3 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 period/templates/cycle/update.html diff --git a/period/cycles.py b/period/cycles.py index e918863..c6cc0f1 100644 --- a/period/cycles.py +++ b/period/cycles.py @@ -1,4 +1,5 @@ from itertools import cycle +from time import get_clock_info from tracemalloc import start from flask import ( Blueprint, flash, g, redirect, render_template, request, url_for @@ -23,11 +24,13 @@ def index(): @bp.route('/create', methods=('GET', 'POST')) def create(): if request.method == 'POST': - start_time = pendulum.from_format(request.form['start_time'], 'YYYY-MM-DD') - # start_time = start_time.to_datetime_string() + start_time_stamp = request.form['start_time'] + start_time = pendulum.from_format(start_time_stamp, 'YYYY-MM-DD') try: + end_time_stamp = request.form['end_time'] end_time = pendulum.from_format(request.form['end_time'], 'YYYY-MM-DD') except ValueError: + end_time_stamp = "1900-01-01" end_time = pendulum.datetime(1900,1,1) error = None print(request.form['end_time']) @@ -47,13 +50,72 @@ def create(): if end_time > start_time: db.execute( 'INSERT INTO cycle (start_time, end_time, cycle_length) VALUES (?, ?, ?)', - (start_time.to_datetime_string(), end_time.to_datetime_string(), cycle_length) + (start_time_stamp, end_time_stamp, cycle_length) ) else: db.execute( 'INSERT INTO cycle (start_time, cycle_length) VALUES (?, ?)', - (start_time.to_datetime_string(), cycle_length) + (start_time_stamp, cycle_length) ) db.commit() return redirect(url_for('index')) - return render_template('cycle/create.html') \ No newline at end of file + return render_template('cycle/create.html') + +def get_cycle(id): + cycle = get_db().execute( + 'SELECT * from cycle WHERE id = ?', + (id,) + ).fetchone() + + if cycle is None: + abort(404, f"Cycle id {id} doesn't exist.") + + return cycle + +@bp.route('//update', methods=('GET', 'POST')) +def update(id): + cycle = get_cycle(id) + print(cycle['end_time']) + + if request.method == 'POST': + start_time_stamp = request.form['start_time'] + start_time = pendulum.from_format(start_time_stamp, 'YYYY-MM-DD') + try: + end_time_stamp = request.form['end_time'] + end_time = pendulum.from_format(end_time_stamp, 'YYYY-MM-DD') + print(end_time) + except ValueError: + end_time_stamp = "1900-01-01" + end_time = pendulum.datetime(1900,1,1) + error = None + if end_time is None: + end_time = "2999-01-01" + print(end_time) + + if not start_time: + error = "You must provide a start date" + + if end_time > start_time: + cycle_length = (end_time - start_time).days + else: + cycle_length = (pendulum.now() - start_time).days + + if error is not None: + flash(error) + else: + db = get_db() + if end_time > start_time: + db.execute( + 'UPDATE cycle SET start_time = ?, end_time = ?, cycle_length = ?' + ' WHERE id = ?', + (start_time.to_datetime_string(), end_time.to_datetime_string(), cycle_length, id) + ) + else: + db.execute( + 'UPDATE cycle SET start_time = ?, cycle_length = ?' + ' WHERE id = ?', + (start_time.to_datetime_string(), cycle_length, id) + ) + db.commit() + return redirect(url_for('index')) + return render_template('cycle/update.html', cycle=cycle) \ No newline at end of file diff --git a/period/templates/cycle/index.html b/period/templates/cycle/index.html index 8ae4fb1..972a5ab 100644 --- a/period/templates/cycle/index.html +++ b/period/templates/cycle/index.html @@ -10,6 +10,7 @@

{{ cycle['id'] }}

+ Edit
Started: {{ cycle['start_time'] }} and ended: {{ cycle['end_time'] }}
diff --git a/period/templates/cycle/update.html b/period/templates/cycle/update.html new file mode 100644 index 0000000..a3f8234 --- /dev/null +++ b/period/templates/cycle/update.html @@ -0,0 +1,15 @@ +{% extends 'base.html' %} + +{% block header %} +

{% block title %}Edit Cycle{% endblock %}

+{% endblock %} + +{% block content %} +
+ + + + + +
+{% endblock %} \ No newline at end of file