def get(self, request, view_id, field_options): """ Lists all the rows of a grid view, paginated either by a page or offset/limit. If the limit get parameter is provided the limit/offset pagination will be used else the page number pagination. Optionally the field options can also be included in the response if the the `field_options` are provided in the include GET parameter. """ search = request.GET.get('search') view_handler = ViewHandler() view = view_handler.get_view(view_id, GridView) view.table.database.group.has_user(request.user, raise_error=True, allow_if_template=True) model = view.table.get_model() queryset = model.objects.all().enhance_by_fields() # Applies the view filters and sortings to the queryset if there are any. queryset = view_handler.apply_filters(view, queryset) queryset = view_handler.apply_sorting(view, queryset) if search: queryset = queryset.search_all_fields(search) if 'count' in request.GET: return Response({'count': queryset.count()}) if LimitOffsetPagination.limit_query_param in request.GET: paginator = LimitOffsetPagination() else: paginator = PageNumberPagination() page = paginator.paginate_queryset(queryset, request, self) serializer_class = get_row_serializer_class(model, RowSerializer, is_response=True) serializer = serializer_class(page, many=True) response = paginator.get_paginated_response(serializer.data) if field_options: # The serializer has the GridViewFieldOptionsField which fetches the # field options from the database and creates them if they don't exist, # but when added to the context the fields don't have to be fetched from # the database again when checking if they exist. context = { 'fields': [o['field'] for o in model._field_objects.values()] } serialized_view = GridViewSerializer(view, context=context).data response.data['field_options'] = serialized_view['field_options'] return response
def grid_view_field_options_updated(sender, grid_view, user, **kwargs): table_page_type = page_registry.get('table') transaction.on_commit(lambda: table_page_type.broadcast( { 'type': 'grid_view_field_options_updated', 'grid_view_id': grid_view.id, 'grid_view_field_options': GridViewSerializer(grid_view).data['field_options'] }, getattr(user, 'web_socket_id', None), table_id=grid_view.table_id))
def grid_view_field_options_updated(sender, grid_view, user, **kwargs): table_page_type = page_registry.get("table") transaction.on_commit(lambda: table_page_type.broadcast( { "type": "grid_view_field_options_updated", "grid_view_id": grid_view.id, "grid_view_field_options": GridViewSerializer(grid_view).data["field_options"], }, getattr(user, "web_socket_id", None), table_id=grid_view.table_id, ))
def patch(self, request, view_id, data): """ Updates the field options for the provided grid view. The following example body data will only update the width of the FIELD_ID and leaves the others untouched. { FIELD_ID: { 'width': 200 } } """ handler = ViewHandler() view = handler.get_view(request.user, view_id, GridView) handler.update_grid_view_field_options(view, data['field_options']) return Response(GridViewSerializer(view).data)
def get(self, request, view_id, field_options): """ Lists all the rows of a grid view, paginated either by a page or offset/limit. If the limit get parameter is provided the limit/offset pagination will be used else the page number pagination. Optionally the field options can also be included in the response if the the `field_options` are provided in the includes GET parameter. """ view = ViewHandler().get_view(request.user, view_id, GridView) model = view.table.get_model() queryset = model.objects.all().enhance_by_fields().order_by('id') if LimitOffsetPagination.limit_query_param in request.GET: paginator = LimitOffsetPagination() else: paginator = PageNumberPagination() page = paginator.paginate_queryset(queryset, request, self) serializer_class = get_row_serializer_class(model, RowSerializer, is_response=True) serializer = serializer_class(page, many=True) response = paginator.get_paginated_response(serializer.data) if field_options: # The serializer has the GridViewFieldOptionsField which fetches the # field options from the database and creates them if they don't exist, # but when added to the context the fields don't have to be fetched from # the database again when checking if they exist. context = { 'fields': [o['field'] for o in model._field_objects.values()] } response.data.update( **GridViewSerializer(view, context=context).data) return response