diff --git a/app/db.py b/app/db.py index fbc8bed..b71d0d2 100644 --- a/app/db.py +++ b/app/db.py @@ -48,6 +48,10 @@ class Accomplishment(db.Model): @property def difficulty_class(self): + if self.difficulty < 0: + return "negative" + if self.difficulty == 0: + return "zero" if self.difficulty <= 5: return "easy" if self.difficulty <= 10: diff --git a/app/main.py b/app/main.py index a906878..865554d 100644 --- a/app/main.py +++ b/app/main.py @@ -2,8 +2,8 @@ from . import timeutils from flask import Blueprint, render_template, redirect, url_for, abort from flask_login import current_user, login_required from flask_wtf import FlaskForm -from wtforms import StringField, SubmitField -from wtforms.validators import DataRequired, Length +from wtforms import StringField, SubmitField, IntegerField +from wtforms.validators import DataRequired, Length, NumberRange from .db import db, Accomplishment from datetime import datetime, timedelta @@ -128,3 +128,35 @@ def delete_accomplishment(accomplishment_id): form=form, accomplishment=a, cancel=back_url) + + +class EditForm(FlaskForm): + text = StringField( + 'Accomplishment', + validators=[DataRequired(), Length(max=256)] + ) + difficulty = IntegerField( + 'Difficulty (XP)', + validators=[DataRequired(), NumberRange(max=100, min=-100)] + ) + submit = SubmitField('Save') + + +@main.route('/accomplishment//edit', methods=['GET', 'POST']) +@login_required +def edit_accomplishment(accomplishment_id): + a = Accomplishment.query.get_or_404(accomplishment_id) + if a.user_id != current_user.id: + abort(403) + + back_url = url_for( + 'main.edit_day', day=timeutils.as_str(timeutils.day(a.time))) + + form = EditForm(obj=a) + if form.validate_on_submit(): + a.text = form.text.data + a.difficulty = form.difficulty.data + db.session.commit() + return redirect(back_url) + + return render_template('main/edit.html', form=form, cancel=back_url) diff --git a/app/templates/_formhelpers.html b/app/templates/_formhelpers.html index f12c0a6..a96822f 100644 --- a/app/templates/_formhelpers.html +++ b/app/templates/_formhelpers.html @@ -1,7 +1,7 @@ -{% macro render_field(field, label=True) %} -
+{% macro render_field(field, label=True, wrapper_class="") %} +
+ class="block mb-2 text-sm font-bold text-gray-700">{% if label %}{{ field.label }}{% endif %} {{ field(**kwargs)|safe }} {% if field.errors %}
    diff --git a/app/templates/main/app.html b/app/templates/main/app.html index f304b4f..7e9fd2d 100644 --- a/app/templates/main/app.html +++ b/app/templates/main/app.html @@ -37,6 +37,8 @@ delete
+
edit
{% endif %} diff --git a/app/templates/main/edit.html b/app/templates/main/edit.html new file mode 100644 index 0000000..0245ace --- /dev/null +++ b/app/templates/main/edit.html @@ -0,0 +1,14 @@ +{% extends "_skel.html" %} +{% block title %}Edit accomplishment{% endblock %} +{% from "_formhelpers.html" import render_field %} +{% block content %} +
+ {{ form.csrf_token }} + {{ render_field(form.text) }} + {{ render_field(form.difficulty) }} +
+ {{ render_field(form.submit, False, class_="bg-blue-700 text-white hover:bg-blue-500") }} + cancel +
+
+{% endblock %}