Skip to content

Commit

Permalink
Fixed migrate_threaded_comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ericflo committed Apr 1, 2009
1 parent 6cb9219 commit 981f280
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 12 deletions.
3 changes: 2 additions & 1 deletion threadedcomments/forms.py
@@ -1,9 +1,10 @@
from django import forms
from django.contrib.comments.forms import CommentForm
from threadedcomments.models import ThreadedComment
from django.conf import settings
from django.utils.hashcompat import sha_constructor

from threadedcomments.models import ThreadedComment

class ThreadedCommentForm(CommentForm):
parent = forms.IntegerField(required=False, widget=forms.HiddenInput)

Expand Down
27 changes: 27 additions & 0 deletions threadedcomments/management/commands/migrate_comments.py
@@ -0,0 +1,27 @@
from django.core.management.base import NoArgsCommand
from django.contrib import comments
from django.db import transaction, connection
from django.conf import settings

PATH_SEPARATOR = getattr(settings, 'COMMENT_PATH_SEPARATOR', '/')
PATH_DIGITS = getattr(settings, 'COMMENT_PATH_DIGITS', 10)

SQL = """
INSERT INTO threadedcomments_comment(comment_ptr_id, tree_path) VALUES(%s, %s)
"""

class Command(NoArgsCommand):
help = "Migrates from django.contrib.comments to django-threadedcomments"

def handle(self, *args, **options):
transaction.commit_unless_managed()
transaction.enter_transaction_management()
transaction.managed(True)

cursor = connection.cursor()

for comment in comments.models.Comment.objects.all():
cursor.execute(SQL, [comment.id, ''])

transaction.commit()
transaction.leave_transaction_management()
28 changes: 18 additions & 10 deletions threadedcomments/management/commands/migrate_threaded_comments.py
@@ -1,8 +1,11 @@
from django.core.management.base import NoArgsCommand
from django.contrib import comments
from django.contrib.sites.models import Site
from django.db import transaction, connection
from django.conf import settings

from threadedcomments.models import ThreadedComment

USER_SQL = """
SELECT
content_type_id,
Expand Down Expand Up @@ -50,7 +53,7 @@ def handle(self, *args, **options):
transaction.enter_transaction_management()
transaction.managed(True)

ThreadedComment = comments.get_model()
site = Site.objects.all()[0]

cursor = connection.cursor()
cursor.execute(FREE_SQL)
Expand All @@ -59,16 +62,18 @@ def handle(self, *args, **options):
date_submitted, date_modified, date_approved, comment, markup,
is_public, is_approved, ip_address) = row
tc = ThreadedComment(
content_type_id=content_type,
content_type_id=content_type_id,
object_pk=object_id,
user_name=name,
user_email=email,
user_url=website,
comment=comment,
submit_date=date_submitted,
ip_address=ip_address,
is_approved=is_approved,
parent_id=parent_id
is_public=is_public,
is_removed=not is_approved,
parent_id=parent_id,
site=site,
)
tc.save(skip_tree_path=True)

Expand All @@ -79,14 +84,16 @@ def handle(self, *args, **options):
date_modified, date_approved, comment, markup, is_public,
is_approved, ip_address) = row
tc = ThreadedComment(
content_type_id=content_type,
content_type_id=content_type_id,
object_pk=object_id,
user_id=user_id,
comment=comment,
submit_date=date_submitted,
ip_address=ip_address,
is_approved=is_approved,
parent_id=parent_id
is_public=is_public,
is_removed=not is_approved,
parent_id=parent_id,
site=site,
)
tc.save(skip_tree_path=True)

Expand All @@ -96,10 +103,11 @@ def handle(self, *args, **options):
while current.parent:
current = current.parent
path.append(str(current.id).zfill(PATH_DIGITS))
tree_path = PATH_SEPARATOR.join(reversed(path))
comment.tree_path = PATH_SEPARATOR.join(reversed(path))
comment.save(skip_tree_path=True)
ThreadedComment.objects.filter(pk=comment.parent.pk).update(
last_child=comment)
if comment.parent:
ThreadedComment.objects.filter(pk=comment.parent.pk).update(
last_child=comment)

transaction.commit()
transaction.leave_transaction_management()
3 changes: 2 additions & 1 deletion threadedcomments/models.py
Expand Up @@ -21,8 +21,9 @@ def _root_id(self):
root_id = property(_root_id)

def save(self, *args, **kwargs):
skip_tree_path = kwargs.pop('skip_tree_path', False)
super(ThreadedComment, self).save(*args, **kwargs)
if kwargs.get('skip_tree_path'):
if skip_tree_path:
return None
tree_path = unicode(self.pk).zfill(PATH_DIGITS)
if self.parent:
Expand Down

0 comments on commit 981f280

Please sign in to comment.