• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalHistoryMuseum / ckanext-graph / #131

04 Nov 2024 11:50AM UTC coverage: 9.607%. Remained the same
#131

push

coveralls-python

web-flow
merge: PR #35 from dev

Weekly release 2024-11-04

0 of 5 new or added lines in 3 files covered. (0.0%)

11 existing lines in 3 files now uncovered.

22 of 229 relevant lines covered (9.61%)

0.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/ckanext/graph/logic/validators.py
1
# !/usr/bin/env python
2
# encoding: utf-8
3
#
4
# This file is part of ckanext-graph
5
# Created by the Natural History Museum in London, UK
6

7
from ckan.plugins import toolkit
×
8
from sqlalchemy.exc import DataError
×
9

NEW
10
from ckanext.graph.lib import utils
×
11

12

13
def is_boolean(value, context):
×
14
    """
15
    Validate a field as a boolean. Assuming missing value means false.
16

17
    :param value:
18
    :param context:
19
    """
20

21
    if isinstance(value, bool):
×
22
        return value
×
23
    elif isinstance(value, str) and value.lower() in ['true', 'yes', 't', 'y', '1']:
×
24
        return True
×
25
    elif isinstance(value, str) and value.lower() in ['false', 'no', 'f', 'n', '0']:
×
26
        return False
×
27
    elif isinstance(value, type(toolkit.missing)):
×
28
        return False
×
29
    else:
30
        raise toolkit.Invalid(
×
31
            toolkit._(
32
                'Value must a true/false value (ie. true/yes/t/y/1 or false/no/f/n/0)'
33
            )
34
        )
35

36

37
def in_list(list_possible_values):
×
38
    """
39
    Validator that checks that the input value is one of the given possible values.
40

41
    :param list_possible_values: function that returns list of possible values for
42
        validated field
43
    """
44

45
    def validate(key, data, errors, context):
×
UNCOV
46
        if not data[key] in list_possible_values:
×
UNCOV
47
            raise toolkit.Invalid(f'"{data[key]}" is not a valid parameter')
×
48

49
    return validate
×
50

51

52
def is_date_castable(value, context):
×
53
    """
54
    Validator to ensure the date is castable to a date field.
55

56
    :param value:
57
    :param context:
58
    """
59

60
    if value:
×
61
        fields = utils.get_datastore_field_types()
×
62

63
        field_type = fields[value]
×
64

65
        if field_type == 'date':
×
66
            return value
×
67
        else:
NEW
68
            script = """
×
69
            if (doc['data.{date_field_name}'].value != null) {{
70
             try {{
71
              new SimpleDateFormat('yyyy-MM-dd').parse(doc['data.{date_field_name}'].value);
72
              return true;
73
             }} catch (Exception e) {{
74
              return false;
75
             }}
76
            }} else {{
77
             return false;
78
            }}
79
            """.format(date_field_name=value)
80

81
            data_dict = {
×
82
                'search': {
83
                    'query': {
84
                        'bool': {'filter': {'script': {'script': {'source': script}}}}
85
                    }
86
                },
87
                'resource_id': toolkit.c.resource['id'],
88
            }
89

90
            failure = toolkit.Invalid(
×
91
                f"Field {value} cannot be cast into a date. Are you sure it's a date field?"
92
            )
93

94
            try:
×
95
                result = toolkit.get_action('datastore_search_raw')({}, data_dict)
×
96
                if result['total'] == 0:
×
97
                    raise failure
×
98
            except DataError:
×
99
                raise failure
×
100

101
    return value
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc