Commit 35a737b1 authored by berumuron's avatar berumuron
Browse files

add: #13 Provide project creation

parent 9c48f301
......@@ -3,7 +3,7 @@ import locale
from flask import Flask
from . import jinja_filters, cli, db, auth, events
from . import jinja_filters, cli, db, auth, events, projects
__version__ = "0.1.0"
......@@ -33,6 +33,7 @@ def create_app():
# Register blueprints (collection of views)
app.register_blueprint(auth.bp)
app.register_blueprint(events.bp)
app.register_blueprint(projects.bp)
# Define the default route to the events index page
app.add_url_rule("/", "index", events.index)
......
......@@ -38,7 +38,7 @@ def create():
flash("L’évènement n’a pas pu être ajouté.")
else:
flash("L’évènement a été ajouté.", "success")
return redirect(url_for("index"))
return redirect(url_for("events.index"))
today_date = datetime.strftime(datetime.now(), "%Y-%m-%d")
return render_template("events/create.html", today_date=today_date)
......@@ -55,4 +55,4 @@ def delete(event_id):
else:
flash("L’évènement a été correctement supprimé.", "success")
return redirect(url_for("index"))
return redirect(url_for("events.index"))
......@@ -133,3 +133,44 @@ class Event:
return True
except sqlite3.DatabaseError:
return False
class Project:
def __init__(self, id, name, is_private, responsible, description):
self.id = id
self.name = name
self.is_private = is_private
self.responsible = responsible
self.description = description
@classmethod
def find_by_id(cls, project_id):
database = db.get_db()
project = database.execute(
"SELECT * FROM projects WHERE id = ?", (project_id,)
).fetchone()
if project is None:
return None
return cls(
project["id"],
project["name"],
project["is_private"],
project["responsible"],
project["description"],
)
@classmethod
def create(cls, name, is_private, responsible, description):
database = db.get_db()
cursor = database.execute(
(
"INSERT INTO projects (name, is_private, responsible, description) "
"VALUES (?, ?, ?, ?)"
),
(name, is_private, responsible, description),
)
database.commit()
return cls.find_by_id(cursor.lastrowid)
from flask import Blueprint, request, redirect, url_for, flash, render_template
from .auth import login_required
from .models import Project
bp = Blueprint("projects", __name__, url_prefix="/projects")
@bp.route("/")
def index():
return render_template("projects/index.html")
@bp.route("/create", methods=("GET", "POST"))
@login_required
def create():
if request.method == "POST":
name = request.form["name"].strip()
is_private = request.form["is_private"].strip() == "yes"
responsible = request.form["responsible"].strip()
description = request.form["description"].strip()
if not name:
flash("Le nom est requis.")
elif not Project.create(name, is_private, responsible, description):
flash("Le projet n’a pas pu être ajouté.")
else:
flash("Le projet a été ajouté.", "success")
return redirect(url_for("projects.index"))
return render_template("projects/create.html")
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS projects;
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
......@@ -12,5 +13,15 @@ CREATE TABLE events (
title TEXT NOT NULL,
date DATE NOT NULL,
is_private BOOLEAN NOT NULL DEFAULT true,
description TEXT NOT NULL,
project_id INTEGER,
FOREIGN KEY (project_id) REFERENCES projects (id)
);
CREATE TABLE projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
is_private BOOLEAN NOT NULL DEFAULT true,
responsible TEXT NOT NULL,
description TEXT NOT NULL
);
......@@ -35,8 +35,8 @@
Ajouter
</button>
<a href="{{ url_for('index') }}">
Retour à l'accueil
<a href="{{ url_for('events.index') }}">
Retour aux temps forts
</a>
</div>
</form>
......
......@@ -5,6 +5,10 @@
<div class="container-title">
<h1>Temps forts</h1>
<a href="{{ url_for('projects.index') }}">
les projets
</a>
{% if g.current_user %}
<a class="container-title-action add" href="{{ url_for('events.create') }}">
Nouvel évènement
......
{% extends 'layout.html' %}
{% block content %}
<div class="container container-small">
<h1>Nouveau projet</h1>
<form action="" method="post">
{% include "alerts.html" %}
<div class="form-group">
<label for="name">Nom</label>
<input type="text" name="name" id="name" required autofocus />
</div>
<div class="form-group form-group-radios">
<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="responsible">Responsable <span class="text-muted">(facultatif)</span></label>
<input type="text" name="responsible" id="responsible" />
</div>
<div class="form-group">
<label for="description">Description <span class="text-muted">(facultatif)</span></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('projects.index') }}">
Retour aux projets
</a>
</div>
</form>
</div
{% endblock %}
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<div class="container-title">
<h1>Projets</h1>
<a href="{{ url_for('events.index') }}">
les temps forts
</a>
{% if g.current_user %}
<a class="container-title-action add" href="{{ url_for('projects.create') }}">
Nouveau projet
</a>
{% endif %}
</div>
{% include "alerts.html" %}
</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