summaryrefslogtreecommitdiff
path: root/ishtar_common/widgets.py
blob: a7a59d8180a8ee08b30e441b9adffe411c459132 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2010-2025 Étienne Loks  <etienne.loks at iggdrasil dot net>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

import logging

from django import forms
from django.conf import settings
from django.contrib.gis import forms as gis_forms
from django.core.exceptions import FieldDoesNotExist, ValidationError
from django.core.files import File
from django.forms import ClearableFileInput
from django.forms.utils import flatatt
from django.forms.widgets import (
    CheckboxSelectMultiple as CheckboxSelectMultipleBase,
    NumberInput,
)
from django.template import loader
from django.template.defaultfilters import slugify
from django.urls import reverse, NoReverseMatch
from django.utils.encoding import smart_text
from django.utils.functional import lazy
from django.utils.html import escape
from django.utils.safestring import mark_safe
from json import JSONEncoder
from django.utils.translation import gettext_lazy as _

from ishtar_common import models
from ishtar_common.utils import get_columns_from_class, reverse_coordinates

logger = logging.getLogger(__name__)

reverse_lazy = lazy(reverse, str)


class SelectReadonly(forms.Select):
    template_name = "blocks/readonly_input.html"
    option_template_name = "blocks/readonly_input_option.html"

    def __init__(self, attrs=None, choices=(), model=None, available=None):
        super(SelectReadonly, self).__init__(attrs, choices)
        self.available = available
        self.model = model

    def get_choices(self, value):
        q = self.model.objects
        if self.available:
            q = q.filter(available=True)
        if value:
            q = q.filter(pk=value)
        for i in q.all():
            if hasattr(self.model, "verbose_name"):
                label = i.verbose_name
            else:
                label = str(i)
            yield (i.pk, label)

    def render(self, name, value, attrs=None, choices=(), renderer=None):
        if value:
            self.choices = list(self.get_choices(value))
            value = self.choices[0][0]
        return super(SelectReadonly, self).render(name, value, attrs, renderer=None)


class SelectReadonlyField(forms.ChoiceField):
    def __init__(
        self,
        choices=(),
        required=True,
        widget=None,
        label=None,
        initial=None,
        help_text="",
        *args,
        **kwargs
    ):
        self.available = False
        self.model = None
        if "model" in kwargs:
            self.model = kwargs.pop("model")
        if "available" in kwargs:
            self.available = kwargs.pop("available")
        widget = SelectReadonly(model=self.model, available=self.available)
        super(SelectReadonlyField, self).__init__(
            choices=choices,
            required=required,
            widget=widget,
            label=label,
            initial=initial,
            help_text=help_text,
            *args,
            **kwargs
        )

    def get_q(self):
        q = self.model.objects
        if self.available:
            q = q.filter(available=True)
        return q

    def valid_value(self, value):
        if not self.model:
            return super(SelectReadonlyField, self).valid_value(value)
        return bool(self.get_q().filter(pk=value).count())


class Select2Media:
    @property
    def media(self):
        # TODO: to remove? put in on base.html
        css = {"all": ("select2/css/select2.css",)}
        js = ["select2/js/select2.full.min.js"]
        for lang_code, lang in settings.LANGUAGES:
            js.append("select2/js/i18n/{}.js".format(lang_code))
        media = forms.Media(css=css, js=js)
        return media


class Select2DynamicBase(Select2Media):
    """
    Select input using select, allowing dynamic creation.
    """
    MULTIPLE = False

    def render(self, name, value, attrs=None, choices=(), renderer=None):
        choices = choices or getattr(self, "choices", [])
        if value:
            values = [value]
            if self.MULTIPLE:
                value = value[0]
                values = value
            for va in values:
                if va not in [key for key, v in choices]:
                    choices.insert(1, (va, va))
                    self.choices = choices
        klass = attrs and attrs.get("class") or ""
        klass += " " if klass else "" + "js-select2"
        if not attrs:
            attrs = {}
        attrs["class"] = klass
        if "style" not in attrs:
            if attrs.get("full-width", None):
                attrs["style"] = "width: calc(100% - 60px)"
            else:
                attrs["style"] = "width: 370px"
        options = [
            "tags: true",
        ]
        '''
        msg = str(
            _("Are you sure you want to add this term? (the addition is "
              "effective after registration of the element)")
        )
        options.append("""createTag: function (params) {{
            return confirm("{}");
        }}""".format(msg))
        '''
        if attrs.get("full-width", None):
            options.append("containerCssClass: 'full-width'")

        if self.MULTIPLE:
            options.append("multiple: 'true'")
        html = super(Select2DynamicBase, self).render(name, value, attrs, renderer=None)
        html += """<script type="text/javascript">
        $(document).ready(function() {{
            $("#id_{}").select2({{ {} }});
        }});</script>
        """.format(
            name, ", ".join(options)
        )
        return mark_safe(html)


class Select2Dynamic(Select2DynamicBase, forms.Select):
    MULTIPLE = False


class Select2DynamicMultiple(Select2DynamicBase, forms.SelectMultiple):
    MULTIPLE = True


