示例#1
0
def pdf(uid):
    user = User.get(uid)
    if not user:
        abort(404, "No such user")

    if uid != g.user.id and user.get_profile_item('user_privacy') == consts.USER_PRIVACY_PRIVATE:
        flash(u"由于该用户设置了仅自己可见的权限,所以,我们就看不到了", "tip")
        return redirect("/")

    intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()]
    intros = filter(None, intros)

    pdf_files = []
    start_date = Status.get_oldest_create_time(None, user.id)
    now = datetime.now()
    d = start_date
    while d and d <= now:
        pdf_filename = get_pdf_filename(user.id, d.strftime("%Y%m"))
        if is_pdf_file_exists(pdf_filename):
            full_file_name = get_pdf_full_filename(pdf_filename)
            pdf_files.append([d, pdf_filename, sizeof_fmt(os.path.getsize(full_file_name))])

        days = calendar.monthrange(d.year, d.month)[1]
        d += timedelta(days=days)
        d = datetime(d.year, d.month, 1)
    files_dict = defaultdict(list)
    for date, filename, filesize in pdf_files:
        files_dict[date.year].append([date, filename, filesize])

    pdf_applyed = PdfSettings.is_user_id_exists(g.user.id)
    return render_template("v2/pdf.html", **locals())
示例#2
0
def pdf(uid):
    user = User.get(uid)
    if not user:
        abort(404, "No such user")

    if uid != g.user.id and user.get_profile_item('user_privacy') == consts.USER_PRIVACY_PRIVATE:
        flash(u"由于该用户设置了仅自己可见的权限,所以,我们就看不到了", "tip")
        return redirect(url_for("timeline"))

    intros = [g.user.get_thirdparty_profile(x).get("intro") for x in config.OPENID_TYPE_DICT.values()]
    intros = filter(None, intros)

    pdf_files = []
    start_date = Status.get_oldest_create_time(None, user.id)
    now = datetime.now()
    d = start_date
    while d and d <= now:
        pdf_filename = get_pdf_filename(user.id, d.strftime("%Y%m"))
        if is_pdf_file_exists(pdf_filename):
            full_file_name = get_pdf_full_filename(pdf_filename)
            pdf_files.append([d, pdf_filename, sizeof_fmt(os.path.getsize(full_file_name))])

        days = calendar.monthrange(d.year, d.month)[1]
        d += timedelta(days=days)
        d = datetime(d.year, d.month, 1)
    files_dict = defaultdict(list)
    for date, filename, filesize in pdf_files:
        files_dict[date.year].append([date, filename, filesize])

    pdf_applyed = PdfSettings.is_user_id_exists(g.user.id)
    return render_template("pdf.html", **locals())
示例#3
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())
示例#4
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())
示例#5
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())
示例#6
0
文件: views.py 项目: puzzlega/thepast
def pdf(uid):
    user = User.get(uid)
    if not user:
        abort(404, "No such user")

    pdf_filename = get_pdf_filename(user.id)
    if not is_pdf_file_exists(pdf_filename):
        abort(404, "Please wait one day to  download the PDF version, because the vps memory is limited")

    full_file_name = os.path.join(config.PDF_FILE_DOWNLOAD_DIR, pdf_filename)
    resp = make_response()
    resp.headers["Cache-Control"] = "no-cache"
    resp.headers["Content-Type"] = "application/pdf"
    resp.headers["Content-Disposition"] = "attachment; filename=%s" % pdf_filename
    resp.headers["Content-Length"] = os.path.getsize(full_file_name)
    redir = "/down/pdf/" + pdf_filename
    resp.headers["X-Accel-Redirect"] = redir
    return resp
示例#7
0
文件: views.py 项目: perryhau/thepast
def pdf(uid):
    user = User.get(uid)
    if not user:
        abort(401, "No such user")
    
    pdf_filename = get_pdf_filename(user.id)
    if not is_pdf_file_exists(pdf_filename):
        generate_pdf(pdf_filename, user.id, 0, 10000000)
    if not is_pdf_file_exists(pdf_filename):
        abort(400, "generate pdf fail, please try again...")

    full_file_name = os.path.join(config.PDF_FILE_DOWNLOAD_DIR, pdf_filename)
    resp = make_response()
    resp.headers['Cache-Control'] = 'no-cache'
    resp.headers['Content-Type'] = 'application/pdf'
    resp.headers['Content-Disposition'] = 'attachment; filename=%s' % pdf_filename
    resp.headers['Content-Length'] = os.path.getsize(full_file_name)
    redir = '/down/pdf/' + pdf_filename
    resp.headers['X-Accel-Redirect'] = redir
    return resp
示例#8
0
文件: views.py 项目: chenyc/thepast
def pdf(uid):
    user = User.get(uid)
    if not user:
        abort(404, "No such user")
    
    pdf_filename = get_pdf_filename(user.id)
    if not is_pdf_file_exists(pdf_filename):
        #generate_pdf(pdf_filename, user.id, 0, 10000000)
        #abort(404, u"请明天来下载吧,因为我的内存吃不消了,只能晚上生成PDF^^")
        abort(404, "Please wait one day to  download the PDF version, because the vps memory is limited")
    if not is_pdf_file_exists(pdf_filename):
        abort(400, "generate pdf fail, please try again...")

    full_file_name = os.path.join(config.PDF_FILE_DOWNLOAD_DIR, pdf_filename)
    resp = make_response()
    resp.headers['Cache-Control'] = 'no-cache'
    resp.headers['Content-Type'] = 'application/pdf'
    resp.headers['Content-Disposition'] = 'attachment; filename=%s' % pdf_filename
    resp.headers['Content-Length'] = os.path.getsize(full_file_name)
    redir = '/down/pdf/' + pdf_filename
    resp.headers['X-Accel-Redirect'] = redir
    return resp
示例#9
0
# -*- coding:utf-8 -*-

import sys

sys.path.append("../")

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

if __name__ == "__main__":

    for uid in User.get_ids(0, 10000000):
        try:
            pdf_filename = get_pdf_filename(uid)
            generate_pdf(pdf_filename, uid, 0, 100000, 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:
            print "%s %s" % (datetime.datetime.now(), e)

        time.sleep(1)