~canonical-django/canonical-django/project-template

« back to all changes in this revision

Viewing changes to trunk/python-packages/django/contrib/comments/__init__.py

  • Committer: Matthew Nuzum
  • Date: 2008-11-13 05:46:03 UTC
  • Revision ID: matthew.nuzum@canonical.com-20081113054603-v0kvr6z6xyexvqt3
adding to version control

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from django.conf import settings
 
2
from django.core import urlresolvers
 
3
from django.core.exceptions import ImproperlyConfigured
 
4
 
 
5
# Attributes required in the top-level app for COMMENTS_APP
 
6
REQUIRED_COMMENTS_APP_ATTRIBUTES = ["get_model", "get_form", "get_form_target"]
 
7
 
 
8
def get_comment_app():
 
9
    """
 
10
    Get the comment app (i.e. "django.contrib.comments") as defined in the settings
 
11
    """
 
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)
 
17
 
 
18
    # Try to import the package
 
19
    try:
 
20
        package = __import__(comments_app, '', '', [''])
 
21
    except ImportError:
 
22
        raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
 
23
                                   "a non-existing package.")
 
24
 
 
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" % \
 
30
                                            (package, attribute))
 
31
 
 
32
    return package
 
33
 
 
34
def get_comment_app_name():
 
35
    """
 
36
    Returns the name of the comment app (either the setting value, if it
 
37
    exists, or the default).
 
38
    """
 
39
    return getattr(settings, 'COMMENTS_APP', 'django.contrib.comments')
 
40
 
 
41
def get_model():
 
42
    from django.contrib.comments.models import Comment
 
43
    return Comment
 
44
 
 
45
def get_form():
 
46
    from django.contrib.comments.forms import CommentForm
 
47
    return CommentForm
 
48
 
 
49
def get_form_target():
 
50
    return urlresolvers.reverse("django.contrib.comments.views.comments.post_comment")
 
51
 
 
52
def get_flag_url(comment):
 
53
    """
 
54
    Get the URL for the "flag this comment" view.
 
55
    """
 
56
    if get_comment_app_name() != __name__ and hasattr(get_comment_app(), "get_flag_url"):
 
57
        return get_comment_app().get_flag_url(comment)
 
58
    else:
 
59
        return urlresolvers.reverse("django.contrib.comments.views.moderation.flag", args=(comment.id,))
 
60
 
 
61
def get_delete_url(comment):
 
62
    """
 
63
    Get the URL for the "delete this comment" view.
 
64
    """
 
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)
 
67
    else:
 
68
        return urlresolvers.reverse("django.contrib.comments.views.moderation.delete", args=(comment.id,))
 
69
 
 
70
def get_approve_url(comment):
 
71
    """
 
72
    Get the URL for the "approve this comment from moderation" view.
 
73
    """
 
74
    if get_comment_app_name() != __name__ and hasattr(get_comment_app(), "get_approve_url"):
 
75
        return get_comment_app().get_approve_url(comment)
 
76
    else:
 
77
        return urlresolvers.reverse("django.contrib.comments.views.moderation.approve", args=(comment.id,))