1
+ from collections import namedtuple
2
+
1
3
from django .db .models import Q
2
4
3
5
4
6
def generate_q_objects_by_fields_and_words (fields , search_text ):
5
7
"""
6
8
Author: Milton Lenis
7
9
Date: 16 April 2017
8
- A handy method to generate Q Objects for filtering the queryset, this generates a Q Object by each field with each
9
- word, that means, if you have a search_term with 3 words and 3 fields this generates 9 Q Objects.
10
+ A handy method to generate Q Objects for filtering the queryset, this
11
+ generates a Q Object by each field with each word, that means, if you have
12
+ a search_term with 3 words and 3 fields this generates 9 Q Objects.
10
13
11
- This also looks if the field has choices, if so, it cast to the choices internal representation to do the Q Object
12
- creation correctly
14
+ This also looks if the field has choices, if so, it cast to the choices
15
+ internal representation to do the Q Object creation correctly
13
16
14
- Because Q Objects internally are subclasses of django.utils.tree.Node we can 'add' Q Objects with connectors,
17
+ Because Q Objects internally are subclasses of django.utils.tree.Node we can
18
+ 'add' Q Objects with connectors,
15
19
see: https://bradmontgomery.net/blog/adding-q-objects-in-django/
16
20
"""
17
21
q = Q ()
18
22
search_text = search_text .split (" " )
19
23
for field in fields :
20
24
for word in search_text :
21
25
if field .choices :
22
- # Build a dict with the dictionary to do search by choices display
23
- # This dictionary takes the value (display of the choices) as key and the internal representation as
24
- # value
26
+ # Build a dict with the dictionary to do search by choices
27
+ # display
28
+ # This dictionary takes the value (display of the choices) as
29
+ # key and the internal representation as value
30
+ # TODO: Change this
25
31
field_choices = {value .lower (): key for key , value in field .choices }
26
32
# Search if the searched word exists in the field choices
27
- value_coincidences = [value for key , value in field_choices .items () if word .lower () in key ]
33
+ value_coincidences = [
34
+ value for key , value in field_choices .items ()
35
+ if word .lower () in key
36
+ ]
28
37
if value_coincidences :
29
38
search_criteria = {"%s__in" % field .name : value_coincidences }
30
39
q .add (
@@ -40,3 +49,31 @@ def generate_q_objects_by_fields_and_words(fields, search_text):
40
49
Q .OR
41
50
)
42
51
return q
52
+
53
+
54
+ def arrayfield_keys_to_values (keys , choices ):
55
+ values = []
56
+ for key , value in choices :
57
+ if key in keys :
58
+ values .append (value )
59
+ return values
60
+
61
+
62
+ def create_column_defs_list (column_defs ):
63
+ column_defs_list = []
64
+ for counter , column_def in enumerate (column_defs ):
65
+ if type (column_def ) == str :
66
+ column_defs_list .append ({'title' : column_def , 'targets' : counter , 'orderable' : 1 , 'searchable' : 1 })
67
+ elif type (column_def ) == dict :
68
+ column_def ['targets' ] = counter
69
+ column_defs_list .append (column_def )
70
+
71
+ return column_defs_list
72
+
73
+
74
+ # Using namedtuple for readability, the var name is capitalized because
75
+ # this namedtuple is used like a class
76
+ Draw = namedtuple (
77
+ 'Draw' ,
78
+ ['start' , 'end' , 'sort_column' , 'sort_order' , 'search' , 'draw' ]
79
+ )
0 commit comments