Add math function
Updates the cycle length to match start-to-start Start and end match bleeding
This commit is contained in:
parent
c4b68a8c2f
commit
ccfa49c514
@ -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('/<int:id>/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('/<int:id>/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'))
|
||||
# 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'))
|
@ -8,6 +8,11 @@
|
||||
<div class="table-wrapper">
|
||||
<div class="table-title">
|
||||
<div class="row">
|
||||
<div class="col-sm-6">
|
||||
{% block header %}
|
||||
<h2>{% block title %}Cycles{% endblock %}</h2>
|
||||
{% endblock %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% for cycle in cycles %}
|
||||
@ -24,7 +29,7 @@
|
||||
{% endif %}</h5>
|
||||
<p class="card-text"><b>Started:</b> {{ cycle['start_time'][:10] }}
|
||||
<br>
|
||||
{% if not loop.first %}
|
||||
{% if cycle['end_time'] is not none %}
|
||||
<b>Ended: </b>{{ cycle['end_time'][:10] }}
|
||||
{% endif %}
|
||||
</p>
|
||||
|
Loading…
Reference in New Issue
Block a user