csv-library-website/library/page.py

82 lines
2.0 KiB
Python
Raw Normal View History

"""This is the main flask library page"""
import flask
2021-02-14 22:11:58 +01:00
from requests import get
from icalendar import Calendar
import datetime
from flask import render_template
2021-02-14 22:11:58 +01:00
from rnrfeed.rnrfeeder import getevents, getlatestevent
from csvparser.csvparser import (
getlicenses,
getpublications,
gettypes,
getyears,
getfullpublication,
)
APP = flask.Flask(__name__, static_folder="static")
@APP.route("/")
def index():
"""Main route, shows all the books and you can filter them, a bit"""
pubtypes = gettypes()
2020-12-21 18:58:46 +01:00
pubyears = getyears()
publicenses = getlicenses()
publicatons = getpublications()
2020-12-21 18:58:46 +01:00
template = render_template(
"index.html",
publications=publicatons,
pubtypes=pubtypes,
pubyears=pubyears,
publicenses=publicenses,
2020-12-21 18:58:46 +01:00
)
return template
@APP.route("/<publicationID>")
def show_book(publicationID):
"""route for a publication, still needs to be made"""
fullpublication = getfullpublication(publicationID)
# parse csv, render template with full list.
return render_template("publication.html", fullpublication=fullpublication)
2021-02-06 17:08:07 +01:00
@APP.route("/pastevents")
def pastevents():
"""show past events and book recommendations"""
events = getevents()
2021-02-06 17:08:07 +01:00
return render_template("pastevents.html", events=events)
2021-02-14 22:11:58 +01:00
@APP.route("/upcoming")
def latestevent():
"""show upcoming or latest event and book recommendations"""
event = getlatestevent()
return render_template("upcomingevent.html", event=event)
@APP.context_processor
def upcoming_or_latest():
upcoming = True
ics = get("https://varia.zone/events.ics").text
gcal = Calendar.from_ical(ics)
eventtimes = [
c.get("dtstart").dt for c in gcal.walk()
if c.name == "VEVENT"
and "Read & Repair" in c.get("summary")
]
now = datetime.datetime.now()
eventtimes.sort()
eventtimes.reverse()
if now > eventtimes[0]:
upcoming = False
return dict(upcoming=upcoming)
if __name__ == "__main__":
APP.debug = True
APP.run(port=5000)