# Path: hakoblog/db.py
# class DB:
#     def __init__(self):
#         self.conn = MySQLdb.connect(
#             db=CONFIG.DATABASE,
#             host=CONFIG.DATABASE_HOST,
#             user=CONFIG.DATABASE_USER,
#             password=CONFIG.DATABASE_PASS,
#             cursorclass=MySQLdb.cursors.DictCursor,
#             charset="utf8",
#         )
#         self.conn.autocommit(True)
# 
#     def cursor(self):
#         return self.conn.cursor()
# 
#     def close(self):
#         self.conn.close()
# 
#     def uuid_short(self):
#         with self.conn.cursor() as cursor:
#             cursor.execute("SELECT UUID_SHORT() as uuid")
#             return cursor.fetchone().get("uuid")
# 
# Path: hakoblog/config.py
# CONFIG = config()
# 
# Path: hakoblog/action/blog.py
# class BlogAction:
#     @classmethod
#     def create(self, db, owner_id, title):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO blog (
#                         id, owner_id, title, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, owner_id, title, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_blog_created(cls, db):
#         global_user = UserAction.ensure_global_user_created(db)
#         blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         if blog is None:
#             cls.create(db, global_user.id, "%s's blog" % (global_user.name,))
#             blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         return blog
# 
# Path: hakoblog/loader/entry.py
# class EntryLoader:
#     @classmethod
#     def find_by_id(cls, db, entry_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, blog_id, title, body, created, modified
#                 FROM entry
#                 WHERE id = %s
#                 LIMIT 1
#                 """,
#                 (entry_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Entry(**row)
# 
#     @classmethod
#     def find_entries(cls, db, blog_id, limit=30):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, blog_id, title, body, created, modified
#                 FROM entry
#                 WHERE blog_id = %s
#                 ORDER BY created DESC
#                 LIMIT %s
#                 """,
#                 (blog_id, limit),
#             )
#             rows = cursor.fetchall()
# 
#         return [Entry(**r) for r in rows]
# 
# Path: hakoblog/action/entry.py
# class EntryAction:
#     @classmethod
#     def post(cls, db, blog_id, title, body):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 INSERT INTO entry (
#                     id, blog_id, title, body, created, modified
#                 ) VALUES (
#                     %s, %s, %s, %s, %s, %s
#                 )
#                 """,
#                 (new_id, blog_id, title, body, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def edit(cls, db, entry_id, title, body):
#         now = datetime.now()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 UPDATE entry SET
#                     title = %s,
#                     body = %s,
#                     modified = %s
#                 WHERE id = %s
#                 """,
#                 (title, body, now, entry_id),
#             )
# 
#     @classmethod
#     def delete(cls, db, entry_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 DELETE FROM entry
#                 WHERE id = %s
#                 """,
#                 (entry_id,),
#             )


from flask import (
    Flask,
    g as flask_g,
    render_template,
    request,
    abort,
    url_for,
    redirect,
)
from hakoblog.db import DB
from hakoblog.config import CONFIG
from hakoblog.action.blog import BlogAction
from hakoblog.loader.entry import EntryLoader
from hakoblog.action.entry import EntryAction
def entry(entry_id):
    blog = BlogAction.ensure_global_blog_created(get_db())

    entry = EntryLoader.find_by_id(get_db(), entry_id)
    if entry is None:
        abort(404)
    if entry.blog_id != blog.id:
        abort(403)

    return render_template("entry.html", blog=blog, entry=entry)


@web.route("/-/post", methods=["GET"])
def post_get():
    blog = BlogAction.ensure_global_blog_created(get_db())

    return render_template("post.html", blog=blog)


@web.route("/-/post", methods=["POST"])
def post_post():
    blog = BlogAction.ensure_global_blog_created(get_db())

    title = request.form["title"]
    body = request.form["body"]
    blog_id = int(request.form["blog_id"])

    if int(blog_id) != blog.id:
        abort(400)


