示例#1
0
文件: views.py 项目: icycore/thepast
def before_request():
    g.user = auth_user_from_session(session)
    #g.user = User.get(2)
    if g.user:
        g.user_alias = UserAlias.gets_by_user_id(g.user.id)
    else:
        g.user_alias = None

    if g.user:
        unbinded = list(set(config.OPENID_TYPE_DICT.values()) - 
                set([ua.type for ua in g.user.get_alias()]) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]]))
        tmp = {}
        for k, v in config.OPENID_TYPE_DICT.items():
            tmp[v] = k
        g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded]

        expired_providers = []
        for t in [ua.type for ua in g.user.get_alias()]:
            p = g.user.get_thirdparty_profile(t)
            if p and p.get("expired"):
                _ = [t, config.OPENID_TYPE_DICT_REVERSE.get(t), config.OPENID_TYPE_NAME_DICT.get(t, "")]
                expired_providers.append(_)
        if expired_providers:
            msg = " ".join([x[-1] for x in expired_providers])
            flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)", "tip")
    else:
        g.unbinded = None

    g.start = int(request.args.get('start', 0))
    g.count = int(request.args.get('count', 30))
    g.cate = request.args.get("cate", "")
    if not g.cate.isdigit():
        g.cate = ""
示例#2
0
def generate(user_id, date, order='asc'):
    try:
        uas = UserAlias.gets_by_user_id(user_id)
        if not uas:
            return

        start_date = datetime.datetime(date.year, date.month, 1)
        end_date = datetime.datetime(date.year, date.month,
                calendar.monthrange(date.year, date.month)[1], 23, 59, 59)
        pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m"))
        print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename

        if is_pdf_file_exists(pdf_filename):
            print '---- %s exists, so ignore...' % pdf_filename
            return

        status_ids = Status.get_ids_by_date(user_id, start_date, end_date)[:900]
        if order == 'asc':
            status_ids = status_ids[::-1]
        if not status_ids:
            print '----- status ids is none', status_ids
            return
        generate_pdf(pdf_filename, user_id, status_ids, capacity=-1)
        if not is_pdf_file_exists(pdf_filename):
            print '----%s generate pdf for user:%s fail' % (datetime.datetime.now(), user_id)
        else:
            print '----%s generate pdf for user:%s succ' % (datetime.datetime.now(), user_id)
    except Exception, e:
        import traceback
        print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
示例#3
0
def sync_wordpress(t, refresh=False):
    if not t:
        log.warning('no_wordpress_sync_task')
        return

    #一个人可以有多个wordpress的rss源地址
    rs = UserAlias.gets_by_user_id(t.user_id)
    uas = []
    for x in rs:
        if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]:
            uas.append(x)
    if not uas:
        log.warning('no_wordpress_alias')
        return
    for ua in uas:
        try:
            client = Wordpress(ua.alias)
            rs = client.get_feeds(refresh)
            if rs:
                log.info("get wordpress succ, result length is:%s" % len(rs))
                for x in rs:
                    Status.add_from_obj(t.user_id, x,
                                        json_encode(x.get_data()))
                return
        except:
            import traceback
            print traceback.format_exc()
示例#4
0
def sync_wordpress(t, refresh=False):
    if not t:
        log.warning('no_wordpress_sync_task')
        return

    #一个人可以有多个wordpress的rss源地址
    rs = UserAlias.gets_by_user_id(t.user_id)
    uas = []
    for x in rs:
        if x.type == config.OPENID_TYPE_DICT[config.OPENID_WORDPRESS]:
            uas.append(x)
    if not uas:
        log.warning('no_wordpress_alias')
        return
    for ua in uas:
        try:
            client = Wordpress(ua.alias)
            rs = client.get_feeds(refresh)
            if rs:
                log.info("get wordpress succ, result length is:%s" % len(rs))
                for x in rs:
                    Status.add_from_obj(t.user_id, x, json_encode(x.get_data()))
                return 
        except:
            import traceback; print traceback.format_exc()
