1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import transaction

from sqlalchemy import engine_from_config

from pyramid.paster import (
    get_appsettings,
    setup_logging,
)

from ..models import (
    get_engine,
    get_session_factory,
    get_tm_session,
    User,
    get_user_by_email,
    get_or_create_node_by_uri,
    is_user_name_valid,
)

from ..lib import generate_password

import argparse

import json


def get_arg_parser():
    parser = argparse.ArgumentParser(description = 'import comments from JSON')
    parser.add_argument('-c', '--config', default='development.ini')
    parser.add_argument('-d', '--debug', action='store_true', default=False)
    parser.add_argument('--https', action='store_true', default=False)
    parser.add_argument('--group')
    #parser.add_argument('--dry-run', action='store_true', default=False)
    parser.add_argument('json_path', help="path to JSON file")
    parser.add_argument('domain', help="domain to import into")
    return parser


def main():
    parser = get_arg_parser()
    args = parser.parse_args()

    settings = get_appsettings(args.config)
    engine = get_engine(settings)
    session_factory = get_session_factory(engine)

    if args.debug:
        setup_logging(args.config)

    if args.domain.endswith('/') == False:
        args.domain = args.domain + '/'

    group = generate_password(3)
    if args.group:
        group = args.group

    scheme = 'http://'
    if args.https:
        scheme = 'https://'

    with open(args.json_path) as f:
        pages = json.load(f)

    with transaction.manager:
        dbsession = get_tm_session(session_factory, transaction.manager)

        users = {}
        roots = {}
        nodes = {}

        for slug, page_data in pages.items():

            comments = page_data["comments"]

            if len(comments) == 0:
                # skipping because this page has no comments.
                continue

            uri = unicode(scheme + args.domain + slug)
            print("creating page for {}".format(uri))
            roots[slug] = get_or_create_node_by_uri(dbsession, uri)
            roots[slug].commit()

            for comment in comments:

                email = comment["email"]

                if email not in users:
                    user = get_user_by_email(dbsession, email)
                    if user:
                        users[email] = user
                        print(u"found user for {} ({})".format(email, user.name))
                    else:
                        desired_name = unicode(comment["author"].replace(" ", "-").replace("_", "-") + '-' + group)
                        if not is_user_name_valid(desired_name):
                            desired_name = ''
                        users[email] = User(email, desired_name=desired_name)
                        print(u"created user for {} ({})".format(email, desired_name))

                dbsession.add(users[email])
                dbsession.flush()

                node = roots[slug].new_child(unicode(comment["content"]))
                node.created = int(comment["timestamp"]) * 1000
                node.changed = int(comment["timestamp"]) * 1000
                node.user = users[email]
                node.verified = True
                nodes[comment["id"]] = node

                dbsession.add(nodes[comment["id"]])
                dbsession.flush()

            for comment in comments:
                if comment["parent_id"]:
                    print("id {}, parent_id {}".format(
                        comment["id"],
                        comment["parent_id"],
                    ))
                    nodes[comment["id"]].parent_id = nodes[comment["parent_id"]].id
                    dbsession.add(nodes[comment["id"]])
                    dbsession.flush()