Skip to content

Commit d5dfadd

Browse files
committed
First working version without modals
1 parent 3bb1999 commit d5dfadd

File tree

11 files changed

+710
-82
lines changed

11 files changed

+710
-82
lines changed

datatables_listview/core/utils.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
1+
from collections import namedtuple
2+
13
from django.db.models import Q
24

35

46
def generate_q_objects_by_fields_and_words(fields, search_text):
57
"""
68
Author: Milton Lenis
79
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.
1013
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
1316
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,
1519
see: https://bradmontgomery.net/blog/adding-q-objects-in-django/
1620
"""
1721
q = Q()
1822
search_text = search_text.split(" ")
1923
for field in fields:
2024
for word in search_text:
2125
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
2531
field_choices = {value.lower(): key for key, value in field.choices}
2632
# 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+
]
2837
if value_coincidences:
2938
search_criteria = {"%s__in" % field.name: value_coincidences}
3039
q.add(
@@ -40,3 +49,31 @@ def generate_q_objects_by_fields_and_words(fields, search_text):
4049
Q.OR
4150
)
4251
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

Comments
 (0)