-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms.py
160 lines (142 loc) · 5.51 KB
/
forms.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
154
155
156
157
158
159
160
# forms.py
import uuid
from typing import List
from markupsafe import Markup
from starlette_wtf import StarletteForm
from wtforms import BooleanField, FieldList, Form, FormField, SelectField, URLField
from wtforms.validators import URL, Optional
from wtforms.widgets import ListWidget, html_params
import maptomethod
class ListWidgetBootstrap(ListWidget):
def __init__(self, html_tag="div", prefix_label=True, col_class=""):
assert html_tag in ("div", "a")
self.html_tag = html_tag
self.prefix_label = prefix_label
self.col_class = col_class
def __call__(self, field, **kwargs):
kwargs.setdefault("id", field.id)
html = [f"<{self.html_tag} {html_params(**kwargs)}>"]
for subfield in field:
# if subfield we have to traverse once more down
if isinstance(subfield, Form):
for subsubfield in subfield:
if self.prefix_label:
html.append(
f"<div class={self.col_class}>{subsubfield.label} {subsubfield()}</div>"
)
else:
html.append(
f"<div class={self.col_class}>{subsubfield()} {subsubfield.label}</div>"
)
# print(dir(subfield))
else:
if self.prefix_label:
html.append(
f"<div class={self.col_class}>{subfield.label} {subfield()}</div>"
)
else:
html.append(
f"<div class={self.col_class}>{subfield()} {subfield.label}</div>"
)
html.append("</%s>" % self.html_tag)
return Markup("".join(html))
class AdvancedForm(Form):
data_subject_super_class_uris = FieldList(
URLField(
"URI", validators=[Optional(), URL()], render_kw={"class": "form-control"}
),
min_entries=3,
default=[maptomethod.OA.Annotation, maptomethod.CSVW.Column],
widget=ListWidgetBootstrap(col_class="col-sm-4"),
render_kw={"class": "row"},
description="URI of superclass to query for subjects in data.",
)
mapping_predicate_uri = URLField(
"URL Mapping Predicat",
# validators=[DataRequired(),URL()],
render_kw={"class": "form-control"},
default=maptomethod.ContentToBearingRelation,
description="URI of object property to use as predicate.",
)
method_object_super_class_uris = FieldList(
URLField(
"URI", validators=[Optional(), URL()], render_kw={"class": "form-control"}
),
min_entries=3,
default=[maptomethod.InformtionContentEntity, maptomethod.TemporalRegionClass],
widget=ListWidgetBootstrap(col_class="col-sm-4"),
render_kw={"class": "row"},
description="URI of superclass to query for objects in method.",
)
class StartForm(StarletteForm):
data_url = URLField(
"URL Meta Data",
# validators=[DataRequired(),URL()],
render_kw={
"placeholder": "https://github.com/Mat-O-Lab/CSVToCSVW/raw/main/examples/example-metadata.json",
"class": "form-control",
},
description="Paste URL to meta data json file create from CSVToCSVW",
)
method_url = URLField(
"URL Method Data",
render_kw={"class": "form-control"},
validators=[Optional(), URL()],
description="Paste URL to method graph create with MSEO",
)
method_sel = SelectField(
"Method Graph",
render_kw={"class": "form-control"},
# [(v, k) for k, v in app.methods_dict.items()]
choices=[],
description=(
"Alternativly select a method graph"
"from https://github.com/Mat-O-Lab/MSEO/tree/main/methods"
),
)
# disabled uing d-none bootrap class
use_template_rowwise = BooleanField(
"Duplicate Template for Table Data",
render_kw={
"class": "form-check form-check-input form-control-lg",
"role": "switch",
},
description="Check to duplicate the method template for each row in the table.",
default=False,
)
advanced = FormField(
AdvancedForm, render_kw={"class": "collapse"}, widget=ListWidgetBootstrap()
)
class SelectForm(Form):
select = SelectField(
"Placeholder",
default=(0, "None"),
choices=[],
validate_choice=False,
render_kw={"class": "form-control col-s-3"},
)
class MappingFormList(StarletteForm):
assignments = FieldList(
FormField(SelectForm, render_kw={"class": "form-control"}),
widget=ListWidgetBootstrap(col_class="col-sm-4"),
render_kw={"class": "row"},
)
def get_select_entries(names: List, choices: List) -> List[SelectForm]:
"""Converts custom metadata to a forms.SelectForm(), which can then be
used by SelectFormlist() to dynamically render select items.
Args:
names (List): List of names for selects to create
choices (List): List of choices for the selects
Returns:
List[SelectForm]: _description_
"""
all_select_items = []
for name in names:
_id = uuid.uuid1() # allows for multiple selects
select_form = SelectForm()
select_form.select.label = name
select_form.select.name = name
select_form.select.id = f"{name}-{_id}"
select_form.select.choices = choices
all_select_items.append(select_form)
return all_select_items