You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.9 KiB
Python
93 lines
2.9 KiB
Python
2 years ago
|
# THE LIBRARY #
|
||
|
|
||
2 years ago
|
|
||
|
from itertools import groupby # to handle complex iterations
|
||
2 years ago
|
import os
|
||
2 years ago
|
|
||
|
import sqlite3
|
||
|
from flask import Flask, render_template, url_for, request, redirect
|
||
|
|
||
|
|
||
|
# ----- functions ----- #
|
||
|
|
||
2 years ago
|
def get_db_connection():
|
||
|
conn = sqlite3.connect('library.db')
|
||
|
conn.row_factory = sqlite3.Row
|
||
|
return conn
|
||
|
# Added row_factory attribute to the sqlite connection, in this way you can have name-based access to columns; this means that the database connection will return rows that behave like regular Python dictionaries.
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
# ----- FLASK ----- #
|
||
|
|
||
|
class PrefixMiddleware(object):
|
||
|
def __init__(self, app, prefix=""):
|
||
|
self.app = app
|
||
|
self.prefix = prefix
|
||
|
|
||
|
def __call__(self, environ, start_response):
|
||
|
|
||
|
if environ["PATH_INFO"].startswith(self.prefix):
|
||
2 years ago
|
environ["PATH_INFO"] = environ["PATH_INFO"][len(self.prefix):]
|
||
2 years ago
|
environ["SCRIPT_NAME"] = self.prefix
|
||
|
return self.app(environ, start_response)
|
||
|
else:
|
||
|
start_response("404", [("Content-Type", "text/plain")])
|
||
|
return ["This url does not belong to the app.".encode()]
|
||
|
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
2 years ago
|
# register the middleware to prefix all the requests with our base_url
|
||
2 years ago
|
app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix='/soupboat/library')
|
||
|
|
||
2 years ago
|
|
||
2 years ago
|
@app.route("/")
|
||
2 years ago
|
def home():
|
||
2 years ago
|
conn = get_db_connection()
|
||
|
todos = conn.execute('SELECT c.content, cat.title FROM cards c JOIN categories cat \
|
||
2 years ago
|
ON c.category_id = cat.id ORDER BY cat.title').fetchall()
|
||
|
|
||
2 years ago
|
categories = {}
|
||
2 years ago
|
|
||
2 years ago
|
# for each category and group of cards for each cat in groupby() grouper object
|
||
|
for k, g in groupby(todos, key=lambda t: t['title']):
|
||
|
categories[k] = list(g)
|
||
2 years ago
|
print(categories[k])
|
||
|
for cat, cards in categories.items(): # ♥ .items is a build in attribute of the dictionary(?)
|
||
|
print(cat)
|
||
|
for card in cards:
|
||
|
print(' ', card['content'])
|
||
|
|
||
2 years ago
|
# if request.method == 'POST':
|
||
|
# title = request.form.get('title')
|
||
|
# author = request.form.get('author')
|
||
|
# description = request.form.get('description')
|
||
|
# add_book(author, title, description)
|
||
|
# return redirect(url_for('home'))
|
||
2 years ago
|
|
||
2 years ago
|
return render_template('home.html', categories=categories)
|
||
2 years ago
|
|
||
2 years ago
|
|
||
2 years ago
|
# @app.route("/add", methods=['GET', 'POST'])
|
||
|
# def create():
|
||
|
# # the goal here is to choose from a list of parameters what you want to add
|
||
|
# # 1- ogni blocco del form dovrebbe essere una tabella a parte
|
||
|
# if request.method == 'POST':
|
||
|
# title = request.form.get('title')
|
||
|
# author = request.form.get('author')
|
||
|
# description = request.form.get('description')
|
||
|
# add_book(author, title, description)
|
||
|
# # if author:
|
||
|
# # return url_for('add_new_author')
|
||
2 years ago
|
|
||
2 years ago
|
# return redirect(url_for('home'))
|
||
|
# return render_template('add_new.html')
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
# app.run(port=3148)
|
||
2 years ago
|
|
||
|
|
||
2 years ago
|
# TODO:
|
||
|
# - list the cards
|
||
2 years ago
|
# - put its category inside
|