class Select2DynamicField(forms.ChoiceField):
    widget = Select2Dynamic

    def validate(self, value):
        """
        Key can be added dynamically. Only check that the character " is not
        used.
        """
        if value and '"' in value:
            raise ValidationError(_('The character " is not accepted.'))

    def to_python(self, value):
        """
        Strip value
        """
        return super(Select2DynamicField, self).to_python(value).strip()


class Select2DynamicMultipleField(forms.MultipleChoiceField):
    widget = Select2DynamicMultiple

    def validate(self, value):
        """
        Key can be added dynamically. Only check that the character " is not
        used.
        """
        if value and '"' in value:
            raise ValidationError(_('The character " is not accepted.'))

    def to_python(self, value):
        """
        Strip value
        """
        return [v.strip() for v in super().to_python(value)]


class Select2Base(Select2Media):
    def __init__(
        self, attrs=None, choices=(), remote=None, model=None, new=None, available=None
    ):
        self.remote = remote
        self.available = available
        self.model = model
        self.new = new
        super(Select2Base, self).__init__(attrs, choices)

    def get_q(self):
        q = self.model.objects
        if self.available:
            q = q.filter(available=True)
        return q

    def get_choices(self):
        for i in self.get_q().all():
            yield (i.pk, str(i))

    def render(self, name, value, attrs=None, choices=(), renderer=None):
        self.remote = str(self.remote)
        if self.remote in ("None", "false"):
            # test on lazy object is buggy... so we have this ugly test
            self.remote = None
        if not choices:
            if not self.remote and self.model:
                choices = self.get_choices()
            if hasattr(self, "choices") and self.choices:
                choices = self.choices
        new_attrs = self.attrs.copy()
        new_attrs.update(attrs)
        attrs = new_attrs
        klass = attrs and attrs.get("class") or ""
        klass += " " if klass else "" + "js-select2"
        if not attrs:
            attrs = {}
        attrs["class"] = klass
        if "style" not in attrs:
            if attrs.get("full-width", None):
                attrs["style"] = "width: calc(100% - 60px)"
            else:
                attrs["style"] = "width: 370px"

        if value:
            if type(value) not in (list, tuple):
                value = value.split(",")

        options = ""
        if self.remote:
            options = (
                """{
            ajax: {
                url: '%s',
                delay: 250,
                dataType: 'json',
                minimumInputLength: 2,
                processResults: function (data) {
                 if(!data) return {results: []};
                 var result = $.map(data, function (item) {
                        return {
                            text: item['value'],
                            id: item['id']
                        }
                      });
                 return {
                    results: result
                  }
                }
              }
            }"""
                % self.remote
            )
            if value:
                choices = []
                for v in value:
                    try:
                        choices.append((v, self.model.objects.get(pk=v)))
                    except (self.model.DoesNotExist, ValueError):
                        # an old reference? it should not happen
                        pass
        if attrs.get("full-width", None):
            if options:
                options = options[:-1] + ", "
            else:
                options = "{"
            options += "     containerCssClass: 'full-width'}"
        self.choices = choices

        new, html = "", ""
        if self.new:
            html = "<div class='input-group'>"
            url_new = "new-" + self.model.SLUG
            url_new = reverse(url_new, args=["id_" + name])
            # WARNING: the modal for the form must be in the main template
            # "extra_form_modals" list is used for that in form or view
            new = (
                """<span class="input-group-append">"""
                """<a href="#" class="add-button input-group-text" """
                """onclick="dt_qa_open('{}', 'modal-dynamic-form-{}');">"""
                """+</a></span></div>""".format(url_new, self.model.SLUG)
            )

        html += super(Select2Base, self).render(name, value, attrs, renderer=None)
        html += new
        html += """<script type="text/javascript">
        $(document).ready(function() {{
            $("#id_{}").select2({});
        }});</script>
        """.format(
            name, options
        )
        return mark_safe(html)


class Select2Simple(Select2Base, forms.Select):
    pass


class Select2Multiple(Select2Base, forms.SelectMultiple):
    pass


class CheckboxSelectMultiple(CheckboxSelectMultipleBase):
    """
    Fix initialization bug.
    Should be corrected on recent Django version.
    TODO: test and remove (test case: treatment type not keep on modif)
    """

    def render(self, name, value, attrs=None, choices=(), renderer=None):
        if type(value) in (str, str):
            value = value.split(",")
        if not isinstance(value, (list, tuple)):
            value = [value]
        return super(CheckboxSelectMultiple, self).render(
            name, value, attrs, renderer=None
        )


