Commit 688a560b authored by berumuron's avatar berumuron
Browse files

add: #1 Display events

parent ab009de5
import os
import locale
from flask import Flask, render_template
from flask import Flask
from . import cli, db, auth, events
from . import jinja_filters, cli, db, auth, events
__version__ = "0.1.0"
locale.setlocale(locale.LC_ALL, "")
def create_app():
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
......@@ -20,6 +24,7 @@ def create_app():
pass
# Register commands and teardowns to the app.
jinja_filters.init_app(app)
cli.init_app(app)
db.init_app(app)
......@@ -27,9 +32,7 @@ def create_app():
app.register_blueprint(auth.bp)
app.register_blueprint(events.bp)
# Define a default basic route (it will be improved later).
@app.route("/")
def index():
return render_template("index.html")
# Define the default route to the events index page
app.add_url_rule("/", "index", events.index)
return app
from flask import Blueprint, request, redirect, url_for, flash, render_template
from flask import Blueprint, request, redirect, url_for, flash, render_template, g
from .auth import login_required
from .models import Event
......@@ -7,6 +7,16 @@ from .models import Event
bp = Blueprint("events", __name__, url_prefix="/events")
@bp.route("/")
def index():
if g.current_user is None:
events = Event.items_where(is_private=False)
else:
events = Event.items()
return render_template("events/index.html", events=events)
@bp.route("/create", methods=("GET", "POST"))
@login_required
def create():
......
from datetime import datetime
def to_month_filter(number):
"""Convert a number to its equivalent month string."""
return datetime.strptime(str(number), "%m").strftime("%B")
def init_app(app):
"""Register filters to the application Jinja."""
app.jinja_env.filters["to_month"] = to_month_filter
from datetime import datetime
import werkzeug.security as security
from . import db
......@@ -56,6 +58,42 @@ class Event:
self.is_private = is_private
self.description = description
@classmethod
def items(cls):
database = db.get_db()
cursor = database.execute("SELECT * FROM events")
event = cursor.fetchone()
while event:
yield cls(
event["id"],
event["title"],
event["date"],
event["is_private"],
event["description"],
)
event = cursor.fetchone()
@classmethod
def items_where(cls, is_private):
database = db.get_db()
cursor = database.execute(
"SELECT * FROM events WHERE is_private = ?", (is_private,)
)
event = cursor.fetchone()
while event:
yield cls(
event["id"],
event["title"],
event["date"],
event["is_private"],
event["description"],
)
event = cursor.fetchone()
@classmethod
def find_by_id(cls, event_id):
database = db.get_db()
......
{% extends 'layout.html' %}
{% block content %}
<div class="container">
<h1>Évènements</h1>
{% if g.current_user %}
<a href="{{ url_for('events.create') }}">
Nouvel évènement
</a>
{% endif %}
{% for group_by_year in events|groupby('date.year')|sort(reverse=True) %}
<h2>{{ group_by_year.grouper }}</h2>
{% for group_by_month in group_by_year.list|groupby('date.month')|sort(reverse=True) %}
<div>
<h3>{{ group_by_month.grouper|to_month }}</h3>
<ul>
{% for event in group_by_month.list|sort(attribute='date', reverse=True) %}
<li>
{% if event.is_private %}
<span>🔒</span>
{% endif %}
{{ event.date.strftime('%d %b') }}&nbsp;: {{ event.title }}
</li>
{% endfor %}
</ul>
</div>
{% endfor %}
{% endfor %}
</div>
{% endblock %}
{% extends 'layout.html' %}
{% block content %}
<div class="container">
{% if g.current_user %}
<h1>Bonjour {{ g.current_user.username }}&nbsp;!</h1>
{% else %}
<h1>Bonjour Monde&nbsp;!</h1>
{% 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