def delete(self, request, table_id): """Deletes an existing table.""" TableHandler().delete_table( request.user, TableHandler().get_table(request.user, table_id)) return Response(status=204)
def patch(self, request, data, table_id): """Updates the values a table instance.""" table = TableHandler().update_table(request.user, TableHandler().get_table( request.user, table_id), name=data['name']) serializer = TableSerializer(table) return Response(serializer.data)
def patch(self, request, data, table_id): """Updates the values a table instance.""" table = TableHandler().update_table( request.user, TableHandler().get_table(table_id), base_queryset=Table.objects.select_for_update(), name=data['name']) serializer = TableSerializer(table) return Response(serializer.data)
def test_delete_database_table(send_mock, data_fixture): user = data_fixture.create_user() user_2 = data_fixture.create_user() group = data_fixture.create_group(user=user) database = data_fixture.create_database_application(group=group) table = data_fixture.create_database_table(user=user, database=database) handler = TableHandler() with pytest.raises(UserNotInGroup): handler.delete_table(user=user_2, table=table) assert Table.objects.all().count() == 1 assert f"database_table_{table.id}" in connection.introspection.table_names( ) table_id = table.id handler.delete_table(user=user, table=table) send_mock.assert_called_once() assert send_mock.call_args[1]["table_id"] == table_id assert send_mock.call_args[1]["user"].id == user.id assert Table.objects.all().count() == 0 assert f"database_table_{table.id}" not in connection.introspection.table_names( )
def get(self, request, table_id): """Responds with a serialized table instance.""" table = TableHandler().get_table(table_id) table.database.group.has_user(request.user, raise_error=True) serializer = TableSerializer(table) return Response(serializer.data)
def get(self, request, table_id): """ Lists all the rows of the given table id paginated. It is also possible to provide a search query. """ table = TableHandler().get_table(request.user, table_id) TokenHandler().check_table_permissions(request, 'read', table, False) model = table.get_model() search = request.GET.get('search') order_by = request.GET.get('order_by') queryset = model.objects.all().enhance_by_fields().order_by('id') if search: queryset = queryset.search_all_fields(search) if order_by: queryset = queryset.order_by_fields_string(order_by) paginator = PageNumberPagination( limit_page_size=settings.ROW_PAGE_SIZE_LIMIT) page = paginator.paginate_queryset(queryset, request, self) serializer_class = get_row_serializer_class(model, RowSerializer, is_response=True) serializer = serializer_class(page, many=True) return paginator.get_paginated_response(serializer.data)
def get(self, request, table_id, filters, sortings): """ Responds with a list of serialized views that belong to the table if the user has access to that group. """ table = TableHandler().get_table(request.user, table_id) views = View.objects.filter(table=table).select_related('content_type') if filters: views = views.prefetch_related('viewfilter_set') if sortings: views = views.prefetch_related('viewsort_set') data = [ view_type_registry.get_serializer( view, ViewSerializer, filters=filters, sortings=sortings ).data for view in views ] return Response(data)
def post(self, request, table_id): """ Creates a new row for the given table_id. Also the post data is validated according to the tables field types. """ table = TableHandler().get_table(table_id) TokenHandler().check_table_permissions(request, 'create', table, False) model = table.get_model() validation_serializer = get_row_serializer_class(model) data = validate_data(validation_serializer, request.data) before_id = request.GET.get('before') before = (RowHandler().get_row(request.user, table, before_id, model) if before_id else None) row = RowHandler().create_row(request.user, table, data, model, before=before) serializer_class = get_row_serializer_class(model, RowSerializer, is_response=True) serializer = serializer_class(row) return Response(serializer.data)
def post(self, request, data, database_id): """Updates to order of the tables in a table.""" database = CoreHandler().get_application( database_id, base_queryset=Database.objects) TableHandler().order_tables(request.user, database, data["table_ids"]) return Response(status=204)
def get(self, request, table_id, filters, sortings): """ Responds with a list of serialized views that belong to the table if the user has access to that group. """ table = TableHandler().get_table(table_id) table.database.group.has_user( request.user, raise_error=True, allow_if_template=True ) views = View.objects.filter(table=table).select_related("content_type") if filters: views = views.prefetch_related("viewfilter_set") if sortings: views = views.prefetch_related("viewsort_set") data = [ view_type_registry.get_serializer( view, ViewSerializer, filters=filters, sortings=sortings ).data for view in views ] return Response(data)
def patch(self, request, table_id, row_id): """ Updates the row with the given row_id for the table with the given table_id. Also the post data is validated according to the tables field types. """ table = TableHandler().get_table(request.user, table_id) TokenHandler().check_table_permissions(request, 'update', table, False) # Small side effect of generating the model for only the fields that need to # change is that the response it not going to contain the other fields. It is # however much faster because it doesn't need to get the specific version of # all the field objects. field_ids = RowHandler().extract_field_ids_from_dict(request.data) model = table.get_model(field_ids=field_ids) validation_serializer = get_row_serializer_class(model) data = validate_data(validation_serializer, request.data) row = RowHandler().update_row(request.user, table, row_id, data, model) serializer_class = get_row_serializer_class(model, RowSerializer, is_response=True) serializer = serializer_class(row) return Response(serializer.data)
def post(self, request, data, table_id): """Creates a new view for a user.""" table = TableHandler().get_table(request.user, table_id) view = ViewHandler().create_view( request.user, table, data.pop('type'), **data) serializer = view_type_registry.get_serializer(view, ViewSerializer) return Response(serializer.data)
def delete(self, request, table_id, row_id): """ Deletes an existing row with the given row_id for table with the given table_id. """ table = TableHandler().get_table(request.user, table_id) RowHandler().delete_row(request.user, table, row_id) return Response(status=204)
def post(self, request, data, table_id): """Creates a new field for a table.""" type_name = data.pop('type') table = TableHandler().get_table(request.user, table_id) field = FieldHandler().create_field(request.user, table, type_name, **data) serializer = field_type_registry.get_serializer(field, FieldSerializer) return Response(serializer.data)
def post(self, request, data, table_id, filters, sortings): """Creates a new view for a user.""" table = TableHandler().get_table(table_id) view = ViewHandler().create_view(request.user, table, data.pop("type"), **data) serializer = view_type_registry.get_serializer( view, ViewSerializer, filters=filters, sortings=sortings ) return Response(serializer.data)
def delete(self, request, table_id, row_id): """ Deletes an existing row with the given row_id for table with the given table_id. """ table = TableHandler().get_table(table_id) TokenHandler().check_table_permissions(request, 'delete', table, False) RowHandler().delete_row(request.user, table, row_id) return Response(status=204)
def test_table_updated(mock_broadcast_to_group, data_fixture): user = data_fixture.create_user() table = data_fixture.create_database_table(user=user) table = TableHandler().update_table(user=user, table=table, name='Test') mock_broadcast_to_group.delay.assert_called_once() args = mock_broadcast_to_group.delay.call_args assert args[0][0] == table.database.group_id assert args[0][1]['type'] == 'table_updated' assert args[0][1]['table_id'] == table.id assert args[0][1]['table']['id'] == table.id
def test_table_updated(mock_broadcast_to_group, data_fixture): user = data_fixture.create_user() table = data_fixture.create_database_table(user=user) table = TableHandler().update_table(user=user, table=table, name="Test") mock_broadcast_to_group.delay.assert_called_once() args = mock_broadcast_to_group.delay.call_args assert args[0][0] == table.database.group_id assert args[0][1]["type"] == "table_updated" assert args[0][1]["table_id"] == table.id assert args[0][1]["table"]["id"] == table.id
def post(self, request, data, database_id): """Creates a new table in a database.""" database = CoreHandler().get_application( request.user, database_id, base_queryset=Database.objects) table = TableHandler().create_table(request.user, database, fill_initial=True, name=data['name']) serializer = TableSerializer(table) return Response(serializer.data)
def test_tables_reordered(mock_broadcast_to_channel_group, data_fixture): user = data_fixture.create_user() database = data_fixture.create_database_application(user=user) table = data_fixture.create_database_table(database=database) TableHandler().order_tables(user=user, database=database, order=[table.id]) mock_broadcast_to_channel_group.delay.assert_called_once() args = mock_broadcast_to_channel_group.delay.call_args assert args[0][0] == table.database.group_id assert args[0][1]["type"] == "tables_reordered" assert args[0][1]["database_id"] == database.id assert args[0][1]["order"] == [table.id]
def test_fill_example_table_data(data_fixture): user = data_fixture.create_user() database = data_fixture.create_database_application(user=user) table_handler = TableHandler() table_handler.create_table(user, database, fill_example=True, name='Table 1') assert Table.objects.all().count() == 1 assert GridView.objects.all().count() == 1 assert TextField.objects.all().count() == 1 assert LongTextField.objects.all().count() == 1 assert BooleanField.objects.all().count() == 1 assert GridViewFieldOptions.objects.all().count() == 2
def test_table_deleted(mock_broadcast_to_users, data_fixture): user = data_fixture.create_user() table = data_fixture.create_database_table(user=user) table_id = table.id database_id = table.database_id TableHandler().delete_table(user=user, table=table) mock_broadcast_to_users.delay.assert_called_once() args = mock_broadcast_to_users.delay.call_args assert args[0][0] == table.database.group_id assert args[0][1]["type"] == "table_deleted" assert args[0][1]["database_id"] == database_id assert args[0][1]["table_id"] == table_id
def get(self, request, table_id): """ Responds with a list of serialized fields that belong to the table if the user has access to that group. """ table = TableHandler().get_table(request.user, table_id) fields = Field.objects.filter(table=table).select_related('content_type') data = [ field_type_registry.get_serializer(field, FieldSerializer).data for field in fields ] return Response(data)
def test_get_database_table(data_fixture): user = data_fixture.create_user() table = data_fixture.create_database_table(user=user) table_2 = data_fixture.create_database_table() handler = TableHandler() with pytest.raises(UserNotInGroupError): handler.get_table(user=user, table_id=table_2.id) with pytest.raises(TableDoesNotExist): handler.get_table(user=user, table_id=99999) table_copy = handler.get_table(user=user, table_id=table.id) assert table_copy.id == table.id
def prepare_values(self, values, user): """ This method checks if the provided link row table is an int because then it needs to be converted to a table instance. """ if 'link_row_table' in values and isinstance(values['link_row_table'], int): from baserow.contrib.database.table.handler import TableHandler values['link_row_table'] = TableHandler().get_table( user, values['link_row_table']) return values
def test_order_tables(send_mock, data_fixture): user = data_fixture.create_user() user_2 = data_fixture.create_user() database = data_fixture.create_database_application(user=user) table_1 = data_fixture.create_database_table(database=database, order=1) table_2 = data_fixture.create_database_table(database=database, order=2) table_3 = data_fixture.create_database_table(database=database, order=3) handler = TableHandler() with pytest.raises(UserNotInGroup): handler.order_tables(user=user_2, database=database, order=[]) with pytest.raises(TableNotInDatabase): handler.order_tables(user=user, database=database, order=[0]) handler.order_tables(user=user, database=database, order=[table_3.id, table_2.id, table_1.id]) table_1.refresh_from_db() table_2.refresh_from_db() table_3.refresh_from_db() assert table_1.order == 3 assert table_2.order == 2 assert table_3.order == 1 send_mock.assert_called_once() assert send_mock.call_args[1]["database"].id == database.id assert send_mock.call_args[1]["user"].id == user.id assert send_mock.call_args[1]["order"] == [ table_3.id, table_2.id, table_1.id ] handler.order_tables(user=user, database=database, order=[table_1.id, table_3.id, table_2.id]) table_1.refresh_from_db() table_2.refresh_from_db() table_3.refresh_from_db() assert table_1.order == 1 assert table_2.order == 3 assert table_3.order == 2 handler.order_tables(user=user, database=database, order=[table_1.id]) table_1.refresh_from_db() table_2.refresh_from_db() table_3.refresh_from_db() assert table_1.order == 1 assert table_2.order == 0 assert table_3.order == 0
def prepare_values(self, values, user): """ This method checks if the provided link row table is an int because then it needs to be converted to a table instance. """ if "link_row_table" in values and isinstance(values["link_row_table"], int): from baserow.contrib.database.table.handler import TableHandler table = TableHandler().get_table(values["link_row_table"]) table.database.group.has_user(user, raise_error=True) values["link_row_table"] = table return values
def post(self, request, data, table_id): """Creates a new field for a table.""" type_name = data.pop('type') field_type = field_type_registry.get(type_name) table = TableHandler().get_table(request.user, table_id) # Because each field type can raise custom exceptions while creating the # field we need to be able to map those to the correct API exceptions which are # defined in the type. with field_type.map_api_exceptions(): field = FieldHandler().create_field(request.user, table, type_name, **data) serializer = field_type_registry.get_serializer(field, FieldSerializer) return Response(serializer.data)
def can_add(self, user, web_socket_id, table_id, **kwargs): """ The user should only have access to this page if the table exists and if he has access to the table. """ if not table_id: return False try: handler = TableHandler() handler.get_table(user, table_id) except (UserNotInGroupError, TableDoesNotExist): return False return True
def post(self, request, table_id): """ Starts a new export job for the provided table, view, export type and options. """ table = TableHandler().get_table(table_id) table.database.group.has_user(request.user, raise_error=True) option_data = _validate_options(request.data) view_id = option_data.pop("view_id", None) view = ViewHandler().get_view(view_id) if view_id else None job = ExportHandler.create_and_start_new_job(request.user, table, view, option_data) return Response(ExportJobSerializer(job).data)