Adam Knight avatar Adam Knight committed 5289b71

Allow the Drupal importer to import to a different site ID.

Comments (0)

Files changed (3)

drupal_support/management/commands/convertdrupal.py

 from django.core.management.base import BaseCommand, CommandError
 
 from milton.drupal_support.models import *
-from milton.models import *
+from milton.models import Section, Page, Story, Redirect
 from milton.templatetags.slugify import slugify
 
 from milton.threadedcomments.models import MPTTComment
     #     make_option('-v', '--verbose', dest='verbose', action="store", default="0", type="choice", choices=['0','1','2'], help='Verbose output'),
         make_option(None,'--comments', dest='comments', action="store_true", default=True, help="Import comments (default)."),
         make_option(None,'--no-comments', dest='comments', action="store_false", default=True, help="Do not import comments."),
+        make_option(None,'--site-id', dest='site_id', action="store", help="Site ID to import records to (defaults to the one in settings)."),
     )
     help = 'Convert Drupal 6 tables to SP objects.    The tables must be in the same database as SP.'
     # args = '[appname ...]'
         if debug: print "* Debug logging enabled."
         if debug: print "* Importing data with %s as the content owner." % user.username
         
+        # Prefetch the destination site
+        if options.get("site_id", None):
+            site_obj = Site.objects.get(pk=options.get("site_id"))
+        else:
+            site_obj = Site.objects.get_current()
+        
+        if verbose: print "* Using site %s" % (site_obj.name,)
+        
         try:
-            # Get a list of nodes together and filter out blog nodes.
+            # Get a list of nodes together and filter to core nodes.
             node_list = DrupalNode.objects.filter(type__in=("blog", "story", "page")).order_by("nid")
             if verbose: print "Found %d nodes." % len(node_list)
             
             if len(node_list) == 0:
-                raise CommandError("No Drupal nodes found in the current database.")
+                raise CommandError("No supported Drupal nodes found in the current database.")
             
             # Create some sections
             (blog_section, c) = Section.objects.get_or_create(name="Blog", slug="blog")
                 if debug: print "* Created blog section."
                 blog_section.save()
             
-            (story_section, c) = Section.objects.get_or_create(name="story", slug="story")
+            (story_section, c) = Section.objects.get_or_create(name="Story", slug="story")
             if c:
                 if debug: print "* Created story section."
                 story_section.save()
                 obj = None
                 
                 # See if we've imported this node before
-                node_aliases = Redirect.objects.filter(original='/node/%d' % node.nid)
+                node_aliases = Redirect.objects.filter(original='/node/%d' % node.nid, site=site_obj)
                 if node_aliases.count() > 0:
                     if verbose: print "Skipping node %d" % node.nid
                     continue
                     # Save the story
                     obj.save()
                     
-                    # Add to the current site
-                    obj.sites.add(Site.objects.get_current())
+                    # Add to the site
+                    obj.sites.add(site_obj)
                     
                     # Import terms as tags
                     terms = node.current_revision.terms.all()
-                    tags = []
-                    for term in terms:
-                        tags.append(term.name)
+                    tags = [t.name for t in terms]
                     obj.tags = ','.join(tags) + ','
                     if debug: print "* Set tags to: %s" % obj.tags
                     
                     obj.save()
                     
                     # Add to the current site
-                    obj.sites.add(Site.objects.get_current())
+                    obj.sites.add(site_obj)
                     
                     # Find the most recent URL for this object and assign it
                     aliases = DrupalUrlAlias.objects.filter(src='node/%d' % node.nid).order_by('-pid')
                 aliases = DrupalUrlAlias.objects.filter(src='node/%d' % node.nid)
                 
                 # First, the core "node/1" links should still work
-                r = Redirect(original="/node/%d" % node.nid, target_object=obj)
+                r = Redirect(original="/node/%d" % node.nid, target_object=obj, site=site_obj)
                 r.save()
                 
                 # Now we get any others that were made
                     dst = "/" + alias.dst
                     if obj.__class__ == Page and dst == obj.url: continue
                     try:
-                        r = Redirect(original=dst, target_object=obj)
+                        r = Redirect(original=dst, target_object=obj, site=site_obj)
                         r.save()
                         if debug: print " Created redirect from", dst
                     except Exception, e:
                         user_url = unicode(comment.homepage)[:200],
                         submit_date = datetime.datetime.fromtimestamp(comment.timestamp),
                         comment = unicode(comment.comment),
-                        site = Site.objects.get_current(),
+                        site = site_obj,
                         is_public = (not comment.status),
                     )
                     c.save()
         return super(PublishedManager,self).get_query_set().filter(
             Q(date_published=None) | Q(date_published__lte=datetime.datetime.now),
             Q(date_hidden=None) | Q(date_hidden__gt =datetime.datetime.now),
-            status = True,
+            status = 1,
             sites__in=[Site.objects.get_current()]
         )
     object_id = models.PositiveIntegerField("Object ID", null=True, blank=True, help_text='The ID of the object this redirect will point to.')
     target_object = generic.GenericForeignKey()
     
-    permanent = models.BooleanField(default=True, help_text='Is this redirect permanent (code 301)?')
+    permanent = models.BooleanField(default=True, help_text='Is this redirect permanent (301)?')
     
     class Meta:
         verbose_name = "redirect"
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.