Commit 5527611d authored by berumuron's avatar berumuron
Browse files

add: #11 Provide events modification

parent 722ae801
from datetime import datetime
from flask import Blueprint, request, redirect, url_for, flash, render_template, g
from flask import (
Blueprint,
request,
redirect,
url_for,
flash,
render_template,
g,
abort,
)
from .auth import login_required
from .models import Event, Project
......@@ -61,6 +70,45 @@ def create():
)
@bp.route("/update/<event_id>", methods=("GET", "POST"))
@login_required
def update(event_id):
event = Event.find_by_id(event_id)
if not event:
abort(404)
if request.method == "POST":
event.title = request.form["title"].strip()
event.date = request.form["date"].strip()
event.date_end = request.form["date_end"].strip()
event.is_private = request.form["is_private"].strip() == "yes"
event.description = request.form["description"].strip()
project_name = request.form["project"].strip()
if project_name:
project = Project.find_by_name(project_name)
if not project:
project = Project.create(project_name, True, "", "")
event.project_id = project.id
else:
event.project_id = None
if not event.title:
flash("Le titre est requis.")
elif not event.date:
flash("La date est requise.")
elif event.date_end and event.date >= event.date_end:
flash("La date de fin doit être supérieure à la date de début.")
elif not event.save():
flash("L’évènement n’a pas pu être modifié.")
else:
flash("L’évènement a été modifié.", "success")
return redirect(url_for("events.index"))
projects = Project.items()
return render_template("events/update.html", event=event, projects=projects)
@bp.route("/delete/<event_id>", methods=["DELETE"])
@login_required
def delete(event_id):
......
......@@ -158,6 +158,31 @@ class Event:
return cls.find_by_id(cursor.lastrowid)
def save(self):
try:
database = db.get_db()
database.execute(
(
"UPDATE events "
"SET title = ?, date = ?, date_end = ?, is_private = ?, "
" project_id = ?, description = ? "
"WHERE id = ?"
),
(
self.title,
self.date,
self.date_end,
self.is_private,
self.project_id,
self.description,
self.id,
),
)
database.commit()
return True
except sqlite3.DatabaseError:
return False
def delete(self):
try:
database = db.get_db()
......
......@@ -385,18 +385,37 @@ button.button-ghost {
content: "🔒";
}
.events .event .event-delete {
.events .event .event-actions {
position: absolute;
top: .38rem;
right: 0;
opacity: 0;
visibility: hidden;
transition: opacity .2s ease-out;
}
.events .event:hover .event-delete {
.events .event:hover .event-actions {
opacity: 1;
visibility: visible;
}
.event .event-actions .event-update {
vertical-align: middle;
margin-right: 1rem;
padding: .25rem .5rem;
text-decoration: none;
background-color: var(--contrast-color);
border: 1px solid var(--secondary-color);
border-radius: .25rem;
}
.event .event-actions .event-update::before {
content: "✎";
}
.events .event .event-delete {
margin-right: 0;
}
.events .event .event-delete::before {
content: "🗑";
}
......
......@@ -71,14 +71,20 @@
{% endif %}
{% if g.current_user %}
<button
class="button-ghost event-delete"
data-confirm="Êtes-vous certain·e de vouloir supprimer cet évènement ?"
data-action="{{ url_for('events.delete', event_id=event.id ) }}"
data-method="delete"
>
Supprimer
</button>
<div class="event-actions">
<a class="event-update" href="{{ url_for('events.update', event_id=event.id ) }}">
Modifier
</a>
<button
class="button-ghost event-delete"
data-confirm="Êtes-vous certain·e de vouloir supprimer cet évènement ?"
data-action="{{ url_for('events.delete', event_id=event.id ) }}"
data-method="delete"
>
Supprimer
</button>
</div>
{% endif %}
{% if event.description %}
......
{% extends 'layout.html' %}
{% block content %}
<div class="container container-small">
<h1>Modifier un évènement</h1>
<form action="" method="post">
{% include "alerts.html" %}
<div class="form-group">
<label for="title">Titre</label>
<input type="text" name="title" id="title" value="{{ event.title }}" required autofocus />
</div>
<div class="form-row">
<div class="form-group">
<label for="date">Date</label>
<input type="date" name="date" id="date" required value="{{ event.date }}" />
</div>
<div class="form-group hidden">
<label for="date-end">Date de fin</label>
<input type="date" name="date_end" id="date-end" value="{{ event.date_end }}" />
</div>
</div>
<div class="form-group form-group-checkbox">
<input type="checkbox" id="is-duration" {{ 'checked' if event.date_end }} />
<label for="is-duration">Évènement sur plusieurs jours&nbsp;?</label>
</div>
<div class="form-group form-group-radios">
<input type="radio" name="is_private" id="is_private" value="yes" {{ 'checked' if event.is_private }} />
<label for="is_private">Privé</label>
<input type="radio" name="is_private" id="is_public" value="no" {{ 'checked' if not event.is_private }} />
<label for="is_public">Public</label>
</div>
<div class="form-group">
<label for="project">Projet <span class="text-muted">(facultatif)</span></label>
<input type="text" name="project" id="project" list="projects" value="{{ event.project.name if event.project }}" />
<datalist id="projects">
{% for project in projects %}
<option value="{{ project.name }}">
{% endfor %}
</datalist>
</div>
<div class="form-group">
<label for="description">Description <span class="text-muted">(facultatif)</span></label>
<textarea name="description" id="description">{{ event.description }}</textarea>
</div>
<div class="form-group form-group-actions">
<button type="submit" class="button-primary">
Modifier
</button>
<a href="{{ url_for('events.index') }}">
Retour aux temps forts
</a>
</div>
</form>
</div>
<script>
(function() {
var isDurationInput = document.getElementById('is-duration');
var dateInput = document.getElementById('date');
var dateEndInput = document.getElementById('date-end');
// Consider the case where user check the input, reload the page
// and the browser keep in cache the check.
if (isDurationInput.checked) {
const dayStart = dateInput.valueAsDate;
if (dayStart) {
var nextDay = new Date(dayStart);
nextDay.setDate(nextDay.getDate() + 1);
dateEndInput.valueAsDate = nextDay;
}
dateEndInput.parentElement.classList.remove('hidden');
}
isDurationInput.addEventListener('click', function (e) {
if (isDurationInput.checked) {
const dayStart = dateInput.valueAsDate;
if (dayStart) {
var nextDay = new Date(dayStart);
nextDay.setDate(nextDay.getDate() + 1);
dateEndInput.valueAsDate = nextDay;
}
dateEndInput.parentElement.classList.remove('hidden');
} else {
dateEndInput.parentElement.classList.add('hidden');
dateEndInput.value = '';
}
});
}());
</script>
{% endblock %}
......@@ -63,14 +63,20 @@
{% endif %}
{% if g.current_user %}
<button
class="button-ghost event-delete"
data-confirm="Êtes-vous certain·e de vouloir supprimer cet évènement ?"
data-action="{{ url_for('events.delete', event_id=event.id ) }}"
data-method="delete"
>
Supprimer
</button>
<div class="event-actions">
<a class="event-update" href="{{ url_for('events.update', event_id=event.id ) }}">
Modifier
</a>
<button
class="button-ghost event-delete"
data-confirm="Êtes-vous certain·e de vouloir supprimer cet évènement ?"
data-action="{{ url_for('events.delete', event_id=event.id ) }}"
data-method="delete"
>
Supprimer
</button>
</div>
{% endif %}
{% if event.description %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment