# Path: maple/model.py
# class User(db.Model, UserMixin):
#     __tablename__ = 'user'
# 
#     @property
#     def is_admin(self):
#         return self.is_authenticated and self.is_superuser
# 
#     @property
#     def token(self):
#         return self.email_token
# 
#     @classmethod
#     def check_token(cls, token, max_age=24 * 3600 * 30):
#         return cls.check_email_token(token, max_age)
# 
# Path: maple/blog/db.py
# class Article(db.Model, ModelUserMixin):
#     __tablename__ = 'article'
# 
#     CONTENT_TYPE_MARKDOWN = 0
#     CONTENT_TYPE_ORGMODE = 1
# 
#     CONTENT_TYPE = ((0, 'markdown'), (1, 'org-mode'))
# 
#     id = db.Column(db.Integer, primary_key=True)
#     title = db.Column(db.String(50), nullable=False)
#     content = db.Column(db.Text, nullable=False)
#     content_type = db.Column(
#         db.Integer, nullable=False, default=CONTENT_TYPE_MARKDOWN)
# 
#     category_id = db.Column(
#         db.Integer,
#         db.ForeignKey('category.id', ondelete="CASCADE"),
#         nullable=False)
#     category = db.relationship(
#         'Category',
#         backref=db.backref(
#             'articles', cascade='all,delete-orphan', lazy='dynamic'),
#         uselist=False,
#         lazy='joined')
# 
#     __mapper_args__ = {"order_by": text("created_at desc")}
# 
#     def __repr__(self):
#         return "<Article %r>" % self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def to_json(self):
#         return {
#             'id': self.id,
#             'title': self.title,
#             'category': self.category.name,
#             'tags': ','.join([tag.name for tag in self.tags]),
#         }
# 
#     def to_html(self, length=None, truncate=False):
#         length = length or current_app.config.get("SUMMARY_MAX_LENGTH")
#         if not truncate:
#             length = None
#         if self.content_type == self.CONTENT_TYPE_MARKDOWN:
#             return markdown_to_html(self.content, length)
#         return orgmode_to_html(self.content, length)
# 
#     @property
#     def htmlcontent(self):
#         return self.to_html()
# 
#     @property
#     def next_article(self):
#         return Article.query.filter_by(id__lt=self.id).order_by("-id").first()
# 
#     @property
#     def previous_article(self):
#         return Article.query.filter_by(id__gt=self.id).order_by("id").first()
# 
#     @property
#     def read_times(self):
#         return Count.get('article:{}'.format(self.id))
# 
#     @read_times.setter
#     def read_times(self, value):
#         Count.set('article:{}'.format(self.id))
# 
# class Tag(db.Model, ModelMixin):
#     __tablename__ = 'tag'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     articles = db.relationship(
#         'Article',
#         secondary=article_tags,
#         backref=db.backref('tags', lazy='dynamic'),
#         lazy='dynamic')
# 
#     def __repr__(self):
#         return '<Tags %r>' % self.name
# 
#     def __str__(self):
#         return self.name
# 
# class Category(db.Model, ModelMixin):
#     __tablename__ = 'category'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), nullable=False)
# 
#     def __repr__(self):
#         return '<Category %r>' % self.name
# 
#     def __str__(self):
#         return self.name


import os
import re
from maple.model import User
from maple.blog.db import Article, Tag, Category
from getpass import getpass
from datetime import datetime
    category = Category.query.filter_by(name=name).first()
    if not category:
        category = Category(name=name)
        category.save()
    return category


def add_author(username):
    username = "honmaple"
    email = "mail@honmaple.com"
    author = User.query.filter_by(username=username).first()
    if not author:
        author = User(
            username=username,
            email=email,
            is_superuser=True,
            is_confirmed=True,
        )
        author.set_password(getpass("Password: "))
        author.save()
    return author


def add_article(title,
                content,
                category,
                author,
                tags,
                date,
                content_type="org-mode"):

# Path: maple/model.py
# class User(db.Model, UserMixin):
#     __tablename__ = 'user'
# 
#     @property
#     def is_admin(self):
#         return self.is_authenticated and self.is_superuser
# 
#     @property
#     def token(self):
#         return self.email_token
# 
#     @classmethod
#     def check_token(cls, token, max_age=24 * 3600 * 30):
#         return cls.check_email_token(token, max_age)
# 
# Path: maple/blog/db.py
# class Article(db.Model, ModelUserMixin):
#     __tablename__ = 'article'
# 
#     CONTENT_TYPE_MARKDOWN = 0
#     CONTENT_TYPE_ORGMODE = 1
# 
#     CONTENT_TYPE = ((0, 'markdown'), (1, 'org-mode'))
# 
#     id = db.Column(db.Integer, primary_key=True)
#     title = db.Column(db.String(50), nullable=False)
#     content = db.Column(db.Text, nullable=False)
#     content_type = db.Column(
#         db.Integer, nullable=False, default=CONTENT_TYPE_MARKDOWN)
# 
#     category_id = db.Column(
#         db.Integer,
#         db.ForeignKey('category.id', ondelete="CASCADE"),
#         nullable=False)
#     category = db.relationship(
#         'Category',
#         backref=db.backref(
#             'articles', cascade='all,delete-orphan', lazy='dynamic'),
#         uselist=False,
#         lazy='joined')
# 
#     __mapper_args__ = {"order_by": text("created_at desc")}
# 
#     def __repr__(self):
#         return "<Article %r>" % self.title
# 
#     def __str__(self):
#         return self.title
# 
#     def to_json(self):
#         return {
#             'id': self.id,
#             'title': self.title,
#             'category': self.category.name,
#             'tags': ','.join([tag.name for tag in self.tags]),
#         }
# 
#     def to_html(self, length=None, truncate=False):
#         length = length or current_app.config.get("SUMMARY_MAX_LENGTH")
#         if not truncate:
#             length = None
#         if self.content_type == self.CONTENT_TYPE_MARKDOWN:
#             return markdown_to_html(self.content, length)
#         return orgmode_to_html(self.content, length)
# 
#     @property
#     def htmlcontent(self):
#         return self.to_html()
# 
#     @property
#     def next_article(self):
#         return Article.query.filter_by(id__lt=self.id).order_by("-id").first()
# 
#     @property
#     def previous_article(self):
#         return Article.query.filter_by(id__gt=self.id).order_by("id").first()
# 
#     @property
#     def read_times(self):
#         return Count.get('article:{}'.format(self.id))
# 
#     @read_times.setter
#     def read_times(self, value):
#         Count.set('article:{}'.format(self.id))
# 
# class Tag(db.Model, ModelMixin):
#     __tablename__ = 'tag'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     articles = db.relationship(
#         'Article',
#         secondary=article_tags,
#         backref=db.backref('tags', lazy='dynamic'),
#         lazy='dynamic')
# 
#     def __repr__(self):
#         return '<Tags %r>' % self.name
# 
#     def __str__(self):
#         return self.name
# 
# class Category(db.Model, ModelMixin):
#     __tablename__ = 'category'
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(64), nullable=False)
# 
#     def __repr__(self):
#         return '<Category %r>' % self.name
# 
#     def __str__(self):
#         return self.name


# Path: script/page.py
import os
import re
from maple.model import User
from maple.blog.db import Article, Tag, Category
from getpass import getpass
from datetime import datetime

    category = Category.query.filter_by(name=name).first()
    if not category:
        category = Category(name=name)
        category.save()
    return category


def add_author(username):
    username = "honmaple"
    email = "mail@honmaple.com"
    author = User.query.filter_by(username=username).first()
    if not author:
        author = User(
            username=username,
            email=email,
            is_superuser=True,
            is_confirmed=True,
        )
        author.set_password(getpass("Password: "))
        author.save()
    return author


def add_article(title,
                content,
                category,
                author,
                tags,
                date,
                content_type="org-mode"):

    article = Article(