示例#5
0
def before_request():
    g.user = auth_user_from_session(session)
    #g.user = User.get(2)
    if g.user:
        g.user_alias = UserAlias.gets_by_user_id(g.user.id)
    else:
        g.user_alias = None

    if g.user:
        unbinded = list(
            set(config.OPENID_TYPE_DICT.values()) -
            set([ua.type for ua in g.user.get_alias()]) -
            set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]]))
        tmp = {}
        for k, v in config.OPENID_TYPE_DICT.items():
            tmp[v] = k
        g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]]
                      for x in unbinded]
    else:
        g.unbinded = None

    g.start = int(request.args.get('start', 0))
    g.count = int(request.args.get('count', 30))
    g.cate = request.args.get("cate", "")
    if not g.cate.isdigit():
        g.cate = ""
示例#6
0
def generate(user_id, date, order='asc'):
    try:
        uas = UserAlias.gets_by_user_id(user_id)
        if not uas:
            return

        start_date = datetime.datetime(date.year, date.month, 1)
        end_date = datetime.datetime(
            date.year, date.month,
            calendar.monthrange(date.year, date.month)[1], 23, 59, 59)
        pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m"))
        print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename

        if is_pdf_file_exists(pdf_filename):
            print '---- %s exists, so ignore...' % pdf_filename
            return

        status_ids = Status.get_ids_by_date(user_id, start_date,
                                            end_date)[:900]
        if order == 'asc':
            status_ids = status_ids[::-1]
        if not status_ids:
            print '----- status ids is none', status_ids
            return
        generate_pdf(pdf_filename, user_id, status_ids, capacity=-1)
        if not is_pdf_file_exists(pdf_filename):
            print '----%s generate pdf for user:%s fail' % (
                datetime.datetime.now(), user_id)
        else:
            print '----%s generate pdf for user:%s succ' % (
                datetime.datetime.now(), user_id)
    except Exception, e:
        import traceback
        print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
示例#7
0
文件: views.py 项目: kadefor/thepast
def before_request():
    g.user = auth_user_from_session(session)
    if g.user:
        g.user_alias = UserAlias.gets_by_user_id(g.user.id)
    else:
        g.user_alias = None
    g.start = int(request.args.get("start", 0))
    g.count = int(request.args.get("count", 30))
    g.cate = request.args.get("cate", None)
示例#8
0
def before_request():
    g.config = config
    g.user = auth_user_from_session(session)
    #g.user = User.get(2)
    g.user_alias = UserAlias.gets_by_user_id(g.user.id) if g.user else None

    if request.method == 'POST':
        try:
            g.start = int(request.form.get('start', 0))
        except ValueError:
            g.start = 0
        try:
            g.count = int(request.form.get('count', 24))
        except ValueError:
            g.count = 0
        g.cate = request.form.get("cate", "")
    else:
        try:
            g.start = int(request.args.get('start', 0))
        except ValueError:
            g.start = 0
        try:
            g.count = int(request.args.get('count', 24))
        except ValueError:
            g.count = 0
        g.cate = request.args.get("cate", "")
    g.cate = int(g.cate) if g.cate.isdigit() else ""

    if g.user:
        g.binds = [ua.type for ua in g.user.get_alias()]
        unbinded = list(
            set(config.OPENID_TYPE_DICT.values()) - set(g.binds) -
            set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]]))
        tmp = {}
        for k, v in config.OPENID_TYPE_DICT.items():
            tmp[v] = k
        g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]]
                      for x in unbinded]

        expired_providers = []
        for t in [ua.type for ua in g.user.get_alias()]:
            p = g.user.get_thirdparty_profile(t)
            if p and p.get("expired"):
                _ = [
                    t,
                    config.OPENID_TYPE_DICT_REVERSE.get(t),
                    config.OPENID_TYPE_NAME_DICT.get(t, "")
                ]
                expired_providers.append(_)
        g.expired = expired_providers
        if expired_providers:
            msg = " ".join([x[-1] for x in expired_providers])
            flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)" % msg, "tip")
    else:
        g.unbinded = None