class Select2BaseField(object):
    multiple = False

    def __init__(self, *args, **kwargs):
        new = None
        if "new" in kwargs:
            new = kwargs.pop("new")
        remote = None
        if "remote" in kwargs:
            remote = kwargs.pop("remote")
        self.model, self.remote = None, None
        if "model" in kwargs:
            self.model = kwargs.pop("model")
            if remote:
                self.remote = reverse_lazy(
                    "autocomplete-" + self.model.__name__.lower()
                )
        long_widget = False
        if "long_widget" in kwargs:
            long_widget = kwargs.pop("long_widget")
        self.available = False
        if "available" in kwargs:
            self.available = kwargs.pop("available")
        attrs = {}
        if long_widget:
            attrs["cols"] = True
            attrs["full-width"] = True
        if self.multiple:
            widget = Select2Multiple
        else:
            widget = Select2Simple
        if kwargs.get("style", None):
            attrs["style"] = kwargs.pop("style")
        kwargs["widget"] = widget(
            model=self.model,
            available=self.available,
            remote=self.remote,
            new=new,
            attrs=attrs,
        )
        super(Select2BaseField, self).__init__(*args, **kwargs)

    def get_q(self):
        q = self.model.objects
        if self.available:
            q = q.filter(available=True)
        return q

    def valid_value(self, value):
        if not self.model:
            return super(Select2BaseField, self).valid_value(value)
        return bool(self.get_q().filter(pk=value).count())


class Select2MultipleField(Select2BaseField, forms.MultipleChoiceField):
    multiple = True

    def to_python(self, value):
        if not isinstance(value, (list, tuple)):
            if value:
                value = value.split(",")
            else:
                value = []
        return super(Select2MultipleField, self).to_python(value)


class Select2SimpleField(Select2BaseField, forms.ChoiceField):
    pass


class DeleteWidget(forms.CheckboxInput):
    def render(self, name, value, attrs=None, renderer=None):
        final_attrs = flatatt(
            self.build_attrs(
                attrs, {"name": name, "value": "1", "class": "btn btn-danger"}
            )
        )
        output = "<button%s>%s</button>" % (final_attrs, _("Delete"))

        return mark_safe(output)


class SwitchWidget(forms.CheckboxInput):
    extra_class = ""
    extra_label = ""

    def render(self, name, value, attrs=None, renderer=None):
        extra_class = (" " + self.extra_class) if self.extra_class else ""
        default = {
            "name": name,
            "value": "1",
            "class": "switch" + extra_class,
            "type": "checkbox",
        }
        if value:
            default["checked"] = "checked"
        attrs = self.build_attrs(attrs, default)
        final_attrs = flatatt(attrs)
        extra_label = ""
        if self.extra_label:
            extra_label = '<label for="{}">{}</label>'.format(
                attrs["id"], self.extra_label
            )
        output = """<span class="switch{}">
      <input{}>{}
</span>""".format(
            extra_class, final_attrs, extra_label
        )
        return mark_safe(output)


class DeleteSwitchWidget(SwitchWidget):
    extra_class = "danger"
    extra_label = _("Delete")


class BSClearableFileInput(ClearableFileInput):
    template_name = "widgets/clearable_file_input.html"


class ImageFileInput(ClearableFileInput):
    template_name = "widgets/image_input.html"
    NO_FORM_CONTROL = True

    def format_value(self, value):
        if self.is_initial(value):
            return value
        # try to display posted images
        try:
            has_file = hasattr(value, "file")
        except ValueError:
            has_file = False
        if has_file:
            if hasattr(value, "file"):
                full_path = str(value.file)
                if full_path.startswith(settings.MEDIA_ROOT):
                    value.url = (
                        settings.MEDIA_URL + full_path[len(settings.MEDIA_ROOT) :]
                    )
        elif value:
            full_path = settings.MEDIA_ROOT + str(value)
            try:
                with open(full_path) as f:
                    f = File(f)
                    f.url = settings.MEDIA_URL + str(value)
                    value = f
            except IOError:
                return value

        return value

    def get_context(self, name, value, attrs):
        context = super(ImageFileInput, self).get_context(name, value, attrs)
        if getattr(self, "hidden", None):
            context["hidden_value"] = self.hidden
        # on post memory file is used: display the name
        if getattr(self, "hidden_name", None):
            context["hidden_name_value"] = self.hidden_name
        return context

    def value_from_datadict(self, data, files, name):
        value = super(ImageFileInput, self).value_from_datadict(data, files, name)
        hidden_name = name + "-hidden"
        hidden_name_value = name + "-hidden-name"
        self.hidden, self.hidden_name = None, None
        if name in files:
            # new file posted
            self.hidden_name = files.get(name).name
        elif data.get(hidden_name_value, None):
            # file posted previously - keep the name
            self.hidden_name = data.get(hidden_name_value)
        elif hidden_name in data:
            # initial file
            self.hidden = data.get(hidden_name)
        return value


class CustomWidget(forms.TextInput):
    TEMPLATE = ""
    EXTRA_DCT = {}

    def render(self, name, value, attrs=None, renderer=None):
        if not value:
            value = ""
        final_attrs = flatatt(self.build_attrs(attrs, {"name": name, "value": value}))
        dct = {
            "final_attrs": final_attrs,
            "id": attrs["id"],
            "safe_id": attrs["id"].replace("-", "_"),
        }
        dct.update(self.EXTRA_DCT)
        t = loader.get_template(self.TEMPLATE)
        rendered = t.render(dct)
        return mark_safe(rendered)


