aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/forms/sent.py
blob: 65a2708ceea97d25e2e6c185dfa3f9422ce370aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me>
#
# SPDX-License-Identifier: BSD-3-Clause

"""Filters for the sent Webmentions page."""

from flask import current_app
from flask_wtf import FlaskForm
from wtforms import (
    DateField,
    HiddenField,
    IntegerField,
    SearchField,
    SelectField,
    ValidationError,
)
from wtforms.validators import AnyOf, NumberRange, Optional

from .validators import CanonicalDate

SENT_STATUS_CHOICES = {
    "": "All source documents",
    "failing": "Failing sends",
    "retrying": "Awaiting retry",
    "deleted": "Deleted source documents",
}


def default_sent_page_size() -> int:
    """Use 25 rows unless the configured maximum is lower."""
    return min(25, current_app.config["SOURCES_MAX_PAGE_SIZE"])


class SentFilterForm(FlaskForm):
    """GET filters for sent Webmentions grouped by source document."""

    status = SelectField("Status", choices=SENT_STATUS_CHOICES, default="")
    q = SearchField("Path or URL")
    date_from = DateField(
        "Last seen from (UTC)", validators=[Optional(), CanonicalDate()]
    )
    date_to = DateField("Last seen to (UTC)", validators=[Optional(), CanonicalDate()])
    sort = HiddenField(
        default="last_seen",
        validators=[
            AnyOf(
                (
                    "path",
                    "url",
                    "webmentions",
                    "revision",
                    "created",
                    "last_seen",
                    "status",
                )
            )
        ],
    )
    order = HiddenField(default="desc", validators=[AnyOf(("asc", "desc"))])
    per_page = IntegerField(
        "Rows per page",
        default=default_sent_page_size,
        validators=[NumberRange(min=1)],
    )

    def validate_per_page(self, field: IntegerField) -> None:
        """Enforce the deployment's maximum source-document page size."""
        if (
            field.data is not None
            and field.data > current_app.config["SOURCES_MAX_PAGE_SIZE"]
        ):
            raise ValidationError("Rows per page exceeds the configured maximum.")

    def validate_date_to(self, field: DateField) -> None:
        """Require an end date on or after the start date."""
        if (
            self.date_from.data is not None
            and field.data is not None
            and field.data < self.date_from.data
        ):
            raise ValidationError("End date must be on or after start date.")