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

hasgeek / baseframe / 16218997531

11 Jul 2025 11:32AM UTC coverage: 67.889% (+1.0%) from 66.866%
16218997531

push

github

web-flow
Fix linter issues; fix ValidCoordinates; add OptionalCoordinates (#511)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

120 of 149 new or added lines in 14 files covered. (80.54%)

3 existing lines in 2 files now uncovered.

1592 of 2345 relevant lines covered (67.89%)

2.72 hits per line

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

35.59
/src/baseframe/forms/auto.py
1
"""Automatic form rendering."""
2

3
from __future__ import annotations
4✔
4

5
from typing import TYPE_CHECKING, Any, Optional, Union
4✔
6

7
import wtforms
4✔
8
from flask import (
4✔
9
    abort,
10
    current_app,
11
    flash,
12
    make_response,
13
    redirect,
14
    render_template,
15
    request,
16
    url_for,
17
)
18
from markupsafe import Markup
4✔
19
from werkzeug.wrappers import Response
4✔
20

21
from coaster.utils import buid
4✔
22

23
from ..blueprint import THEME_FILES
4✔
24
from ..extensions import __
4✔
25
from ..utils import request_is_xhr
4✔
26
from .fields import SubmitField
4✔
27
from .form import Form
4✔
28

29
if TYPE_CHECKING:
4✔
30
    from flask_sqlalchemy import SQLAlchemy
×
31

32
_submit_str = __("Submit")
4✔
33

34

35
class ConfirmDeleteForm(Form):
4✔
36
    """Confirm a delete operation."""
37

38
    # The labels on these widgets are not used. See delete.html.
39
    delete = SubmitField(__("Delete"))
4✔
40
    cancel = SubmitField(__("Cancel"))
4✔
41

42

43
def render_form(
4✔
44
    form: Form,
45
    title: str,
46
    message: Optional[Union[str, Markup]] = None,
47
    formid: Optional[str] = None,
48
    submit: str = _submit_str,
49
    cancel_url: Optional[str] = None,
50
    ajax: bool = False,
51
    with_chrome: bool = True,
52
    action: Optional[str] = None,
53
    autosave: bool = False,
54
    draft_revision: Optional[Any] = None,
55
    template: str = '',
56
) -> Response:
57
    """Render a form."""
58
    multipart = False
×
59
    ref_id = 'form-' + (formid or buid())
×
60
    if not action:
×
61
        action = request.url
×
62
    for field in form:
×
63
        if isinstance(field.widget, wtforms.widgets.FileInput):
×
64
            multipart = True
×
65
    if not with_chrome:
×
66
        if not template:
×
67
            template = THEME_FILES[current_app.config['theme']]['ajaxform.html.jinja2']
×
68
        return make_response(
×
69
            render_template(
70
                template,
71
                form=form,
72
                title=title,
73
                message=message,
74
                formid=formid,
75
                ref_id=ref_id,
76
                action=action,
77
                submit=submit,
78
                cancel_url=cancel_url,
79
                ajax=ajax,
80
                multipart=multipart,
81
                with_chrome=with_chrome,
82
                autosave=autosave,
83
                draft_revision=draft_revision,
84
            )
85
        )
86
    if not template and request_is_xhr() and ajax:
×
87
        template = THEME_FILES[current_app.config['theme']]['ajaxform.html.jinja2']
×
88
    elif not template:
×
89
        template = THEME_FILES[current_app.config['theme']]['autoform.html.jinja2']
×
90
    return make_response(
×
91
        render_template(
92
            template,
93
            form=form,
94
            title=title,
95
            message=message,
96
            formid=formid,
97
            ref_id=ref_id,
98
            action=action,
99
            submit=submit,
100
            cancel_url=cancel_url,
101
            ajax=ajax,
102
            multipart=multipart,
103
            autosave=autosave,
104
            draft_revision=draft_revision,
105
        )
106
    )
107

108

109
def render_message(title: str, message: str, code: int = 200) -> Response:
4✔
110
    """Render a message."""
111
    template = THEME_FILES[current_app.config['theme']]['message.html.jinja2']
×
112
    if request_is_xhr():
×
NEW
113
        return make_response(Markup('<p>{}</p>').format(message), code)
×
114
    return make_response(render_template(template, title=title, message=message), code)
×
115

116

117
def render_redirect(url: str, code: int = 302) -> Response:
4✔
118
    """Render a redirect, using a JS redirect for XHR requests, HTTP otherwise."""
119
    template = THEME_FILES[current_app.config['theme']]['redirect.html.jinja2']
×
120
    if request_is_xhr():
×
121
        return make_response(render_template(template, url=url))
×
122
    return redirect(url, code=code)
×
123

124

125
def render_delete_sqla(
4✔
126
    obj: Any,
127
    db: SQLAlchemy,
128
    title: str,
129
    message: str,
130
    success: str = '',
131
    next: Optional[str] = None,  # noqa: A002  # pylint: disable=W0622
132
    cancel_url: Optional[str] = None,
133
    delete_text: Optional[str] = None,
134
    cancel_text: Optional[str] = None,
135
) -> Response:
136
    """Render a delete page for SQLAlchemy objects."""
137
    if not obj:
×
138
        abort(404)
×
139
    form = ConfirmDeleteForm()
×
140
    if request.method in ('POST', 'DELETE') and form.validate():
×
141
        if 'delete' in request.form or request.method == 'DELETE':
×
142
            db.session.delete(obj)
×
143
            db.session.commit()
×
144
            if success:
×
145
                flash(success, 'success')
×
146
            return render_redirect(next or url_for('index'), code=303)
×
147
        return render_redirect(cancel_url or next or url_for('index'), code=303)
×
148
    template = THEME_FILES[current_app.config['theme']]['delete.html.jinja2']
×
149
    return make_response(
×
150
        render_template(
151
            template,
152
            form=form,
153
            title=title,
154
            message=message,
155
            delete_text=delete_text,
156
            cancel_text=cancel_text,
157
        )
158
    )
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