Пример #1
0
def info_packet(username, method="html"):

    print(f"starting {username}")

    packet = {}

    with app.test_request_context("/my_info"):

        db = db_session()
        g.timestamp = int(time.time())
        g.db = db

        user = get_user(username)

        print('submissions')
        #submissions
        post_ids = db.query(Submission.id).filter_by(
            author_id=user.id).order_by(Submission.id.desc()).all()
        posts = get_posts([i[0] for i in post_ids], v=user)
        packet["posts"] = {
            'html':
            lambda: render_template("userpage.html",
                                    v=None,
                                    u=user,
                                    listing=posts,
                                    page=1,
                                    next_exists=False),
            'json':
            lambda: [x.self_download_json for x in posts]
        }

        print('comments')
        comment_ids = db.query(Comment.id).filter_by(
            author_id=user.id).order_by(Comment.id.desc()).all()
        comments = get_comments([i[0] for i in comment_ids], v=user)
        packet["comments"] = {
            'html':
            lambda: render_template("userpage_comments.html",
                                    v=None,
                                    u=user,
                                    comments=comments,
                                    page=1,
                                    next_exists=False),
            'json':
            lambda: [x.self_download_json for x in comments]
        }

        print('post_upvotes')
        upvote_query = db.query(Vote.submission_id).filter_by(
            user_id=user.id, vote_type=1).order_by(Vote.id.desc()).all()
        upvote_posts = get_posts([i[0] for i in upvote_query], v=user)
        upvote_posts = [i for i in upvote_posts]
        for post in upvote_posts:
            post.__dict__['voted'] = 1
        packet['upvoted_posts'] = {
            'html':
            lambda: render_template(
                "home.html", v=None, listing=posts, page=1, next_exists=False),
            'json':
            lambda: [x.json_core for x in upvote_posts]
        }

        print('post_downvotes')
        downvote_query = db.query(Vote.submission_id).filter_by(
            user_id=user.id, vote_type=-1).order_by(Vote.id.desc()).all()
        downvote_posts = get_posts([i[0] for i in downvote_query], v=user)
        packet['downvoted_posts'] = {
            'html':
            lambda: render_template(
                "home.html", v=None, listing=posts, page=1, next_exists=False),
            'json':
            lambda: [x.json_core for x in downvote_posts]
        }

        print('comment_upvotes')
        upvote_query = db.query(CommentVote.comment_id).filter_by(
            user_id=user.id,
            vote_type=1).order_by(CommentVote.id.desc()).all()
        upvote_comments = get_comments([i[0] for i in upvote_query], v=user)
        packet["upvoted_comments"] = {
            'html':
            lambda: render_template("notifications.html",
                                    v=None,
                                    comments=upvote_comments,
                                    page=1,
                                    next_exists=False),
            'json':
            lambda: [x.json_core for x in upvote_comments]
        }

        print('comment_downvotes')
        downvote_query = db.query(CommentVote.comment_id).filter_by(
            user_id=user.id,
            vote_type=-1).order_by(CommentVote.id.desc()).all()
        downvote_comments = get_comments([i[0] for i in downvote_query],
                                         v=user)
        packet["downvoted_comments"] = {
            'html':
            lambda: render_template("notifications.html",
                                    v=None,
                                    comments=downvote_comments,
                                    page=1,
                                    next_exists=False),
            'json':
            lambda: [x.json_core for x in downvote_comments]
        }

        # blocked_users=db.query(UserBlock.target_id).filter_by(user_id=user.id).order_by(UserBlock.id.desc()).all()
        # users=[get_account(base36encode(x[0])) for x in blocked_users]
        # packet["blocked_users"]={
        #     "html":lambda:render_template
        #     "json":lambda:[x.json_core for x in users]
        # }

        send_mail(user.email,
                  "Your Ruqqus Data",
                  "Your Ruqqus data is attached.",
                  "Your Ruqqus data is attached.",
                  files={
                      f"{user.username}_{entry}.{method}":
                      io.StringIO(convert_file(packet[entry][method]()))
                      for entry in packet
                  })

    print("finished")