class SquareMeterWidget(CustomWidget):
    TEMPLATE = "widgets/UnitWidget.html"
    EXTRA_DCT = {"unit1": "m²", "unit2": "ha", "factor": 10000}


class GramKilogramWidget(CustomWidget):
    TEMPLATE = "widgets/UnitWidget.html"
    EXTRA_DCT = {"unit1": "g", "unit2": "kg", "factor": 1000}


class CentimeterMeterWidget(CustomWidget):
    TEMPLATE = "widgets/UnitWidget.html"
    EXTRA_DCT = {"unit1": "cm", "unit2": "m", "factor": 100}


class MeterKilometerWidget(CustomWidget):
    TEMPLATE = "widgets/UnitWidget.html"
    EXTRA_DCT = {"unit1": "m", "unit2": "km", "factor": 1000}


class MeterCentimeterWidget(CustomWidget):
    TEMPLATE = "widgets/UnitWidget.html"
    EXTRA_DCT = {"unit1": "m", "unit2": "cm", "factor": "0.01"}


AreaWidget = forms.TextInput
if settings.SURFACE_UNIT == "square-metre":
    AreaWidget = SquareMeterWidget


class ISBNWidget(CustomWidget):
    TEMPLATE = "widgets/CheckTextWidget.html"
    EXTRA_DCT = {"validator": "is_valid_isbn"}


class ISSNWidget(CustomWidget):
    TEMPLATE = "widgets/CheckTextWidget.html"
    EXTRA_DCT = {"validator": "is_valid_issn"}


class CheckboxInput(forms.CheckboxInput):
    NO_FORM_CONTROL = True


class SearchWidget(forms.TextInput):
    template_name = "widgets/search_input.html"

    def __init__(self, app_name=None, model=None, pin_model=None, attrs=None):
        super().__init__(attrs)
        self.app_name = app_name
        self.model = model
        if not pin_model:
            pin_model = self.model.lower()
        self.pin_model = pin_model

    def get_context(self, name, value, attrs):
        context = super(SearchWidget, self).get_context(name, value, attrs)
        context["app_name"] = self.app_name
        context["model"] = self.model
        context["pin_model"] = self.pin_model
        context["widget"]["ISHTAR_DOCUMENT_VERSION"] = settings.ISHTAR_DOCUMENT_VERSION
        return context


class ModelFieldMixin(object):
    def to_python(self, value):
        if not value:
            return
        if not self.multiple:
            value = [value]
        values = []
        for v in value:
            if not v:
                continue
            try:
                values.append(self.model.objects.get(pk=v))
            except self.model.DoesNotExist:
                raise ValidationError(
                    str(_("{} is not a valid key for {}")).format(v, self.model)
                )
        if not self.multiple:
            return values[0]
        return values


class ModelChoiceField(ModelFieldMixin, forms.ChoiceField):
    def __init__(self, model, multiple=False, *args, **kwargs):
        self.model = model
        self.multiple = multiple
        super(ModelFieldMixin, self).__init__(*args, **kwargs)

    def valid_value(self, value):
        if value and getattr(value, "pk", None) in [v for v, l in self.choices]:
            return True
        return super(ModelChoiceField, self).valid_value(value)


class ModelJQueryAutocompleteField(ModelFieldMixin, forms.CharField):
    def __init__(
        self, model, multiple=False, new=False, long_widget=False, *args, **kwargs
    ):
        self.model = model
        self.multiple = multiple
        attrs = {}
        if long_widget:
            attrs["cols"] = True
            attrs["full-width"] = True
        kwargs["widget"] = JQueryAutoComplete(
            reverse_lazy("autocomplete-" + self.model.SLUG),
            associated_model=self.model,
            new=new,
            multiple=multiple,
            attrs=attrs,
        )
        super(ModelJQueryAutocompleteField, self).__init__(*args, **kwargs)


