示例#1
0
 def get_resource(self):
     """Return the rest API resource (View) for the data table"""
     try:
         return self.resource
     except AttributeError:
         model = self.model
         return resources.get_resource_for_model(model)
示例#2
0
 def get_resource(self):
     """Return the rest API resource (View) for the data table"""
     try:
         return self.resource
     except AttributeError:
         model = self.model
         return resources.get_resource_for_model(model)
示例#3
0
    def __init__(self, resource, rowlink=None, options=None, hidden=None, url=None, ordering=None,
                 format="json", filters=None, extra_args=None):
        """
        Default ordering is "id" if possible.

        @param resource: handler to base datatable on
        @type resource: AmCATResource

        @param hidden: hidden fields
        @type hidden: set

        @param filters: an optional list of selector/value pairs for filtering
        @extra_args: an optional list of field/value pairs for extra 'get' options
        """
        if inspect.isclass(resource) and issubclass(resource, Model):
            resource = get_resource_for_model(resource)
        self.resource = resource() if callable(resource) else resource
            
        self.options = options or dict()
        self.rowlink = rowlink or getattr(self.resource, "get_rowlink", lambda  : None)()
        self.ordering = ordering

        self.format = format
        self.hidden = set(hidden) if isinstance(hidden, collections.Iterable) else set()
        self.filters = filters or [] # list of name : value tuples for filtering
        self.extra_args = extra_args or [] # list of name : value tuples for GET arguments
        self.base_url = url if url is not None else self.resource.url
示例#4
0
    def __init__(self, resource, rowlink=None, rowlink_open_in="same", options=None, hidden=None, url=None,
                 ordering=None, format="json", filters=None, extra_args=None, url_kwargs=()):
        """
        Default ordering is "id" if possible.

        @param resource: handler to base datatable on
        @type resource: AmCATResource

        @param hidden: hidden fields
        @type hidden: set

        @param filters: an optional list of selector/value pairs for filtering
        @param extra_args: an optional list of field/value pairs for extra 'get' options
        @param url_kwargs: if a ViewSet is given, also provide url_kwargs which are used to determine the url
        """
        if inspect.isclass(resource) and issubclass(resource, Model):
            resource = get_resource_for_model(resource)
        self.resource = resource() if callable(resource) else resource

        self.options = options or dict()
        self.rowlink = rowlink or getattr(self.resource, "get_rowlink", lambda: None)()
        self.rowlink_open_in = rowlink_open_in
        self.ordering = ordering

        self.format = format
        self.hidden = set(hidden) if isinstance(hidden, collections.Iterable) else set()
        self.filters = filters or []  # list of name : value tuples for filtering
        self.extra_args = extra_args or []  # list of name : value tuples for GET arguments

        if url is None:
            if isinstance(self.resource, AmCATResource):
                url = self.resource.url
            else:
                url = self.resource.get_url(**dict(url_kwargs))
        self.base_url = url
示例#5
0
文件: metadata.py 项目: amcat/amcat
    def determine_metadata(self, request, view):
        from api.rest.resources import get_resource_for_model
        metadata = super(AmCATMetadata, self).determine_metadata(request, view)

        if self._get_model(view) is None:
            return metadata

        metadata['label'] = self.get_label(view)

        try:
            metadata['label'] = self.get_label(view)
        except ValueError:
            log.debug("No resource for model, returning minimal metadata.")
            return metadata

        serializer = view.get_serializer()
        if hasattr(serializer, "Meta") and  hasattr(serializer.Meta, "model"):
            model = serializer.Meta.model

            metadata['models'] = {
                name: get_resource_for_model(_get_model_by_field(model, name)).get_url()
                for (name, field) in serializer.get_fields().items()
                if hasattr(field, 'queryset')
            }

        metadata['filter_fields'] = list(view.get_filter_fields())
        metadata["fields"] = dict(self.get_metadata_fields(view))
        metadata["field_list"] = [field_name for field_name, _ in self.get_metadata_fields(view)]
        return metadata
示例#6
0
    def __init__(self,
                 resource,
                 rowlink=None,
                 rowlink_open_in="same",
                 options=None,
                 hidden=None,
                 url=None,
                 ordering=None,
                 format="json",
                 filters=None,
                 extra_args=None,
                 url_kwargs=()):
        """
        Default ordering is "id" if possible.

        @param resource: handler to base datatable on
        @type resource: AmCATResource

        @param hidden: hidden fields
        @type hidden: set

        @param filters: an optional list of selector/value pairs for filtering
        @param extra_args: an optional list of field/value pairs for extra 'get' options
        @param url_kwargs: if a ViewSet is given, also provide url_kwargs which are used to determine the url
        """
        if inspect.isclass(resource) and issubclass(resource, Model):
            resource = get_resource_for_model(resource)
        self.resource = resource() if callable(resource) else resource

        self.options = options or dict()
        self.rowlink = rowlink or getattr(self.resource, "get_rowlink",
                                          lambda: None)()
        self.rowlink_open_in = rowlink_open_in
        self.ordering = ordering

        self.format = format
        self.hidden = set(hidden) if isinstance(
            hidden, collections.Iterable) else set()
        self.filters = filters or [
        ]  # list of name : value tuples for filtering
        self.extra_args = extra_args or [
        ]  # list of name : value tuples for GET arguments

        if url is None:
            if isinstance(self.resource, AmCATResource):
                url = self.resource.url
            else:
                url = self.resource.get_url(**dict(url_kwargs))
        self.base_url = url
示例#7
0
    def __init__(self,
                 resource,
                 rowlink=None,
                 options=None,
                 hidden=None,
                 url=None,
                 ordering=None,
                 format="json",
                 filters=None,
                 extra_args=None):
        """
        Default ordering is "id" if possible.

        @param resource: handler to base datatable on
        @type resource: AmCATResource

        @param hidden: hidden fields
        @type hidden: set

        @param filters: an optional list of selector/value pairs for filtering
        @extra_args: an optional list of field/value pairs for extra 'get' options
        """
        if inspect.isclass(resource) and issubclass(resource, Model):
            resource = get_resource_for_model(resource)
        self.resource = resource() if callable(resource) else resource

        self.options = options or dict()
        self.rowlink = rowlink or getattr(self.resource, "get_rowlink",
                                          lambda: None)()
        self.ordering = ordering

        self.format = format
        self.hidden = set(hidden) if isinstance(
            hidden, collections.Iterable) else set()
        self.filters = filters or [
        ]  # list of name : value tuples for filtering
        self.extra_args = extra_args or [
        ]  # list of name : value tuples for GET arguments
        self.base_url = url if url is not None else self.resource.url