Skip to content

Commit

Permalink
Merge branch 'jowilf:main' into locale/row-action/delete/submit_btn_text
Browse files Browse the repository at this point in the history
  • Loading branch information
hasansezertasan authored Dec 29, 2023
2 parents 9f057e5 + 3a3df85 commit ba01a3e
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 30 deletions.
3 changes: 2 additions & 1 deletion docs/advanced/base-model-view/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class PostView(BaseModelView):
```

!!! important

`identity` is used to identify the model associated to this view and should be unique.

## Primary key
Expand Down Expand Up @@ -97,6 +98,7 @@ Finally, you need to implement these CRUD methods:
* [delete()][starlette_admin.BaseModelView.delete]

## Full example

```python
from dataclasses import dataclass
from typing import Any, Dict, Iterable, List, Optional, Union
Expand Down Expand Up @@ -221,5 +223,4 @@ class PostView(BaseModelView):
del db[int(pk)]
cnt += 1
return cnt

```
12 changes: 9 additions & 3 deletions docs/advanced/custom-field/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
according to your need.

!!! important

Before creating a new field, try first to extend the existing ones. They are flexible enough to fit most use cases.

The first step is to define a new class, which derives from [BaseField][starlette_admin.fields.BaseField] or any others fields to customize it
Expand All @@ -28,7 +29,9 @@ read [Datatables documentation](https://datatables.net/reference/option/columns.
the admin class

!!! Example

This is simple example with SQLAlchemy backend

```python
from starlette_admin.contrib.sqla import Admin as BaseAdmin

Expand All @@ -47,7 +50,9 @@ the admin class
},
});
```

!!! note

`fieldOptions` is your field as javascript object. Your field attributes is serialized into
javascript object by using dataclass `asdict` function.

Expand All @@ -74,6 +79,7 @@ These jinja2 variables are available:
* `action`: `EDIT` or `CREATE`

!!! Example

```html title="forms/custom.html"
<div class="{%if error%}is-invalid{%endif%}">
<input id="{{field.id}}" name="{{field.id}}" ... />
Expand All @@ -85,6 +91,7 @@ These jinja2 variables are available:
<div class="invalid-feedback">{{error}}</div>
{%endif%}
```

```python
from starlette_admin import BaseField
from dataclasses import dataclass
Expand All @@ -104,11 +111,12 @@ These jinja2 variables are available:
* `field`: Your field instance
* `data`: value to display


!!! Example

```html title="displays/custom.html"
<span>Hello {{data}}</span>
```

```python
from starlette_admin import BaseField
from dataclasses import dataclass
Expand All @@ -128,7 +136,6 @@ For data processing you will need to override two functions:
* `serialize_field_value`: Will be call when serializing value to send through the API. This is the same data
you will get in your *render* function


```python
from dataclasses import dataclass
from typing import Any, Dict
Expand All @@ -155,6 +162,5 @@ class CustomField(BaseField):

```


!!! important
Override `dict` function to get control of the options which is available in javascript.
1 change: 1 addition & 0 deletions docs/index.es.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ admin.add_view(ModelView(Post))
# Montar admin a tu app
admin.mount_to(app)
```

