1 from django
.conf
import settings
2 from django
.core
import urlresolvers
3 from django
.core
.exceptions
import ImproperlyConfigured
4 from django
.contrib
.comments
.models
import Comment
5 from django
.contrib
.comments
.forms
import CommentForm
6 from django
.utils
.importlib
import import_module
8 DEFAULT_COMMENTS_APP
= 'django.contrib.comments'
10 def get_comment_app():
12 Get the comment app (i.e. "django.contrib.comments") as defined in the settings
14 # Make sure the app's in INSTALLED_APPS
15 comments_app
= get_comment_app_name()
16 if comments_app
not in settings
.INSTALLED_APPS
:
17 raise ImproperlyConfigured("The COMMENTS_APP (%r) "\
18 "must be in INSTALLED_APPS" % settings
.COMMENTS_APP
)
20 # Try to import the package
22 package
= import_module(comments_app
)
24 raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
25 "a non-existing package.")
29 def get_comment_app_name():
31 Returns the name of the comment app (either the setting value, if it
32 exists, or the default).
34 return getattr(settings
, 'COMMENTS_APP', DEFAULT_COMMENTS_APP
)
38 Returns the comment model class.
40 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_model"):
41 return get_comment_app().get_model()
47 Returns the comment ModelForm class.
49 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_form"):
50 return get_comment_app().get_form()
54 def get_form_target():
56 Returns the target URL for the comment form submission view.
58 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_form_target"):
59 return get_comment_app().get_form_target()
61 return urlresolvers
.reverse("django.contrib.comments.views.comments.post_comment")
63 def get_flag_url(comment
):
65 Get the URL for the "flag this comment" view.
67 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_flag_url"):
68 return get_comment_app().get_flag_url(comment
)
70 return urlresolvers
.reverse("django.contrib.comments.views.moderation.flag",
73 def get_delete_url(comment
):
75 Get the URL for the "delete this comment" view.
77 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_delete_url"):
78 return get_comment_app().get_delete_url(comment
)
80 return urlresolvers
.reverse("django.contrib.comments.views.moderation.delete",
83 def get_approve_url(comment
):
85 Get the URL for the "approve this comment from moderation" view.
87 if get_comment_app_name() != DEFAULT_COMMENTS_APP
and hasattr(get_comment_app(), "get_approve_url"):
88 return get_comment_app().get_approve_url(comment
)
90 return urlresolvers
.reverse("django.contrib.comments.views.moderation.approve",