API¶
Application Object¶
- class djask.Djask(import_name, config=None, swagger_path='/admin/api/docs', redoc_path='/admin/api/redoc', title='Djask API', version='0.1.0', *args, **kwargs)[source]¶
The djask object implements an APIFlask application and acts as a central object for all djask applications. You can refer to the flask documentation and the apiflask documentation for more detailed information. Note that config class or dict can be passed directly to the __init__ function. I achieved this by adding an optional argument named
configto the argument list.New in version 0.1.0.
- Parameters:
swagger_path (Optional[str]) – The url path to swagger-ui web api documentation.
redoc_path (Optional[str]) – The url path to redoc web api documentation.
config (Optional[Any]) – The config object for the application, can be a dict or another Python object.
import_name (str) – Exactly the same as the
import_nameparameter inFlask.title (Optional[str]) –
version (Optional[str]) –
- model(model)¶
A decorator to register a database model directly to the app.
New in version 0.1.0.
Blueprint Objects¶
- class djask.Blueprint(name, import_name, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=<object object>)[source]¶
Flask’s
Blueprintobject with some SQL support.- Parameters:
name (str) –
import_name (str) –
static_folder (Optional[Union[str, PathLike]]) –
static_url_path (Optional[str]) –
template_folder (Optional[str]) –
url_prefix (Optional[str]) –
subdomain (Optional[str]) –
url_defaults (Optional[dict]) –
root_path (Optional[str]) –
cli_group (Optional[str]) –
- model(model)¶
A decorator to register a database model directly to the app.
New in version 0.1.0.
- class djask.APIBlueprint(name, import_name, tag=None, enable_openapi=True, static_folder=None, static_url_path=None, template_folder=None, url_prefix=None, subdomain=None, url_defaults=None, root_path=None, cli_group=<object object>)[source]¶
APIFlask’s
APIBlueprintobject with some SQL support.- Parameters:
name (str) –
import_name (str) –
tag (Optional[Union[str, dict]]) –
enable_openapi (bool) –
static_folder (Optional[str]) –
static_url_path (Optional[str]) –
template_folder (Optional[str]) –
url_prefix (Optional[str]) –
subdomain (Optional[str]) –
url_defaults (Optional[dict]) –
root_path (Optional[str]) –
cli_group (Optional[str]) –
- model(model)¶
A decorator to register a database model directly to the app.
New in version 0.1.0.
Admin Decorators¶
- class djask.admin.ext.Admin(app=None, admin_prefix=None, mode=None)[source]¶
The admin interface for Djask applications.
- Parameters:
app (Djask) –
admin_prefix (Optional[str]) –
mode (Tuple[Union[Literal['api', 'ui'], ~typing.Sequence[~typing.Literal['api', 'ui']]]]) –
- init_app(app, admin_prefix='/admin', mode=('api', 'ui'))[source]¶
Another way to initialize the Admin extension.
New in version 0.1.0.
Changed in version 0.5.0.
- Parameters:
app (Djask) –
admin_prefix (Optional[str]) –
mode (Optional[Tuple[Union[Literal['api', 'ui'], ~typing.Sequence[~typing.Literal['api', 'ui']]]]]) –
- Return type:
None
Helper Functions¶
- djask.helpers.get_model_form(model_name)[source]¶
Generate and return a form for a model
- Parameters:
model_name (str) – The name of the model
- Returns:
A tuple of the model and the form
- Return type:
tuple[Type[sqlalchemy.orm.decl_api.Model], flask_wtf.form.FlaskForm]
- djask.helpers.get_user_from_headers()[source]¶
Get the user from the request headers. Expected to be called from the web api.
- Return type:
AbstractUser | None
- djask.helpers.get_user_from_token(token)[source]¶
Get the user from an access token
- Parameters:
token (str) – The access token
- Returns:
A user
- Return type:
AbstractUser | None
Models¶
- class djask.auth.models.User(**kwargs)[source]¶
An implementation of the AbstractUser class used for admin interface.
New in version 0.1.0.
- created_at¶
- email¶
- id¶
- is_admin¶
- name¶
- password_hash¶
- updated_at¶
- username¶
- class djask.auth.abstract.AbstractUser[source]¶
A base class for all user models.
It enables you to define a user model other than the
Usermodel below.New in version 0.1.0.
- api_token(expiration=604800)[source]¶
Generate a new API token for the user.
- Parameters:
expiration – The expiration time of the token in seconds
- Returns:
The API token
- Return type:
str
New in version 0.3.0.
Changed in version 0.4.2.
- check_password(password)[source]¶
Check if the password is correct.
- Parameters:
password (str) – The password to check
- Return type:
bool
New in version 0.1.0.
- class djask.auth.anonymous.AnonymousUser[source]¶
An implementation of the AnonymousUserMixin provided by flask-login.
New in version 0.1.0.
- class djask.db.models.PureModel(**kwargs)¶
Provide a base model class with no pre-built columns.
New in version 0.4.1.
- to_dict(exclude=None)¶
Convert Model to dict.
New in version 0.4.1.
- Parameters:
exclude (Optional[Iterable[str]]) –
- Return type:
dict[str, Any]
- classmethod to_schema()¶
Convert Model to a marshmallow schema.
New in version 0.4.1.
- Return type:
type[marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchema]
- class djask.db.models.Model(**kwargs)¶
The base model class.
New in version 0.1.0.
- id = Column(None, Integer(), table=None, primary_key=True, nullable=False)¶
- created_at = Column(None, DateTime(), table=None, default=ColumnDefault(<function datetime.utcnow>))¶
- updated_at = Column(None, DateTime(), table=None, default=ColumnDefault(<function datetime.utcnow>))¶
- to_dict(exclude=None)¶
Convert Model to dict.
New in version 0.4.1.
- Parameters:
exclude (Optional[Iterable[str]]) –
- Return type:
dict[str, Any]
- classmethod to_schema()¶
Convert Model to a marshmallow schema.
New in version 0.4.1.
- Return type:
type[marshmallow_sqlalchemy.schema.SQLAlchemyAutoSchema]
More Information¶
This documentation only gives new functionalities and differences between those implemented in Djask and those in Flask and APIFlask.
To learn more about Djask, please refer to the documentations of Flask and APIFlask: