Commit a70443ee authored by berumuron's avatar berumuron
Browse files

add: #10 Provide creation of events

parent b7f207d3
......@@ -2,7 +2,7 @@ import os
from flask import Flask, render_template
from . import cli, db, auth
from . import cli, db, auth, events
__version__ = "0.1.0"
......@@ -25,6 +25,7 @@ def create_app():
# Register blueprints (collection of views)
app.register_blueprint(auth.bp)
app.register_blueprint(events.bp)
# Define a default basic route (it will be improved later).
@app.route("/")
......
from flask import Blueprint, request, redirect, url_for, flash, render_template
from .models import Event
bp = Blueprint("events", __name__, url_prefix="/events")
@bp.route("/create", methods=("GET", "POST"))
def create():
if request.method == "POST":
title = request.form["title"].strip()
date = request.form["date"].strip()
is_private = request.form["is_private"].strip() == "yes"
description = request.form["description"].strip()
if not title:
flash("Le titre est requis.")
elif not date:
flash("La date est requise.")
elif not Event.create(title, date, is_private, description):
flash("L’évènement n’a pas pu être créé.")
else:
return redirect(url_for("index"))
return render_template("events/create.html")
......@@ -46,3 +46,44 @@ class User:
database.commit()
return cls.find_by_id(cursor.lastrowid)
class Event:
def __init__(self, id, title, date, is_private, description):
self.id = id
self.title = title
self.date = date
self.is_private = is_private
self.description = description
@classmethod
def find_by_id(cls, event_id):
database = db.get_db()
event = database.execute(
"SELECT * FROM events WHERE id = ?", (event_id,)
).fetchone()
if event is None:
return None
return cls(
event["id"],
event["title"],
event["date"],
event["is_private"],
event["description"],
)
@classmethod
def create(cls, title, date, is_private, description):
database = db.get_db()
cursor = database.execute(
(
"INSERT INTO events (title, date, is_private, description) "
"VALUES (?, ?, ?, ?)"
),
(title, date, is_private, description),
)
database.commit()
return cls.find_by_id(cursor.lastrowid)
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS events;
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL
);
CREATE TABLE events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
date DATE NOT NULL,
is_private BOOLEAN NOT NULL DEFAULT true,
description TEXT NOT NULL
);
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<h1>Nouvel évènement</h1>
<form action="" method="post">
<div class="alert alert-error">
{% for message in get_flashed_messages() %}
<p>{{ message }}</p>
{% endfor %}
</div>
<div class="form-group">
<label for="title">Titre</label>
<input type="text" name="title" id="title" autofocus />
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" name="date" id="date" />
</div>
<div class="form-group">
<input type="radio" name="is_private" id="is_private" value="yes" checked />
<label for="is_private">Privé</label>
<input type="radio" name="is_private" id="is_public" value="no" />
<label for="is_public">Public</label>
</div>
<div class="form-group">
<label for="description">Description</label>
<textarea name="description" id="description"></textarea>
</div>
<div class="form-group form-group-actions">
<button type="submit" class="button-primary">
Ajouter
</button>
<a href="{{ url_for('index') }}">
Retour à l'accueil
</a>
</div>
</form>
</div
{% endblock %}
......@@ -9,5 +9,9 @@
{% endif %}
<p>Bienvenue sur Reflets.</p>
<a href="{{ url_for('events.create') }}">
Nouvel évènement
</a>
</div>
{% endblock %}
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