• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rafalp / Misago / 9487666026

12 Jun 2024 06:16PM UTC coverage: 97.699% (-1.0%) from 98.716%
9487666026

push

github

web-flow
Replace forum options with account settings (#1742)

1947 of 1979 new or added lines in 68 files covered. (98.38%)

661 existing lines in 143 files now uncovered.

52601 of 53840 relevant lines covered (97.7%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.98
/misago/threads/validators.py
1
from django.core.exceptions import ValidationError
1✔
2
from django.utils.module_loading import import_string
1✔
3
from django.utils.translation import npgettext, pgettext
1✔
4

5
from .. import hooks
1✔
6
from ..categories import THREADS_ROOT_NAME
1✔
7
from ..categories.models import Category
1✔
8
from ..categories.permissions import can_browse_category, can_see_category
1✔
9
from ..conf import settings
1✔
10
from ..core.validators import validate_sluggable
1✔
11
from .threadtypes import trees_map
1✔
12

13

14
def validate_category(user_acl, category_id, allow_root=False):
1✔
15
    try:
1✔
16
        threads_tree_id = trees_map.get_tree_id_for_root(THREADS_ROOT_NAME)
1✔
17
        category = Category.objects.get(tree_id=threads_tree_id, id=category_id)
1✔
18
    except Category.DoesNotExist:
×
19
        category = None
×
20

21
    # Skip ACL validation for root category?
22
    if allow_root and category and not category.level:
1✔
23
        return category
×
24

25
    if not category or not can_see_category(user_acl, category):
1✔
26
        raise ValidationError(
1✔
27
            pgettext(
28
                "thread category validator", "Requested category could not be found."
29
            )
30
        )
31

32
    if not can_browse_category(user_acl, category):
1✔
UNCOV
33
        raise ValidationError(
×
34
            pgettext(
35
                "thread category validator",
36
                "You don't have permission to access this category.",
37
            )
38
        )
39
    return category
1✔
40

41

42
def validate_thread_title(settings, title):
1✔
43
    validate_thread_title_length(settings, title)
1✔
44

45
    error_not_sluggable = pgettext(
1✔
46
        "thread title validator",
47
        "Thread title should contain alpha-numeric characters.",
48
    )
49
    error_slug_too_long = pgettext(
1✔
50
        "thread title validator", "Thread title is too long."
51
    )
52
    validate_sluggable(error_not_sluggable, error_slug_too_long)(title)
1✔
53

54

55
def validate_thread_title_length(settings, value):
1✔
56
    value_len = len(value)
1✔
57

58
    if not value_len:
1✔
59
        raise ValidationError(
1✔
60
            pgettext(
61
                "thread title length validator", "You have to enter an thread title."
62
            )
63
        )
64

65
    if value_len < settings.thread_title_length_min:
1✔
66
        # pylint: disable=line-too-long
67
        message = npgettext(
1✔
68
            "thread title length validator",
69
            "Thread title should be at least %(limit_value)s character long (it has %(show_value)s).",
70
            "Thread title should be at least %(limit_value)s characters long (it has %(show_value)s).",
71
            settings.thread_title_length_min,
72
        )
73
        raise ValidationError(
1✔
74
            message
75
            % {"limit_value": settings.thread_title_length_min, "show_value": value_len}
76
        )
77

78
    if value_len > settings.thread_title_length_max:
1✔
79
        # pylint: disable=line-too-long
80
        message = npgettext(
1✔
81
            "thread title length validator",
82
            "Thread title cannot be longer than %(limit_value)s character (it has %(show_value)s).",
83
            "Thread title cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
84
            settings.thread_title_length_max,
85
        )
86
        raise ValidationError(
1✔
87
            message
88
            % {"limit_value": settings.thread_title_length_max, "show_value": value_len}
89
        )
90

91

92
def validate_post_length(settings, value):
1✔
93
    value_len = len(value)
1✔
94

95
    if not value_len:
1✔
96
        raise ValidationError(
1✔
97
            pgettext("post length validator", "You have to enter a message.")
98
        )
99

100
    if value_len < settings.post_length_min:
1✔
101
        # pylint: disable=line-too-long
102
        message = npgettext(
1✔
103
            "post length validator",
104
            "Posted message should be at least %(limit_value)s character long (it has %(show_value)s).",
105
            "Posted message should be at least %(limit_value)s characters long (it has %(show_value)s).",
106
            settings.post_length_min,
107
        )
108
        raise ValidationError(
1✔
109
            message % {"limit_value": settings.post_length_min, "show_value": value_len}
110
        )
111

112
    if settings.post_length_max and value_len > settings.post_length_max:
1✔
113
        # pylint: disable=line-too-long
114
        message = npgettext(
1✔
115
            "post length validator",
116
            "Posted message cannot be longer than %(limit_value)s character (it has %(show_value)s).",
117
            "Posted message cannot be longer than %(limit_value)s characters (it has %(show_value)s).",
118
            settings.post_length_max,
119
        )
120
        raise ValidationError(
1✔
121
            message % {"limit_value": settings.post_length_max, "show_value": value_len}
122
        )
123

124

125
# Post validation framework
126
validators_list = settings.MISAGO_POST_VALIDATORS
1✔
127
POST_VALIDATORS = list(map(import_string, validators_list))
1✔
128

129

130
def validate_post(context, data, validators=None):
1✔
131
    validators = validators or POST_VALIDATORS
1✔
132

133
    for validator in validators:
1✔
134
        data = validator(context, data) or data
1✔
135
    for validator in hooks.post_validators:
1✔
136
        data = validator(context, data) or data
1✔
137

138
    return data
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc