예제 #1
0
from flask import Blueprint
from flask import request
from flask import g
from flask import url_for
from flask import redirect

from pastecookie import app
from pastecookie import render

#from pastecookie.forms import *
from pastecookie.models import *

PAGE_SIZE = app.config.get('PAGE_SIZE')
SIDEBAR_PAGE_SIZE = app.config.get('SIDEBAR_PAGE_SIZE')

rankview = Blueprint('rankview', __name__)


@rankview.route('/', methods=['GET'])
def rank():
    g.top_tags = Tag.query.order_by('times DESC')[:SIDEBAR_PAGE_SIZE]
    g.top_view_pastes = Paste.query.filter_by(is_private=False).filter_by(
        is_delete=False).order_by('view_num DESC')[:SIDEBAR_PAGE_SIZE]
    g.top_comment_pastes = Paste.query.filter_by(
        is_private=False).order_by('comment_num DESC')[:SIDEBAR_PAGE_SIZE]
    g.users = User.query.order_by('paste_num DESC')[:SIDEBAR_PAGE_SIZE]
    return render('rankview/rank.html')


app.register_blueprint(rankview, url_prefix='/rank')
예제 #2
0
def posts():
    page = request.args.get('page', 1)
    try:
        page = int(page)
    except:
        page = 1
    g.pagination = Post.query.order_by("created_time DESC").paginate(page, 10)
    return render('siteview/posts.html')

@siteview.route('/post/<object_id>')
def post(object_id=None):
    if not object_id:
        abort(404)
    g.post = Post.query.get_or_404(object_id)
    return render('siteview/post.html')

@siteview.route("/test")
def test():
    send_mail_to_queue(from_user="******",
                       to_user="******",
                       title=u"来试试中文",
                       content=u"打断的中文熬撒旦噶速度过来说的话过来撒很给力")
    return "hello!"

@siteview.route('/rss.xml')
def rss():
    g.pastes = Paste.query.filter_by(is_private=False).order_by("created_time DESC").all()
    return render('rss/site.xml')

app.register_blueprint(siteview)
예제 #3
0
        title = request.form.get('title', None)
        content = request.form.get('content', None)
        if post_id:
            post = Post.query.get_or_404(id=post_id)
            post.title = title
            post.content = content
            post.modified_time = datetime.now()
            db.session.add(post)
            try:
                db.session.commit()
            except:
                db.session.rollback()
                abort(500)
            return redirect(url_for('adminview.posts'))
        else:
            post = Post(title, content)
            db.session.add(post)
            return redirect(url_for('adminview.posts'))
    if request.args.get('object_id', None) and request.args.get(
            'delete', None) == 'True':
        post = Post.query.filter_by(
            id=request.args.get('object_id', None)).first()
        if post:
            db.session.delete(post)
        return redirect(url_for('adminview.posts'))
    g.posts = Post.query.all()
    return render_template('adminview/posts.html')


app.register_blueprint(adminview, url_prefix='/admin')
예제 #4
0
@userview.route('/follow')
def follow():
    state = ''
    if 'user' not in session:
        return json_response({'result': 'fail', 'message': u'请先登陆!'})
    object_id = request.args.get('id', None)
    if object_id:
        model = User.query.get_or_404(object_id)
        user = getUserObject()
        if model.id == user.id:
            return json_response({'result': 'fail', 'message': u'自己不能关注自己'})
        if user not in model.followers:
            model.followers.append(user)
            state = 'follow'
        else:
            model.followers.remove(user)
            state = 'unfollow'
        db.session.add(model)
        return json_response({'result': 'success', 'state': state})


@userview.route('/<user_id>/rss.xml')
def rss(user_id):
    g.user = User.query.get_or_404(user_id)
    g.pastes = Paste.query.filter_by(
        user_id=user_id, is_private=False).order_by("created_time DESC").all()
    return render('rss/user.xml')


app.register_blueprint(userview, url_prefix='/user')
예제 #5
0
#-*-coding:utf-8-*-
import simplejson as json

from flask import Blueprint
from flask import request
from flask import g
from flask import url_for
from flask import redirect

from pastecookie import app
from pastecookie import render

#from pastecookie.forms import *
from pastecookie.models import *

PAGE_SIZE = app.config.get('PAGE_SIZE')
SIDEBAR_PAGE_SIZE = app.config.get('SIDEBAR_PAGE_SIZE')

rankview = Blueprint('rankview', __name__)

@rankview.route('/', methods=['GET'])
def rank():
    g.top_tags = Tag.query.order_by('times DESC')[:SIDEBAR_PAGE_SIZE]
    g.top_view_pastes = Paste.query.filter_by(is_private=False).filter_by(is_delete=False).order_by('view_num DESC')[:SIDEBAR_PAGE_SIZE]
    g.top_comment_pastes = Paste.query.filter_by(is_private=False).order_by('comment_num DESC')[:SIDEBAR_PAGE_SIZE]
    g.users = User.query.order_by('paste_num DESC')[:SIDEBAR_PAGE_SIZE]
    return render('rankview/rank.html')

app.register_blueprint(rankview,  url_prefix='/rank')
예제 #6
0
        for paste in pagination.items]})

@tagview.route('/follow', methods=['POST'])
def follow():
    if 'user' not in session:
        return json_response({'result':'fail', 'message': u'请先登录'})
    state = ''
    object_id = request.form.get('id', None)
    if object_id:
        tag = Tag.query.get_or_404(object_id)
        user = getUserObject()
        if user not in tag.followers:
            tag.followers.append(user)
            state = 'follow'
        else:
            tag.followers.remove(user)
            state = 'unfollow'
        db.session.add(tag)
        return json_response({'result':'success', 'message':u'关注成功', 'state':state})
    return json_response({'result':'fail', 'message':u'服务器错误, 请稍后再试'})