Acceda a su interfaz de administrador en su navegador en [http://localhost:8000/admin](http://localhost:8000/admin)

## Terceros
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ admin.add_view(ModelView(Post))
# Mount admin to your app
admin.mount_to(app)
```

Access your admin interface in your browser at [http://localhost:8000/admin](http://localhost:8000/admin)

## Third party
Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial/authentication/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ class ReportView(CustomView):
def is_accessible(self, request: Request) -> bool:
return "admin" in request.state.user["roles"]
```

!!! important

When view is inaccessible, it does not appear in menu structure

### For [ModelView][starlette_admin.views.BaseModelView]
Expand Down
1 change: 0 additions & 1 deletion docs/tutorial/configurations/admin/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ admin = Admin(
)
```


## Parameters

* `title`: Admin title.
Expand Down
23 changes: 15 additions & 8 deletions docs/tutorial/configurations/modelview/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Here are some of the most commonly used options:

You can use the `fields` property of the ModelView class to customize which fields are included in the admin view.

```Python hl_lines="21"
```python hl_lines="21"
from sqlalchemy import JSON, Column, Integer, String, Text, create_engine
from sqlalchemy.ext.declarative import declarative_base
from starlette.applications import Starlette
Expand Down Expand Up @@ -50,12 +50,13 @@ view. These options include:
* `exclude_fields_from_create`: List of fields to exclude from the creation page.
* `exclude_fields_from_edit`: List of fields to exclude from the editing page.\

```Python
```python
class PostView(ModelView):
exclude_fields_from_list = [Post.content, Post.tags]
```

!!! note

For more advanced use cases, you can override
the [ModelView.get_fields_list()][starlette_admin.views.BaseModelView.get_fields_list] function.

Expand All @@ -68,7 +69,8 @@ Several options are available to specify which fields can be sorted or searched.
* `fields_default_sort` for initial order (sort) to apply to the table

!!! Usage
```Python

```python
class PostView(ModelView):
sortable_fields = [Post.id, "title"]
searchable_fields = [Post.id, Post.title, "tags"]
Expand All @@ -86,7 +88,8 @@ You can specify the export options for each ModelView using the following attrib
exports are `['csv', 'excel', 'pdf', 'print']`. By default, only `pdf` is disabled.

!!! Example
```Python

```python
from starlette_admin import ExportType

class PostView(ModelView):
Expand All @@ -103,9 +106,9 @@ The pagination options in the list page can be configured. The available options
* `page_size_options`: Pagination choices displayed in List page. Default value is set to `[10, 25, 50, 100]`.
Use `-1`to display All


!!! Example
```Python

```python
class PostView(ModelView):
page_size = 5
page_size_options = [5, 10, 25, 50, -1]
Expand All @@ -120,7 +123,8 @@ The template files are built using Jinja2 and can be completely overridden in th
* `edit_template`: Edit view template. Default is `edit.html`.

!!! Example
```Python

```python
class PostView(ModelView):
detail_template = "post_detail.html"
```
Expand All @@ -136,6 +140,7 @@ in your `ModelView` by overridden following options:
* `save_state`: Enable/Disable [state saving](https://datatables.net/examples/basic_init/state_save.html)

!!! Example

```python
class PostView(ModelView):
column_visibility = False
Expand All @@ -155,6 +160,7 @@ interface. By default, only the value of the object's primary key attribute is d
`__admin_repr__`, you can return a string that better represents the object in the admin interface.

!!! Example

For example, the following implementation for a `User` model will display the user's full name instead of their primary
key in the admin interface:

Expand All @@ -170,17 +176,18 @@ interface. By default, only the value of the object's primary key attribute is d

![Custom Object representation](../../../images/tutorial/configurations/modelview/object_text_representation.png){ width="200" }


### `__admin_select2_repr__`

This method is similar to `__admin_repr__`, but it returns an HTML string that is used to display the object in
a `select2` widget. By default, all the object's attributes allowed for detail page are used except relation and file
fields.

!!! note

The returned value should be valid HTML.

!!! danger

Escape your database value to avoid Cross-Site Scripting (XSS) attack.
You can use Jinja2 Template render with `autoescape=True`.
For more information, visit [OWASP website](https://owasp.org/www-community/attacks/xss/)
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorial/files/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class BookView(ModelView):

admin.add_view(BookView(Book))
```

!!! note
You can also use `multiple=True` to save multiple files.

You can also use `multiple=True` to save multiple files.

## MongoEngine

Expand Down
1 change: 0 additions & 1 deletion docs/tutorial/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class HomeView(CustomView):
admin.add_view(HomeView(label="Home", icon="fa fa-home", path="/home"))
```


### Link

Use [Link][starlette_admin.views.Link] to add arbitrary hyperlinks to the menu
Expand Down
17 changes: 10 additions & 7 deletions docs/tutorial/validations/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ capabilities using Python's type hints.
To automatically validate submitted data with Pydantic, you only need to define a Pydantic model and
use `starlette_admin.contrib.sqla.ext.pydantic.ModelView`

!!!Example
!!! Example

```python
from starlette_admin.contrib.sqla.ext.pydantic import ModelView
Expand Down Expand Up @@ -53,7 +53,8 @@ use `starlette_admin.contrib.sqla.ext.pydantic.ModelView`

You can also create your own validation functions to enforce specific data requirements.

!!!Example
!!! Example

```python
from starlette_admin.contrib.sqla import ModelView
from starlette_admin.exceptions import FormValidationError
Expand Down Expand Up @@ -83,15 +84,16 @@ You can also create your own validation functions to enforce specific data requi
![SQLAlchemy Form Validations](../../images/validations/sqla.png)

??? info
Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/sqla)

Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/sqla)

## SQLModel

With SQLModel, validating your data is made easy. Once you've defined your model, any data submitted to it will be
automatically validated.

!!! Example

```python
from sqlmodel import SQLModel, Field
from pydantic import validator
Expand All @@ -113,16 +115,16 @@ automatically validated.
![SQLModel Form Validations](../../images/validations/sqlmodel.png)

??? info
Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/sqlmodel)


Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/sqlmodel)

## Odmantic

Validation of submitted data is handled seamlessly by Odmantic. Any data that you submit to your defined model will be
validated automatically.

!!! Example

```python
from typing import List, Optional

Expand All @@ -147,16 +149,16 @@ validated automatically.

![SQLModel Form Validations](../../images/validations/odmantic.png)


??? info
Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/odmantic)

Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/odmantic)

## MongoEngine

The submitted data will be automatically validated according to your model definition.

!!! Example

```python
import mongoengine as db

Expand All @@ -175,4 +177,5 @@ The submitted data will be automatically validated according to your model defin
![SQLModel Form Validations](../../images/validations/mongoengine.png)

??? info

Full example available [here](https://github.com/jowilf/starlette-admin/tree/main/examples/mongoengine)
4 changes: 4 additions & 0 deletions starlette_admin/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def action(
!!! usage
```python
class ArticleView(ModelView):
actions = ['make_published', 'redirect']
Expand Down Expand Up @@ -129,6 +130,7 @@ def row_action(
!!! usage
```python
@row_action(
name="make_published",
Expand Down Expand Up @@ -182,6 +184,7 @@ def link_row_action(
Decorator to add custom row link actions to a ModelView for URL redirection.
!!! note
This decorator is designed to create row actions that redirect to a URL, making it ideal for cases where a
row action should simply navigate users to a website or internal page.
Expand All @@ -195,6 +198,7 @@ def link_row_action(
!!! usage
```python
@link_row_action(
name="go_to_example",
Expand Down
2 changes: 1 addition & 1 deletion starlette_admin/contrib/sqla/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def get_count_query(self):
return super().get_count_query().where(Post.published == true())
```
"""
return select(func.count("*")).select_from(self.model)
return select(func.count()).select_from(self.model)

def get_search_query(self, request: Request, term: str) -> Any:
"""
Expand Down
Loading

0 comments on commit ba01a3e

Please sign in to comment.