class JQueryAutoComplete(forms.TextInput):
    def __init__(
        self,
        source,
        associated_model=None,
        options=None,
        attrs=None,
        new=False,
        url_new="",
        multiple=False,
        limit=None,
        dynamic_limit=None,
        detail=False,
        modify=False,
        tips="",
    ):
        """
        Source can be a list containing the autocomplete values or a
        string containing the url used for the request.
        """
        self.source = source
        self.associated_model = associated_model
        self.tips = tips
        self.user = None

        self.options = None
        if options and len(options) > 0:
            self.options = JSONEncoder().encode(options)
        self.attrs = {}

        if attrs:
            self.attrs.update(attrs)

        self.new = new
        self.url_new = url_new
        self.modify = modify
        self.detail = detail
        self.multiple = multiple
        self.limit = limit or {}
        self.dynamic_limit = dynamic_limit or []
        if (self.new or self.modify) and not self.associated_model:
            raise AttributeError(
                "JQueryAutoComplete - associated_model must be defined if new or "
                "modify are set to True"
            )

    def value_from_datadict(self, data, files, name):
        v = data.get(name, None)
        if not self.multiple:
            return data.get(name, None)
        if type(v) == str and "," in v:
            return [item.strip() for item in v.split(",") if item.strip()]
        return data.getlist(name, None)

    def render_js(self, field_id, current_pk):
        if isinstance(self.source, list):
            source = JSONEncoder().encode(self.source)
        elif isinstance(self.source, str) or isinstance(self.source, str):
            source = "'%s'" % escape(self.source)
        else:
            try:
                source = "'" + str(self.source) + "'"
            except:
                raise ValueError("{} source type is not valid".format(self.source))
        dynamic_limit = []
        for lim in self.dynamic_limit:
            field_ids = field_id.split("-")
            if field_ids[1:-1]:
                dynamic_limit.append(
                    "id_"
                    + lim.replace("_", "")
                    + "-"
                    + "-".join(field_ids[1:-1])
                    + "-"
                    + lim
                )
            else:
                dynamic_limit.append("id_" + lim.replace("_", ""))
        dct = {
            "source": mark_safe(source),
            "field_id": field_id,
            "safe_field_id": field_id.replace("-", "_"),
            "modify": self.modify,
            "dynamic_limit": dynamic_limit,
        }
        if self.attrs.get("autofocus", False):
            dct["autofocus"] = True
        if self.associated_model:
            model_name = self.associated_model._meta.object_name.lower()
            dct["model_name"] = model_name
        if self.detail:
            model_name = self.associated_model._meta.object_name.lower()
            url_detail = "/detail-{}/".format(model_name)
            dct["detail"] = url_detail
        if self.options:
            dct["options"] = mark_safe("%s" % self.options)

        tpl = "blocks/JQueryAutocomplete.js"
        if self.multiple:
            tpl = "blocks/JQueryAutocompleteMultiple.js"
        t = loader.get_template(tpl)
        js = t.render(dct)
        return js

    def render(self, name, value, attrs=None, renderer=None):
        attrs_hidden = self.build_attrs(attrs, {"name": name})
        attrs_select = self.build_attrs(attrs)
        attrs_select["placeholder"] = _("Search...")
        values = []
        if value:
            hiddens = []
            selects = []
            if type(value) not in (list, tuple):
                values = str(escape(smart_text(value)))
                values = values.replace("[", "").replace("]", "")
                values = values.split(",")
            else:
                values = []
                for v in value:
                    values += v.split(",")
            for v in values:
                if not v:
                    continue
                hiddens.append(v)
                selects.append(v)
                if self.associated_model:
                    try:
                        selects[-1] = str(self.associated_model.objects.get(pk=v))
                    except (self.associated_model.DoesNotExist, ValueError):
                        selects.pop()
                        hiddens.pop()
            if self.multiple:
                attrs_hidden["value"] = ", ".join(hiddens)
                if selects:
                    selects.append("")
                attrs_select["value"] = ", ".join(selects)
            else:
                if hiddens and selects:
                    attrs_hidden["value"] = hiddens[0]
                    attrs_select["value"] = selects[0]
        if "id" not in self.attrs:
            attrs_hidden["id"] = "id_%s" % name
            attrs_select["id"] = "id_select_%s" % name
        if "class" not in attrs_select:
            attrs_select["class"] = "autocomplete"

        has_previous_value = "value" in attrs_select and attrs_select["value"]
        attrs_select["class"] += " form-control"
        new = ""
        html = ""
        if self.tips or self.new or self.modify:
            klass = "input-group"
            if has_previous_value:
                klass += " has-previous-value"
            html = "<div class='{}'>".format(klass)
            # WARNING: the modal for the form must be in the main template
            # "extra_form_modals" list is used for that in form or view
            model_name = self.associated_model._meta.object_name.lower()
            if self.tips:
                tips = self.tips
                if callable(tips):
                    tips = tips()
                new += """<span class="input-group-append">
                <span class="add-button input-group-text">
                <em id="{}-tips">{}</em>
                </span></span>
                """.format(
                    attrs_hidden["id"], tips
                )
            meta = ""
            if self.associated_model:
                meta = self.associated_model._meta
            base_permission = f"{meta.app_label}.{{}}_{meta.model_name}"
            permission = base_permission.format("change")
            own_permission = base_permission.format("change_own")
            if self.modify and self.associated_model and self.user:
                modify_ok = False
                if self.user.has_permission(permission):
                    modify_ok = True
                else:
                    try:
                        obj = self.associated_model.objects.get(pk=attrs_hidden["id"])
                        modify_ok = self.user.has_permission(own_permission, obj)
                    except self.associated_model.DoesNotExist:
                        pass
                if modify_ok:
                    new += """
                    <span class="input-group-append">
                    <a href="#" id="{}-modify" class="modify-button input-group-text"
                        onclick="{}_modify();">
                        <i class="fa fa-pencil"></i></a>
                    </span>""".format(
                        attrs_hidden["id"], name.replace("-", "_")
                    )
            permission = base_permission.format("add")
            if self.new and self.associated_model and self.user and \
                    self.user.has_permission(permission):
                limits = []
                for k in self.limit:
                    limits.append(k + "__" + "-".join([str(v) for v in self.limit[k]]))
                args = [attrs_select["id"]]
                if limits:
                    args.append(";".join(limits))
                url_new = "new-" + model_name
                if self.url_new:
                    url_new = self.url_new
                url_new = reverse(url_new, args=args)
                new += """
                <span class="input-group-append">
                <a href="#" class="add-button input-group-text"
                    onclick="dt_qa_open('{}', 'modal-dynamic-form-{}');">+</a>
                </span>
                """.format(
                    url_new, model_name
                )
            new += "</div>"
        detail = ""
        if self.detail:
            detail = """<div class="form-control detail-value" id="{}-detail">
            </div>""".format(
                attrs_hidden["id"]
            )

        old_value = ""
        if has_previous_value:
            old_value = """<div class='input-group previous-value'>
                <div class="input-group-prepend">
                    <span class="input-group-text font-italic">{}</span>
                </div>
                <input class="form-control" id="{}" type="text" value="{}"
                   disabled="disabled" />
                <span class="input-group-append">
                <button class="btn btn-secondary small-button" type="button"
                    id="{}" title="{}">
                    <i class="fa fa-refresh" aria-hidden="true"></i>
                </button>
                </span>
            </div>""".format(
                _("Prev.:"),
                attrs_hidden["id"] + "_previous_label",
                attrs_select["value"],
                attrs_hidden["id"] + "_previous_button",
                _("Restore previous"),
            )
            attrs_hidden_previous = attrs_hidden.copy()
            attrs_hidden_previous["name"] += "_previous"
            attrs_hidden_previous["id"] += "_previous"
            old_value += "<input type='hidden'{}>".format(
                flatatt(attrs_hidden_previous)
            )
        pk = None
        if values:
            pk = values[0]
        html += """
<input{attrs_select}/>{new}\
<input type="hidden"{attrs_hidden}/>\
{detail}{old_value}
        <script type="text/javascript"><!--//
        {js}//--></script>
        """.format(
            old_value=old_value,
            attrs_select=flatatt(attrs_select),
            attrs_hidden=flatatt(attrs_hidden),
            js=self.render_js(name, pk),
            new=new,
            detail=detail,
        )
        return html


