PageRenderTime 138ms CodeModel.GetById 133ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/django/contrib/comments/__init__.py

https://code.google.com/p/mango-py/
Python | 91 lines | 88 code | 2 blank | 1 comment | 1 complexity | 4c2c972793e6e45fd203aef9b1facc01 MD5 | raw file
 1from django.conf import settings
 2from django.core import urlresolvers
 3from django.core.exceptions import ImproperlyConfigured
 4from django.contrib.comments.models import Comment
 5from django.contrib.comments.forms import CommentForm
 6from django.utils.importlib import import_module
 7
 8DEFAULT_COMMENTS_APP = 'django.contrib.comments'
 9
10def 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
29def 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
36def 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
45def 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
54def 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
63def 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
73def 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
83def 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,))