|
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 |
|
7 |
|
8 DEFAULT_COMMENTS_APP = 'django.contrib.comments' |
|
9 |
|
10 def get_comment_app(): |
|
11 """ |
|
12 Get the comment app (i.e. "django.contrib.comments") as defined in the settings |
|
13 """ |
|
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) |
|
19 |
|
20 # Try to import the package |
|
21 try: |
|
22 package = import_module(comments_app) |
|
23 except ImportError: |
|
24 raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\ |
|
25 "a non-existing package.") |
|
26 |
|
27 return package |
|
28 |
|
29 def get_comment_app_name(): |
|
30 """ |
|
31 Returns the name of the comment app (either the setting value, if it |
|
32 exists, or the default). |
|
33 """ |
|
34 return getattr(settings, 'COMMENTS_APP', DEFAULT_COMMENTS_APP) |
|
35 |
|
36 def get_model(): |
|
37 """ |
|
38 Returns the comment model class. |
|
39 """ |
|
40 if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_model"): |
|
41 return get_comment_app().get_model() |
|
42 else: |
|
43 return Comment |
|
44 |
|
45 def get_form(): |
|
46 """ |
|
47 Returns the comment ModelForm class. |
|
48 """ |
|
49 if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form"): |
|
50 return get_comment_app().get_form() |
|
51 else: |
|
52 return CommentForm |
|
53 |
|
54 def get_form_target(): |
|
55 """ |
|
56 Returns the target URL for the comment form submission view. |
|
57 """ |
|
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() |
|
60 else: |
|
61 return urlresolvers.reverse("django.contrib.comments.views.comments.post_comment") |
|
62 |
|
63 def get_flag_url(comment): |
|
64 """ |
|
65 Get the URL for the "flag this comment" view. |
|
66 """ |
|
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) |
|
69 else: |
|
70 return urlresolvers.reverse("django.contrib.comments.views.moderation.flag", |
|
71 args=(comment.id,)) |
|
72 |
|
73 def get_delete_url(comment): |
|
74 """ |
|
75 Get the URL for the "delete this comment" view. |
|
76 """ |
|
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) |
|
79 else: |
|
80 return urlresolvers.reverse("django.contrib.comments.views.moderation.delete", |
|
81 args=(comment.id,)) |
|
82 |
|
83 def get_approve_url(comment): |
|
84 """ |
|
85 Get the URL for the "approve this comment from moderation" view. |
|
86 """ |
|
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) |
|
89 else: |
|
90 return urlresolvers.reverse("django.contrib.comments.views.moderation.approve", |
|
91 args=(comment.id,)) |