示例#9
0
def before_request():
    g.config = config
    g.user = auth_user_from_session(session)
    #g.user = User.get(2)
    g.user_alias = UserAlias.gets_by_user_id(g.user.id) if g.user else None

    if request.method == 'POST':
        try:
            g.start = int(request.form.get('start', 0))
        except ValueError:
            g.start = 0
        try:
            g.count = int(request.form.get('count', 24))
        except ValueError:
            g.count = 0
        g.cate = request.form.get("cate", "")
    else:
        try:
            g.start = int(request.args.get('start', 0))
        except ValueError:
            g.start = 0
        try:
            g.count = int(request.args.get('count', 24))
        except ValueError:
            g.count = 0
        g.cate = request.args.get("cate", "")
    g.cate = int(g.cate) if g.cate.isdigit() else ""
        
    if g.user:
        g.binds = [ua.type for ua in g.user.get_alias()]
        unbinded = list(set(config.OPENID_TYPE_DICT.values()) - 
                set(g.binds) - set([config.OPENID_TYPE_DICT[config.OPENID_THEPAST]]))
        tmp = {}
        for k, v in config.OPENID_TYPE_DICT.items():
            tmp[v] = k
        g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded]

        expired_providers = []
        for t in [ua.type for ua in g.user.get_alias()]:
            p = g.user.get_thirdparty_profile(t)
            if p and p.get("expired"):
                _ = [t, config.OPENID_TYPE_DICT_REVERSE.get(t), config.OPENID_TYPE_NAME_DICT.get(t, "")]
                expired_providers.append(_)
        g.expired = expired_providers
        if expired_providers:
            msg = " ".join([x[-1] for x in expired_providers])
            flash(u"你的 %s 授权已经过期了,会影响数据同步,你可以重新授权 :)" % msg, "tip")
    else:
        g.unbinded = None
示例#10
0
文件: views.py 项目: puzzlega/thepast
def before_request():
    g.user = auth_user_from_session(session)
    if g.user:
        g.user_alias = UserAlias.gets_by_user_id(g.user.id)
    else:
        g.user_alias = None

    if g.user:
        unbinded = list(set(config.OPENID_TYPE_DICT.values()) - set([ua.type for ua in g.user.get_alias()]))
        tmp = {}
        for k, v in config.OPENID_TYPE_DICT.items():
            tmp[v] = k
        g.unbinded = [[x, tmp[x], config.OPENID_TYPE_NAME_DICT[x]] for x in unbinded]
    else:
        g.unbinded = None

    g.start = int(request.args.get("start", 0))
    g.count = int(request.args.get("count", 30))
    g.cate = request.args.get("cate", None)
示例#11
0
def generate(user_id, date, order='asc'):
    try:
        uas = UserAlias.gets_by_user_id(user_id)
        if not uas:
            return

        start_date = datetime.datetime(date.year, date.month, 1)
        end_date = datetime.datetime(
            date.year, date.month,
            calendar.monthrange(date.year, date.month)[1], 23, 59, 59)

        pdf_filename = get_pdf_filename(user_id, date.strftime("%Y%m"), "")
        pdf_filename_compressed = get_pdf_filename(user_id,
                                                   date.strftime("%Y%m"))
        print '----generate pdf:', start_date, ' to ', end_date, ' file is', pdf_filename

        if is_pdf_file_exists(pdf_filename_compressed):
            print '---- %s exists, so ignore...' % pdf_filename_compressed
            return

        status_ids = Status.get_ids_by_date(user_id, start_date,
                                            end_date)[:900]
        if order == 'asc':
            status_ids = status_ids[::-1]
        if not status_ids:
            print '----- status ids is none', status_ids
            return
        generate_pdf(pdf_filename, user_id, status_ids)

        if not is_pdf_file_exists(pdf_filename):
            print '----%s generate pdf for user:%s fail' % (
                datetime.datetime.now(), user_id)
        else:
            commands.getoutput(
                "cd %s && tar -zcf %s %s && rm %s" %
                (config.PDF_FILE_DOWNLOAD_DIR, pdf_filename_compressed,
                 pdf_filename, pdf_filename))
            print '----%s generate pdf for user:%s succ' % (
                datetime.datetime.now(), user_id)
    except Exception, e:
        import traceback
        print '%s %s' % (datetime.datetime.now(), traceback.format_exc())
