validators.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from django.core.exceptions import ValidationError
  2. from django.utils.module_loading import import_string
  3. from django.utils.translation import gettext as _
  4. from django.utils.translation import ngettext
  5. from misago.categories import THREADS_ROOT_NAME
  6. from misago.categories.models import Category
  7. from misago.categories.permissions import can_browse_category, can_see_category
  8. from misago.conf import settings
  9. from misago.core.validators import validate_sluggable
  10. from .threadtypes import trees_map
  11. def validate_category(user_acl, category_id, allow_root=False):
  12. try:
  13. threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
  14. category = Category.objects.get(
  15. tree_id=threads_tree_id,
  16. id=category_id,
  17. )
  18. except Category.DoesNotExist:
  19. category = None
  20. # Skip ACL validation for root category?
  21. if allow_root and category and not category.level:
  22. return category
  23. if not category or not can_see_category(user_acl, category):
  24. raise ValidationError(_("Requested category could not be found."))
  25. if not can_browse_category(user_acl, category):
  26. raise ValidationError(_("You don't have permission to access this category."))
  27. return category
  28. def validate_thread_title(settings, title):
  29. validate_thread_title_length(settings, title)
  30. error_not_sluggable = _("Thread title should contain alpha-numeric characters.")
  31. error_slug_too_long = _("Thread title is too long.")
  32. validate_sluggable(error_not_sluggable, error_slug_too_long)(title)
  33. def validate_thread_title_length(settings, value):
  34. value_len = len(value)
  35. if not value_len:
  36. raise ValidationError(_("You have to enter an thread title."))
  37. if value_len < settings.thread_title_length_min:
  38. message = ngettext(
  39. "Thread title should be at least %(limit_value)s character long (it has %(show_value)s).",
  40. "Thread title should be at least %(limit_value)s characters long (it has %(show_value)s).",
  41. settings.thread_title_length_min,
  42. )
  43. raise ValidationError(
  44. message % {
  45. 'limit_value': settings.thread_title_length_min,
  46. 'show_value': value_len,
  47. }
  48. )
  49. if value_len > settings.thread_title_length_max:
  50. message = ngettext(
  51. "Thread title cannot be longer than %(limit_value)s character (it has %(show_value)s).",
  52. "Thread title cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
  53. settings.thread_title_length_max,
  54. )
  55. raise ValidationError(
  56. message % {
  57. 'limit_value': settings.thread_title_length_max,
  58. 'show_value': value_len,
  59. }
  60. )
  61. def validate_post_length(settings, value):
  62. value_len = len(value)
  63. if not value_len:
  64. raise ValidationError(_("You have to enter a message."))
  65. if value_len < settings.post_length_min:
  66. message = ngettext(
  67. "Posted message should be at least %(limit_value)s character long (it has %(show_value)s).",
  68. "Posted message should be at least %(limit_value)s characters long (it has %(show_value)s).",
  69. settings.post_length_min,
  70. )
  71. raise ValidationError(
  72. message % {
  73. 'limit_value': settings.post_length_min,
  74. 'show_value': value_len,
  75. }
  76. )
  77. if settings.post_length_max and value_len > settings.post_length_max:
  78. message = ngettext(
  79. "Posted message cannot be longer than %(limit_value)s character (it has %(show_value)s).",
  80. "Posted message cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
  81. settings.post_length_max,
  82. )
  83. raise ValidationError(
  84. message % {
  85. 'limit_value': settings.post_length_max,
  86. 'show_value': value_len,
  87. }
  88. )
  89. # Post validation framework
  90. validators_list = settings.MISAGO_POST_VALIDATORS
  91. POST_VALIDATORS = list(map(import_string, validators_list))
  92. def validate_post(context, data, validators=None):
  93. validators = validators or POST_VALIDATORS
  94. for validator in validators:
  95. data = validator(context, data) or data
  96. return data