From 20849b8ee55ddc08440c815713dd08c7a66a9703 Mon Sep 17 00:00:00 2001 From: Wojciech Kwolek Date: Mon, 31 Aug 2020 22:30:08 +0200 Subject: [PATCH] Allow editing accomplishments Squashed commit of the following: commit 97029311b87954d28b127c4d036c10ed5c13c632 Author: Wojciech Kwolek Date: Mon Aug 31 22:29:53 2020 +0200 allow editing accomplishments commit 199462a9a62723ebe7183925f2d616d41e21eded Merge: d094e32 4f1b750 Author: Wojciech Kwolek Date: Mon Aug 31 21:48:09 2020 +0200 Merge branch 'master' into feature/edit commit d094e321c7e9d526646adad411d47fd03dc4d37b Author: Wojciech Kwolek Date: Mon Aug 31 20:59:53 2020 +0200 implement deleting accomplishments commit 5c33caba1baf0ad6a0c62cccba35209c7e28a71c Author: Wojciech Kwolek Date: Mon Aug 31 17:33:12 2020 +0200 add edit buttons to the day view commit 6cc826c5cbb0a0260f5745a8392c3bc92afb6c71 Author: Wojciech Kwolek Date: Mon Aug 31 16:38:35 2020 +0200 add __pycache__ to gitignore commit 1593e4c6cfa1a45096421292152185579a449e53 Author: Wojciech Kwolek Date: Mon Aug 31 16:35:40 2020 +0200 extract a method for getting all day related data for a template --- app/db.py | 4 ++++ app/main.py | 36 +++++++++++++++++++++++++++++++-- app/templates/_formhelpers.html | 6 +++--- app/templates/main/app.html | 2 ++ app/templates/main/edit.html | 14 +++++++++++++ 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 app/templates/main/edit.html 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
+
{% 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 %}