diff --git a/period/cycles.py b/period/cycles.py index dc33e6b..ec4be5e 100644 --- a/period/cycles.py +++ b/period/cycles.py @@ -16,16 +16,14 @@ bp = Blueprint('cycles', __name__) def index(): db = get_db() current_cycle = db.execute('SELECT * from cycle ORDER BY id DESC LIMIT 1').fetchone() - print(current_cycle['start_time']) start_time = pendulum.parse(current_cycle['start_time']) - if current_cycle['end_time'] is None: - cycle_length = (pendulum.now() - start_time).days + cycle_length = (pendulum.now() - start_time ).days - db.execute( - 'UPDATE cycle SET cycle_length = ?' - ' WHERE id = ?', - (cycle_length, current_cycle['id']) - ) + db.execute( + 'UPDATE cycle SET cycle_length = ?' + ' WHERE id = ?', + (cycle_length, current_cycle['id']) + ) cycles = db.execute( 'SELECT id, start_time, end_time, cycle_length' @@ -45,7 +43,7 @@ def create(): end_time_stamp = "1900-01-01" end_time = pendulum.datetime(1900,1,1) error = None - print(request.form['end_time']) + # print(request.form['end_time']) if not start_time: error = "You must provide a start date" @@ -71,7 +69,8 @@ def create(): (start_time.to_datetime_string(),) ) db.commit() - return redirect(url_for('index')) + # return redirect(url_for('index')) + return redirect(url_for('cycles.cycle_math')) return render_template('cycle/create.html') def get_cycle(id): @@ -88,7 +87,6 @@ def get_cycle(id): @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'] @@ -96,14 +94,12 @@ def update(id): 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" @@ -130,7 +126,7 @@ def update(id): (start_time.to_datetime_string(), cycle_length, id) ) db.commit() - return redirect(url_for('index')) + return redirect(url_for('cycles.cycle_math')) return render_template('cycle/update.html', cycle=cycle) @bp.route('//delete', methods=('POST',)) @@ -139,4 +135,32 @@ def delete(id): db = get_db() db.execute('DELETE FROM cycle WHERE id = ?', (id,)) db.commit() - return redirect(url_for('index')) \ No newline at end of file + # return redirect(url_for('index')) + return redirect(url_for('cycles.cycle_math')) + +@bp.route('/cycle_math') +def cycle_math(): + db = get_db() + + all_cycles = db.execute( + 'SELECT id, start_time, end_time, cycle_length' + ' FROM cycle ORDER BY start_time DESC' + ).fetchall() + + + for num, cycle in enumerate(all_cycles): + # http://127.0.0.1:5000/cycle_math + # cycle length = next start - this start + if num > 0: + this_start = pendulum.parse(cycle['start_time']) + next_start = pendulum.parse(all_cycles[num-1]['start_time']) + cycle_length = (next_start - this_start).days + db.execute( + 'UPDATE cycle SET cycle_length = ?' + ' WHERE id = ?', + (cycle_length, cycle['id']) + ) + db.commit() + + + return redirect(url_for('cycles.index')) \ No newline at end of file diff --git a/period/templates/cycle/index.html b/period/templates/cycle/index.html index 554dde2..87b6efc 100644 --- a/period/templates/cycle/index.html +++ b/period/templates/cycle/index.html @@ -8,6 +8,11 @@
+
+ {% block header %} +

{% block title %}Cycles{% endblock %}

+ {% endblock %} +
{% for cycle in cycles %} @@ -24,7 +29,7 @@ {% endif %}

Started: {{ cycle['start_time'][:10] }}
- {% if not loop.first %} + {% if cycle['end_time'] is not none %} Ended: {{ cycle['end_time'][:10] }} {% endif %}