From 2e2ea9a72c11c6da0e2b44c659ac0d4ad51ae8ea Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Thu, 4 Nov 2021 20:58:35 -0700 Subject: [PATCH] Add all the code --- libertywiki/__init__.py | 3 +++ libertywiki/__main__.py | 5 ++++ libertywiki/app.py | 26 +++++++++++++++++++ libertywiki/db.py | 17 +++++++++++++ libertywiki/models.py | 45 +++++++++++++++++++++++++++++++++ libertywiki/templates/base.html | 32 +++++++++++++++++++++++ libertywiki/templates/edit.html | 18 +++++++++++++ libertywiki/templates/page.html | 19 ++++++++++++++ libertywiki/utils.py | 6 +++++ libertywiki/views.py | 39 ++++++++++++++++++++++++++++ setup.cfg | 2 ++ 11 files changed, 212 insertions(+) create mode 100644 libertywiki/__init__.py create mode 100644 libertywiki/__main__.py create mode 100644 libertywiki/app.py create mode 100644 libertywiki/db.py create mode 100644 libertywiki/models.py create mode 100644 libertywiki/templates/base.html create mode 100644 libertywiki/templates/edit.html create mode 100644 libertywiki/templates/page.html create mode 100644 libertywiki/utils.py create mode 100644 libertywiki/views.py create mode 100644 setup.cfg diff --git a/libertywiki/__init__.py b/libertywiki/__init__.py new file mode 100644 index 0000000..43f8419 --- /dev/null +++ b/libertywiki/__init__.py @@ -0,0 +1,3 @@ +from libertywiki.app import get_app + +__all__ = ['get_app'] diff --git a/libertywiki/__main__.py b/libertywiki/__main__.py new file mode 100644 index 0000000..1d443ed --- /dev/null +++ b/libertywiki/__main__.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +from libertywiki import get_app + +if __name__ == "__main__": + get_app().run(port=8080, debug=True) diff --git a/libertywiki/app.py b/libertywiki/app.py new file mode 100644 index 0000000..1834bb3 --- /dev/null +++ b/libertywiki/app.py @@ -0,0 +1,26 @@ +from flask import Flask + +from rst2html import rst2html + +from libertywiki.db import db +from libertywiki.views import wiki + + +def get_app(): + """Create and configure the application object""" + app = Flask(__name__) + db.init_app(app) + + with app.app_context(): + db.create_all() + + @app.template_filter('rst2html') + def rst2html_filter(text): + html, warning = rst2html(text) + print(html) + print(warning) + return html + + app.register_blueprint(wiki) + + return app diff --git a/libertywiki/db.py b/libertywiki/db.py new file mode 100644 index 0000000..485568f --- /dev/null +++ b/libertywiki/db.py @@ -0,0 +1,17 @@ +from flask_sqlalchemy import SQLAlchemy + +db = SQLAlchemy() +Model = db.Model +Boolean = db.Boolean +Column = db.Column +DateTime = db.DateTime +Float = db.Float +ForeignKey = db.ForeignKey +Integer = db.Integer +LargeBinary = db.LargeBinary +Table = db.Table +String = db.String +Text = db.Text +relationship = db.relationship +inspect = db.inspect +session = db.session diff --git a/libertywiki/models.py b/libertywiki/models.py new file mode 100644 index 0000000..802056c --- /dev/null +++ b/libertywiki/models.py @@ -0,0 +1,45 @@ +from datetime import datetime + +from sqlalchemy.ext.hybrid import hybrid_property + +from libertywiki.db import Model, Column, ForeignKey, DateTime, Integer, String, Text +from libertywiki.utils import bcrypt + + +class Page(Model): + """ + Page model + """ + __tablename__ = 'pages' + + id = Column(Integer, primary_key=True) + user_id = Column(Integer, ForeignKey('users.id')) + title = Column(String(255), nullable=False) + body = Column(Text) + slug = Column(String(255), nullable=False, index=True, unique=True) + created = Column(DateTime, default=datetime.now()) + modified = Column(DateTime, default=datetime.now()) + + def __str__(self): + return self.title + + +class User(Model): + """ + User model + """ + __tablename__ = 'users' + + id = Column(Integer, primary_key=True) + name = Column(String(255)) + email = Column(String(255), nullable=False, index=True, unique=True) + _password = Column('password', String(255), nullable=False) + activation_code = Column(String(255)) + + @hybrid_property + def password(self): + return self.password + + @password.setter + def password(self, value): + self._password = bcrypt.generate_password_hash(value) diff --git a/libertywiki/templates/base.html b/libertywiki/templates/base.html new file mode 100644 index 0000000..bbcd9b7 --- /dev/null +++ b/libertywiki/templates/base.html @@ -0,0 +1,32 @@ + + + + + + Wiki + + + + + + +
+{% block content %}{% endblock %} +
+ + + + diff --git a/libertywiki/templates/edit.html b/libertywiki/templates/edit.html new file mode 100644 index 0000000..33be6b3 --- /dev/null +++ b/libertywiki/templates/edit.html @@ -0,0 +1,18 @@ +{% extends 'base.html' %} +{% block content %} +
+
+
+ + +
The title of the page
+
+
+ + +
The content of the page, in reStructuredText
+
+ +
+
+{% endblock %} diff --git a/libertywiki/templates/page.html b/libertywiki/templates/page.html new file mode 100644 index 0000000..5d23267 --- /dev/null +++ b/libertywiki/templates/page.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% block content %} +
+ +

{{ page.title }}

+
+
+ {{ page.body | rst2html | safe }} +
+
+
+{% endblock %} diff --git a/libertywiki/utils.py b/libertywiki/utils.py new file mode 100644 index 0000000..938efa2 --- /dev/null +++ b/libertywiki/utils.py @@ -0,0 +1,6 @@ +from flask_bcrypt import Bcrypt + +bcrypt = Bcrypt() + + +__all__ = ['bcrypt'] diff --git a/libertywiki/views.py b/libertywiki/views.py new file mode 100644 index 0000000..40907e1 --- /dev/null +++ b/libertywiki/views.py @@ -0,0 +1,39 @@ +from datetime import datetime + +from flask import Blueprint, request, render_template, redirect + +from libertywiki.db import session +from libertywiki.models import Page + +wiki = Blueprint('wiki', __name__, url_prefix='') + + +@wiki.route('/', defaults={'path': 'Main_Page'}, methods=['GET']) +@wiki.route('/', methods=['GET']) +def index(path=None): + page = Page.query.filter_by(slug=path).first() + if not page: + return redirect('/{}/edit'.format(path)) + return render_template('page.html', page=page) + + +@wiki.route('//edit', methods=['GET']) +def edit(path): + page = Page.query.filter_by(slug=path).first() + if not page: + page = Page(title=path.replace('_', ' ')) + return render_template('edit.html', page=page) + + +@wiki.route("//edit", methods=['POST']) +def save(path): + page = Page.query.filter_by(slug=path).first() + if not page: + page = Page() + page.title = request.form.get('page-title') + page.body = request.form.get('page-body') + page.slug = page.title.replace(' ', '_') + page.modified = datetime.now() + session.add(page) + session.commit() + return redirect('/{}'.format(page.slug)) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..6deafc2 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,2 @@ +[flake8] +max-line-length = 120