diff --git a/wagtail/admin/edit_handlers.py b/wagtail/admin/edit_handlers.py index 599fd3e2d3..b225bc75f7 100644 --- a/wagtail/admin/edit_handlers.py +++ b/wagtail/admin/edit_handlers.py @@ -34,6 +34,9 @@ from .forms.models import ( # NOQA from .forms.pages import WagtailAdminPageForm +COMMENTS_RELATION_NAME = getattr(settings, 'WAGTAIL_COMMENTS_RELATION_NAME', 'wagtail_admin_comments') + + def widget_with_script(widget, script): return mark_safe('{0}'.format(widget, script)) @@ -847,7 +850,7 @@ class CommentPanel(EditHandler): } return { - 'wagtail_admin_comments': { + COMMENTS_RELATION_NAME: { 'form': CommentFormWithRequest, 'fields': ['text', 'contentpath', 'position'], 'formset_name': 'comments', diff --git a/wagtail/admin/views/pages/edit.py b/wagtail/admin/views/pages/edit.py index ffc47bc265..c3916ef70c 100644 --- a/wagtail/admin/views/pages/edit.py +++ b/wagtail/admin/views/pages/edit.py @@ -25,6 +25,9 @@ from wagtail.core.models import ( Comment, CommentReply, Page, PageSubscription, UserPagePermissionsProxy, WorkflowState) +COMMENTS_RELATION_NAME = getattr(settings, 'WAGTAIL_COMMENTS_RELATION_NAME', 'wagtail_admin_comments') + + class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): def get_template_names(self): if self.page.alias_of_id: @@ -141,15 +144,15 @@ class EditView(TemplateResponseMixin, ContextMixin, HookResponseMixin, View): comments = Comment.objects.filter(id__in=relevant_comment_ids) thread_users = get_user_model().objects.exclude(pk=self.request.user.pk).exclude(pk__in=subscribers.values_list('user_id', flat=True)).prefetch_related( Prefetch('comment_replies', queryset=replies), - Prefetch('wagtail_admin_comments', queryset=comments) + Prefetch(COMMENTS_RELATION_NAME, queryset=comments) ).exclude( - Q(comment_replies__isnull=True) & Q(wagtail_admin_comments__isnull=True) + Q(comment_replies__isnull=True) & Q(**{('%s__isnull' % COMMENTS_RELATION_NAME): True}) ) # Skip if no recipients if not (global_recipient_users or thread_users): return - thread_users = [(user, set(list(user.comment_replies.values_list('comment_id', flat=True)) + list(user.wagtail_admin_comments.values_list('pk', flat=True)))) for user in thread_users] + thread_users = [(user, set(list(user.comment_replies.values_list('comment_id', flat=True)) + list(getattr(user, COMMENTS_RELATION_NAME).values_list('pk', flat=True)))) for user in thread_users] mailed_users = set() for current_user, current_threads in thread_users: diff --git a/wagtail/core/migrations/0062_comment_models_and_pagesubscription.py b/wagtail/core/migrations/0062_comment_models_and_pagesubscription.py index ba6719c47d..818450978b 100644 --- a/wagtail/core/migrations/0062_comment_models_and_pagesubscription.py +++ b/wagtail/core/migrations/0062_comment_models_and_pagesubscription.py @@ -6,6 +6,9 @@ import django.db.models.deletion import modelcluster.fields +COMMENTS_RELATION_NAME = getattr(settings, 'WAGTAIL_COMMENTS_RELATION_NAME', 'wagtail_admin_comments') + + class Migration(migrations.Migration): dependencies = [ @@ -24,10 +27,10 @@ class Migration(migrations.Migration): ('created_at', models.DateTimeField(auto_now_add=True)), ('updated_at', models.DateTimeField(auto_now=True)), ('resolved_at', models.DateTimeField(blank=True, null=True)), - ('page', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_admin_comments', to='wagtailcore.Page')), + ('page', modelcluster.fields.ParentalKey(on_delete=django.db.models.deletion.CASCADE, related_name=COMMENTS_RELATION_NAME, to='wagtailcore.Page')), ('resolved_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='comments_resolved', to=settings.AUTH_USER_MODEL)), ('revision_created', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='created_comments', to='wagtailcore.PageRevision')), - ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='wagtail_admin_comments', to=settings.AUTH_USER_MODEL)), + ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name=COMMENTS_RELATION_NAME, to=settings.AUTH_USER_MODEL)), ], options={ 'verbose_name': 'comment', diff --git a/wagtail/core/models/__init__.py b/wagtail/core/models/__init__.py index 28b867b332..e6264e9f4a 100644 --- a/wagtail/core/models/__init__.py +++ b/wagtail/core/models/__init__.py @@ -77,6 +77,7 @@ from .view_restrictions import BaseViewRestriction logger = logging.getLogger('wagtail.core') PAGE_TEMPLATE_VAR = 'page' +COMMENTS_RELATION_NAME = getattr(settings, 'WAGTAIL_COMMENTS_RELATION_NAME', 'wagtail_admin_comments') @receiver(pre_validate_delete, sender=Locale) @@ -330,7 +331,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): # An array of additional field names that will not be included when a Page is copied. exclude_fields_in_copy = [] - default_exclude_fields_in_copy = ['id', 'path', 'depth', 'numchild', 'url_path', 'path', 'postgres_index_entries', 'index_entries', 'wagtail_admin_comments'] + default_exclude_fields_in_copy = ['id', 'path', 'depth', 'numchild', 'url_path', 'path', 'postgres_index_entries', 'index_entries', COMMENTS_RELATION_NAME] # Define these attributes early to avoid masking errors. (Issue #3078) # The canonical definition is in wagtailadmin.edit_handlers. @@ -863,7 +864,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): if clean: self.full_clean() - new_comments = self.wagtail_admin_comments.filter(pk__isnull=True) + new_comments = getattr(self, COMMENTS_RELATION_NAME).filter(pk__isnull=True) for comment in new_comments: # We need to ensure comments have an id in the revision, so positions can be identified correctly comment.save() @@ -879,7 +880,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): for comment in new_comments: comment.revision_created = revision - update_fields = ['wagtail_admin_comments'] + update_fields = [COMMENTS_RELATION_NAME] self.latest_revision_created_at = revision.created_at update_fields.append('latest_revision_created_at') @@ -2255,7 +2256,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): * ``latest_revision_created_at`` * ``first_published_at`` * ``alias_of`` - * ``wagtail_admin_comments`` + * ``wagtail_admin_comments`` (COMMENTS_RELATION_NAME) """ obj = self.specific_class.from_json(content_json) @@ -2288,8 +2289,8 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): obj.translation_key = self.translation_key obj.locale = self.locale obj.alias_of_id = self.alias_of_id - revision_comments = obj.wagtail_admin_comments - page_comments = self.wagtail_admin_comments.filter(resolved_at__isnull=True) + revision_comments = getattr(obj, COMMENTS_RELATION_NAME) + page_comments = getattr(self, COMMENTS_RELATION_NAME).filter(resolved_at__isnull=True) for comment in page_comments: # attempt to retrieve the comment position from the revision's stored version # of the comment @@ -2298,7 +2299,7 @@ class Page(AbstractPage, index.Indexed, ClusterableModel, metaclass=PageBase): comment.position = revision_comment.position except Comment.DoesNotExist: pass - obj.wagtail_admin_comments = page_comments + setattr(obj, COMMENTS_RELATION_NAME, page_comments) return obj @@ -2578,7 +2579,7 @@ class PageRevision(models.Model): page.save() - for comment in page.wagtail_admin_comments.all().only('position'): + for comment in getattr(page, COMMENTS_RELATION_NAME).all().only('position'): comment.save(update_fields=['position']) self.submitted_for_moderation = False @@ -4052,8 +4053,8 @@ class Comment(ClusterableModel): """ A comment on a field, or a field within a streamfield block """ - page = ParentalKey(Page, on_delete=models.CASCADE, related_name='wagtail_admin_comments') - user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='wagtail_admin_comments') + page = ParentalKey(Page, on_delete=models.CASCADE, related_name=COMMENTS_RELATION_NAME) + user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name=COMMENTS_RELATION_NAME) text = models.TextField() contentpath = models.TextField()