Source

py4u-opensource / mysignal.py

Full commit
# -*- coding: utf-8 *-*
from flask import redirect,session
from blinker import signal
from functools import wraps
from news import Post,Comment
from configure import Configure,Message
from serverpush import message_collection
import gevent
from gevent import monkey
monkey.patch_all()
defer_process = []
register_signal= []

def defer_connect(thesignal):
    tempsignal = signal(thesignal)
    register_signal.append(tempsignal)
    def wrapper(fun):
        @wraps(fun)
        def defer_fun(sender):
            defer_process.append(gevent.spawn(fun,sender))
        tempsignal.connect(defer_fun)
        print "connect", tempsignal.name
        print "id signal receiver", id(tempsignal)
        return defer_fun
    return wrapper

def quick_connect(thesignal):
    tempsignal = signal(thesignal)
    register_signal.append(tempsignal)
    def wrapper(fun):
        @wraps(fun)
        def quick_fun(*sender, **kw):
            fun(*sender,**kw)
        tempsignal.connect(quick_fun)
        #print "connect", tempsignal.name
        return quick_fun
    return wrapper

@quick_connect('book_post_news')
def special_post_book(sender, **kw):

    book = sender
    if(book.postid is None or (not Post.is_exist(book.postid))):
        title = book.title
        link = "/showbook?id=" + str(id)
        post = Post(title,link,None)
        post.special_save()
        print "postid:",post.id
        book.postid = post.id
        book.update_postid()

@quick_connect('init_book_comments')
def init_book_comments(sender, **kw):
    book = sender
    book.comments = Comment.get_comments_by_postid(book.postid)

@quick_connect('init_cache')
def init_cache(sender):
    from application import app
    cache = sender
    cache.init_app(app, config={'CACHE_TYPE': 'filesystem', 'CACHE_DIR':'cache'})

@quick_connect('check_started')
def check_started(sender):
    if('user' not in session):
        return

    bookidset = session['user'].get_stared_bookid()
    for book in sender:
        if(book.id in bookidset):
            book.stared = True