-
Notifications
You must be signed in to change notification settings - Fork 12
/
helpers.py
1201 lines (926 loc) · 33.5 KB
/
helpers.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
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
# -*- coding: utf-8 -
import re
import logging
import json
import uuid
import urllib
import requests
import functools32
try:
# CKAN 2.7 and later
from ckan.common import config
except ImportError:
# CKAN 2.6 and earlier
from pylons import config
import ckan.model as m
from ckan.common import c
from ckan.plugins import toolkit
from ckan.plugins.toolkit import _
import ckan.lib.helpers as h
from ckan import logic
from ckanext.querytool.model import CkanextQueryTool, child_group_search
log = logging.getLogger(__name__)
def _get_context():
return {
'model': m,
'session': m.Session,
'user': c.user or c.author,
'auth_user_obj': c.userobj
}
def _get_action(action, data_dict):
return toolkit.get_action(action)(_get_context(), data_dict)
def _get_functions(module_root, functions={}):
'''
Helper function that scans extension
logic/auth dir for all logic/auth functions.
'''
for module_name in ['create', 'update', 'delete', 'get']:
module_path = '%s.%s' % (module_root, module_name,)
module = __import__(module_path)
for part in module_path.split('.')[1:]:
module = getattr(module, part)
for key, value in module.__dict__.items():
if not key.startswith('_') \
and (hasattr(value, '__call__')
and (value.__module__ == module_path)):
functions[key] = value
return functions
def _isnt_id(v):
return v['id'] != '_id'
def _is_numeric(v):
return v['type'] == 'numeric'
def user_is_registered(context):
'''
Checks if the user is registered user
'''
model = context['model']
user = context['user']
user_obj = model.User.get(user)
if not user_obj:
log.error('User {0} not found').format(user)
return False
return True
def get_all_datasets():
'''
Get all public datasets
:return:
'''
datasets = _get_action('package_list', {})
return datasets
def get_filter_values(resource_id, filter_name, previous_filters=[]):
'''Returns resource field values with no duplicates.'''
resource = _get_action('resource_show', {'id': resource_id})
if not resource.get('datastore_active'):
return []
data = {
'resource_id': resource['id'],
'limit': 0
}
result = _get_action('datastore_search', data)
where_clause = _create_where_clause(previous_filters)
fields = [field['id'] for field in result.get('fields', [])]
values = []
if filter_name in fields:
sql_string = u'''SELECT DISTINCT "{column}"
FROM "{resource}" {where}'''.format(
column=filter_name,
resource=resource_id,
where=where_clause
)
result = _get_action('datastore_search_sql', {'sql': sql_string})
values = [field[filter_name] for field in result.get('records', [])]
values = filter(lambda x: x != "" and x != None, values)
return sorted(values)
def get_chart_types():
'''
Get all available types of chart following c3 specification
:return:
'''
chart_types = [
{'text': _('Line'), 'value': 'line'},
{'text': _('Bar'), 'value': 'bar'},
{'text': _('Horizontal bar'), 'value': 'hbar'},
{'text': _('Stacked bar'), 'value': 'sbar'},
{'text': _('Stacked horizontal bar'), 'value': 'shbar'},
{'text': _('Area'), 'value': 'area'},
{'text': _('Spline'), 'value': 'spline'},
{'text': _('Donut'), 'value': 'donut'},
{'text': _('Pie'), 'value': 'pie'},
{'text': _('Scatter'), 'value': 'scatter'}
]
return chart_types
def get_color_scheme():
'''
Get color schemes for displaying the charts
:return:
'''
colors = [{'value': '#59a14f',
'text': _('Green')},
{'value': '#4e79a7',
'text': _('Blue')},
{'value': '#499894',
'text': _('Teal')},
{'value': '#b6992d',
'text': _('Golden')},
{'value': '#ffa600',
'text': _('Yellow')},
{'value': '#d87c26',
'text': _('Orange')},
{'value': '#9d7660',
'text': _('Brown')},
{'value': '#78549a',
'text': _('Purple')},
{'value': '#b2182b',
'text': _('Red')}
]
return colors
def get_querytool_get_chart_colors(data):
try:
data = json.loads(data)
except:
data = []
return data
def get_map_color_scheme():
'''
Get color schemes for displaying the maps
:return:
'''
colors = [
{
'value': '#feedde,#fdbe85,#fd8d3c,#e6550d,#a63603',
'text': _('Sequential reds')
},
{
'value': '#7b3294,#c2a5cf,#EFD9CE,#a6dba0,#008837',
'text': _('Green-Purple')
},
{
'value': '#d7191c,#fdae61,#ffffbf,#abdda4,#2b83ba',
'text': _('Blue-Red')
},
{
'value': '#a6611a,#dfc27d,#FCD0A1,#80cdc1,#018571',
'text': _('Teal-Brown')
},
{
'value': '#e66101,#fdb863,#EFD9CE,#b2abd2,#5e3c99',
'text': _('Purple-Orange')
}
]
return colors
def get_tick_text_rotation():
'''
Get available options for rotating chart x axis
:return:
'''
options = [{'text': _('Horizontal'), 'value': '0'},
{'text': _('Diagonal'), 'value': '30'},
{'text': _('Vertical'), 'value': '90'},
{'text': _('Reverse'), 'value': '180'}]
return options
def get_charts_data_formats(num=None):
'''
Get available formats for charts tooltip and axis ticks
:return:
'''
options = [{'text': _('Default'), 'value': ''},
{'text': _('Integer e.g 2'), 'value': '.0f'},
{'text': _('Decimal (1 digit) e.g 2.5'), 'value': '.1f'},
{'text': _('Decimal (2 digit) e.g 2.50'), 'value': '.2f'},
{'text': _('Decimal (3 digit) e.g 2.501'), 'value': '.3f'},
{'text': _('Decimal (4 digit) e.g 2.5012'), 'value': '.4f'},
{'text': _('Currency e.g. $2,000'), 'value': '$'},
{'text': _('Rounded e.g 2k'), 'value': 's'},
{'text': _('Percentage (0 digit) e.g 25%% for 0.25'),
'value': '.0%'},
{'text': _('Percentage (1 digit) e.g 25.1%% for 0.251'),
'value': '.1%'},
{'text': _('Percentage (2 digit) e.g 25.12%% for 0.2512'),
'value': '.2%'},
{'text': _('Comma thousands separator (0 digit) e.g 2,512'),
'value': ',.0f'},
{'text': _('Comma thousands separator (1 digit) e.g 2,512.3'),
'value': ',.1f'},
{'text': _('Comma thousands separator (2 digit) e.g 2,512.34'),
'value': ',.2f'},
{'text': _('Date DD/MM/YYYY .e.g. 01/01/2014'),
'value': '%d/%m/%Y'},
{'text': _('Date YYYY/MM/DD .e.g. 2014/01/01'),
'value': '%Y/%m/%d'},
{'text': _('Date YYYY/MM .e.g. 2014/01'),
'value': '%Y/%m'},
{'text': _('Date MM/YYYY .e.g. 01/2014'),
'value': '%m/%Y'},
{'text': _('Date DD/MM .e.g. 01/01'),
'value': '%d/%m'},
{'text': _('Date MM/YY .e.g. 01/14'),
'value': '%m/%y'},
{'text': _('Date YYYY .e.g. 2014'),
'value': '%Y'},
{'text': _('Date MMM DD, YYYY .e.g. Jan 01, 2014'),
'value': '%b %d, %Y'},
{'text': _('Date MMM DD .e.g. Jan 01'),
'value': '%b %d'},
{'text': _('Date MMM YY .e.g. Jan 14'),
'value': '%b %y'}]
if num:
return options[:num]
return options
def get_maps_data_formats():
options = [{'text': _('Integer e.g 2'), 'value': '.0f'},
{'text': _('Decimal (1 digit) e.g 2.5'), 'value': '.1f'},
{'text': _('Decimal (2 digit) e.g 2.50'), 'value': '.2f'},
{'text': _('Decimal (3 digit) e.g 2.501'), 'value': '.3f'},
{'text': _('Decimal (4 digit) e.g 2.5012'), 'value': '.4f'},
{'text': _('Currency e.g. $2,000'), 'value': '$,.2f'},
{'text': _('Rounded e.g 2k'), 'value': '.4s'},
{'text': _('Percentage (0 digit) e.g 25%% for 0.25'),
'value': '.0%'},
{'text': _('Percentage (1 digit) e.g 25.1%% for 0.251'),
'value': '.1%'},
{'text': _('Percentage (2 digit) e.g 25.12%% for 0.2512'),
'value': '.2%'},
{'text': _('Comma thousands separator (0 digit) e.g 2,512'),
'value': ',.0f'},
{'text': _('Comma thousands separator (1 digit) e.g 2,512.3'),
'value': ',.1f'},
{'text': _('Comma thousands separator (2 digit) e.g 2,512.34'),
'value': ',.2f'}]
return options
def hex_to_rgba(value, alpha):
value = value.lstrip('#')
if len(value) == 3:
value = ''.join([v*2 for v in list(value)])
return tuple(int(value[i:i+2], 16) for i in range(0, 6, 2))+(alpha,)
def get_visualization_size():
'''
Get available sizes for displaying visualizations: charts, text box
:return:
'''
options = [{'text': _('Small Rectangle (1x2)'), 'value': 'size-sm'},
{'text': _('Small Wide Rectangle (1x6)'),
'value': 'size-sm wide'},
{'text': _('Medium Square (2x2)'), 'value': 'size-sm square'},
{'text': _('Medium Rectangle (2x3)'), 'value': 'size-lg'},
{'text': _('Large Rectangle (2x4)'),
'value': 'size-sm double square'},
{'text': _('Extra Large Rectangle (2x6)'), 'value': 'size-xl'},
{'text': _('Large Square (4x4)'), 'value': 'size-lg square'},
{'text': _('Medium Vertical (4x2)'),
'value': 'size-sm vertical'},
{'text': _('Large Vertical (4x3)'),
'value': 'size-lg vertical'}]
return options
def _create_where_clause(filters):
where_clause = u''
if any(filters):
if len(filters) > 1:
# Loop through filters and create SQL query
for idx, _ in enumerate(filters):
op = u'='
name = _['name']
if isinstance(_['value'], basestring):
value = _['value'].replace('\'', '\'\'')
else:
value = _['value']
if idx == 0:
where_clause = u'WHERE ("{0}" {1} \'{2}\')'.format(
name, op, value)
else:
where_clause += u' AND ("{0}" {1} \'{2}\')'.format(
name, op, value)
else:
_ = filters[0]
op = u'='
name = _['name']
if isinstance(_['value'], basestring):
value = _['value'].replace('\'', '\'\'')
else:
value = _['value']
where_clause = \
u'WHERE ("{0}" {1} \'{2}\')'.format(
name,
op,
value
)
return where_clause
def create_query_str(resource_id, filters):
columns = map(lambda f: u'"{0}"'.format(f['name']), filters)
select = ', '.join(columns)
where_clause = _create_where_clause(filters)
# generate the final SQL query string
sql_string = u'''SELECT * FROM "{resource}" {where}'''.format(
select=select,
resource=resource_id,
where=where_clause)
return sql_string
def get_dataset_resources(dataset_name):
'''
Get resources for particular dataset
:param dataset_name: Name of the dataset
:return:
'''
dataset_resources = []
if dataset_name:
try:
dataset = _get_action('package_show', {'id': dataset_name})
except Exception:
return dataset_resources
for res in dataset.get('resources'):
if res['format'].lower() != 'geojson':
dataset_resources.append({
'value': res.get('id'), 'text': res.get('name')
})
return dataset_resources
def get_resource_columns(res_id, escape_columns=[]):
'''
Get the names of the columns for the resource stored in Datastore
- res_id: (string) ID of the CKAN resource
- escape_columns: (array) names of the columns that should be omitted
'''
data = {
'resource_id': res_id,
'limit': 0
}
try:
result = toolkit.get_action('datastore_search')({}, data)
except Exception:
return []
fields = [field['id'] for field in result.get('fields', [])
if field['id'] not in escape_columns and _isnt_id(field)]
return fields
def get_numeric_resource_columns(res_id):
'''
Get the names of the columns from numeric type
for the resource stored in Datastore
- res_id: (string) ID of the CKAN resource
'''
data = {
'resource_id': res_id,
'limit': 0
}
try:
result = toolkit.get_action('datastore_search')({}, data)
except Exception:
return []
fields = [field['id'] for field in result.get('fields', [])
if field['id'] and _isnt_id(field) and _is_numeric(field)]
return fields
def get_uuid():
return uuid.uuid4()
def get_geojson_resources():
data = {
'query': 'format:geojson',
'order_by': 'name',
}
result = _get_action('resource_search', data)
return [{'text': r['name'], 'value': r['url']}
for r in result.get('results', [])]
def get_geojson_properties(url):
# TODO handle if no url
resp = requests.get(url, verify=False)
geojson = resp.json()
result = []
exclude_keys = [
'marker-symbol',
'marker-color',
'marker-size',
'stroke',
'stroke-width',
'stroke-opacity',
'fill',
'fill-opacity'
]
for k, v in geojson.get('features')[0].get('properties').iteritems():
if k not in exclude_keys:
result.append({'value': k, 'text': k})
return result
def get_map_data(geojson_url, map_key_field, data_key_field,
data_value_field, from_where_clause):
geojson_keys = []
# response = urllib.urlopen(geojson_url)
resp = requests.get(geojson_url, verify=False)
# geojson_data = json.loads(response.read())
geojson_data = resp.json()
for feature in geojson_data['features']:
geojson_keys.append(feature['properties'][map_key_field])
sql = u'SELECT ' + u'"' + data_key_field + \
u'", SUM("' + data_value_field + u'") as ' + \
u'"' + data_value_field + u'"' + from_where_clause + \
u' GROUP BY "' + data_key_field + u'"'
response = toolkit.get_action('datastore_search_sql')(
{}, {'sql': sql}
)
records_to_lower = []
for record in response['records']:
records_to_lower.append({k.lower(): v for k, v in record.items()})
response['records'] = records_to_lower
mapping = {}
for record in records_to_lower:
key = record[data_key_field.lower()]
value = record[data_value_field.lower()]
# Make sure key is number when geojson keys are number:
try:
if isinstance(geojson_keys[0], (int, float)) and key.isdigit():
try:
key = int(key)
except ValueError:
key = float(key)
except Exception, e:
log.error(e)
if key not in geojson_keys:
continue
mapping[key] = {
'key': key,
'value': value
}
map_data = {
'geojson_data': geojson_data,
'features_values': mapping
}
return map_data
# @functools32.lru_cache(maxsize=128)
def get_resource_data(sql_string):
context = {}
if not c.userobj or get_is_admin_or_editor_of_any_group(c.userobj):
context['ignore_auth'] = True
# We need to un-escape the "&" replacement or else charts won't render
if '\\0026' in sql_string:
sql_string = sql_string.replace('\\0026', '&')
response = toolkit.get_action('datastore_search_sql')(
{}, {'sql': sql_string}
)
records_to_lower = []
for record in response['records']:
records_to_lower.append({k.lower(): v for k, v in record.items()})
return records_to_lower
def get_chart_sort():
'''
Get available values for sorting charts data
:return:
'''
options = [{'text': _('Default'), 'value': 'default'},
{'text': _('Ascending'), 'value': 'asc'},
{'text': _('Descending'), 'value': 'desc'}]
return options
def get_groups(parent_name=None, all_children=False):
'''
Get available Groups from the database
return: list of groups
'''
groups = _get_action('group_list', {'all_fields': True})
if all_children is True:
child_groups = []
for group in groups:
group_dict = _get_action('group_show', {'id': group['id']})
if group_dict.get('group_relationship_type') != 'parent':
child_groups.append(group)
return child_groups
if parent_name:
if parent_name == '__misc__group__':
misc_groups = []
for group in groups:
group_dict = _get_action('group_show', {'id': group['id']})
if group_dict.get('parent') in [None, ''] \
and group_dict.get('children') in [None, ''] \
and group_dict.get('group_relationship_type') != 'parent':
misc_groups.append(group)
return misc_groups
else:
child_groups = []
for group in groups:
group_dict = _get_action('group_show', {'id': group['id']})
if group_dict.get('parent') == parent_name:
child_groups.append(group)
return child_groups
return groups
def get_available_groups(group_name=None):
'''
Get available Groups from the database
return: list of groups
'''
groups = _get_action('get_available_groups', {})
if group_name:
# Remove the group from the list
groups = [group for group in groups if group.get('name') != group_name]
groups = [
{'text': '', 'value': ''},
] + [
{'text': group.get('title'), 'value': group.get('name')}
for group in groups
]
return groups
def get_organizations():
'''
Get available Organization from the database
return: list of organizations
'''
groups = _get_action('organization_list', {'all_fields': True})
return groups
def get_group_title(group_name):
groups = get_groups()
group_title = None
for group in groups:
if group.get('name') == group_name:
group_title = group.get('title')
return group_title
def get_map_config():
map_config = {
'osm_url': config.get('ckanext.querytool.map_osm_url',
'https://cartodb-basemaps-{s}.global.ssl'
'.fastly.net/'
'light_nolabels/{z}/{x}/{y}{r}.png'),
'osm_attribute': config.get('ckanext.querytool.map_osm_attribute',
'© <a href='
'"http://www.openstreetmap.org/'
'copyright">OpenStreetMap</a> © '
'<a href="http://cartodb.com/'
'attributions">CartoDB</a>')
}
return json.dumps(map_config)
def parse_y_axis_columns(value):
# The input string can be:
# - falsy (None/empty/etc)
# - modern (as JSON of list of {name,alias})
# - legacy (as a comma separated string of column names)
# Falsy
if not value:
return []
# Modern
try:
columns = json.loads(value)
if not isinstance(columns, list):
raise TypeError()
return columns
# Legacy
except Exception:
names = value.split(',')
columns = map(lambda name: {'name': name, 'alias': ''}, names)
return columns
def pick_first_by_attr_value(items, attr, value, default=False):
for item in items:
if item.get(attr) == value:
return item
return default
def slugify(string):
string = string or ''
return re.sub(r'\W+', '_', string, flags=re.UNICODE).strip('_').lower()
def parse_json(string):
return json.loads(string)
def dump_json(value):
return json.dumps(value)
def get_dataset_url_path(url):
# Remove http://host:port/lang part
parts = url.split('/dataset')
if len(parts) == 1:
return ''
return '/dataset%s' % parts[1]
def get_all_reports(q=None):
groups = get_groups()
reports = _get_action('querytool_list_other', {'groups': groups})
if q:
querytool_search_results = querytool_search(query_string=q)
querytool_search_results_names = [
querytool.name for querytool in querytool_search_results
]
reports = [
querytool for querytool in reports if
querytool['name'] in querytool_search_results_names
]
return reports
def get_orphaned_reports():
groups = get_groups()
reports = _get_action('querytool_list_other', {'groups': groups})
orphaned_reports = []
all_group_names = [group['name'] for group in groups]
for report in reports:
if report.get('group') not in all_group_names:
orphaned_reports.append(report)
return orphaned_reports
def sort_by_orphaned_reports(reports, q=None):
orphaned_reports = get_orphaned_reports()
sorted_reports = []
# Reports in `orphaned_reports` will be sorted to the top of `reports`
reports = [report for report in reports if report not in orphaned_reports]
sorted_reports.extend(orphaned_reports)
sorted_reports.extend(reports)
if q:
querytool_search_results = querytool_search(query_string=q)
querytool_search_results_names = [
querytool.name for querytool in querytool_search_results
]
sorted_reports = [
querytool for querytool in sorted_reports if
querytool['name'] in querytool_search_results_names
]
return sorted_reports
def get_user_permission(userobj):
if not userobj:
return True
org = _get_action('organization_list_for_user', {'id': userobj.id})
group = _get_action('group_list_authz', {'id': userobj.id})
if len(org) == 0 and len(group) == 0:
return False
else:
return True
def get_orgs_for_user(userobj, org):
if not userobj:
orgs = get_organizations()
else:
orgs = _get_action('organization_list_for_user', {'id': userobj.id})
org_names = [o['name'] for o in orgs]
if org in org_names:
return True
else:
return False
def get_all_orgs_for_user(userobj):
if not userobj:
orgs = get_organizations()
else:
orgs = _get_action('organization_list_for_user', {'id': userobj.id})
if orgs:
return orgs
def get_organization(org_id):
return _get_action('organization_show', {'id': org_id}) if org_id else []
def get_datasets_for_user(userobj, package_name):
package = _get_action('package_show', {'name_or_id': package_name})
org_access = None
if package.get('organization'):
org_access = get_orgs_for_user(userobj, package['organization']['name'])
if org_access or (userobj and userobj.sysadmin):
return True
for group in package.get('groups'):
group_access = get_groups_for_user(userobj, group['name'])
if group_access:
return True
return False
def get_groups_for_user(userobj, group=None):
if not userobj:
return False
groups = _get_action('group_list_authz', {'id': userobj.id})
group_names = [g['name'] for g in groups]
if group is None:
return group_names
if len(groups) != 0 and group in group_names:
return True
else:
return False
def get_is_admin_or_editor_of_any_group(userobj):
if userobj is None:
return False
groups = _get_action('group_list_authz', {'id': userobj.id})
is_admin_or_editor = [get_user_permission_type(userobj, group['id']) for group in groups]
if len(groups) != 0 and any(t in ['admin', 'editor'] for t in is_admin_or_editor):
return True
else:
return False
def get_edit_permission_for_user(userobj, group):
if c.userobj:
if c.userobj.sysadmin:
return True
try:
member_list = toolkit.get_action('member_list')(
{}, {'id': group}, permissions_check=True
)
if c.userobj.id in member_list:
return True
return False
except logic.NotFound:
return False
def get_user_permission_type(userobj, group):
if c.userobj:
if c.userobj.sysadmin:
return 'admin'
try:
member_list = toolkit.get_action('member_list')(
{}, {'id': group}, permissions_check=True
)
for m in member_list:
if c.userobj.id in m:
if 'admin' in m:
return 'admin'
if 'member' in m:
return 'member'
if 'editor' in m:
return 'editor'
except (logic.NotFound, logic.NotAuthorized):
return
def get_all_org_permissions(userobj):
orgs = get_all_orgs_for_user(userobj) or []
permissions = [
get_user_permission_type(userobj, org.get('id')) for org in orgs]
return any(p in ['admin', 'editor'] for p in permissions)
def get_querytool_get_chart_colors(data):
try:
data = json.loads(data)
except:
data = []
return data
def convert_bar_width(bar_width, convert=True, type='bar'):
if not bar_width or bar_width == 'None':
if type == 'donut':
return 4.0
else:
return 5.0
if convert:
return str(round(float(bar_width) * 10, 2))
else:
return str(round(float(bar_width) / 10, 2))
def get_cookie_control_config():
cookie_control_config = {}
enabled = config.get(
'ckanext.querytool.cc.enabled', 'false').lower()
enabled = enabled if enabled in ['true', 'false'] else 'false'
cookie_control_config['enabled'] = enabled
api_key = config.get(
'ckanext.querytool.cc.api_key', '')
cookie_control_config['api_key'] = api_key
license_type = config.get(
'ckanext.querytool.cc.license_type', '')
cookie_control_config['license_type'] = license_type
popup_position = config.get(
'ckanext.querytool.cc.popup_position', '')
cookie_control_config['popup_position'] = popup_position
theme_color = config.get(
'ckanext.querytool.cc.theme_color', '')
cookie_control_config['theme_color'] = theme_color
initial_state = config.get(
'ckanext.querytool.cc.initial_state', '')
cookie_control_config['initial_state'] = initial_state
text = json.dumps({
"title": config.get('ckanext.querytool.cc.text.title', ''),
"intro": config.get('ckanext.querytool.cc.text.intro', ''),
"accept": config.get('ckanext.querytool.cc.text.accept', ''),
"reject": config.get('ckanext.querytool.cc.text.reject', ''),
"on": config.get('ckanext.querytool.cc.text.on', ''),
"off": config.get('ckanext.querytool.cc.text.off', ''),
"necessary_title": config.get('ckanext.querytool.cc.text.necessary_title', ''),
"necessary_description": config.get('ckanext.querytool.cc.text.necessary_description', ''),
"analytical_title": config.get('ckanext.querytool.cc.text.analytical_title', ''),
"analytical_description": config.get('ckanext.querytool.cc.text.analytical_description', '')
})
cookie_control_config['text'] = text
return cookie_control_config
def get_recaptcha_config():