@tagview.route('/<tag>/rss.xml')
def rss(tag):
    g.tag = Tag.query.filter_by(name=tag).first()
    if not g.tag:
        abort(404)
    g.pastes = Paste.query.filter(Paste.tags.contains(g.tag)).filter_by(is_private=False).order_by("created_time DESC").all()
    return render('rss/tag.xml')

app.register_blueprint(tagview,   url_prefix='/tag')
예제 #7
0
    paste = Paste.query.get_or_404(object_id)
    user = g.user
    rate = PasteRate.query.filter_by(user_id=user.id, paste_id=paste.id).all()
    if rate:
        return json_response({'result': 'fail',
                           'message': u'已经评价过了, 不能再评价了'})
    number = request.form.get('number', None)
    if not number:
        return json_response({'result': 'fail',
                           'message': u'服务器出错啦, 一会再评价吧'})
    rate = PasteRate(user.id,
                     paste.id,
                     int(number))
    db.session.add(rate)
    rates = PasteRate.query.filter_by(paste_id=paste.id).all()
    if rates:
        total = 0
        for rate in rates:
            total = total + rate.rate
        paste.rate_num = total / len(rates)
    else:
        paste.rate_num = number
    db.session.add(paste)
    return json_response({'result': 'success',
                       'message': u'评价成功!',
                       'rate': paste.rate_num})

app.register_blueprint(pasteview, url_prefix='/paste')

예제 #8
0
def posts():
    if request.method == 'POST':
        post_id = request.form.get('object_id', None)
        title = request.form.get('title', None)
        content = request.form.get('content', None)
        if post_id:
            post = Post.query.get_or_404(id=post_id)
            post.title = title
            post.content = content
            post.modified_time = datetime.now()
            db.session.add(post)
            try:
                db.session.commit()
            except:
                db.session.rollback()
                abort(500)
            return redirect(url_for('adminview.posts'))
        else:
            post = Post(title, content)
            db.session.add(post)
            return redirect(url_for('adminview.posts'))
    if request.args.get('object_id', None) and request.args.get('delete', None) == 'True':
        post = Post.query.filter_by(id=request.args.get('object_id', None)).first()
        if post:
            db.session.delete(post)
        return redirect(url_for('adminview.posts'))
    g.posts = Post.query.all()
    return render_template('adminview/posts.html')

app.register_blueprint(adminview, url_prefix='/admin')
예제 #9
0
                                'nickname': user.nickname}
                     })

@userview.route('/follow')
def follow():
    state = ''
    if 'user' not in session:
        return json_response({'result': 'fail', 'message': u'请先登陆!'})
    object_id = request.args.get('id', None)
    if object_id:
        model = User.query.get_or_404(object_id)
        user = getUserObject()
        if model.id == user.id:
            return json_response({'result': 'fail', 'message': u'自己不能关注自己'})
        if user not in model.followers:
            model.followers.append(user)
            state = 'follow'
        else:
            model.followers.remove(user)
            state = 'unfollow'
        db.session.add(model)
        return json_response({'result':'success', 'state': state})

@userview.route('/<user_id>/rss.xml')
def rss(user_id):
    g.user = User.query.get_or_404(user_id)
    g.pastes = Paste.query.filter_by(user_id=user_id, is_private=False).order_by("created_time DESC").all()
    return render('rss/user.xml')

app.register_blueprint(userview,  url_prefix='/user')
예제 #10
0
        page = 1
    g.pagination = Post.query.order_by("created_time DESC").paginate(page, 10)
    return render('siteview/posts.html')


@siteview.route('/post/<object_id>')
def post(object_id=None):
    if not object_id:
        abort(404)
    g.post = Post.query.get_or_404(object_id)
    return render('siteview/post.html')


@siteview.route("/test")
def test():
    send_mail_to_queue(from_user="******",
                       to_user="******",
                       title=u"来试试中文",
                       content=u"打断的中文熬撒旦噶速度过来说的话过来撒很给力")
    return "hello!"


@siteview.route('/rss.xml')
def rss():
    g.pastes = Paste.query.filter_by(
        is_private=False).order_by("created_time DESC").all()
    return render('rss/site.xml')


app.register_blueprint(siteview)
예제 #11
0
    if 'user' not in session:
        return json_response({'result': 'fail', 'message': u'请登陆再评价'})

    paste = Paste.query.get_or_404(object_id)
    user = g.user
    rate = PasteRate.query.filter_by(user_id=user.id, paste_id=paste.id).all()
    if rate:
        return json_response({'result': 'fail', 'message': u'已经评价过了, 不能再评价了'})
    number = request.form.get('number', None)
    if not number:
        return json_response({'result': 'fail', 'message': u'服务器出错啦, 一会再评价吧'})
    rate = PasteRate(user.id, paste.id, int(number))
    db.session.add(rate)
    rates = PasteRate.query.filter_by(paste_id=paste.id).all()
    if rates:
        total = 0
        for rate in rates:
            total = total + rate.rate
        paste.rate_num = total / len(rates)
    else:
        paste.rate_num = number
    db.session.add(paste)
    return json_response({
        'result': 'success',
        'message': u'评价成功!',
        'rate': paste.rate_num
    })


app.register_blueprint(pasteview, url_prefix='/paste')