From 26259b63f6a1ca05cb6416ec95171f66a242aa5a Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 29 Jul 2015 19:43:11 -0400 Subject: [PATCH] Migrate to use a local model for document comments. Closes gh-issue #233. --- mayan/apps/document_comments/admin.py | 14 +++++ mayan/apps/document_comments/apps.py | 19 ++----- mayan/apps/document_comments/forms.py | 3 +- .../migrations/0001_initial.py | 52 +++++++++++++++++++ .../migrations/0002_auto_20150729_2144.py | 23 ++++++++ .../migrations/0003_auto_20150729_2144.py | 46 ++++++++++++++++ .../document_comments/migrations/__init__.py | 0 mayan/apps/document_comments/models.py | 29 +++++++++++ mayan/apps/document_comments/views.py | 36 +++++++------ 9 files changed, 192 insertions(+), 30 deletions(-) create mode 100644 mayan/apps/document_comments/admin.py create mode 100644 mayan/apps/document_comments/migrations/0001_initial.py create mode 100644 mayan/apps/document_comments/migrations/0002_auto_20150729_2144.py create mode 100644 mayan/apps/document_comments/migrations/0003_auto_20150729_2144.py create mode 100644 mayan/apps/document_comments/migrations/__init__.py create mode 100644 mayan/apps/document_comments/models.py diff --git a/mayan/apps/document_comments/admin.py b/mayan/apps/document_comments/admin.py new file mode 100644 index 0000000000..2ca2190d21 --- /dev/null +++ b/mayan/apps/document_comments/admin.py @@ -0,0 +1,14 @@ +from __future__ import unicode_literals + +from django.contrib import admin + +from .models import Comment + + +class CommentAdmin(admin.ModelAdmin): + date_hierarchy = 'submit_date' + list_display = ('document', 'submit_date', 'user', 'comment') + readonly_fields = ('document', 'submit_date', 'user', 'comment') + + +admin.site.register(Comment, CommentAdmin) diff --git a/mayan/apps/document_comments/apps.py b/mayan/apps/document_comments/apps.py index 1734f1cf4c..1c82786a64 100644 --- a/mayan/apps/document_comments/apps.py +++ b/mayan/apps/document_comments/apps.py @@ -1,12 +1,9 @@ -from __future__ import absolute_import, unicode_literals +from __future__ import unicode_literals -from django.contrib.comments.models import Comment -from django.contrib.contenttypes import generic from django.utils.translation import ugettext_lazy as _ from acls import ModelPermission from common import MayanAppConfig, menu_facet, menu_object, menu_sidebar -from common.classes import ModelAttribute from common.utils import encapsulate from documents.models import Document from navigation import SourceColumn @@ -14,6 +11,7 @@ from navigation import SourceColumn from .links import ( link_comment_add, link_comment_delete, link_comments_for_document ) +from .models import Comment from .permissions import ( permission_comment_create, permission_comment_delete, permission_comment_view @@ -29,15 +27,6 @@ class DocumentCommentsApp(MayanAppConfig): def ready(self): super(DocumentCommentsApp, self).ready() - Document.add_to_class( - 'comments', - generic.GenericRelation( - Comment, - content_type_field='content_type', - object_id_field='object_pk' - ) - ) - ModelPermission.register( model=Document, permissions=( permission_comment_create, permission_comment_delete, @@ -61,7 +50,9 @@ class DocumentCommentsApp(MayanAppConfig): 'comments:comment_delete', 'comments:comment_multiple_delete' ) ) - menu_object.bind_links(links=(link_comment_delete,), sources=(Comment,)) + menu_object.bind_links( + links=(link_comment_delete,), sources=(Comment,) + ) menu_facet.bind_links( links=(link_comments_for_document,), sources=(Document,) ) diff --git a/mayan/apps/document_comments/forms.py b/mayan/apps/document_comments/forms.py index 064f59d9ad..ac6f02c69a 100644 --- a/mayan/apps/document_comments/forms.py +++ b/mayan/apps/document_comments/forms.py @@ -1,7 +1,8 @@ from __future__ import unicode_literals from django import forms -from django.contrib.comments.models import Comment + +from .models import Comment class CommentForm(forms.ModelForm): diff --git a/mayan/apps/document_comments/migrations/0001_initial.py b/mayan/apps/document_comments/migrations/0001_initial.py new file mode 100644 index 0000000000..cd9fd3f8c4 --- /dev/null +++ b/mayan/apps/document_comments/migrations/0001_initial.py @@ -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,), + ), + ] diff --git a/mayan/apps/document_comments/migrations/0002_auto_20150729_2144.py b/mayan/apps/document_comments/migrations/0002_auto_20150729_2144.py new file mode 100644 index 0000000000..b1f527a5ca --- /dev/null +++ b/mayan/apps/document_comments/migrations/0002_auto_20150729_2144.py @@ -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, + ), + ] diff --git a/mayan/apps/document_comments/migrations/0003_auto_20150729_2144.py b/mayan/apps/document_comments/migrations/0003_auto_20150729_2144.py new file mode 100644 index 0000000000..889689de4e --- /dev/null +++ b/mayan/apps/document_comments/migrations/0003_auto_20150729_2144.py @@ -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), + ] diff --git a/mayan/apps/document_comments/migrations/__init__.py b/mayan/apps/document_comments/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/mayan/apps/document_comments/models.py b/mayan/apps/document_comments/models.py new file mode 100644 index 0000000000..7586c9865e --- /dev/null +++ b/mayan/apps/document_comments/models.py @@ -0,0 +1,29 @@ +from __future__ import unicode_literals + +from django.conf import settings +from django.db import models +from django.utils.translation import ugettext_lazy as _ + +from documents.models import Document + + +class Comment(models.Model): + document = models.ForeignKey( + Document, db_index=True, related_name='comments', + verbose_name=_('Document') + ) + user = models.ForeignKey( + settings.AUTH_USER_MODEL, editable=False, related_name='comments', + verbose_name=_('User'), + ) + comment = models.TextField(verbose_name=_('comment')) + submit_date = models.DateTimeField( + auto_now_add=True, db_index=True, + verbose_name=_('Date time submitted') + ) + + class Meta: + get_latest_by = 'submit_date' + ordering = ('-submit_date',) + verbose_name = _('Comment') + verbose_name_plural = _('Comments') diff --git a/mayan/apps/document_comments/views.py b/mayan/apps/document_comments/views.py index 09e0a781db..c30e3f8744 100644 --- a/mayan/apps/document_comments/views.py +++ b/mayan/apps/document_comments/views.py @@ -2,9 +2,6 @@ from __future__ import absolute_import, unicode_literals from django.conf import settings from django.contrib import messages -from django.contrib.comments.models import Comment -from django.contrib.contenttypes.models import ContentType -from django.contrib.sites.models import Site from django.core.exceptions import PermissionDenied from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect @@ -17,6 +14,7 @@ from documents.models import Document from permissions import Permission from .forms import CommentForm +from .models import Comment from .permissions import ( permission_comment_create, permission_comment_delete, permission_comment_view @@ -36,19 +34,27 @@ def comment_delete(request, comment_id=None, comment_id_list=None): ] try: - Permission.check_permissions(request.user, [permission_comment_delete]) + Permission.check_permissions(request.user, ( + permission_comment_delete,)) except PermissionDenied: comments = AccessControlList.objects.filter_by_access( permission_comment_delete, request.user, comments, - related='content_object' ) if not comments: messages.error(request, _('Must provide at least one comment.')) - return HttpResponseRedirect(request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))) + return HttpResponseRedirect( + request.META.get( + 'HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL) + ) + ) - previous = request.POST.get('previous', request.GET.get('previous', request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL)))) - next = request.POST.get('next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL)))) + previous = request.POST.get( + 'previous', request.GET.get('previous', request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))) + ) + next = request.POST.get( + 'next', request.GET.get('next', post_action_redirect if post_action_redirect else request.META.get('HTTP_REFERER', reverse(settings.LOGIN_REDIRECT_URL))) + ) if request.method == 'POST': for comment in comments: @@ -74,7 +80,7 @@ def comment_delete(request, comment_id=None, comment_id_list=None): 'next': next, } if len(comments) == 1: - context['object'] = comments[0].content_object + context['object'] = comments[0].document context['title'] = _('Delete comment?') elif len(comments) > 1: context['title'] = _('Delete comments?') @@ -93,7 +99,9 @@ def comment_add(request, document_id): document = get_object_or_404(Document, pk=document_id) try: - Permission.check_permissions(request.user, [permission_comment_create]) + Permission.check_permissions( + request.user, (permission_comment_create,) + ) except PermissionDenied: AccessControlList.objects.check_access( permission_comment_create, request.user, document @@ -108,9 +116,7 @@ def comment_add(request, document_id): if form.is_valid(): comment = form.save(commit=False) comment.user = request.user - comment.content_type = ContentType.objects.get_for_model(document) - comment.object_pk = document.pk - comment.site = Site.objects.get_current() + comment.document = document comment.save() messages.success(request, _('Comment added successfully.')) @@ -133,7 +139,7 @@ def comments_for_document(request, document_id): document = get_object_or_404(Document, pk=document_id) try: - Permission.check_permissions(request.user, [permission_comment_view]) + Permission.check_permissions(request.user, (permission_comment_view,)) except PermissionDenied: AccessControlList.objects.check_access( permission_comment_view, request.user, document @@ -143,7 +149,7 @@ def comments_for_document(request, document_id): 'object': document, 'access_object': document, 'title': _('Comments for document: %s') % document, - 'object_list': Comment.objects.for_model(document).order_by('-submit_date'), + 'object_list': document.comments.all(), 'hide_link': True, 'hide_object': True, }, context_instance=RequestContext(request))