Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sites optional #119

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
History
=======

Pending
-------

* 'sites' framework continues to be supported but is no longer required.

1.8.0 (2017-02-03)
------------------

Expand Down
9 changes: 6 additions & 3 deletions django_comments/abstracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class BaseCommentAbstractModel(models.Model):
content_object = GenericForeignKey(ct_field="content_type", fk_field="object_pk")

# Metadata about the comment
site = models.ForeignKey(Site, on_delete=models.CASCADE)
site = models.ForeignKey(Site, on_delete=models.CASCADE, null=True)

class Meta:
abstract = True
Expand Down Expand Up @@ -168,7 +168,10 @@ def get_as_text(self):
'user': self.user or self.name,
'date': self.submit_date,
'comment': self.comment,
'domain': self.site.domain,
'domain': self.site.domain if self.site else '',
'url': self.get_absolute_url()
}
return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d
if self.site:
return _('Posted by %(user)s at %(date)s\n\n%(comment)s\n\nhttp://%(domain)s%(url)s') % d
else:
return _('Posted by %(user)s at %(date)s\n\n%(comment)s') % d
2 changes: 1 addition & 1 deletion django_comments/feeds.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def description(self):

def items(self):
qs = django_comments.get_model().objects.filter(
site__pk=self.site.pk,
site__pk=getattr(self.site, 'pk', None),
is_public=True,
is_removed=False,
)
Expand Down
1 change: 0 additions & 1 deletion django_comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
class Migration(migrations.Migration):

dependencies = [
('sites', '0001_initial'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot simply remove that line, because later in that migration, you have a reference to sites.Site which won't resolve with this removal. I can imagine various scenarios to workaround this:

  • just make the Site fk nullable without removing the contrib.sites requirement.
  • replace the sites.Site by some sort of placeholder which will either import the Site model if contrib.sites is installed or replace it by some fake model otherwise. Not sure if it is feasible.
    Sorry, it's probably harder than it appears, but worth a try.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're creating a migration for a model that uses another model that has no migrations, shouldn't you be able to do that without requiring a '0001_initial' from that other object (since it doesn't exist)? However, even if it ran without that migration dependency, the 'sites' would still need to be installed for that ForeignKey to be created (forgot about that).

Okay, I guess I'll have to re-think this a bit. The "placeholder" seems like a good idea but if someone ever turned sites on/off after installing this package they'd likely get some confusing errors.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an extension of ForeignKey would be needed. One that could handle 'sites' being turned on/off or possibly give a descriptive warning if 'sites' is turned on/off without changing the database properly.

migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('contenttypes', '0001_initial'),
]
Expand Down
20 changes: 20 additions & 0 deletions django_comments/migrations/0004_allow_null_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('django_comments', '0003_add_submit_date_index'),
]

operations = [
migrations.AlterField(
model_name='comment',
name='site',
field=models.ForeignKey(to='sites.Site', on_delete=models.CASCADE, null=True),
preserve_default=True,
),
]
2 changes: 1 addition & 1 deletion django_comments/templatetags/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def get_queryset(self, context):
# get_current_site operates.
site_id = getattr(settings, "SITE_ID", None)
if not site_id and ('request' in context):
site_id = get_current_site(context['request']).pk
site_id = getattr(get_current_site(context['request']), 'pk', None)

qs = self.comment_model.objects.filter(
content_type=ctype,
Expand Down
2 changes: 1 addition & 1 deletion django_comments/views/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def post_comment(request, next=None, using=None):
)

# Otherwise create the comment
comment = form.get_comment_object(site_id=get_current_site(request).id)
comment = form.get_comment_object(site_id=getattr(get_current_site(request), 'id', None))
comment.ip_address = request.META.get("REMOTE_ADDR", None)
if user_is_authenticated:
comment.user = request.user
Expand Down
7 changes: 3 additions & 4 deletions django_comments/views/moderation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import

from django.conf import settings
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import get_object_or_404, render
Expand All @@ -24,7 +23,7 @@ def flag(request, comment_id, next=None):
"""
comment = get_object_or_404(django_comments.get_model(),
pk=comment_id,
site__pk=get_current_site(request).pk)
site__pk=getattr(get_current_site(request), 'pk', None))

# Flag on POST
if request.method == 'POST':
Expand All @@ -51,7 +50,7 @@ def delete(request, comment_id, next=None):
"""
comment = get_object_or_404(django_comments.get_model(),
pk=comment_id,
site__pk=get_current_site(request).pk)
site__pk=getattr(get_current_site(request), 'pk', None))

# Delete on POST
if request.method == 'POST':
Expand Down Expand Up @@ -79,7 +78,7 @@ def approve(request, comment_id, next=None):
"""
comment = get_object_or_404(django_comments.get_model(),
pk=comment_id,
site__pk=get_current_site(request).pk)
site__pk=getattr(get_current_site(request), 'pk', None))

# Delete on POST
if request.method == 'POST':
Expand Down
2 changes: 1 addition & 1 deletion docs/models.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The comment models

A :class:`~django.db.models.ForeignKey` to the
:class:`~django.contrib.sites.models.Site` on which the comment was
posted.
posted. Will be `None` if not using the 'sites' framework.

.. attribute:: user

Expand Down
4 changes: 0 additions & 4 deletions docs/quickstart.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ To get started using the ``comments`` app, follow these steps:

#. Install the comments app by running ``pip install django-contrib-comments``.

#. :ref:`Enable the "sites" framework <enabling-the-sites-framework>` by adding
``'django.contrib.sites'`` to :setting:`INSTALLED_APPS` and defining
:setting:`SITE_ID`.

#. Install the comments framework by adding ``'django_comments'`` to
:setting:`INSTALLED_APPS`.

Expand Down