/examples/contrib/typo_import.py

http://blogmaker.googlecode.com/ · Python · 175 lines · 118 code · 29 blank · 28 comment · 8 complexity · e7724d1c522f4e0cc2b8b52fbb4eca09 MD5 · raw file

  1. #!/usr/bin/python2.5
  2. """
  3. Quick and Dirty Typo Blog Migration Script
  4. (c) Russell Neches, March 31, 2008
  5. --- BSD License Boilerplate ---
  6. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  7. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  8. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  9. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  10. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  12. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  13. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  14. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  15. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  16. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  17. --- BSD License Boilerplate ---
  18. NOTE : Don't forget to set DJANGO_SETTINGS_MODULE if you want to use
  19. this script without Django's "./manage shell" thingy.
  20. NOTE : If you hage any 'pages' in Typo, they will be treated as
  21. ordinary blog entries for the dates on which they were created in
  22. Typo. If you want something else to happen to them, it's not difficult
  23. to pick them out later.
  24. Mapping from Typo articles to Blogmaker Entries
  25. row field Entry
  26. index name member
  27. ---------------------------------------------------------------------
  28. a[0] -> id
  29. a[1] -> type
  30. a[2] -> title -> Entry.headline
  31. a[3] -> author -> Entry.user (foreign key to User)
  32. a[4] -> body -> Entry.body
  33. a[5] -> extended
  34. a[6] -> excerpt
  35. a[7] -> keywords -> Entry.tags (many-to-many key to Tag)
  36. a[8] -> created_at -> Entry.pub_date
  37. a[9] -> updated_at
  38. a[10] -> user_id
  39. a[11] -> permalink -> Entry.slug (if exists...)
  40. a[12] -> guid
  41. a[13] -> text_filter_id
  42. a[14] -> whiteboard
  43. a[15] -> name
  44. a[16] -> published -> Entry.active (note: convert int to bool)
  45. a[17] -> allow_pings
  46. a[18] -> allow_comments
  47. a[19] -> pubished_at
  48. a[20] -> state
  49. """
  50. from blogmaker.blog.models import Entry, Tag
  51. from blogmaker.comments.models import Comment
  52. #from django.contrib.auth.models import User
  53. #from django.contrib.contenttypes.models import ContentType
  54. #from django.contrib.sites.models import Site
  55. import MySQLdb
  56. import pickle
  57. import string
  58. conn = MySQLdb.connect( host = '************',
  59. user = '************',
  60. passwd = '************',
  61. db = '************' )
  62. cursor = conn.cursor()
  63. cursor.execute( "SELECT * FROM contents" )
  64. articles = cursor.fetchall()
  65. cursor.close()
  66. cursor = conn.cursor()
  67. cursor.execute( "SELECT * FROM feedback" )
  68. feedback = cursor.fetchall()
  69. cursor.close()
  70. conn.close()
  71. for a in articles :
  72. # create a fresh entry instance
  73. print a[2]
  74. e = Entry()
  75. e.headline = a[2]
  76. e.pub_date = a[8]
  77. # We assume you only have one user
  78. e.user_id = 1
  79. # use the permalink string as the slug, or generate it
  80. # from the title
  81. if a[11] == None :
  82. e.slug = '-'.join(
  83. string.translate( a[2],
  84. string.maketrans('',''),
  85. string.punctuation
  86. ).lower().split() )
  87. else :
  88. e.slug = a[11]
  89. print " :: " + e.slug
  90. # if you used any Typo-specific HTML, now would be a good time to
  91. # strip it out.
  92. e.body = string.replace( a[4], 'typo:code', 'pre' )
  93. # we have to save the entry here before we continue; adding
  94. # the tags requires a pk.
  95. e.save()
  96. # handle the article tags, if there are any
  97. if a[7] :
  98. tagstrings = string.translate( a[7],
  99. string.maketrans('',''),
  100. string.punctuation
  101. ).lower().split()
  102. # if there are any new tags, create them.
  103. # attach tags to the Entry object
  104. for t in tagstrings :
  105. if not Tag.objects.filter( tag = t ) :
  106. T = Tag()
  107. T.tag = t
  108. T.save()
  109. print " ++ " + t
  110. e.tags.add( Tag.objects.filter( tag = t )[0] )
  111. print " .. ", t
  112. e.save()
  113. # create and attach the comments for this Entry
  114. for f in feedback :
  115. if a[0] == f[12] :
  116. print " -> Comment from " + f[3]
  117. c = Comment()
  118. c.person_name = f[3]
  119. c.person_email = f[13]
  120. c.person_www = f[14]
  121. c.ip_address = f[15]
  122. c.comment = f[4]
  123. # You should only have one Site instance. If you've
  124. # got more than one, it's up to you to figure out what
  125. # to do with the site_id.
  126. c.site_id = 1
  127. # We'll assume all comments are on Entry objects.
  128. c.content_type_id = 9
  129. # set the comment to active
  130. c.is_public = True
  131. # attach the comment to the Entry we just created
  132. c.object_id = e.id
  133. c.save()
  134. c.submit_date = f[6]
  135. c.save()
  136. f = open( 'typo_articles.pickle', 'w' )
  137. pickle.dump( articles, f )
  138. f.close()
  139. f = open( 'typo_comments.pickle', 'w' )
  140. pickle.dump( feedback, f )
  141. f.close()