class JQueryTown(forms.TextInput):
    """
    Town fields with state and department pre-selections
    """

    def __init__(self, source, options={}, attrs={}, new=False, limit={}):
        self.options = None
        self.attrs = {}
        self.source = source
        if len(options) > 0:
            self.options = JSONEncoder().encode(options)
        self.attrs.update(attrs)
        self.new = new
        self.limit = limit

    @classmethod
    def encode_source(cls, source):
        if isinstance(source, list):
            encoded_src = JSONEncoder().encode(source)
        elif isinstance(source, str):
            src = escape(source)
            if not src.endswith("/"):
                src += "/"
            encoded_src = "'%s'" % src
        else:
            try:
                src = str(source)
                if not src.endswith("/"):
                    src += "/"
                encoded_src = "'%s'" % src
            except:
                raise ValueError("source type is not valid")
        return encoded_src

    def render(self, name, value, attrs=None, renderer=None):
        attrs_hidden = self.build_attrs(attrs, {"name": name})
        attrs_select = self.build_attrs(attrs)
        attrs_select["placeholder"] = _("Search...")
        selected = ""
        selected_state = ""
        selected_department = ""
        if value:
            hiddens = []
            selects = []
            if type(value) not in (list, tuple):
                values = str(escape(smart_text(value)))
                values = values.replace("[", "").replace("]", "")
                values = values.split(",")
            else:
                values = []
                for v in value:
                    values += v.split(",")
            for v in values:
                if not v:
                    continue
                hiddens.append(v)
                selects.append(v)
                try:
                    item = models.Town.objects.get(pk=v)
                    selects[-1] = str(item)
                    if item.departement:
                        selected_department = item.departement.number
                        if item.departement.state:
                            selected_state = item.departement.state.number
                    selected = item.pk
                except (models.Town.DoesNotExist, ValueError):
                    selects.pop()
                    hiddens.pop()
            if hiddens and selects:
                attrs_hidden["value"] = hiddens[0]
                attrs_select["value"] = selects[0]
        if "id" not in self.attrs:
            attrs_hidden["id"] = "id_%s" % name
            attrs_select["id"] = "id_select_%s" % name
        if "class" not in attrs_select:
            attrs_select["class"] = "autocomplete"

        source = self.encode_source(self.source)
        dct = {
            "source": mark_safe(source),
            "selected": selected,
            "safe_field_id": slugify(name).replace("-", "_"),
            "field_id": name,
        }
        if self.options:
            dct["options"] = mark_safe("%s" % self.options)

        dct.update(
            {
                "attrs_select": mark_safe(flatatt(attrs_select)),
                "attrs_hidden": mark_safe(flatatt(attrs_hidden)),
                "name": name,
                "states": models.State.objects.all().order_by("label"),
                "selected_department": selected_department,
                "selected_state": selected_state,
            }
        )
        html = loader.get_template("blocks/JQueryAdvancedTown.html").render(dct)
        return html


