validators.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(tree_id=threads_tree_id, id=category_id)
  15. except Category.DoesNotExist:
  16. category = None
  17. # Skip ACL validation for root category?
  18. if allow_root and category and not category.level:
  19. return category
  20. if not category or not can_see_category(user_acl, category):
  21. raise ValidationError(_("Requested category could not be found."))
  22. if not can_browse_category(user_acl, category):
  23. raise ValidationError(_("You don't have permission to access this category."))
  24. return category
  25. def validate_thread_title(settings, title):
  26. validate_thread_title_length(settings, title)
  27. error_not_sluggable = _("Thread title should contain alpha-numeric characters.")
  28. error_slug_too_long = _("Thread title is too long.")
  29. validate_sluggable(error_not_sluggable, error_slug_too_long)(title)
  30. def validate_thread_title_length(settings, value):
  31. value_len = len(value)
  32. if not value_len:
  33. raise ValidationError(_("You have to enter an thread title."))
  34. if value_len < settings.thread_title_length_min:
  35. message = ngettext(
  36. "Thread title should be at least %(limit_value)s character long (it has %(show_value)s).",
  37. "Thread title should be at least %(limit_value)s characters long (it has %(show_value)s).",
  38. settings.thread_title_length_min,
  39. )
  40. raise ValidationError(
  41. message
  42. % {"limit_value": settings.thread_title_length_min, "show_value": value_len}
  43. )
  44. if value_len > settings.thread_title_length_max:
  45. message = ngettext(
  46. "Thread title cannot be longer than %(limit_value)s character (it has %(show_value)s).",
  47. "Thread title cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
  48. settings.thread_title_length_max,
  49. )
  50. raise ValidationError(
  51. message
  52. % {"limit_value": settings.thread_title_length_max, "show_value": value_len}
  53. )
  54. def validate_post_length(settings, value):
  55. value_len = len(value)
  56. if not value_len:
  57. raise ValidationError(_("You have to enter a message."))
  58. if value_len < settings.post_length_min:
  59. message = ngettext(
  60. "Posted message should be at least %(limit_value)s character long (it has %(show_value)s).",
  61. "Posted message should be at least %(limit_value)s characters long (it has %(show_value)s).",
  62. settings.post_length_min,
  63. )
  64. raise ValidationError(
  65. message % {"limit_value": settings.post_length_min, "show_value": value_len}
  66. )
  67. if settings.post_length_max and value_len > settings.post_length_max:
  68. message = ngettext(
  69. "Posted message cannot be longer than %(limit_value)s character (it has %(show_value)s).",
  70. "Posted message cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
  71. settings.post_length_max,
  72. )
  73. raise ValidationError(
  74. message % {"limit_value": settings.post_length_max, "show_value": value_len}
  75. )
  76. # Post validation framework
  77. validators_list = settings.MISAGO_POST_VALIDATORS
  78. POST_VALIDATORS = list(map(import_string, validators_list))
  79. def validate_post(context, data, validators=None):
  80. validators = validators or POST_VALIDATORS
  81. for validator in validators:
  82. data = validator(context, data) or data
  83. return data