示例#12
0
文件: views.py 项目: icycore/thepast
def sync(cates):
    cates = cates.split("|")
    if not (cates and isinstance(cates, list)):
        return "no cates"

    cates = filter(lambda x: x in [str(y) for y in config.CATE_LIST], cates)
    if not cates:
        abort(400, "not support such cates")

    provider = category2provider(int(cates[0]))
    redir = "/connect/%s" % provider

    if not g.user:
        print '--- no g.user...'
        return redirect(redir)

    if request.form.get("remove"):
        for c in cates:
            r = SyncTask.gets_by_user_and_cate(g.user, str(c))
            for x in r:
                x.remove()
        return json_encode({'ok':'true'})

    uas = UserAlias.gets_by_user_id(g.user.id)
    r = filter(lambda x: x.type == config.OPENID_TYPE_DICT[provider], uas)
    user_alias = r and r[0]
    
    if not user_alias:
        print '--- no user_alias...'
        return json_encode({'ok':'false', 'redir':redir})

    token = OAuth2Token.get(user_alias.id)   
    
    if not token:
        print '--- no token...'
        return json_encode({'ok':'false', 'redir':redir})

    for c in cates:
        SyncTask.add(c, g.user.id)
    
    return json_encode({'ok':'true'})
示例#13
0
def sync(cates):
    cates = cates.split("|")
    if not (cates and isinstance(cates, list)):
        return "no cates"

    cates = filter(lambda x: x in [str(y) for y in config.CATE_LIST], cates)
    if not cates:
        abort(400, "not support such cates")

    provider = category2provider(int(cates[0]))
    redir = "/connect/%s" % provider

    if not g.user:
        print '--- no g.user...'
        return redirect(redir)

    if request.form.get("remove"):
        for c in cates:
            r = SyncTask.gets_by_user_and_cate(g.user, str(c))
            for x in r:
                x.remove()
        return json_encode({'ok': 'true'})

    uas = UserAlias.gets_by_user_id(g.user.id)
    r = filter(lambda x: x.type == config.OPENID_TYPE_DICT[provider], uas)
    user_alias = r and r[0]

    if not user_alias:
        print '--- no user_alias...'
        return json_encode({'ok': 'false', 'redir': redir})

    token = OAuth2Token.get(user_alias.id)

    if not token:
        print '--- no token...'
        return json_encode({'ok': 'false', 'redir': redir})

    for c in cates:
        SyncTask.add(c, g.user.id)

    return json_encode({'ok': 'true'})
示例#14
0
import time
import datetime

activate_this = '../env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from past.utils.pdf import generate_pdf, get_pdf_filename, is_pdf_file_exists
from past.model.user import User, UserAlias
from past import config

if __name__ == "__main__":
    
    #for uid in User.get_ids(0, 10000000):
    for uid in range(634,652):
        try:
            uas = UserAlias.gets_by_user_id(uid)
            if not uas:
                continue
            types = [x.type for x in uas]
            count = 300
            if config.OPENID_TYPE_DICT[config.OPENID_SINA] in types \
                    or config.OPENID_TYPE_DICT[config.OPENID_QQ] in types:
                count = 250
            pdf_filename = get_pdf_filename(uid)
            print pdf_filename
            generate_pdf(pdf_filename, uid, 0, count, capacity=-1)
            if not is_pdf_file_exists(pdf_filename):
                print '%s generate pdf for user:%s fail' % (datetime.datetime.now(), uid)
            else:
                print '%s generate pdf for user:%s succ' % (datetime.datetime.now(), uid)
        except Exception, e: