Allow editing accomplishments
Squashed commit of the following:
commit 97029311b87954d28b127c4d036c10ed5c13c632
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 22:29:53 2020 +0200
allow editing accomplishments
commit 199462a9a62723ebe7183925f2d616d41e21eded
Merge: d094e32 4f1b750
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 21:48:09 2020 +0200
Merge branch 'master' into feature/edit
commit d094e321c7e9d526646adad411d47fd03dc4d37b
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 20:59:53 2020 +0200
implement deleting accomplishments
commit 5c33caba1baf0ad6a0c62cccba35209c7e28a71c
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 17:33:12 2020 +0200
add edit buttons to the day view
commit 6cc826c5cbb0a0260f5745a8392c3bc92afb6c71
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 16:38:35 2020 +0200
add __pycache__ to gitignore
commit 1593e4c6cfa1a45096421292152185579a449e53
Author: Wojciech Kwolek <wojciech@kwolek.xyz>
Date: Mon Aug 31 16:35:40 2020 +0200
extract a method for getting all day related data for a template
This commit is contained in:
parent
4f1b7504a3
commit
20849b8ee5
|
|
@ -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:
|
||||
|
|
|
|||
36
app/main.py
36
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/<accomplishment_id>/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)
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{% macro render_field(field, label=True) %}
|
||||
<div class="mb-4 {% if field.errors %}error{% endif %}">
|
||||
{% macro render_field(field, label=True, wrapper_class="") %}
|
||||
<div class="mb-4 {% if field.errors %}error{% endif %} {{ wrapper_class }}">
|
||||
<label for="{{ field.id }}"
|
||||
class="block text-gray-700 text-sm font-bold mb-2">{% if label %}{{ field.label }}{% endif %}</label>
|
||||
class="block mb-2 text-sm font-bold text-gray-700">{% if label %}{{ field.label }}{% endif %}</label>
|
||||
{{ field(**kwargs)|safe }}
|
||||
{% if field.errors %}
|
||||
<ul class="errors">
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@
|
|||
<a class="text-red-700 underline hover:text-red-500"
|
||||
href="{{ url_for('main.delete_accomplishment', accomplishment_id=accomplishment.id) }}">delete</a>
|
||||
</div>
|
||||
<div><a class="text-blue-700 underline hover:text-blue-500"
|
||||
href="{{ url_for('main.edit_accomplishment', accomplishment_id=accomplishment.id) }}">edit</a></div>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
{% extends "_skel.html" %}
|
||||
{% block title %}Edit accomplishment{% endblock %}
|
||||
{% from "_formhelpers.html" import render_field %}
|
||||
{% block content %}
|
||||
<form method="POST" class="w-full max-w-lg mx-auto card">
|
||||
{{ form.csrf_token }}
|
||||
{{ render_field(form.text) }}
|
||||
{{ render_field(form.difficulty) }}
|
||||
<div class="text-center">
|
||||
{{ render_field(form.submit, False, class_="bg-blue-700 text-white hover:bg-blue-500") }}
|
||||
<a href="{{ cancel }}" class="block mt-2 text-sm text-blue-700 hover:text-blue-500">cancel</a>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
||||
Loading…
Reference in New Issue