PageRenderTime 203ms CodeModel.GetById 80ms app.highlight 4ms RepoModel.GetById 77ms app.codeStats 0ms

/util/migrate_pastebin.py

https://bitbucket.org/bbangert/kai/
Python | 35 lines | 27 code | 7 blank | 1 comment | 5 complexity | 18149d9fd38b516314cc23236a71d7fe MD5 | raw file
 1"""To be run from inside the pylonshq project, with kai also installed"""
 2import pylons
 3from couchdb import Server, Database
 4from pytz import UTC
 5
 6from kai.model import Human, Paste
 7from kai.model.generics import all_doc_tags
 8
 9pylons.c.db = db = Database('http://localhost:25984/kai')
10
11
12def make_couch_paste(old):
13    from datetime import datetime
14    from couchdb import Server, Database
15    from pytz import UTC
16    import pylons
17    from kai.model import Human, Paste
18    from kai.model.generics import all_doc_tags
19    
20    pylons.c.db = db = Database('http://localhost:25984/kai')
21    
22    od = old.date.astimezone(UTC)
23    created = datetime(od.year, od.month, od.day, od.hour, od.minute, od.second)
24    new = Paste(old_id=old.id, old_poster=old.author, title=old.title,
25                code=old.code, language=old.language, created=created)
26    for tag in old.tags:
27        new.tags.append(tag.name)
28    new.store(db)
29
30results = model.Session.query(model.Paste).order_by(model.Paste.id.desc())
31for result in results:
32    doc = list(Paste.by_old_id(db)[str(result.id)])
33    if doc:
34        break
35    make_couch_paste(result)