1
from django.conf import settings
2
from django.core import urlresolvers
3
from django.core.exceptions import ImproperlyConfigured
5
# Attributes required in the top-level app for COMMENTS_APP
6
REQUIRED_COMMENTS_APP_ATTRIBUTES = ["get_model", "get_form", "get_form_target"]
10
Get the comment app (i.e. "django.contrib.comments") as defined in the settings
12
# Make sure the app's in INSTALLED_APPS
13
comments_app = get_comment_app_name()
14
if comments_app not in settings.INSTALLED_APPS:
15
raise ImproperlyConfigured("The COMMENTS_APP (%r) "\
16
"must be in INSTALLED_APPS" % settings.COMMENTS_APP)
18
# Try to import the package
20
package = __import__(comments_app, '', '', [''])
22
raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
23
"a non-existing package.")
25
# Make sure some specific attributes exist inside that package.
26
for attribute in REQUIRED_COMMENTS_APP_ATTRIBUTES:
27
if not hasattr(package, attribute):
28
raise ImproperlyConfigured("The COMMENTS_APP package %r does not "\
29
"define the (required) %r function" % \
34
def get_comment_app_name():
36
Returns the name of the comment app (either the setting value, if it
37
exists, or the default).
39
return getattr(settings, 'COMMENTS_APP', 'django.contrib.comments')
42
from django.contrib.comments.models import Comment
46
from django.contrib.comments.forms import CommentForm
49
def get_form_target():
50
return urlresolvers.reverse("django.contrib.comments.views.comments.post_comment")
52
def get_flag_url(comment):
54
Get the URL for the "flag this comment" view.
56
if get_comment_app_name() != __name__ and hasattr(get_comment_app(), "get_flag_url"):
57
return get_comment_app().get_flag_url(comment)
59
return urlresolvers.reverse("django.contrib.comments.views.moderation.flag", args=(comment.id,))
61
def get_delete_url(comment):
63
Get the URL for the "delete this comment" view.
65
if get_comment_app_name() != __name__ and hasattr(get_comment_app(), "get_delete_url"):
66
return get_comment_app().get_flag_url(get_delete_url)
68
return urlresolvers.reverse("django.contrib.comments.views.moderation.delete", args=(comment.id,))
70
def get_approve_url(comment):
72
Get the URL for the "approve this comment from moderation" view.
74
if get_comment_app_name() != __name__ and hasattr(get_comment_app(), "get_approve_url"):
75
return get_comment_app().get_approve_url(comment)
77
return urlresolvers.reverse("django.contrib.comments.views.moderation.approve", args=(comment.id,))