class JQueryPersonOrganization(forms.TextInput):
    """
    Complex widget which manage:
     * link between person and organization
     * display addresses of the person and of the organization
     * create new person and new organization
    """

    def __init__(
        self,
        source,
        edit_source,
        model,
        options={},
        attrs={},
        new=False,
        limit={},
        html_template="blocks/PersonOrganization.html",
        js_template="blocks/JQueryPersonOrganization.js",
    ):
        self.options = None
        self.attrs = {}
        self.model = model
        self.source = source
        self.edit_source = edit_source
        if len(options) > 0:
            self.options = JSONEncoder().encode(options)
        self.attrs.update(attrs)
        self.new = new
        self.limit = limit
        self.js_template = js_template
        self.html_template = html_template

    @classmethod
    def encode_source(cls, source):
        if isinstance(source, list):
            encoded_src = JSONEncoder().encode(source)
        elif isinstance(source, str) or isinstance(source, str):
            encoded_src = "'%s'" % escape(source)
        else:
            try:
                encoded_src = "'" + str(source) + "'"
            except:
                raise ValueError("source type is not valid")
        return encoded_src

    def render_js(self, field_id, selected=""):
        source = self.encode_source(self.source)
        edit_source = self.encode_source(self.edit_source)
        dct = {
            "source": mark_safe(source),
            "edit_source": mark_safe(edit_source),
            "selected": selected,
            "safe_field_id": slugify(field_id).replace("-", "_"),
            "field_id": field_id,
        }
        if self.options:
            dct["options"] = mark_safe("%s" % self.options)
        js = loader.get_template(self.js_template).render(dct)
        return js

    def render(self, name, value, attrs=None, renderer=None):
        attrs_hidden = self.build_attrs(attrs, {"name": name})
        attrs_select = self.build_attrs(attrs)
        attrs_select["placeholder"] = _("Search...")
        selected = ""
        if value:
            hiddens = []
            selects = []
            if type(value) not in (list, tuple):
                values = str(escape(smart_text(value)))
                values = values.replace("[", "").replace("]", "")
                values = values.split(",")
            else:
                values = []
                for v in value:
                    values += v.split(",")
            for v in values:
                if not v:
                    continue
                hiddens.append(v)
                selects.append(v)
                if self.model:
                    try:
                        item = self.model.objects.get(pk=v)
                        selects[-1] = str(item)
                        selected = item.pk
                    except (self.model.DoesNotExist, ValueError):
                        selects.pop()
                        hiddens.pop()
            if hiddens and selects:
                attrs_hidden["value"] = hiddens[0]
                attrs_select["value"] = selects[0]
        if "id" not in self.attrs:
            attrs_hidden["id"] = "id_%s" % name
            attrs_select["id"] = "id_select_%s" % name
        if "class" not in attrs_select:
            attrs_select["class"] = "autocomplete"
        new = ""
        dct = {
            "attrs_select": mark_safe(flatatt(attrs_select)),
            "attrs_hidden": mark_safe(flatatt(attrs_hidden)),
            "name": name,
            "js": self.render_js(name, selected),
            "new": mark_safe(new),
        }
        html = loader.get_template(self.html_template).render(dct)
        return html


