allow adding accomplishments to previous days
This commit is contained in:
parent
07e6660199
commit
1e7bd63910
55
app/main.py
55
app/main.py
|
|
@ -1,5 +1,5 @@
|
||||||
from . import timeutils
|
from . import timeutils
|
||||||
from flask import Blueprint, render_template, redirect, url_for, abort
|
from flask import Blueprint, render_template, redirect, url_for, abort, request
|
||||||
from flask_login import current_user, login_required
|
from flask_login import current_user, login_required
|
||||||
from flask_wtf import FlaskForm
|
from flask_wtf import FlaskForm
|
||||||
from wtforms import StringField, SubmitField, IntegerField
|
from wtforms import StringField, SubmitField, IntegerField
|
||||||
|
|
@ -35,7 +35,7 @@ def handle_accomplishment_submission(form):
|
||||||
return redirect(url_for('main.index'))
|
return redirect(url_for('main.index'))
|
||||||
|
|
||||||
|
|
||||||
def get_day_template_data(day_string):
|
def parse_day(day_string):
|
||||||
day_datetime = None
|
day_datetime = None
|
||||||
if day_string == "today":
|
if day_string == "today":
|
||||||
day_datetime = timeutils.today()
|
day_datetime = timeutils.today()
|
||||||
|
|
@ -43,6 +43,17 @@ def get_day_template_data(day_string):
|
||||||
day_datetime = timeutils.from_str(day_string)
|
day_datetime = timeutils.from_str(day_string)
|
||||||
|
|
||||||
day_string_clean = timeutils.as_str(day_datetime)
|
day_string_clean = timeutils.as_str(day_datetime)
|
||||||
|
return {
|
||||||
|
"datetime": day_datetime,
|
||||||
|
"string": day_string_clean,
|
||||||
|
"fancy": timeutils.as_fancy_str(day_datetime),
|
||||||
|
"is_today": timeutils.is_today(day_datetime)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_day_template_data(day_string):
|
||||||
|
day = parse_day(day_string)
|
||||||
|
day_datetime = day["datetime"]
|
||||||
|
|
||||||
accomplishments = list(reversed(
|
accomplishments = list(reversed(
|
||||||
Accomplishment.get_day(current_user.id, day_datetime)))
|
Accomplishment.get_day(current_user.id, day_datetime)))
|
||||||
|
|
@ -54,18 +65,11 @@ def get_day_template_data(day_string):
|
||||||
tomorrow = None
|
tomorrow = None
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"day": {
|
"day": day,
|
||||||
"datetime": day_datetime,
|
|
||||||
"string": day_string_clean,
|
|
||||||
"fancy": timeutils.as_fancy_str(day_datetime),
|
|
||||||
"is_today": timeutils.is_today(day_datetime)
|
|
||||||
},
|
|
||||||
|
|
||||||
"links": {
|
"links": {
|
||||||
"yesterday": url_for('main.index', day=timeutils.as_str(yesterday)),
|
"yesterday": url_for('main.index', day=timeutils.as_str(yesterday)),
|
||||||
"tomorrow": url_for('main.index', day=timeutils.as_str(tomorrow)) if tomorrow is not None else None
|
"tomorrow": url_for('main.index', day=timeutils.as_str(tomorrow)) if tomorrow is not None else None
|
||||||
},
|
},
|
||||||
|
|
||||||
"accomplishments": accomplishments,
|
"accomplishments": accomplishments,
|
||||||
"total_xp": sum(a.difficulty for a in accomplishments),
|
"total_xp": sum(a.difficulty for a in accomplishments),
|
||||||
}
|
}
|
||||||
|
|
@ -160,3 +164,34 @@ def edit_accomplishment(accomplishment_id):
|
||||||
return redirect(back_url)
|
return redirect(back_url)
|
||||||
|
|
||||||
return render_template('main/edit.html', form=form, cancel=back_url)
|
return render_template('main/edit.html', form=form, cancel=back_url)
|
||||||
|
|
||||||
|
|
||||||
|
@main.route('/day/<day>/add', methods=['GET', 'POST'])
|
||||||
|
@login_required
|
||||||
|
def add_day(day):
|
||||||
|
day_parsed = parse_day(day)
|
||||||
|
form = EditForm()
|
||||||
|
|
||||||
|
back_url = ""
|
||||||
|
|
||||||
|
from_top = ("from" in request.args) and ("top" in request.args["from"])
|
||||||
|
back_to_day = url_for('main.index', day=day_parsed["string"])
|
||||||
|
back_to_edit = url_for('main.edit_day', day=day_parsed["string"])
|
||||||
|
|
||||||
|
if form.validate_on_submit():
|
||||||
|
accomplishment = Accomplishment()
|
||||||
|
accomplishment.user_id = current_user.id
|
||||||
|
accomplishment.text = form.text.data
|
||||||
|
accomplishment.difficulty = form.difficulty.data
|
||||||
|
accomplishment.time = timeutils.from_str(day)
|
||||||
|
db.session.add(accomplishment)
|
||||||
|
db.session.commit()
|
||||||
|
return redirect(back_to_day)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
'main/edit.html',
|
||||||
|
day=day_parsed,
|
||||||
|
form=form,
|
||||||
|
edit=True,
|
||||||
|
cancel=back_to_day if from_top else back_to_edit
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<div class="max-w-lg mx-auto card">
|
<div class="max-w-lg mx-auto card">
|
||||||
<div class="flex items-baseline justify-between mb-4">
|
<div class="flex items-center justify-between mb-4">
|
||||||
<div>
|
<div>
|
||||||
<h3 class="text-2xl">{{ day.fancy }}</h3>
|
<h3 class="text-2xl">{{ day.fancy }}</h3>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -22,9 +22,17 @@
|
||||||
{% else %}
|
{% else %}
|
||||||
{% if accomplishments %}
|
{% if accomplishments %}
|
||||||
<div><a href="{{ url_for('main.edit_day', day=day.string) }}" class="link">edit</a></div>
|
<div><a href="{{ url_for('main.edit_day', day=day.string) }}" class="link">edit</a></div>
|
||||||
|
{% else %}
|
||||||
|
<div><a href="{{ url_for('main.add_day', day=day.string, from="top") }}" class="link">add</a></div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
|
{% if edit %}
|
||||||
|
<div class="my-1 ml-2 text-sm accomplishment">
|
||||||
|
<div><a href="{{ url_for('main.add_day', day=day.string) }}" class="link">Add accomplishment</a></div>
|
||||||
|
</div>
|
||||||
|
<hr>
|
||||||
|
{% endif %}
|
||||||
{% for accomplishment in accomplishments %}
|
{% for accomplishment in accomplishments %}
|
||||||
<div class="flex ml-2 accomplishment">
|
<div class="flex ml-2 accomplishment">
|
||||||
<div class="py-1 text">{{ accomplishment.text }}</div>
|
<div class="py-1 text">{{ accomplishment.text }}</div>
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,16 @@
|
||||||
{% extends "_skel.html" %}
|
{% extends "_skel.html" %}
|
||||||
{% block title %}Edit accomplishment{% endblock %}
|
{% block title %}{% if not day %}Edit{% else %}Add{% endif %} accomplishment{% endblock %}
|
||||||
{% from "_formhelpers.html" import render_field %}
|
{% from "_formhelpers.html" import render_field %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<form method="POST" class="w-full max-w-lg mx-auto card">
|
{% if day %}
|
||||||
|
<div class="w-full max-w-lg mx-auto card">
|
||||||
|
<div class="text-xl text-center">
|
||||||
|
You're adding an accomplishment made on {{ day.fancy }}.
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
<form method="POST" class="w-full max-w-lg mx-auto card"
|
||||||
|
{% if day %}action="{{ url_for('main.add_day', day=day.string) }}" {% endif %}>
|
||||||
{{ form.csrf_token }}
|
{{ form.csrf_token }}
|
||||||
{{ render_field(form.text) }}
|
{{ render_field(form.text) }}
|
||||||
{{ render_field(form.difficulty) }}
|
{{ render_field(form.difficulty) }}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue