Beispiel #1
0
def edit_one(post_id):
    post_id = Obfuscator.restore(post_id)
    db = g.db
    post = db.query(Post).filter_by(id=post_id).one()
    if not post:
        return abort(400)
    return render_template('edit.html', post=post, action=url_for('post_update', post_id=post_id), method='PUT')
Beispiel #2
0
def index():
    p = int(request.args.get('p') or 1)
    tag_id = request.args.get('tag')
    author_id = request.args.get('author')
    month_str = request.args.get('month')
    tag_idint = None
    author_idint = None
    tag_name = None
    author_name = None
    month = None

    if tag_id:
        tag_idint = Obfuscator.restore(tag_id)
        tag_name = getattr(Tag.get(tag_idint), 'name', None)
    if author_id:
        author_idint = Obfuscator.restore(author_id)
        author_name = getattr(Author.get(author_idint), 'name', None)
    if month_str:
        try:
            month = datetime.strptime(month_str, '%Y-%m')
        except ValueError:
            month = None

    posts, count = Post.query(p=p, num=SITE['num_per_page'], tag_id=tag_idint, author_id=author_idint, month=month)

    page_num = math.ceil(count / SITE['num_per_page'])
    next_url = url_for('index', p=p+1, tag=tag_id, author=author_id, month=month_str) if p < page_num else None
    prev_url = url_for('index', p=p-1, tag=tag_id, author=author_id, month=month_str) if p > 1 else None

    context = dict(
        posts=posts,
        tag_name=tag_name,
        author_name=author_name,
        month=month,
        next_url=next_url,
        prev_url=prev_url,
        p=p,
        title=SITE['title'],
    )
    return render_template('index.html', **context)
Beispiel #3
0
def manage():
    p = int(request.args.get('p') or 1)
    tag_id = request.args.get('tag') or None
    tag_idint = None
    if tag_id:
        tag_idint = Obfuscator.restore(tag_id)
    deleted = int(request.args.get('deleted') or 0)

    posts, count = Post.query(p=p, num=SITE['num_per_page'], tag_id=tag_idint, author_id=session['author_id'],
                              deleted=deleted)

    tp = math.ceil(count / SITE['num_per_page'])
    prev_url = url_for('manage', p=p-1, tag=tag_id) if p > 1 else ''
    next_url = url_for('manage', p=p+1, tag=tag_id) if p < tp else ''
    return render_template('manage.html', posts=posts, prev_url=prev_url, next_url=next_url, deleted=deleted)
Beispiel #4
0
def archive():
    db = g.db
    dates = db.query(Post.published).all()
    month = {}
    for d in dates:
        date_str = d.published.strftime('%Y-%m')
        if month.get(date_str, None) is None:
            month[date_str] = 1
        else:
            month[date_str] += 1

    tags = db.query(Tag.id, Tag.name, func.count(Tag.id).label('count'))\
             .join(PostTag, Tag.id == PostTag.tag_id)\
             .group_by(Tag.id).all()
    for t in tags:
        t.idstr = Obfuscator.obfuscate(t.id)

    return render_template('archive.html', month=month, tags=tags, title=SITE['title'])
Beispiel #5
0
def post_get(post_id):
    post_id = Obfuscator.restore(post_id)
    db = g.db
    post = db.query(Post).filter_by(id=post_id).first()
    return render_template('post.html', post=post, title=SITE['title'])
Beispiel #6
0
def mpost_get(post_id):
    db = g.db
    post = db.query(Post).filter_by(id=Obfuscator.restore(post_id)).first()
    return render_template('mpost.html', post=post, title=SITE['title'], url=SITE['url'] + '/posts/' + post_id)
Beispiel #7
0
__author__ = 'wangdai'

import os

from flask import Flask, g
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

JERRY_DIR = os.path.dirname(__file__)
DB_PATH = 'sqlite:///%s/jerry.db' % JERRY_DIR
DB_ENGINE = create_engine(DB_PATH)
MODEL_BASE = declarative_base(bind=DB_ENGINE)
DB_SESSION = sessionmaker(bind=DB_ENGINE)

APP = Flask(__name__)
APP.secret_key = os.urandom(24)
# APP.permanent_session_lifetime = 10
APP.jinja_env.filters['date_format'] = lambda date, format_string='%Y-%m-%d': date.strftime(format_string)

import jerry.views  # register routes to APP
import jerry.models  # register models to MODEL_BASE.metadata
from jerry.utils import Obfuscator

MODEL_BASE.metadata.create_all()
MODEL_BASE.to_dict = lambda self: {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
MODEL_BASE.get = classmethod(lambda cls, obj_id: None if not obj_id else g.db.query(cls).filter_by(id=obj_id).first())
MODEL_BASE.idstr = property(lambda self: Obfuscator.obfuscate(self.id))