Esempio n. 1
0
 def generate_bound_variables():
     for x in self.variables:
         yield BoundVariable(**merged(
             Struct(x),
             query=self,
             attr=x.attr if x.attr is not MISSING else x.name
         ))
Esempio n. 2
0
    def form(self):
        """
        Create a form and validate input based on a request.
        """
        if self._form:
            return self._form
        fields = []

        if any(v.freetext for v in self.variables):
            fields.append(Field(name=FREETEXT_SEARCH_NAME, label='Search', required=False))

        for variable in self.bound_variables:
            if variable.gui is not None and variable.gui.show:
                # pass gui__* parameters to the GUI component
                assert variable.name is not MISSING
                assert variable.attr is not MISSING
                params = merged(variable.gui, name=variable.name, attr=variable.attr)
                fields.append(params.pop('class')(**params))

        form = Form(
            request=self.request,
            fields=fields,
            endpoint_dispatch_prefix='__'.join([self.endpoint_dispatch_prefix, 'gui']),
            **self.gui_kwargs)
        form.tri_query = self
        form.tri_query_advanced_value = request_data(self.request).get(ADVANCED_QUERY_PARAM, '')
        self._form = form
        return form
Esempio n. 3
0
def test_merged_with_kwarg_constructor(Struct):
    class MyStruct(Struct):
        def __init__(self, **kwargs):
            super(MyStruct, self).__init__(**kwargs)

    s = MyStruct(foo='foo')
    assert MyStruct(foo='foo', bar='bar') == merged(s, dict(bar='bar'))
Esempio n. 4
0
 def bind_columns():
     for index, column in enumerate(self.columns):
         values = evaluate_recursive(Struct(column),
                                     table=self,
                                     column=column)
         values = merged(values, column=column, table=self, index=index)
         yield BoundColumn(**values)
Esempio n. 5
0
    def __init__(self, request=None, variables=None, **kwargs):  # variables=None to make pycharm tooling not confused
        """
        :type variables: list of Variable
        :type request: django.http.request.HttpRequest
        """
        self.request = request

        assert variables is not None
        if isinstance(variables, dict):  # Declarative case
            self.variables = [merged(variable, name=name) for name, variable in variables.items()]
        else:
            self.variables = variables

        bound_variables = [BoundVariable(**merged(Struct(x), query=self)) for x in self.variables]
        bound_variables = [evaluate_recursive(x, query=self, variable=x) for x in bound_variables]
        self.bound_variables = filter_show_recursive(bound_variables)

        self.bound_variable_by_name = {variable.name: variable for variable in self.bound_variables}

        self.gui_kwargs = extract_subkeys(kwargs, 'gui')
Esempio n. 6
0
    def __init__(self, request=None, data=None, instance=None, fields=None, model=None, post_validation=None):
        """
        :type fields: list of Field
        :type data: dict[basestring, basestring]
        :type model: django.db.models.Model
        """
        self.request = request
        if data is None and request:
            data = request.POST if request.method == 'POST' else request.GET

        if isinstance(fields, dict):  # Declarative case
            fields = [merged(field, dict(name=name)) for name, field in fields.items()]
        self.fields = sort_after([BoundField(field, self) for field in fields])

        if instance is not None:
            for field in self.fields:
                if field.attr:
                    initial = getattr_path(instance, field.attr)
                    if field.is_list:
                        field.initial_list = initial
                    else:
                        field.initial = initial

        if data:
            for field in self.fields:
                if field.is_list:
                    try:
                        # noinspection PyUnresolvedReferences
                        raw_data_list = data.getlist(field.name)
                    except AttributeError:  # pragma: no cover
                        raw_data_list = data.get(field.name)
                    if raw_data_list and field.strip_input:
                        raw_data_list = [x.strip() for x in raw_data_list]
                    if raw_data_list is not None:
                        field.raw_data_list = raw_data_list
                else:
                    field.raw_data = data.get(field.name)
                    if field.raw_data and field.strip_input:
                        field.raw_data = field.raw_data.strip()

        if post_validation is not None:
            self.post_validation = post_validation
        self.fields_by_name = {field.name: field for field in self.fields}
        self.style = None
        self.model = model
        self._valid = None
        self.errors = []
        self.should_parse = bool(data)
        self.evaluate()
        self.is_valid()
Esempio n. 7
0
    def form(self, request):
        """
        Create a form and validate input based on a request.
        """
        fields = []

        if any(v.freetext for v in self.variables):
            fields.append(Field(name=FREETEXT_SEARCH_NAME, label='Search', required=False))

        for variable in self.variables:
            if variable.gui.show:
                # pass gui__* parameters to the GUI component
                params = merged(variable.gui, name=variable.name)
                fields.append(params.pop('class')(**params))

        form = Form(request=request, fields=fields, **self.gui_kwargs)
        form.request = request
        form.tri_query = self
        form.tri_query_advanced_value = request_data(request).get(ADVANCED_QUERY_PARAM, '')
        return form
Esempio n. 8
0
 def bind_columns():
     for index, column in enumerate(self.columns):
         values = evaluate_recursive(Struct(column), table=self, column=column)
         values = merged(values, column=column, table=self, index=index)
         yield BoundColumn(**values)
Esempio n. 9
0
def test_merge_to_other_type(Struct):
    s1 = Struct(x=1)
    s2 = dict(y=2)
    m = merged(FrozenStruct(), s1, s2)
    assert FrozenStruct(x=1, y=2) == m
    assert isinstance(m, FrozenStruct)
Esempio n. 10
0
def test_merged(Struct):
    assert Struct(x=1, y=2) == merged(Struct(x=1), Struct(y=2))
    assert Struct(x=1, y=2) == merged(Struct(x=1), FrozenStruct(y=2))
    assert FrozenStruct(x=1, y=2) == merged(FrozenStruct(x=1), Struct(y=2))
    assert {} == merged()
    assert Struct(x=1, y=2) == merged(Struct(x=1), y=2)