-
Notifications
You must be signed in to change notification settings - Fork 17
/
auto.py
153 lines (138 loc) · 3.9 KB
/
auto.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask import (
Markup,
abort,
current_app,
escape,
flash,
make_response,
redirect,
render_template,
request,
url_for,
)
import wtforms
from coaster.utils import buid
from .. import THEME_FILES
from .. import b__ as __
from .. import request_is_xhr
from .fields import SubmitField
from .form import Form
class ConfirmDeleteForm(Form):
"""
Confirm a delete operation
"""
# The labels on these widgets are not used. See delete.html.
delete = SubmitField(__("Delete"))
cancel = SubmitField(__("Cancel"))
def render_form(
form,
title,
message='',
formid=None,
submit=__("Submit"),
cancel_url=None,
ajax=False,
with_chrome=True,
action=None,
autosave=False,
draft_revision=None,
template='',
):
multipart = False
ref_id = 'form-' + (formid or buid())
if not action:
action = request.url
for field in form:
if isinstance(field.widget, wtforms.widgets.FileInput):
multipart = True
if not with_chrome:
if not template:
template = THEME_FILES[current_app.config['theme']]['ajaxform.html.jinja2']
return render_template(
template,
form=form,
title=title,
message=message,
formid=formid,
ref_id=ref_id,
action=action,
submit=submit,
cancel_url=cancel_url,
ajax=ajax,
multipart=multipart,
with_chrome=with_chrome,
autosave=autosave,
draft_revision=draft_revision,
)
if not template and request_is_xhr() and ajax:
template = THEME_FILES[current_app.config['theme']]['ajaxform.html.jinja2']
elif not template:
template = THEME_FILES[current_app.config['theme']]['autoform.html.jinja2']
return make_response(
render_template(
template,
form=form,
title=title,
message=message,
formid=formid,
ref_id=ref_id,
action=action,
submit=submit,
cancel_url=cancel_url,
ajax=ajax,
multipart=multipart,
autosave=autosave,
draft_revision=draft_revision,
),
200,
)
def render_message(title, message, code=200):
template = THEME_FILES[current_app.config['theme']]['message.html.jinja2']
if request_is_xhr():
return make_response(Markup("<p>%s</p>" % escape(message)), code)
else:
return make_response(
render_template(template, title=title, message=message), code
)
def render_redirect(url, code=302):
template = THEME_FILES[current_app.config['theme']]['redirect.html.jinja2']
if request_is_xhr():
return make_response(render_template(template, url=url))
else:
return redirect(url, code=code)
def render_delete_sqla(
obj,
db,
title,
message,
success='',
next=None, # NOQA: A002
cancel_url=None,
delete_text=None,
cancel_text=None,
):
if not obj:
abort(404)
form = ConfirmDeleteForm()
if request.method in ('POST', 'DELETE') and form.validate():
if 'delete' in request.form or request.method == 'DELETE':
db.session.delete(obj)
db.session.commit()
if success:
flash(success, 'success')
return render_redirect(next or url_for('index'), code=303)
else:
return render_redirect(cancel_url or next or url_for('index'), code=303)
template = THEME_FILES[current_app.config['theme']]['delete.html.jinja2']
return make_response(
render_template(
template,
form=form,
title=title,
message=message,
delete_text=delete_text,
cancel_text=cancel_text,
)
)