From 2f7474f898a95bb4c111a194a29c1f255d3fa2f7 Mon Sep 17 00:00:00 2001 From: haskal Date: Mon, 17 Jun 2019 10:53:59 +0200 Subject: [PATCH] Commit things that haven't been committed for a few days --- wikilain/blueprints/article_default.py | 23 +++++++++-------- .../blueprints/templates/article_index.html | 25 +++++++++++++++++++ wikilain/blueprints/templates/user_list.html | 2 +- wikilain/blueprints/user_default.py | 4 +-- wikilain/models.py | 7 ++++-- wikilain/templates/base.html | 2 +- wikilain/util.py | 17 +++++++++++++ 7 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 wikilain/blueprints/templates/article_index.html create mode 100644 wikilain/util.py diff --git a/wikilain/blueprints/article_default.py b/wikilain/blueprints/article_default.py index 37878f4..dd701b2 100644 --- a/wikilain/blueprints/article_default.py +++ b/wikilain/blueprints/article_default.py @@ -1,23 +1,26 @@ from flask import Blueprint, render_template, g, session, redirect, request, url_for, flash, current_app, abort import secrets from datetime import datetime -import re from sqlalchemy import func from ..models import * from ..markdown import * +from ..util import * article_provider = Blueprint("article", __name__, template_folder='templates') -blacklisted_titles = ["new", "by-id"] -allowed_titles = re.compile(r"^[a-zA-Z0-9 \(\)\.\,:;]+$") - -def urlify(title): - return title.replace(" ", "_") +class DefaultSection: + def __init__(self, articles): + self.title = "No Section" + self.articles = articles -def unurlify(title): - return title.replace("_", " ") +@article_provider.route("/") +def index(): + sections = Section.query.all() + uncat = Article.query.filter_by(section_id=None).all() + sections.append(DefaultSection(uncat)) + return render_template("article_index.html", sections=sections) @article_provider.route("/") @@ -55,9 +58,7 @@ def new(): return render_template("article_new.html", csrf=csrf, article_title=article_title, article_content=article_content) - if (len(article_title) == 0 - or article_title in blacklisted_titles - or not allowed_titles.match(article_title)): + if not title_valid(article_title): flash("Invalid title", "error") return render_template("article_new.html", csrf=csrf, article_title=article_title, diff --git a/wikilain/blueprints/templates/article_index.html b/wikilain/blueprints/templates/article_index.html new file mode 100644 index 0000000..029a4fd --- /dev/null +++ b/wikilain/blueprints/templates/article_index.html @@ -0,0 +1,25 @@ +{% extends 'base.html' %} + +{% block title %} +Articles +{% endblock %} + +{% block content %} + <h2> + Articles + </h2> + <ul> + {% for section in sections %} + <li> + {{ section.title }} + <ul> + {% for article in section.articles %} + <a href="{{ url_for("article.show", title=article.title_urlify()) }}"> + {{ article.title }} + </a> + {% endfor %} + </ul> + </li> + {% endfor %} + </ul> +{% endblock %} diff --git a/wikilain/blueprints/templates/user_list.html b/wikilain/blueprints/templates/user_list.html index 6a5c38e..48ec428 100644 --- a/wikilain/blueprints/templates/user_list.html +++ b/wikilain/blueprints/templates/user_list.html @@ -6,7 +6,7 @@ User List {% block content %} <h2> - User list + User List </h2> <ul> {% for user in users %} diff --git a/wikilain/blueprints/user_default.py b/wikilain/blueprints/user_default.py index 683d100..488b46f 100644 --- a/wikilain/blueprints/user_default.py +++ b/wikilain/blueprints/user_default.py @@ -8,13 +8,13 @@ __all__ = ["user_provider"] user_provider = Blueprint("user", __name__, template_folder='templates') @user_provider.route("/") -def show_user_list(): +def list(): users = User.query.all() # TODO: pagination... return render_template("user_list.html", users=users) @user_provider.route("/<username>") -def show_user(username): +def show(username): is_self = g.wl_user is not None and username == g.wl_user.username if is_self: user = g.wl_user diff --git a/wikilain/models.py b/wikilain/models.py index 39a596d..2980462 100644 --- a/wikilain/models.py +++ b/wikilain/models.py @@ -1,6 +1,8 @@ from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate +from .util import urlify + db = SQLAlchemy() migrate = Migrate(None, db) @@ -23,8 +25,6 @@ class Section(db.Model): def __repr__(self): return f"<Section \"{self.title}\">" -DEFAULT_SECTION = 1 - class Article(db.Model): __tablename__ = "articles" id = db.Column(db.Integer, primary_key=True) @@ -35,6 +35,9 @@ class Article(db.Model): def __repr__(self): return f"<Article \"{self.title}\">" + def title_urlify(self): + return urlify(self.title) + class ArticleRevision(db.Model): __tablename__ = "articlerevisions" id = db.Column(db.Integer, primary_key=True) diff --git a/wikilain/templates/base.html b/wikilain/templates/base.html index fd42516..5621e3e 100644 --- a/wikilain/templates/base.html +++ b/wikilain/templates/base.html @@ -16,7 +16,7 @@ <input type="text" name="q" placeholder="Search" /> </form> </li> - <li><a href="/sections">Sections</a></li> + <li><a href="{{ url_for("article.index") }}">Article Index</a></li> {% if not g.wl_user %} <li> <a href="{{ url_for("session.login") }}?redirect={{ request.path }}"> diff --git a/wikilain/util.py b/wikilain/util.py new file mode 100644 index 0000000..c1d1816 --- /dev/null +++ b/wikilain/util.py @@ -0,0 +1,17 @@ +import re + +blacklisted_titles = ["new", "by-id"] +allowed_titles = re.compile(r"^[a-zA-Z0-9 \(\)\.\,:;]+$") + +def urlify(title): + return title.replace(" ", "_") + + +def unurlify(title): + return title.replace("_", " ") + + +def title_valid(title): + return (len(article_title) > 0 + and article_title not in blacklisted_titles + and allowed_titles.match(article_title))