Add Update Function and view
This commit is contained in:
parent
f2e48f61b8
commit
a8b0095da3
@ -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')
|
||||
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('/<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']
|
||||
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)
|
@ -10,6 +10,7 @@
|
||||
<header>
|
||||
<div>
|
||||
<h1>{{ cycle['id'] }}</h1>
|
||||
<a class="action" href="{{ url_for('cycles.update', id=cycle['id'])}}">Edit</a>
|
||||
<div class="about">Started: {{ cycle['start_time'] }} and ended: {{ cycle['end_time'] }}</div>
|
||||
</div>
|
||||
</header>
|
||||
|
15
period/templates/cycle/update.html
Normal file
15
period/templates/cycle/update.html
Normal file
@ -0,0 +1,15 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
{% block header %}
|
||||
<h1>{% block title %}Edit Cycle{% endblock %}</h1>
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<label for="start_time">Start Time</label>
|
||||
<input name="start_time" id="title" value="{{ request.form['start_time'] or cycle['start_time'][:10] }}" required type="date">
|
||||
<label for="end_time">End Time</label>
|
||||
<input name="end_time" id="end_time" value="{{ request.form['end_time'] or cycle['end_time'][:10] if cycle['end_time'] is not none }}" type="date">
|
||||
<input type="submit" value="Save">
|
||||
</form>
|
||||
{% endblock %}
|
Loading…
Reference in New Issue
Block a user