Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1import uuid
3from django_bleach.forms import BleachField
5from crispy_forms.bootstrap import FormActions, Tab, TabHolder
6from crispy_forms.helper import FormHelper
7from crispy_forms.layout import HTML, Div, Field, Layout, Submit
8from django import forms
9from django.forms import ModelForm
10from django.utils.translation import gettext as _
12from discuss_data.core.helpers import get_help_texts
13from discuss_data.dddatasets.models import (
14 Category,
15 DataFile,
16 DataList,
17 DataSet,
18 DataSetCreator,
19 DataSetPublication,
20 DataSubmissionAgreement,
21 License,
22)
23from discuss_data.dddatasets.utils import get_man_page
25UPDATED_MSG = """
26 {% if updated %}<div id='updated-msg' class='alert alert-success' ic-trigger-on='scrolled-into-view' ic-action='delay:2500;fadeOut;remove' ic-target='#updated-msg' role='alert'>
27 <span class='spinner-sm'></span><strong>{{ target|title }} saved</strong></div>{% endif %}
28 {% if err %}<div id='updated-msg' class='alert alert-danger' role='alert'>
29 <span class='spinner-sm'></span><strong>Error! {{ target|title }} could not be saved. Error encountered in fields {% for field in err %} {{ field }}, {% endfor %}</strong></div> {% endif %}"""
31ADD_EDIT_HEADING = (
32 "<h2>{% if new %}Edit{% else %}Add{% endif %} {{ target|title }}</h2><hr>"
33)
35REQ_FIELD = "<p class='help-block'>[*] {}</p>".format(_("Required fields"))
37EDIT_DATASET_BTN = "<button class='btn btn-primary' type='submit'>{% if btn_text %}{{ btn_text }}{% else %}Save{% endif %} <span class='spinner-sm'></span></button>"
40class DataSetPublicationForm(ModelForm):
41 class Meta:
42 model = DataSetPublication
43 fields = [
44 "pub_type",
45 ]
47 helper = FormHelper()
48 helper.form_tag = False
49 helper.layout = Layout(
50 Div(Div("pub_type", css_class="col-md-12",), css_class="row",),
51 )
54class DataSetIndividualLicenseForm(ModelForm):
55 class Meta:
56 model = License
57 fields = [
58 "license_text",
59 ]
60 help_texts = get_help_texts("edit-license-access")
61 manpage = get_man_page("edit-license-access")
63 helper = FormHelper()
64 helper.form_tag = False
65 helper.layout = Layout(
66 HTML(
67 "{% include '_help_text.html' with field=Meta.help_texts key='D-Customized-Data-Usage-Licence-Agreement' %}"
68 ),
69 HTML(
70 "{% include 'dddatasets/_manual_page.html' with manpage=Meta.manpage key='D-Customized-Data-Usage-Licence-Agreement' %}"
71 ),
72 Div(Div("license_text", css_class="col-md-12",), css_class="row",),
73 )
76class DataSetValidateForm(ModelForm):
77 class Meta:
78 model = DataSet
79 fields = "__all__"
82class DataSetCopyrightForm(ModelForm):
83 class Meta:
84 model = DataSet
85 fields = ("copyright_declaration",)
87 helper = FormHelper()
88 helper.form_tag = False
90 helper.layout = Layout(
91 Div(Div("copyright_declaration", css_class="col-md-12",), css_class="row",),
92 )
95class DataSetPrivacyRightsForm(ModelForm):
96 class Meta:
97 model = DataSet
98 fields = ("privacy_rights_declaration",)
100 helper = FormHelper()
101 helper.form_tag = False
103 helper.layout = Layout(
104 Div(
105 Div("privacy_rights_declaration", css_class="col-md-12",), css_class="row",
106 ),
107 )
110class DataSubmissionAgreementAcceptForm(ModelForm):
111 class Meta:
112 model = DataSubmissionAgreement
113 fields = ("dsa_accepted",)
115 helper = FormHelper()
116 helper.form_tag = False
118 helper.layout = Layout(
119 Div(
120 HTML(
121 "<div class='col-md-12 smallskip'>{} {}.</div>".format(
122 _("Finally, you need also to accept the"),
123 _("Data Submission Agreement"),
124 )
125 ),
126 Div("dsa_accepted", css_class="col-md-12",),
127 css_class="row",
128 ),
129 HTML(EDIT_DATASET_BTN),
130 # FormActions(Submit("save", "Save {{ target }}"),),
131 # Div(HTML(UPDATED_MSG),),
132 )
135class DataSetCreatorFormSetHelper(FormHelper):
136 def __init__(self, *args, **kwargs):
137 super().__init__(*args, **kwargs)
138 self.form_method = "post"
139 self.form_tag = False
140 self.layout = Layout(
141 Div(
142 Div("name", css_class="col-md-4",),
143 Div("first_name", css_class="col-md-4",),
144 Div("creator_type", css_class="col-md-2",),
145 Field("DELETE", template="core/_bool_button.html"),
146 css_class="row crispyformset",
147 ),
148 )
149 self.render_required_fields = True
152class DataSubmissionAgreementForm(ModelForm):
153 class Meta:
154 model = DataSubmissionAgreement
155 fields = (
156 "birthdate",
157 "postal_address",
158 "project_type",
159 "project_title",
160 # "dsa_accepted",
161 # "dsa_accepted_date",
162 )
163 help_texts = get_help_texts("data-submission-agreement-form")
165 helper = FormHelper()
166 helper.form_tag = False
167 helper.layout = Layout(
168 Div(
169 Div("postal_address", css_class="col-md-12",),
170 Div("birthdate", css_class="col-md-12",),
171 css_class="row",
172 ),
173 Div(
174 Div("project_type", css_class="col-md-6",),
175 Div("project_title", css_class="col-md-6",),
176 css_class="row",
177 ),
178 # Div(
179 # Div("dsa_accepted_date", readonly=True, css_class="col-md-6",),
180 # Div("dsa_accepted", css_class="col-md-6",),
181 # css_class="row",
182 # ),
183 HTML(REQ_FIELD),
184 HTML(EDIT_DATASET_BTN),
185 )
188class DataSetForm(ModelForm):
189 class Meta:
190 model = DataSet
191 fields = (
192 "date_of_data_creation_from",
193 "date_of_data_creation_to",
194 "date_of_data_creation_text",
195 "time_period_from",
196 "time_period_to",
197 "time_period_text",
198 "sources_of_data",
199 "datatypes",
200 "datatype_text",
201 "institutional_affiliation",
202 "funding",
203 "related_dataset_text",
204 # "related_dataset", TODO: commenting out as the field lists also unpublished datasets. We need a custom widget/control for this.
205 "related_projects",
206 )
207 help_texts = get_help_texts("edit-metadata")
208 widgets = {
209 "datatypes": forms.CheckboxSelectMultiple,
210 }
212 helper = FormHelper()
213 helper.form_tag = False
215 helper.layout = Layout(
216 HTML(ADD_EDIT_HEADING),
217 Div(
218 Div(
219 HTML(
220 "{% load crispy_forms_tags i18n %}<h4>{% trans 'Data Creators' %}{% include '_help_text.html' with field=help_texts key='creators' %}</h4><p></p>{% crispy formset helper %}"
221 ),
222 css_class="col-md-12",
223 ),
224 HTML(
225 "{% load i18n %}<div class='col-md-12 smallskip'><button type='button' class='btn btn-outline-primary' id='add-creator'>{% trans 'Add Creator' %}</button></div>",
226 ),
227 HTML("<hr>"),
228 css_class="row",
229 ),
230 Div(
231 Div("date_of_data_creation_from", css_class="col-md-6",),
232 Div("date_of_data_creation_to", css_class="col-md-6",),
233 Div("date_of_data_creation_text", css_class="col-md-12",),
234 css_class="row",
235 ),
236 Div(
237 Div("time_period_from", css_class="col-md-6",),
238 Div("time_period_to", css_class="col-md-6",),
239 Div("time_period_text", css_class="col-md-12",),
240 css_class="row",
241 ),
242 Div(
243 Div("sources_of_data", css_class="col-md-6",),
244 Div(Div("datatypes"), Div("datatype_text"), css_class="col-md-6",),
245 css_class="row",
246 ),
247 Div(
248 Div("institutional_affiliation", css_class="col-md-3",),
249 Div("funding", css_class="col-md-3", wrapper_class="controls"),
250 Div("related_projects", css_class="col-md-3",),
251 Div("related_dataset_text", css_class="col-md-3",),
252 css_class="row",
253 ),
254 # Div(
255 # Div("related_dataset_text", css_class="col-md-6",),
256 # Div("related_dataset", css_class="col-md-6",),
257 # css_class="row",
258 # ),
259 HTML(REQ_FIELD),
260 HTML(EDIT_DATASET_BTN),
261 # FormActions(
262 # Submit("save", "Save {{ target }}"),
263 # # Button('cancel', 'Cancel', data_toggle="collapse", data_target="#collapse-{{ object_type }}-{{ object.id }}"),
264 # ),
265 Div(HTML(UPDATED_MSG),),
266 )
267 # def __init__(self, *args, **kwargs):
268 # super().__init__(*args, **kwargs)
269 # self.fields['datatypes'].widget = forms.CheckboxSelectMultiple
270 # #self.fields['comment'].widget.attrs.update(size='40')
273class DataSetCreatorForm(ModelForm):
274 class Meta:
275 model = DataSetCreator
276 fields = ("name", "creator_type")
279class DataSetEditForm(ModelForm):
280 main_category = forms.CharField(required=False)
281 categories = forms.MultipleChoiceField(required=False, choices=[])
283 class Meta:
284 model = DataSet
285 fields = (
286 "title",
287 "subtitle",
288 "description",
289 "main_category",
290 "categories",
291 "shorttitle",
292 )
293 help_texts = get_help_texts("edit-dataset-description")
295 helper = FormHelper()
296 helper.form_tag = False
297 helper.help_text_inline = False
298 helper.layout = Layout(
299 Div(
300 Div("title", css_class="col-md-12",),
301 Div("subtitle", css_class="col-md-12",),
302 Div("shorttitle", css_class="col-md-12",),
303 Div("description", css_class="col-md-8",),
304 HTML(
305 "<span ic-get-from='{% url 'core:load' %}' ic-trigger-on='load'></span>"
306 ),
307 css_class="row",
308 ),
309 HTML(REQ_FIELD),
310 HTML(EDIT_DATASET_BTN),
311 Div(HTML(UPDATED_MSG),),
312 )
314 # init form at every request to update categories queried from db
315 def __init__(self, *args, **kwargs):
316 super(DataSetEditForm, self).__init__(*args, **kwargs)
317 self.fields["categories"] = forms.MultipleChoiceField(
318 required=False,
319 choices=[
320 (category.id, category.name)
321 for category in Category.objects.all().order_by("name")
322 ],
323 )
324 # forms.ChoiceField(choices=[(category.id, category.name) for color in Color.objects.all()])
327class DataSetImageForm(ModelForm):
328 class Meta:
329 model = DataSet
330 fields = [
331 "image",
332 ]
335class DataSetImageUploadForm(ModelForm):
336 # TODO: updated image needs to be loaded at save
337 remove_photo = forms.BooleanField(required=False)
339 class Meta:
340 model = DataSet
341 fields = ("image", "remove_photo")
343 helper = FormHelper()
344 helper.form_tag = False
345 helper.layout = Layout(
346 # HTML(ADD_EDIT_HEADING),
347 Div(
348 Div("image", css_class="col-md-12",),
349 Div("remove_photo", css_class="col-md-12",),
350 css_class="row",
351 ),
352 HTML(REQ_FIELD),
353 HTML(
354 '<button type="submit">Save {{ target }}<span class="spinner-sm"></span></button>'
355 ),
356 Div(HTML(UPDATED_MSG),),
357 )
360class DataFileUploadForm(ModelForm):
361 class Meta:
362 model = DataFile
363 fields = ("data_file_type", "file")
365 helper = FormHelper()
366 helper.form_tag = False
367 helper.use_custom_control = True
368 helper.layout = Layout(
369 Div("data_file_type", css_class="col-md-4",),
370 Div(HTML(REQ_FIELD), css_class="col-md-12",),
371 Div(HTML(UPDATED_MSG),),
372 )
375class DataSetPublishAccept(forms.Form):
376 choices = (
377 ("accept", "accept"),
378 ("decline", "decline"),
379 )
380 decision = forms.ChoiceField(choices=choices)
381 message = BleachField(required=False)
384class DataListForm(ModelForm):
385 class Meta:
386 model = DataList
387 fields = [
388 "name",
389 "public",
390 "description",
391 ]
393 helper = FormHelper()
394 helper.form_tag = False
395 helper.layout = Layout(
396 Div(
397 Div("name", css_class="col-md-11",),
398 Div("public", css_class="col-md-1",),
399 Div("description", css_class="col-md-12",),
400 css_class="row",
401 ),
402 HTML(REQ_FIELD),
403 FormActions(Submit("save", "Save {{ target }}"),),
404 Div(HTML(UPDATED_MSG),),
405 )
408class DataSetToListForm(forms.Form):
409 ds_uuid = forms.UUIDField()
410 dl_uuid = forms.UUIDField()
411 form_type = forms.CharField()