/lib/python/django/contrib/comments/__init__.py

https://github.com/mozilla/moztrap-vendor-lib · Python · 94 lines · 63 code · 12 blank · 19 comment · 21 complexity · 4723dbd8e74e3f151af5e930f515f602 MD5 · raw file

  1. import warnings
  2. from django.conf import settings
  3. from django.core import urlresolvers
  4. from django.core.exceptions import ImproperlyConfigured
  5. from django.contrib.comments.models import Comment
  6. from django.contrib.comments.forms import CommentForm
  7. from django.utils.importlib import import_module
  8. warnings.warn("django.contrib.comments is deprecated and will be removed before Django 1.8.", PendingDeprecationWarning)
  9. DEFAULT_COMMENTS_APP = 'django.contrib.comments'
  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. # Try to import the package
  20. try:
  21. package = import_module(comments_app)
  22. except ImportError as e:
  23. raise ImproperlyConfigured("The COMMENTS_APP setting refers to "\
  24. "a non-existing package. (%s)" % e)
  25. return package
  26. def get_comment_app_name():
  27. """
  28. Returns the name of the comment app (either the setting value, if it
  29. exists, or the default).
  30. """
  31. return getattr(settings, 'COMMENTS_APP', DEFAULT_COMMENTS_APP)
  32. def get_model():
  33. """
  34. Returns the comment model class.
  35. """
  36. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_model"):
  37. return get_comment_app().get_model()
  38. else:
  39. return Comment
  40. def get_form():
  41. """
  42. Returns the comment ModelForm class.
  43. """
  44. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form"):
  45. return get_comment_app().get_form()
  46. else:
  47. return CommentForm
  48. def get_form_target():
  49. """
  50. Returns the target URL for the comment form submission view.
  51. """
  52. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_form_target"):
  53. return get_comment_app().get_form_target()
  54. else:
  55. return urlresolvers.reverse("django.contrib.comments.views.comments.post_comment")
  56. def get_flag_url(comment):
  57. """
  58. Get the URL for the "flag this comment" view.
  59. """
  60. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_flag_url"):
  61. return get_comment_app().get_flag_url(comment)
  62. else:
  63. return urlresolvers.reverse("django.contrib.comments.views.moderation.flag",
  64. args=(comment.id,))
  65. def get_delete_url(comment):
  66. """
  67. Get the URL for the "delete this comment" view.
  68. """
  69. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_delete_url"):
  70. return get_comment_app().get_delete_url(comment)
  71. else:
  72. return urlresolvers.reverse("django.contrib.comments.views.moderation.delete",
  73. args=(comment.id,))
  74. def get_approve_url(comment):
  75. """
  76. Get the URL for the "approve this comment from moderation" view.
  77. """
  78. if get_comment_app_name() != DEFAULT_COMMENTS_APP and hasattr(get_comment_app(), "get_approve_url"):
  79. return get_comment_app().get_approve_url(comment)
  80. else:
  81. return urlresolvers.reverse("django.contrib.comments.views.moderation.approve",
  82. args=(comment.id,))