diff options
author | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
---|---|---|
committer | lxf <sng@luxagraf.net> | 2022-05-14 16:38:07 -0400 |
commit | bb3973ffb714c932e9ec6dd6a751228dc71fe1d3 (patch) | |
tree | 6fa32f9392ad2ec32271349b86a4c1388fd6ba95 /app/lib/django_comments/__init__.py |
initial commit
Diffstat (limited to 'app/lib/django_comments/__init__.py')
-rw-r--r-- | app/lib/django_comments/__init__.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/app/lib/django_comments/__init__.py b/app/lib/django_comments/__init__.py new file mode 100644 index 0000000..e4c5943 --- /dev/null +++ b/app/lib/django_comments/__init__.py @@ -0,0 +1,104 @@ +from importlib import import_module + +from django.apps import apps +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured +try: + from django.urls import reverse +except ImportError: + from django.core.urlresolvers import reverse # Django < 1.10 + + +DEFAULT_COMMENTS_APP = 'django_comments' + + +def get_comment_app(): + """ + Get the comment app (i.e. "django_comments") as defined in the settings + """ + # Make sure the app's in INSTALLED_APPS + comments_app = get_comment_app_name() + if not apps.is_installed(comments_app): + raise ImproperlyConfigured( + "The COMMENTS_APP (%r) must be in INSTALLED_APPS" % comments_app + ) + + # Try to import the package + try: + package = import_module(comments_app) + except ImportError as e: + raise ImproperlyConfigured( + "The COMMENTS_APP setting refers to a non-existing package. (%s)" % e + ) + + return package + + +def get_comment_app_name(): + """ + Returns the name of the comment app (either the setting value, if it + exists, or the default). + """ + return getattr(settings, 'COMMENTS_APP', DEFAULT_COMMENTS_APP) + + +def get_model(): + """ + Returns the comment model class. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_model"): + return get_comment_app().get_model() + else: + from django_comments.models import Comment + return Comment + + +def get_form(): + from django_comments.forms import CommentForm + """ + Returns the comment ModelForm class. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form"): + return get_comment_app().get_form() + else: + return CommentForm + + +def get_form_target(): + """ + Returns the target URL for the comment form submission view. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form_target"): + return get_comment_app().get_form_target() + else: + return reverse("comments-post-comment") + + +def get_flag_url(comment): + """ + Get the URL for the "flag this comment" view. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_flag_url"): + return get_comment_app().get_flag_url(comment) + else: + return reverse("comments-flag", args=(comment.id,)) + + +def get_delete_url(comment): + """ + Get the URL for the "delete this comment" view. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_delete_url"): + return get_comment_app().get_delete_url(comment) + else: + return reverse("comments-delete", args=(comment.id,)) + + +def get_approve_url(comment): + """ + Get the URL for the "approve this comment from moderation" view. + """ + if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_approve_url"): + return get_comment_app().get_approve_url(comment) + else: + return reverse("comments-approve", args=(comment.id,)) |