Migrate to use a local model for document comments. Closes gh-issue #233.

This commit is contained in:
Roberto Rosario
2015-07-29 19:43:11 -04:00
parent 97f3451caf
commit 26259b63f6
9 changed files with 192 additions and 30 deletions
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
from django.conf import settings
class Migration(migrations.Migration):
dependencies = [
('documents', '0026_auto_20150729_2140'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
(
'id', models.AutoField(
verbose_name='ID', serialize=False, auto_created=True,
primary_key=True
)
),
('comment', models.TextField(verbose_name='comment')),
(
'submit_date', models.DateTimeField(
verbose_name='Date time submitted', db_index=True
)
),
(
'document', models.ForeignKey(
related_name='comments', verbose_name='Document',
to='documents.Document'
)
),
(
'user', models.ForeignKey(
related_name='comments', editable=False,
to=settings.AUTH_USER_MODEL, verbose_name='User'
)
),
],
options={
'ordering': ('-submit_date',),
'get_latest_by': 'submit_date',
'verbose_name': 'Comment',
'verbose_name_plural': 'Comments',
},
bases=(models.Model,),
),
]
@@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('document_comments', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='comment',
name='submit_date',
field=models.DateTimeField(
auto_now_add=True, verbose_name='Date time submitted',
db_index=True
),
preserve_default=True,
),
]
@@ -0,0 +1,46 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
from django.conf import settings
def migrate_old_comments(apps, schema_editor):
# https://code.djangoproject.com/ticket/24282
# If someone has a better solution until Django 1.8, would appreciate
# a pull-request :)
try:
from django.contrib.comments.models import Comment as OldComment
except ImportError:
# Django > 1.7
pass
else:
Comment = apps.get_model('document_comments', 'Comment')
Document = apps.get_model('documents', 'Document')
User = apps.get_model(*settings.AUTH_USER_MODEL.split('.'))
for old_comment in OldComment.objects.all():
comment = Comment(
document=Document.objects.get(pk=old_comment.object_pk),
user=User(old_comment.user.pk),
comment=old_comment.comment,
submit_date=old_comment.submit_date,
)
comment.save()
class Migration(migrations.Migration):
dependencies = [
('document_comments', '0002_auto_20150729_2144'),
('documents', '0001_initial'),
('sites', '0001_initial'),
('contenttypes', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL)
]
operations = [
migrations.RunPython(migrate_old_comments),
]