2013-03-09 22:49:37 +08:00
|
|
|
import warnings
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core import urlresolvers
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2009-02-24 06:16:26 +08:00
|
|
|
from django.contrib.comments.models import Comment
|
|
|
|
from django.contrib.comments.forms import CommentForm
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2008-08-26 06:14:22 +08:00
|
|
|
|
2013-03-09 22:49:37 +08:00
|
|
|
warnings.warn("django.contrib.comments is deprecated and will be removed before Django 1.8.", PendingDeprecationWarning)
|
|
|
|
|
2009-02-24 06:16:26 +08:00
|
|
|
DEFAULT_COMMENTS_APP = 'django.contrib.comments'
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_comment_app():
|
|
|
|
"""
|
|
|
|
Get the comment app (i.e. "django.contrib.comments") as defined in the settings
|
|
|
|
"""
|
|
|
|
# Make sure the app's in INSTALLED_APPS
|
2008-08-26 14:58:43 +08:00
|
|
|
comments_app = get_comment_app_name()
|
2008-08-26 06:14:22 +08:00
|
|
|
if comments_app not in settings.INSTALLED_APPS:
|
|
|
|
raise ImproperlyConfigured("The COMMENTS_APP (%r) "\
|
|
|
|
"must be in INSTALLED_APPS" % settings.COMMENTS_APP)
|
|
|
|
|
|
|
|
# Try to import the package
|
|
|
|
try:
|
2009-03-19 00:55:59 +08:00
|
|
|
package = import_module(comments_app)
|
2012-12-17 04:08:47 +08:00
|
|
|
except ImportError as e:
|
2008-08-26 06:14:22 +08:00
|
|
|
raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
|
2012-12-17 04:08:47 +08:00
|
|
|
"a non-existing package. (%s)" % e)
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
return package
|
|
|
|
|
2008-08-26 14:58:43 +08:00
|
|
|
def get_comment_app_name():
|
|
|
|
"""
|
|
|
|
Returns the name of the comment app (either the setting value, if it
|
|
|
|
exists, or the default).
|
|
|
|
"""
|
2009-02-24 06:16:26 +08:00
|
|
|
return getattr(settings, 'COMMENTS_APP', DEFAULT_COMMENTS_APP)
|
2008-08-26 14:58:43 +08:00
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
def get_model():
|
2009-02-24 06:16:26 +08:00
|
|
|
"""
|
|
|
|
Returns the comment model class.
|
|
|
|
"""
|
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_model"):
|
|
|
|
return get_comment_app().get_model()
|
|
|
|
else:
|
|
|
|
return Comment
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_form():
|
2009-02-24 06:16:26 +08:00
|
|
|
"""
|
|
|
|
Returns the comment ModelForm class.
|
|
|
|
"""
|
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form"):
|
|
|
|
return get_comment_app().get_form()
|
|
|
|
else:
|
|
|
|
return CommentForm
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_form_target():
|
2009-02-24 06:16:26 +08:00
|
|
|
"""
|
|
|
|
Returns the target URL for the comment form submission view.
|
|
|
|
"""
|
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form_target"):
|
|
|
|
return get_comment_app().get_form_target()
|
|
|
|
else:
|
|
|
|
return urlresolvers.reverse("django.contrib.comments.views.comments.post_comment")
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_flag_url(comment):
|
|
|
|
"""
|
|
|
|
Get the URL for the "flag this comment" view.
|
|
|
|
"""
|
2009-02-24 06:16:26 +08:00
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_flag_url"):
|
2008-08-26 06:14:22 +08:00
|
|
|
return get_comment_app().get_flag_url(comment)
|
|
|
|
else:
|
2009-02-24 06:16:26 +08:00
|
|
|
return urlresolvers.reverse("django.contrib.comments.views.moderation.flag",
|
|
|
|
args=(comment.id,))
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_delete_url(comment):
|
|
|
|
"""
|
|
|
|
Get the URL for the "delete this comment" view.
|
|
|
|
"""
|
2009-02-24 06:16:26 +08:00
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_delete_url"):
|
|
|
|
return get_comment_app().get_delete_url(comment)
|
2008-08-26 06:14:22 +08:00
|
|
|
else:
|
2009-02-24 06:16:26 +08:00
|
|
|
return urlresolvers.reverse("django.contrib.comments.views.moderation.delete",
|
|
|
|
args=(comment.id,))
|
2008-08-26 06:14:22 +08:00
|
|
|
|
|
|
|
def get_approve_url(comment):
|
|
|
|
"""
|
|
|
|
Get the URL for the "approve this comment from moderation" view.
|
|
|
|
"""
|
2009-02-24 06:16:26 +08:00
|
|
|
if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_approve_url"):
|
2008-08-26 06:14:22 +08:00
|
|
|
return get_comment_app().get_approve_url(comment)
|
|
|
|
else:
|
2009-02-24 06:16:26 +08:00
|
|
|
return urlresolvers.reverse("django.contrib.comments.views.moderation.approve",
|
|
|
|
args=(comment.id,))
|