Пример #2
0
def info_packet(username, method="html"):

    print(f"starting {username}")

    packet={}

    with app.test_request_context("/my_info"):

        db=db_session()
        g.timestamp=int(time.time())
        g.db=db

        user=get_user(username)

        print('submissions')
        #submissions
        post_ids=db.query(Submission.id).filter_by(author_id=user.id).order_by(Submission.created_utc.desc()).all()
        post_ids=[i[0] for i in post_ids]
        print(f'have {len(post_ids)} ids')
        posts=get_posts(post_ids, v=user)
        print('have posts')
        packet["posts"]={
            'html':lambda:render_template("userpage.html", v=None, u=user, listing=posts, page=1, next_exists=False),
            'json':lambda:[x.self_download_json for x in posts]
        }

        print('comments')
        comment_ids=db.query(Comment.id).filter_by(author_id=user.id).order_by(Comment.created_utc.desc()).all()
        comment_ids=[x[0] for x in comment_ids]
        print(f"have {len(comment_ids)} ids")
        comments=get_comments(comment_ids, v=user)
        print('have comments')
        packet["comments"]={
            'html':lambda:render_template("userpage_comments.html", v=None, u=user, comments=comments, page=1, next_exists=False),
            'json':lambda:[x.self_download_json for x in comments]
        }

        print('post_upvotes')
        upvote_query=db.query(Vote.submission_id).filter_by(user_id=user.id, vote_type=1).order_by(Vote.id.desc()).all()
        upvote_posts=get_posts([i[0] for i in upvote_query], v=user)
        upvote_posts=[i for i in upvote_posts]
        for post in upvote_posts:
            post.__dict__['voted']=1
        packet['upvoted_posts']={
            'html':lambda:render_template("home.html", v=None, listing=posts, page=1, next_exists=False),
            'json':lambda:[x.json_core for x in upvote_posts]
        }

        print('post_downvotes')
        downvote_query=db.query(Vote.submission_id).filter_by(user_id=user.id, vote_type=-1).order_by(Vote.id.desc()).all()
        downvote_posts=get_posts([i[0] for i in downvote_query], v=user)
        packet['downvoted_posts']={
            'html':lambda:render_template("home.html", v=None, listing=posts, page=1, next_exists=False),
            'json':lambda:[x.json_core for x in downvote_posts]
        }

        print('comment_upvotes')
        upvote_query=db.query(CommentVote.comment_id).filter_by(user_id=user.id, vote_type=1).order_by(CommentVote.id.desc()).all()
        upvote_comments=get_comments([i[0] for i in upvote_query], v=user)
        packet["upvoted_comments"]={
            'html':lambda:render_template("notifications.html", v=None, comments=upvote_comments, page=1, next_exists=False),
            'json':lambda:[x.json_core for x in upvote_comments]
        }

        print('comment_downvotes')
        downvote_query=db.query(CommentVote.comment_id).filter_by(user_id=user.id, vote_type=-1).order_by(CommentVote.id.desc()).all()
        downvote_comments=get_comments([i[0] for i in downvote_query], v=user)
        packet["downvoted_comments"]={
            'html':lambda:render_template("notifications.html", v=None, comments=downvote_comments, page=1, next_exists=False),
            'json':lambda:[x.json_core for x in downvote_comments]
        }

    # blocked_users=db.query(UserBlock.target_id).filter_by(user_id=user.id).order_by(UserBlock.id.desc()).all()
    # users=[get_account(base36encode(x[0])) for x in blocked_users]
    # packet["blocked_users"]={
    #     "html":lambda:render_template
    #     "json":lambda:[x.json_core for x in users]
    # }




        send_mail(
            user.email,
            "Your Ruqqus Data",
            "Your Ruqqus data is attached.",
            "Your Ruqqus data is attached.",
            files={f"{user.username}_{entry}.{method}": io.StringIO(convert_file(str(packet[entry][method]()))) for entry in packet}
        )


    print("finished")



# @app.route("/my_info", methods=["POST"])
# @auth_required
# @validate_formkey
# def my_info_post(v):

#     if not v.is_activated:
#         return redirect("/settings/security")

#     method=request.values.get("method","html")
#     if method not in ['html','json']:
#         abort(400)

#     thread=threading.Thread(target=info_packet, args=(v.username,), kwargs={'method':method})
#     thread.setDaemon(True)
#     thread.start()

#     #info_packet(g.db, v)

#     return "started"


# @app.route("/my_info", methods=["GET"])
# @auth_required
# def my_info_get(v):
#     return render_template("my_info.html", v=v)