Commit a2225c97 authored by berumuron's avatar berumuron
Browse files

add: #18 Add a project input during event creation

parent 127e8c2f
......@@ -3,7 +3,7 @@ from datetime import datetime
from flask import Blueprint, request, redirect, url_for, flash, render_template, g
from .auth import login_required
from .models import Event
from .models import Event, Project
bp = Blueprint("events", __name__, url_prefix="/events")
......@@ -28,20 +28,32 @@ def create():
title = request.form["title"].strip()
date = request.form["date"].strip()
is_private = request.form["is_private"].strip() == "yes"
project_name = request.form["project"].strip()
description = request.form["description"].strip()
if project_name:
project = Project.find_by_name(project_name)
if not project:
project = Project.create(project_name, True, "", "")
project_id = project.id
else:
project_id = None
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):
elif not Event.create(title, date, is_private, project_id, description):
flash("L’évènement n’a pas pu être ajouté.")
else:
flash("L’évènement a été ajouté.", "success")
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)
projects = Project.items()
return render_template(
"events/create.html", today_date=today_date, projects=projects
)
@bp.route("/delete/<event_id>", methods=["DELETE"])
......
......@@ -50,11 +50,12 @@ class User:
class Event:
def __init__(self, id, title, date, is_private, description):
def __init__(self, id, title, date, is_private, project_id, description):
self.id = id
self.title = title
self.date = date
self.is_private = is_private
self.project_id = project_id
self.description = description
@classmethod
......@@ -69,6 +70,7 @@ class Event:
event["title"],
event["date"],
event["is_private"],
event["project_id"],
event["description"],
)
......@@ -88,6 +90,7 @@ class Event:
event["title"],
event["date"],
event["is_private"],
event["project_id"],
event["description"],
)
......@@ -108,18 +111,19 @@ class Event:
event["title"],
event["date"],
event["is_private"],
event["project_id"],
event["description"],
)
@classmethod
def create(cls, title, date, is_private, description):
def create(cls, title, date, is_private, project_id, description):
database = db.get_db()
cursor = database.execute(
(
"INSERT INTO events (title, date, is_private, description) "
"VALUES (?, ?, ?, ?)"
"INSERT INTO events (title, date, is_private, project_id, description) "
"VALUES (?, ?, ?, ?, ?)"
),
(title, date, is_private, description),
(title, date, is_private, project_id, description),
)
database.commit()
......@@ -197,6 +201,24 @@ class Project:
project["description"],
)
@classmethod
def find_by_name(cls, name):
database = db.get_db()
project = database.execute(
"SELECT * FROM projects WHERE name = ?", (name,)
).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()
......
......@@ -25,6 +25,17 @@
<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" />
<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"></textarea>
......
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