class DataTable(Select2Media, forms.RadioSelect):
    def __init__(
        self,
        source,
        form,
        associated_model,
        attrs=None,
        table_cols="TABLE_COLS",
        multiple=False,
        multiple_cols=None,
        new=False,
        new_message="",
        source_full=None,
        multiple_select=False,
        sortname="__default__",
        col_prefix="",
        gallery=False,
        map=False,
    ):
        """
        DataTable widget init.

        :param source: url to get the item from -- get_item
        :param form:
        :param associated_model: model of the listed items
        :param attrs:
        :param table_cols:
        :param multiple:
        :param multiple_cols:
        :param new:
        :param new_message:
        :param source_full: url to get full listing
        :param multiple_select: select multiple is available
        :param sortname: column name (model attribute) to use to sort
        :param col_prefix: prefix to remove to col_names
        :param gallery: display the gallery if True
        :param map: display the map if True - can be a callable
        """
        super(DataTable, self).__init__(attrs=attrs)
        self.source = source
        self.form = form
        if not attrs:
            attrs = {}
        self.attrs = attrs.copy()
        self.associated_model = associated_model
        self.table_cols = table_cols
        self.multiple = multiple
        self.multiple_select = multiple_select
        if not multiple_cols:
            multiple_cols = [2]
        self.multiple_cols = multiple_cols
        self.new, self.new_message = new, new_message
        self.source_full = source_full
        self.sortname = sortname
        self.col_prefix = col_prefix
        self.user = None
        self.gallery = gallery
        self.map = map
        self.external_sources = None
        self.form_permission = "view"
        if self.col_prefix and not self.col_prefix.endswith("__"):
            self.col_prefix += "__"

    def get_cols(self, python=False):
        jq_col_names, extra_cols = [], []
        table_cols, col_labels = get_columns_from_class(
            self.associated_model,
            table_cols_attr=self.table_cols
        )
        for col_names in table_cols:
            field_verbose_names = []
            field_verbose_name, field_name = "", ""
            if type(col_names) not in (list, tuple):
                col_names = [col_names]
            for col_name in col_names:
                field = self.associated_model
                keys = col_name.split("__")
                if "." in col_name:
                    keys = col_name.split(".")
                for key in keys:
                    if hasattr(field, "remote_field") and field.remote_field:
                        field = field.remote_field.model
                    try:
                        field = field._meta.get_field(key)
                        field_verbose_name = field.verbose_name
                    except (FieldDoesNotExist, AttributeError, KeyError):
                        if hasattr(field, key + "_lbl"):
                            field_verbose_name = getattr(field, key + "_lbl")
                        else:
                            continue
                if field_name:
                    field_name += "__"
                if col_name.startswith(self.col_prefix):
                    field_name += col_name[len(self.col_prefix) :]
                else:
                    field_name += col_name
                field_verbose_names.append(str(field_verbose_name))
            if not field_name:
                field_name = "__".join(col_names)
            if field_name in col_labels:
                lbl = col_labels[field_name]
                if callable(lbl):
                    lbl = lbl()
                jq_col_names.append(str(lbl))
            elif col_names and col_names[0] in col_labels:
                jq_col_names.append(str(col_labels[col_names[0]]))
            else:
                jq_col_names.append(
                    settings.JOINT.join([f for f in field_verbose_names if f])
                )
            extra_cols.append(field_name)
        return jq_col_names, extra_cols

    def render(self, name, value, attrs=None, renderer=None):
        # t = loader.get_template('blocks/form_flex_snippet.html')
        t = loader.get_template("blocks/bs_form_snippet.html")
        if self.user:
            form = self.form(user=self.user)
            if self.user.ishtaruser and self.user.ishtaruser.show_field_number():
                form.show_field_number = True
        else:
            form = self.form()
        rendered = t.render({"form": form, "search": True})
        dct = {}
        if self.new:
            model_name = self.associated_model._meta.object_name.lower()
            dct["url_new"] = reverse("new-" + model_name, args=["0"])
            dct["new_message"] = self.new_message

        col_names, extra_cols = self.get_cols()

        col_idx = []
        for k in form.get_input_ids():
            col_idx.append('"%s"' % k)
        col_idx = col_idx and ", ".join(col_idx) or ""

        dct["encoding"] = settings.ENCODING or "utf-8"
        try:
            dct["source"] = str(self.source)
        except NoReverseMatch:
            logger.warning("Cannot resolve source for {} widget".format(self.form))

        # full CSV export currently disabled
        # if str(self.source_full) and str(self.source_full) != 'None':
        #    dct['source_full'] = str(self.source_full)

        dct["extra_sources"] = []
        dct["quick_actions"] = []
        if self.associated_model:
            dct["current_model"] = self.associated_model
            model_name = "{}.{}".format(
                self.associated_model.__module__, self.associated_model.__name__
            )
            for imp in models.ImporterType.objects.filter(
                slug__isnull=False,
                associated_models__klass=model_name,
                is_template=True,
            ).all():
                dct["extra_sources"].append(
                    (imp.slug, imp.name, reverse("get-by-importer", args=[imp.slug]))
                )
            if hasattr(self.associated_model, "QUICK_ACTIONS"):
                dct["quick_actions"] = self.associated_model.get_quick_actions(
                    user=self.user
                )
        source = str(self.source)
        dct.update(
            {
                "name": name,
                "col_names": col_names,
                "extra_cols": extra_cols,
                "source": source,
                "form_permission": self.form_permission,
                "col_idx": col_idx,
                "no_result": str(_("No results")),
                "loading": str(_("Loading...")),
                "remove": str(_("Remove")),
                "sname": name.replace("-", ""),
                "external_sources": self.external_sources,
                "gallery": self.gallery,
                "use_map": self.map() if callable(self.map) else self.map,
                "multiple": self.multiple,
                "multiple_select": self.multiple_select,
                "multi_cols": ",".join(('"%d"' % col for col in self.multiple_cols)),
            }
        )
        t = loader.get_template("blocks/DataTables.html")
        rendered += t.render(dct)
        return mark_safe(rendered)


class RangeInput(NumberInput):
    input_type = "range"


class OSMWidget(gis_forms.OSMWidget):
    """
    Replace Media with local ressource
    """
    class Media:
        extend = False
        css = {
            'all': (
                "ol/ol.css",
                'gis/css/ol3.css',
            )
        }
        js = (
            "ol/ol.js",
            "ol-layerswitcher/ol-layerswitcher.js",
            "gis/js/OLMapWidget.js",
        )


class ReversedOSMWidget(OSMWidget):
    def get_context(self, name, value, attrs):
        if value:
            if not isinstance(value, str):  # should be geo
                value = reverse_coordinates(value.ewkt)
        context = super().get_context(name, value, attrs)
        return context