Personal wiki notebook (not under development)

convert_blog_to_forum.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/python2.4
  2. import os
  3. import os.path
  4. import sys
  5. import cherrypy
  6. from datetime import datetime
  7. from controller.Database import Database
  8. from model.Notebook import Notebook
  9. from model.Note import Note
  10. from model.User import User
  11. from model.Tag import Tag
  12. class Blog_converter( object ):
  13. """
  14. Convert the Luminotes blog to be a forum, with one notebook thread per blog post.
  15. """
  16. def __init__( self, database, settings, blog_username ):
  17. self.database = database
  18. self.settings = settings
  19. self.blog_username = blog_username
  20. self.blog_notebook = self.load_original_blog_notebook()
  21. self.convert_posts_to_forum_threads()
  22. self.delete_original_blog_notebook()
  23. self.database.commit()
  24. def load_original_blog_notebook( self ):
  25. anonymous = self.database.select_one( User, User.sql_load_by_username( u"anonymous" ) )
  26. from controller.Users import Users
  27. users = Users(
  28. self.database,
  29. self.settings[ u"global" ].get( u"luminotes.http_url", u"" ),
  30. self.settings[ u"global" ].get( u"luminotes.https_url", u"" ),
  31. self.settings[ u"global" ].get( u"luminotes.support_email", u"" ),
  32. self.settings[ u"global" ].get( u"luminotes.payment_email", u"" ),
  33. self.settings[ u"global" ].get( u"luminotes.rate_plans", [] ),
  34. self.settings[ u"global" ].get( u"luminotes.download_products", [] ),
  35. )
  36. result = users.current( anonymous.object_id )
  37. blog_notebooks = [ nb for nb in result[ "notebooks" ] if nb.name == u"Luminotes blog" ]
  38. return blog_notebooks[ 0 ]
  39. def convert_posts_to_forum_threads( self ):
  40. notes = self.database.select_many( Note, self.blog_notebook.sql_load_recent_notes( reverse = True, start = 0, count = 1000 ) )
  41. for note in notes:
  42. self.convert_post( note )
  43. def convert_post( self, note ):
  44. # create a notebook thread to go in the forum
  45. notebook_id = self.database.next_id( Notebook, commit = False )
  46. thread_notebook = Notebook.create(
  47. notebook_id,
  48. note.title,
  49. )
  50. self.database.save( thread_notebook, commit = False )
  51. anonymous = self.database.select_one( User, User.sql_load_by_username( u"anonymous" ) )
  52. # move the given note into the newly created notebook thread
  53. note.notebook_id = notebook_id
  54. note.startup = True
  55. note.rank = 0
  56. self.database.save( note, commit = False )
  57. # load the forum tag
  58. forum_tag = self.database.select_one( Tag, Tag.sql_load_by_name( u"forum", user_id = anonymous.object_id ) )
  59. # associate the forum tag with the previously created notebook thread, and set that
  60. # association's value to the forum name
  61. self.database.execute(
  62. anonymous.sql_save_notebook_tag( notebook_id, forum_tag.object_id, value = u"blog" ),
  63. commit = False,
  64. )
  65. # give the anonymous user access to the new notebook thread
  66. self.database.execute(
  67. anonymous.sql_save_notebook( notebook_id, read_write = True, owner = False, own_notes_only = True ),
  68. commit = False,
  69. )
  70. blog_user = self.database.select_one( User, User.sql_load_by_username( self.blog_username ) )
  71. self.database.execute(
  72. blog_user.sql_save_notebook( notebook_id, read_write = True, owner = True ),
  73. commit = False,
  74. )
  75. def delete_original_blog_notebook( self ):
  76. self.blog_notebook.deleted = True
  77. self.database.save( self.blog_notebook, commit = False )
  78. def main( args ):
  79. import cherrypy
  80. from config import Common
  81. cherrypy.config.update( Common.settings )
  82. desktop = False
  83. if args and "-d" in args:
  84. from config import Development
  85. settings = Development.settings
  86. args.remove( "-d" )
  87. elif args and "-l" in args:
  88. from config import Desktop
  89. settings = Desktop.settings
  90. desktop = True
  91. args.remove( "-l" )
  92. else:
  93. from config import Production
  94. settings = Production.settings
  95. cherrypy.config.update( settings )
  96. database = Database(
  97. host = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_host" ),
  98. ssl_mode = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_ssl_mode" ),
  99. data_dir = ".",
  100. )
  101. ranker = Blog_converter( database, cherrypy.config.configMap, *args )
  102. if __name__ == "__main__":
  103. main( sys.argv[ 1: ] )