PageRenderTime 45ms CodeModel.GetById 18ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/examples/contrib/typo_import.py

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