# Path: hakoblog/db.py
# class DB:
#     def __init__(self):
#         self.conn = MySQLdb.connect(
#             db=CONFIG.DATABASE,
#             host=CONFIG.DATABASE_HOST,
#             user=CONFIG.DATABASE_USER,
#             password=CONFIG.DATABASE_PASS,
#             cursorclass=MySQLdb.cursors.DictCursor,
#             charset="utf8",
#         )
#         self.conn.autocommit(True)
# 
#     def cursor(self):
#         return self.conn.cursor()
# 
#     def close(self):
#         self.conn.close()
# 
#     def uuid_short(self):
#         with self.conn.cursor() as cursor:
#             cursor.execute("SELECT UUID_SHORT() as uuid")
#             return cursor.fetchone().get("uuid")
# 
# Path: hakoblog/config.py
# CONFIG = config()
# 
# Path: hakoblog/action/blog.py
# class BlogAction:
#     @classmethod
#     def create(self, db, owner_id, title):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                     INSERT INTO blog (
#                         id, owner_id, title, created, modified
#                     ) VALUES (
#                         %s, %s, %s, %s, %s
#                     )
#                 """,
#                 (new_id, owner_id, title, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def ensure_global_blog_created(cls, db):
#         global_user = UserAction.ensure_global_user_created(db)
#         blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         if blog is None:
#             cls.create(db, global_user.id, "%s's blog" % (global_user.name,))
#             blog = BlogLoader.find_by_owner_id(db, global_user.id)
# 
#         return blog
# 
# Path: hakoblog/loader/entry.py
# class EntryLoader:
#     @classmethod
#     def find_by_id(cls, db, entry_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, blog_id, title, body, created, modified
#                 FROM entry
#                 WHERE id = %s
#                 LIMIT 1
#                 """,
#                 (entry_id,),
#             )
#             row = cursor.fetchone()
# 
#             if row is None:
#                 return None
# 
#         return Entry(**row)
# 
#     @classmethod
#     def find_entries(cls, db, blog_id, limit=30):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 SELECT id, blog_id, title, body, created, modified
#                 FROM entry
#                 WHERE blog_id = %s
#                 ORDER BY created DESC
#                 LIMIT %s
#                 """,
#                 (blog_id, limit),
#             )
#             rows = cursor.fetchall()
# 
#         return [Entry(**r) for r in rows]
# 
# Path: hakoblog/action/entry.py
# class EntryAction:
#     @classmethod
#     def post(cls, db, blog_id, title, body):
#         now = datetime.now()
#         new_id = db.uuid_short()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 INSERT INTO entry (
#                     id, blog_id, title, body, created, modified
#                 ) VALUES (
#                     %s, %s, %s, %s, %s, %s
#                 )
#                 """,
#                 (new_id, blog_id, title, body, now, now),
#             )
#         return new_id
# 
#     @classmethod
#     def edit(cls, db, entry_id, title, body):
#         now = datetime.now()
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 UPDATE entry SET
#                     title = %s,
#                     body = %s,
#                     modified = %s
#                 WHERE id = %s
#                 """,
#                 (title, body, now, entry_id),
#             )
# 
#     @classmethod
#     def delete(cls, db, entry_id):
#         with db.cursor() as cursor:
#             cursor.execute(
#                 """
#                 DELETE FROM entry
#                 WHERE id = %s
#                 """,
#                 (entry_id,),
#             )


# Path: hakoblog/web.py
from flask import (
    Flask,
    g as flask_g,
    render_template,
    request,
    abort,
    url_for,
    redirect,
)
from hakoblog.db import DB
from hakoblog.config import CONFIG
from hakoblog.action.blog import BlogAction
from hakoblog.loader.entry import EntryLoader
from hakoblog.action.entry import EntryAction

def entry(entry_id):
    blog = BlogAction.ensure_global_blog_created(get_db())

    entry = EntryLoader.find_by_id(get_db(), entry_id)
    if entry is None:
        abort(404)
    if entry.blog_id != blog.id:
        abort(403)

    return render_template("entry.html", blog=blog, entry=entry)


@web.route("/-/post", methods=["GET"])
def post_get():
    blog = BlogAction.ensure_global_blog_created(get_db())

    return render_template("post.html", blog=blog)


@web.route("/-/post", methods=["POST"])
def post_post():
    blog = BlogAction.ensure_global_blog_created(get_db())

    title = request.form["title"]
    body = request.form["body"]
    blog_id = int(request.form["blog_id"])

    if int(blog_id) != blog.id:
        abort(400)


    EntryAction.post(get_db(), blog_id=blog.id, title=title, body=body)