Personal wiki notebook (not under development)

initdb.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #!/usr/bin/python2.4
  2. import os
  3. import os.path
  4. import sys
  5. from controller.Database import Database
  6. from controller.Schema_upgrader import Schema_upgrader
  7. from model.Notebook import Notebook
  8. from model.Note import Note
  9. from model.User import User
  10. from config.Version import VERSION
  11. class Initializer( object ):
  12. HTML_PATH = u"static/html"
  13. NOTE_FILES = [ # the second element of the tuple is whether to show the note on startup
  14. ( u"sign up.html", False ),
  15. ( u"faq.html", False ),
  16. ( u"meet the team.html", False ),
  17. ( u"contact info.html", False ),
  18. ( u"login.html", False ),
  19. ( u"source code.html", False ),
  20. ( u"password reset.html", False ),
  21. ( u"advanced browser features.html", False ),
  22. ( u"supported browsers.html", False ),
  23. ( u"community.html", False ),
  24. ( u"enable JavaScript.html", False ),
  25. ]
  26. def __init__( self, database, host, settings, desktop, nuke = False ):
  27. self.database = database
  28. self.settings = settings
  29. self.desktop = desktop
  30. self.main_notebook = None
  31. self.anonymous = None
  32. if nuke is True:
  33. self.database.execute( file( "model/drop.sql" ).read(), commit = False )
  34. if host:
  35. self.database.execute( file( "model/schema.sql" ).read(), commit = False )
  36. else:
  37. self.database.execute_script( file( "model/schema.sqlite" ).read(), commit = False )
  38. self.create_main_notebook()
  39. self.create_anonymous_user()
  40. if desktop is True:
  41. self.create_desktop_user()
  42. self.database.commit()
  43. version = Schema_upgrader.version_string_to_tuple( VERSION )
  44. Schema_upgrader.schema_version( database, default_version = version )
  45. self.database.commit()
  46. def create_main_notebook( self ):
  47. # create the main notebook
  48. main_notebook_id = self.database.next_id( Notebook )
  49. self.main_notebook = Notebook.create( main_notebook_id, u"Luminotes", rank = 0 )
  50. self.database.save( self.main_notebook, commit = False )
  51. # no need to create default notes for the desktop version
  52. if self.desktop is True:
  53. return
  54. # create an id for each note
  55. note_ids = {}
  56. for ( filename, startup ) in self.NOTE_FILES:
  57. note_ids[ filename ] = self.database.next_id( Note )
  58. rank = 0
  59. for ( filename, startup ) in self.NOTE_FILES:
  60. full_filename = os.path.join( self.HTML_PATH, filename )
  61. contents = fix_note_contents( file( full_filename ).read(), main_notebook_id, note_ids, self.settings )
  62. if startup:
  63. rank += 1
  64. note = Note.create( note_ids[ filename ], contents, notebook_id = self.main_notebook.object_id, startup = startup, rank = startup and rank or None )
  65. self.database.save( note, commit = False )
  66. def create_anonymous_user( self ):
  67. # create the anonymous user
  68. anonymous_user_id = self.database.next_id( User )
  69. self.anonymous = User.create( anonymous_user_id, u"anonymous", None, None )
  70. self.database.save( self.anonymous, commit = False )
  71. # give the anonymous user read-only access to the main notebook
  72. self.database.execute( self.anonymous.sql_save_notebook( self.main_notebook.object_id, read_write = False, owner = False ), commit = False )
  73. def create_desktop_user( self ):
  74. from controller.Users import Users
  75. users = Users(
  76. self.database,
  77. self.settings[ u"global" ].get( u"luminotes.http_url", u"" ),
  78. self.settings[ u"global" ].get( u"luminotes.https_url", u"" ),
  79. self.settings[ u"global" ].get( u"luminotes.support_email", u"" ),
  80. self.settings[ u"global" ].get( u"luminotes.payment_email", u"" ),
  81. self.settings[ u"global" ].get( u"luminotes.rate_plans", [] ),
  82. self.settings[ u"global" ].get( u"luminotes.download_products", [] ),
  83. )
  84. users.create_user( u"desktopuser" )
  85. def set_schema_version( self ):
  86. try:
  87. from_version = self.__database.select_one( tuple, "select * from schema_version;" );
  88. # if there's no schema version table, set the schema to the current version
  89. except:
  90. self.__database.rollback()
  91. from_version = ( 1, 5, 4 )
  92. self.__database.execute( "create table schema_version ( major numeric, minor numeric, release numeric );", commit = False );
  93. self.__database.execute( "insert into schema_version values ( %s, %s, %s );" % from_version, commit = False );
  94. self.__database.commit()
  95. def main( args = None ):
  96. nuke = False
  97. import cherrypy
  98. from config import Common
  99. cherrypy.config.update( Common.settings )
  100. desktop = False
  101. if args and "-d" in args:
  102. from config import Development
  103. settings = Development.settings
  104. elif args and "-l" in args:
  105. from config import Desktop
  106. settings = Desktop.settings
  107. desktop = True
  108. else:
  109. from config import Production
  110. settings = Production.settings
  111. cherrypy.config.update( settings )
  112. if args and ( "-n" in args or "--nuke" in args ):
  113. nuke = True
  114. print "This will nuke the contents of the database before initializing it with default data. Continue (y/n)? ",
  115. confirmation = sys.stdin.readline().strip()
  116. print
  117. if confirmation.lower()[ 0 ] != 'y':
  118. print "Exiting without touching the database."
  119. return
  120. print "Initializing the database with default data."
  121. host = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_host" )
  122. database = Database(
  123. host = host,
  124. ssl_mode = cherrypy.config.configMap[ u"global" ].get( u"luminotes.db_ssl_mode" ),
  125. data_dir = ".",
  126. )
  127. initializer = Initializer( database, host, cherrypy.config.configMap, desktop, nuke )
  128. def fix_note_contents( contents, notebook_id, note_ids, settings ):
  129. import re
  130. LINK_PATTERN = re.compile( '(<a\s+href=")([^"]+note_id=)([^"]*)("[^>]*>)(.*?)(</a>)' )
  131. TITLE_PATTERN = re.compile( ' title="(.*?)"' )
  132. WHITESPACE_BETWEEN_TAGS_PATTERN = re.compile( '>\s+<' )
  133. # plug in the notebook id and support email address where appropriate
  134. contents = contents.replace( "%s", notebook_id ).replace( "\n", " " )
  135. contents = contents.replace( "support@luminotes.com", settings[ u"global" ].get( u"luminotes.support_email", u"" ) )
  136. contents = WHITESPACE_BETWEEN_TAGS_PATTERN.sub( "><", contents )
  137. contents = contents.replace( "<p> ", "<p>" )
  138. # stitch together note links to use the actual note ids of the referenced notes.
  139. # also, use the https URL for certain links if one is configured
  140. def fix_link( match ):
  141. title = match.group( 5 )
  142. title_match = TITLE_PATTERN.search( title )
  143. if title_match:
  144. title = title_match.group( 1 )
  145. https_url = u""
  146. if title in ( u"sign up", u"login" ):
  147. https_url = settings[ u"global" ].get( u"luminotes.https_url", u"" )
  148. return u"".join( [
  149. match.group( 1 ), https_url, match.group( 2 ), note_ids.get( title + u".html", u"new" ),
  150. match.group( 4 ), match.group( 5 ), match.group( 6 ),
  151. ] )
  152. return LINK_PATTERN.sub( fix_link, contents )
  153. if __name__ == "__main__":
  154. import sys
  155. main( sys.argv[ 1: ] )