예제 #1
0
    def __init__(self, **kwargs):
        """
        :param name: the name of the column
        :param attr: What attribute to use, defaults to same as name. Follows django conventions to access properties of properties, so "foo__bar" is equivalent to the python code `foo.bar`. This parameter is based on the variable name of the Column if you use the declarative style of creating tables.
        :param display_name: the text of the header for this column. By default this is based on the `name` parameter so normally you won't need to specify it.
        :param css_class: CSS class of the header
        :param url: URL of the header. This should only be used if "sorting" is off.
        :param title: title/tool tip of header
        :param show: set this to False to hide the column
        :param sortable: set this to False to disable sorting on this column
        :param sort_key: string denoting what value to use as sort key when this column is selected for sorting. (Or callable when rendering a table from list.)
        :param sort_default_desc: Set to True to make table sort link to sort descending first.
        :param group: string describing the group of the header. If this parameter is used the header of the table now has two rows. Consecutive identical groups on the first level of the header are joined in a nice way.
        :param auto_rowspan: enable automatic rowspan for this column. To join two cells with rowspan, just set this auto_rowspan to True and make those two cells output the same text and we'll handle the rest.
        :param cell__template: name of a template file. The template gets arguments: `table`, `bound_column`, `bound_row`, `row` and `value`.
        :param cell__value: string or callable that receives kw arguments: `table`, `column` and `row`. This is used to extract which data to display from the object.
        :param cell__format: string or callable that receives kw arguments: `table`, `column`, `row` and `value`. This is used to convert the extracted data to html output (use `mark_safe`) or a string.
        :param cell__attrs: dict of attr name to callables that receive kw arguments: `table`, `column`, `row` and `value`.
        :param cell__url: callable that receives kw arguments: `table`, `column`, `row` and `value`.
        :param cell__url_title: callable that receives kw arguments: `table`, `column`, `row` and `value`.
        """

        setdefaults(kwargs, dict(
            bulk=(Struct(extract_subkeys(kwargs, 'bulk', defaults=dict(show=False)))),
            query=(Struct(extract_subkeys(kwargs, 'query', defaults=dict(show=False)))),
            extra=(Struct(extract_subkeys(kwargs, 'extra'))),
        ))

        kwargs = {k: v for k, v in kwargs.items() if not k.startswith('bulk__') and not k.startswith('query__') and not k.startswith('extra__')}

        assert isinstance(kwargs['bulk'], dict)
        assert isinstance(kwargs['query'], dict)
        assert isinstance(kwargs['extra'], dict)

        super(Column, self).__init__(**kwargs)
예제 #2
0
    def __init__(self, data, request=None, columns=None, **kwargs):
        """
        :param data: a list of QuerySet of objects
        :param columns: (use this only when not using the declarative style) a list of Column objects
        :param attrs: dict of strings to string/callable of HTML attributes to apply to the table
        :param row__attrs: dict of strings to string/callable of HTML attributes to apply to the row. Callables are passed the row as argument.
        :param row__template: name of template to use for rendering the row
        :param bulk_filter: filters to apply to the QuerySet before performing the bulk operation
        :param bulk_exclude: exclude filters to apply to the QuerySet before performing the bulk operation
        :param sortable: set this to false to turn off sorting for all columns
        :param filter__template:
        :param header__template:
        :param links__template:

        """
        assert columns is not None, 'columns must be specified. It is only set to None to make linting tools not give false positives on the declarative style'

        self._has_prepared = False

        self.data = data
        self.request = request

        if isinstance(self.data, QuerySet):
            kwargs['model'] = data.model

        if isinstance(columns, dict):
            for name, column in columns.items():
                dict.__setitem__(column, 'name', name)
            self.columns = columns.values()
        else:
            self.columns = columns
            """:type : list of Column"""

        self.bound_columns = None
        self.shown_bound_columns = None
        self.bound_column_by_name = None

        self.Meta = self.get_meta()
        self.Meta.update(**kwargs)

        self.header_levels = None
        self.query = None
        self.query_form = None
        self.query_error = None
        self.bulk_form = None

        self.query_kwargs = extract_subkeys(kwargs, 'query')
        self.bulk_kwargs = extract_subkeys(kwargs, 'bulk')
예제 #3
0
    def __init__(self, table, row, row_index):
        self.table = table
        """ :type : Table """
        self.row = row
        """ :type : object """
        self.row_index = row_index

        args = Struct(evaluate_recursive(extract_subkeys(table.Meta, 'row'), table=table, row=row))
        self.template = args.template
        self.attrs = args.attrs
예제 #4
0
def create_or_edit_object(
    request,
    model,
    is_create,
    instance=None,
    redirect_to=None,
    on_save=lambda **kwargs: None,
    render=render_to_string,
    redirect=lambda request, redirect_to: HttpResponseRedirect(redirect_to),
    **kwargs
):
    kwargs.setdefault("form__class", Form.from_model)
    kwargs.setdefault("template_name", "tri_form/create_or_edit_object_block.html")
    p = extract_subkeys(
        kwargs,
        "form",
        defaults={"model": model, "instance": instance, "data": request.POST if request.method == "POST" else None},
    )
    form = kwargs["form__class"](**p)

    # noinspection PyProtectedMember
    model_verbose_name = kwargs.get("model_verbose_name", model._meta.verbose_name.replace("_", " "))

    if request.method == "POST" and form.is_valid():
        if is_create:
            assert instance is None
            instance = model()
        form.apply(instance)
        instance.save()

        kwargs["instance"] = instance
        on_save(**kwargs)

        return create_or_edit_object_redirect(is_create, redirect_to, request, redirect)

    c = {"form": form, "is_create": is_create, "object_name": model_verbose_name}
    c.update(kwargs.get("render__context", {}))

    kwargs_for_render = extract_subkeys(
        kwargs, "render", {"context_instance": RequestContext(request, c), "template_name": kwargs["template_name"]}
    )
    return render(**kwargs_for_render)
예제 #5
0
    def _prepare_evaluate_members(self):
        self.shown_bound_columns = [bound_column for bound_column in self.bound_columns if bound_column.show]

        self.Meta = evaluate_recursive(self.Meta, table=self)

        if 'class' in self.Meta.attrs and isinstance(self.Meta.attrs['class'], basestring):
            self.Meta.attrs['class'] = {k: True for k in self.Meta.attrs['class'].split(' ')}
        else:
            self.Meta.attrs['class'] = {}
        self.Meta.attrs.update(extract_subkeys(self.Meta, 'attrs'))
        self.Meta.attrs = collect_namespaces(self.Meta.attrs)

        if 'class' in self.Meta.row__attrs and isinstance(self.Meta.row__attrs['class'], basestring):
            self.Meta.row__attrs['class'] = {k: True for k in self.Meta.row__attrs['class'].split(' ')}
        else:
            self.Meta.row__attrs['class'] = {}
        self.Meta.row__attrs.update(extract_subkeys(self.Meta, 'row__attrs'))
        self.Meta.row__attrs = collect_namespaces(self.Meta.row__attrs)

        if not self.Meta.sortable:
            for bound_column in self.bound_columns:
                bound_column.sortable = False
예제 #6
0
 def bound_rows(self):
     row_params = extract_subkeys(self.Meta, 'row')
     for i, row in enumerate(self.data):
         yield BoundRow(table=self, row=row, row_index=i, **row_params)