/util/migrate_pastebin.py

https://bitbucket.org/bbangert/kai/ · Python · 35 lines · 27 code · 7 blank · 1 comment · 3 complexity · 18149d9fd38b516314cc23236a71d7fe MD5 · raw file

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